Idle to do nothing, write casually, implement the simple ls command:
| 1 #include <stdio.h>| 2 #include <stdlib.h>| 3 #include <dirent.h>| 4 #include <string.h>| 5| 6 int main (int argc, char **argv) | 7 {| 8 DIR *pdir;| 9 struct Dirent *stdir;| Ten int flag = 0;| 11| if (argc > 2) | ("Usage:./ls or./ls xxx\n"); | Exit (-1); | 16}| if (argc = = 1) | {| flag = 1;| if (Pdir = Opendir (".")) = = NULL) | {|-printf ("Open dir error!\n"); | + Exit (-1); | 24}| 25}| 26| if (!flag) | {| (Pdir = opendir (argv[1)) = = NULL) | []-printf ("Open dir error!\n"); | Exit (-1); | 33}| 34}| 35| (Stdir = Readdir (pdir)) = NULL) | PNS {| strcmp (Stdir->d_name, ".") = = 0 | | strcmp (Stdir->d_name, "..") = = 0) | e;| -printf ("%s\n", stdir->d_name); | 41}| 42| 43 Closedir (pdir); | 44| Return 0;| 46}
Function:
./ls--list files in the current directory
./ls xxx--list files in the specified directory
Ignore. With.. Two of directories.