#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include < dirent.h> #include <unistd.h>const static int strarraylen=256;typedef char Stringbuffer[strarraylen];typedef struct stringlist_t stringlist;struct stringlist_t{stringbuffer sb; Stringlist *next; stringlist_t () {next = 0; memset (SB, 0, sizeof (SB)); sb[0]= ' + '; } ~stringlist_t () {stringlist *itor = this->next; while (itor! = NULL) {This->next = itor->next; Delete Itor; Itor = this->next; }}};void outputlist (Stringlist *list) {stringlist *itor = list; while (itor! = 0) {printf ("%s\n", ITOR->SB); Itor = itor->next; }}int scanfilestolist (char* folderpath, stringlist *filelist) {//get rid of '/' within folderpath int slen = St Rlen (FolderPath); if (folderpath[slen-1] = = '/') {folderpath[slen-1] = ' + '; } StRingbuffer pwd = ""; GETCWD (pwd, sizeof (PWD)); if (strncmp (FolderPath, ".", 2) = = 0) {char pwdbak[200] = ""; strcpy (Pwdbak, PWD); ChDir (FolderPath); memset (pwd, 0, sizeof (PWD)); GETCWD (pwd, sizeof (PWD)); strcpy (FolderPath, PWD); strcpy (pwd, Pwdbak); } else {chdir (folderpath); } DIR *dir = Opendir (FolderPath); if (dir = = NULL) {printf ("Failed to open dir <%s> \ n", FolderPath); } struct Dirent *dt = NULL; struct stat statbuf; while (dt = Readdir (dir)) {if ('. ' = = Dt->d_name[0]) {continue; } lstat (Dt->d_name, &statbuf); if (S_isdir (Statbuf.st_mode)) {StringBuffer SB = ""; sprintf (SB, "%s/%s", FolderPath, Dt->d_name); ChDir (SB); Scanfilestolist (SB, fileList); ChDir (".."); } else if (S_isreg (Statbuf.st_mode)) {//addfiles to list stringlist *tmp=newStringlist; sprintf (TMP->SB, "%s/%s", FolderPath, Dt->d_name); tmp->next=filelist->next; filelist->next=tmp; }} chdir (PWD); return 0;}
Linux scans the directory to list all plain text