首页 > 代码库 > ls 底层源码简单实现

ls 底层源码简单实现

列出一个目录中所有文件的名字,相当于在Linux/Unix终端命令行模式下键入ls命令的功能实现:


#include "apue.h"
#include <dirent.h>

int
main(int argc, char *argv[])
{
    DIR     *dp;
    struct dirent *dirp;
    
    if(argc != 2) 
        err_quit("usage: ls dictionary_name");
        
    if((dp = opendir(argv[1])) == NULL) 
        err_sys("can't open %s", argv[1]);
        
    while((dirp = readdir(dp))  != NULL) 
        printf("%s\n", dirp->d_name);

    closedir(dp);
    exit(0);
}



ls 底层源码简单实现