/*****************************************************************************\ * inc - http://www.incubus.co.uk -- 14-08-2001 * * Free code so don't come running to me if it fouls up !! Linux ONLY || * * To compile under linux use - cc listDir.c -o listDir * * Then to run ./listDir * \****************************************************************************/ #include #include #include #include #include #include #include #define MAX 1024 void listDir (char *path); int main(int argc, char *argv[]) { if (--argc) listDir(argv[1]); else printf("Missing argument - Directory to list\n"); return EXIT_SUCCESS; } void listDir (char *path) { struct dirent *entry; struct stat status; DIR *dir; int mode, type; char *name, buff[MAX], dirBuff[MAX]; if (dir = opendir(path)) { if ( path[strlen(path)-1] == '/' ) path[strlen(path)-1] = 0; while (entry = readdir(dir)) { name=entry->d_name; memset(buff,0,MAX); sprintf(buff, "%s/%s",path,name); if (lstat(buff,&status)!= -1) mode=status.st_mode; if (S_ISDIR(mode)) { memset(dirBuff,0,MAX); sprintf( dirBuff, "%s/%s", path,name ); if ( (strcmp(name,".")) && (strcmp(name,".."))) listDir(dirBuff); continue; }else if (mode & (S_IXUSR|S_IXGRP|S_IXOTH)) type='X'; /* Executabe File*/ else if (S_ISLNK(mode)) type='L'; /* Symbolic Link */ else if (S_ISBLK(mode)) type='B'; /* Block Special File */ else if (S_ISCHR(mode)) type='C'; /* Character Special File */ else if (S_ISFIFO(mode)) type='F'; /* Pipe or FIFO special file */ else if (S_ISREG(mode)) type='R'; /* Regular File */ else type='-'; fprintf(stdout, "%c\t %s/%s\n", type, path, name); fflush(stdout); } closedir(dir); }else fprintf(stderr, "Could not open %s\n\r", path); }