#include #include #include #include #include enum { Pathmax = 1024, File, Dir, }; int recursive = 0; char * basename(char *s) { char *p; if((p = strrchr(s, '/')) == 0) p = s; if(*p == '/') p++; return p; } void output(struct stat *st, char *path, int type) { switch(type){ case File: printf("%s\n", basename(path)); break; case Dir: if(recursive) printf("\n%s:\n", path); else printf("%s\n", basename(path)); break; } } int ls(char *path) { DIR *d; struct dirent *e; struct stat st; char buf[Pathmax]; if((d = opendir(path)) == NULL) exit(1); while((e = readdir(d)) != NULL){ if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue; snprintf(buf, sizeof buf, "%s/%s", path, e->d_name); if(stat(buf, &st) < 0) exit(1); if(S_ISREG(st.st_mode)){ output(&st, buf, File); }else if(S_ISDIR(st.st_mode)){ output(&st, buf, Dir); if(recursive) ls(buf); } } closedir(d); return 0; } int main(int argc, char *argv[]) { recursive = 1; if(argc == 1) ls("."); else while(--argc) ls(argv[argc]); return 0; }