程序清单1-1 列出一个目录中的所有文件(ls命令的简要实现):
[root@localhost unix_env_advance_prog]# cat prog1-1.c #include "apue.h" #include <dirent.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if(argc != 2) err_quit("Usage: ls directory_name"); if((dp = opendir(argv[1]))==NULL) err_sys("can't open %s", argv[1]); while((dirp = readdir(dp)) != NULL) printf("%s ", dirp->d_name); closedir(dp); exit(0); }