Thread: recursive direcory listings : readdir or ftw or FTS or glob
in windows c++ use (findfirst + findnext) recursively list (patterned) files in directory.
having found linux doco seems
1) use (opendir readdir closedir) read names , fnmatch() reject , recurse.
work slow.
2) ftw file tree walk (recurse) has no pattern matcher built in.
3) fts has no patten matcher.
4) glob asserts greater efficiency. no built in recursion. call glob twice.
once "*" directories.once "*.cpp" files. each time (directory or file)
compares must made (after using glob_mark). seems slower others.
5) 1 exec ls, or don't know yet.
if no recursion needed using glob seems best. if recursion needed using fts appears best.
guesswork. can tell me if analysis flawed.
there thing else better ?? why doesnt glob recurse bitflag
don't know ??
the function displays list of files in directory, including recursively.code:#ifndef _view_h #define _view_h #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> #include <iostream> #include <pwd.h> #include <grp.h> #include <errno.h> using namespace std; int myview(bool l,bool r,const char* path) { dir* dir = opendir(path); if(!dir) return 1; struct dirent *rd; while((rd = readdir(dir))) { if(!strcmp(rd->d_name,".") || !strcmp(rd->d_name,"..")) { continue; } struct stat entryinfo; char pathname[path_max+1]; strncpy(pathname,path,path_max); strncat(pathname,"/",path_max); strncat(pathname,rd->d_name,path_max); if(!lstat(pathname,&entryinfo)) { if(l) { switch(entryinfo.st_mode & s_ifmt) { case s_ifdir: { cout << "d"; break; } case s_ififo: { cout << "p"; break; } case s_ifsock: { cout << "s"; break; } case s_iflnk: { cout << "l"; break; } default: cout << "-"; } //owner //read if(entryinfo.st_mode & s_irusr) cout << "r"; else cout << "-"; //write if(entryinfo.st_mode & s_iwusr) cout << "w"; else cout << "-"; //execute if(entryinfo.st_mode & s_ixusr) cout << "x"; else cout << "-"; //group //read if(entryinfo.st_mode & s_irgrp) cout << "r"; else cout << "-"; //write if(entryinfo.st_mode & s_iwgrp) cout << "w"; else cout << "-"; //execute if(entryinfo.st_mode & s_ixgrp) cout << "x"; else cout << "-"; //other //read if(entryinfo.st_mode & s_iroth) cout << "r"; else cout << "-"; //write if(entryinfo.st_mode & s_iwoth) cout << "w"; else cout << "-"; //execute if(entryinfo.st_mode & s_ixoth) cout << "x"; else cout << "-"; cout << " "; struct passwd* pwd = getpwuid(getuid()); struct group* grp = getgrgid(pwd->pw_gid); cout << " " << grp->gr_name << " " << pwd->pw_name << " "; } if(s_isdir(entryinfo.st_mode)) { cout << rd->d_name << endl; if(r) { myview(l,r,pathname); } }else if(s_isreg(entryinfo.st_mode)) { cout << rd->d_name; }else if(s_islnk(entryinfo.st_mode)) { char targetname[path_max+1]; if(readlink(pathname,targetname,path_max)) { cout << rd->d_name << " link: " << targetname; } }else if(s_issock(entryinfo.st_mode)) { cout << rd->d_name; } cout << endl; } / / added check here, prevent fall of stack, details below else { cout << rd->d_name << " " << strerror(errno) << endl; exit(1); } } closedir(dir); } #endif /* _view_h */
options:
1) displays information file (true / false)
2) recursive (true / false)
3) path (if not specified, ".")
if understand question.
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk recursive direcory listings : readdir or ftw or FTS or glob
Ubuntu
Comments
Post a Comment