![]() ![]() | io/ls.c - Et list directory program - lånt fra Wikepedia. | Lektion 13 - slide 15 : 32 Program 1 |
/************************************************************** * A simpler and shorter implementation of ls. * Adapted from Wikipedia. **************************************************************/ #include <stdio.h> #include <dirent.h> int listdir(const char *path) { struct dirent *entry; DIR *dp; dp = opendir(path); if (dp == NULL) { perror("opendir"); return -1; } while((entry = readdir(dp))) puts(entry->d_name); closedir(dp); return 0; } int main(int argc, char **argv) { int counter = 1; if (argc == 1) listdir("."); else if (argc == 2) listdir(argv[1]); else printf("Usages: ls or ls dirPath\n"); return 0; }