Code:
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
int lsdetails(struct stat *statbuff);
int outputfiletype(mode_t filetype);
int recls(char *path);
int printperms(mode_t mode);
char monthprint[4];
int main(int argc, char **argv){
printf("\n");
if (argc<2)
{
recls(".");
return(0);
}
recls(argv[1]);
}
int recls(char *path) { /* function expects char as argument */
DIR *dir; /* Declare DIR function, create pointer "*dir" */
struct dirent *dirslot; /* Declare struct "dirent", create "*dirslot" */
struct stat statbuff; /* Declare struct "stat", create "statbuff" */
int islnk = 0;
char lbuffer[256];
char *name;
chdir(path);
dir=opendir(".");
if (dir == NULL) {
printf("\nNo such directory\n");
return(1);
}
while (1) {
dirslot=readdir(dir);
if (dirslot==NULL) break;//note change
lstat(dirslot->d_name, &statbuff);
if (strcmp(dirslot->d_name, ".")==0) continue;
if (strcmp(dirslot->d_name, "..")==0) continue;
if (S_ISLNK(statbuff.st_mode)) {
lsdetails(&statbuff);
printf("\033[36m%s\033[m", dirslot->d_name);
}
if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
islnk = 1;
memset(lbuffer, 0, 256);
readlink(name, lbuffer, 256);
printf("%s %s \n", (islnk) ? " -> " : "", (islnk) ? lbuffer : " ");
}
if (S_ISREG(statbuff.st_mode)) {
lsdetails(&statbuff);
printf("\033[0m%s\033[m\n", dirslot->d_name);
}
if (S_ISDIR(statbuff.st_mode)) {
lsdetails(&statbuff);
printf("\033[34;42m%s\033[m\n", dirslot->d_name);
}
if (S_ISCHR(statbuff.st_mode)) {
lsdetails(&statbuff);
printf("\033[01;32m%s\033[m\n", dirslot->d_name);
}
/*if (dirslot->d_type == DT_UNKNOWN) {
lsdetails(&statbuff);
printf("\033[0m%s\033[m\n", dirslot->d_name);
}
if (dirslot->d_type == DT_FIFO) {
lsdetails(&statbuff);
printf("\033[0m%s\033[m\n", dirslot->d_name);
}
if (dirslot->d_type == DT_CHR) {
lsdetails(&statbuff);
printf("\033[01;33m%s\033[m\n", dirslot->d_name);
}
if (dirslot->d_type == DT_BLK) {
lsdetails(&statbuff);
printf("\033[0m%s\033[m\n", dirslot->d_name);
}
if (dirslot->d_type == DT_WHT) {
lsdetails(&statbuff);
printf("\033[0m%s\033[m\n", dirslot->d_name);
}*/
}
rewinddir(dir); /* rewind the directory stream, start from the top */
while (1) {
dirslot=readdir(dir);
if (dirslot==NULL) return(1);
if (strcmp(dirslot->d_name, ".")==0) continue; /* if d_name equals ".", skip rest of loop */
if (strcmp(dirslot->d_name, "..")==0) continue; /* if d_name equals "..", skip rest of loop */
lstat(dirslot->d_name, &statbuff);
if (S_ISDIR(statbuff.st_mode)) {
printf("\n./\033[01;34m%s\033[m\n", dirslot->d_name);
if (chdir(dirslot->d_name)==-1) {
perror("Error: chdir");
return;}
recls(".");
chdir("..");
}
}
}
int printperms(mode_t modebit) {
char perms[10];
int i;
for (i=0;i<9; i++) perms[i]='-';
perms[9] = 0;
if (((modebit) & S_IRUSR)!=0) perms[0] = 'r';
if (((modebit) & S_IWUSR)!=0) perms[1] = 'w'; //User Perms
if (((modebit) & S_IXUSR)!=0) perms[2] = 'x';
if (((modebit) & S_IRGRP)!=0) perms[3] = 'r';
if (((modebit) & S_IWGRP)!=0) perms[4] = 'w'; //Group Perms
if (((modebit) & S_IXGRP)!=0) perms[5] = 'x';
if (((modebit) & S_IROTH)!=0) perms[6] = 'r';
if (((modebit) & S_IWOTH)!=0) perms[7] = 'w'; //Others
if (((modebit) & S_IXOTH)!=0) perms[8] = 'x';
//else fprintf(stderr,"FAIL: Cannot determine file permission mask");
printf("%s ", perms);
return(0);
}
int printfiletype(mode_t filetype) {
char * mode;
if(S_ISREG(filetype)) mode = "-";
else if(S_ISDIR(filetype)) mode = "d";
else if(S_ISCHR(filetype)) mode = "c";
else if(S_ISBLK(filetype)) mode = "b";
else if(S_ISFIFO(filetype)) mode = "f";
else if(S_ISSOCK(filetype)) mode = "s";
else if(S_ISLNK(filetype)) mode = "l";
else fprintf(stderr,"FAIL: Cannot determine file mode");
printf ("%s", mode);
return(0);
}
int lsdetails(struct stat *statbuff) {
struct passwd *pw;
struct group *gp;
struct tm *tm_ptr;
tm_ptr = gmtime(&statbuff->st_ctime);
if(tm_ptr-> tm_mon+ 1 == 1) strcpy(monthprint,"Jan");
else if(tm_ptr-> tm_mon+ 1 == 2) strcpy(monthprint,"Feb");
else if(tm_ptr-> tm_mon+ 1 == 3) strcpy(monthprint,"Mar");
else if(tm_ptr-> tm_mon+ 1 == 4) strcpy(monthprint,"Apr");
else if(tm_ptr-> tm_mon+ 1 == 5) strcpy(monthprint,"May");
else if(tm_ptr-> tm_mon+ 1 == 6) strcpy(monthprint,"Jun");
else if(tm_ptr-> tm_mon+ 1 == 7) strcpy(monthprint,"Jul");
else if(tm_ptr-> tm_mon+ 1 == 8) strcpy(monthprint,"Aug");
else if(tm_ptr-> tm_mon+ 1 == 9) strcpy(monthprint,"Sep");
else if(tm_ptr-> tm_mon+ 1 == 10) strcpy(monthprint,"Oct");
else if(tm_ptr-> tm_mon+ 1 == 11) strcpy(monthprint,"Nov");
else if(tm_ptr-> tm_mon+ 1 == 12) strcpy(monthprint,"Dec");
else fprintf(stderr, "FAIL: Cannot determine month");
printfiletype(statbuff->st_mode);
printperms(statbuff->st_mode);
printf( "%d ", statbuff->st_nlink);
if ((pw = getpwuid(statbuff->st_uid)) != NULL) printf("%s ", pw->pw_name);
else fprintf(stderr,"UID ERROR" );
if ((gp = getgrgid(statbuff->st_gid)) != NULL) printf("%s ", gp->gr_name);
else fprintf(stderr,"The User is not associated with any group");
printf( "%-4d ", statbuff->st_size);
printf( "Inode: %-4d ", statbuff->st_ino);
printf( "%d-%s-%02d %d:%d ",tm_ptr->tm_year+1900, monthprint, tm_ptr-> tm_mday, tm_ptr-> tm_hour, tm_ptr-> tm_min);
return 0;
}
Thank goodness for Visual Studio.