linux中ls源码简单分析

来源:互联网 发布:天猫魔盒如何下载软件 编辑:程序博客网 时间:2024/06/10 04:34

经过简单分析ls的源码,可以大致总结出ls源码的思路和结构如下(其他函数未仔细分析,还有防止环路的措施没有写出)

int main(int argc, char **argv){     //初始化     //设置权限     //设置颜色     //将当前命令行输入文件加到文件表中     //文件表排序     //打印当前文件表     //如果有待输出目录,调用print_dir()输出待输出目录,这里的循环起到递归的作用     while (pending_dirs){          thispend = pending_dirs;          ...           print_dir(thispend->name, thispend->realname,                 thispend->command_line_arg);     }}

/* Read directory NAME, and list the files in it.   If REALNAME is nonzero, print its name instead of NAME;   this is used for symbolic links to directories.   COMMAND_LINE_ARG means this directory was mentioned on the command line.  */print_dir (char const *name, char const *realname, bool command_line_arg){     DIR *dirp;     //print_dir使用opendir打开目录     dirp = opendir (name);     ...     while (1) {          //读取目录下目录项          next = readdir (dirp);          if (next){               //判断文件是否显示               if (! file_ignored (next->d_name)) {                    ...                    //gobble_file()将文件加入当前文件表中并返回文件所占块数                    total_blocks += gobble_file (next->d_name, type,                                           RELIABLE_D_INO (next),                                           false, name);               }          }     }     //对文件表排序     sort_files ();     ...     //如果要递归输出子目录,将当前子目录从文件表删除,加入待输出目录列表     if (recursive)         extract_dirs_from_files (name, command_line_arg);     ...      //如果文件项不为空,输出当前目录     if (cwd_n_used)         print_current_files ();}

0 0
原创粉丝点击