ls的实现
一个简单的ls命令的实现:
#include <stdio.h> #include <stdlib.h> #include <dirent.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if(argc != 2) { fprintf(stderr, "usage: ls directory_name\n"); exit(-1); } if((dp = opendir(argv[1])) == NULL) { perror("opendir"); exit(-1); } while((dirp = readdir(dp)) != NULL) { printf("%s\n", dirp->d_name); } if(closedir(dp) == -1) { exit(-1); } exit(0); }