经过简单分析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打开目录