UNIX基础知识之文件和目录

时间:2021-05-20 17:05:53

  程序清单1-1 列出一个目录中的所有文件(ls命令的简要实现):

[root@localhost unix_env_advance_prog]# cat prog1-.c
#include "apue.h"
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != )
err_quit("Usage: ls directory_name");
if((dp = opendir(argv[]))==NULL)
err_sys("can't open %s", argv[]);
while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
exit();
}