当前位置:网站首页>02 ls 命令的具体实现
02 ls 命令的具体实现
2022-07-04 03:47:00 【蓝风9】
前言
// 呵呵 昨天的有点敷衍, 我总不能说那都不是细节吧?
呵呵 这个命令 应该是 linux 使用者 基本上都会使用到的吧?
呵呵 我们这里主要是 看一下 这个命令是如何工作的
main 中主要是参数处理, 循环各个路径 输出各个路径的信息
print_dir 中主要是遍历当前路径下各个 子文件(夹), 具体的业务委托给 gobble_file, 采集各个 子文件(夹) 的相关信息
gobble_file 主要是使用 lstat/stat 命令获取各个 文件(夹) 的相关元数据
print_current_files 主要是输出各个 子文件(夹) 的相关信息, 这里这涉及到具体的输出格式, 我们这里以 ls -l 为例
print_long_format 的输出是常见的 ll 输出的处理, 输出 fmode, nlink, user, group, size, mtime, fileName
基于 linux-4.4.0 + coreutils-8.25
调试基于命令 "ls -l /"
main
可以看到的是 ls 后面的路径只有一个 "/"
这里是调用 print_dir 来输出 "/" 下面的各个 子文件(夹) 的相关信息
print_dir
基于 opendir, readdir 等相关 api 遍历 "/" 下面的各个 子文件(夹)
调用 gobble_file 来获取各个 子文件(夹) 的相关元数据
gobble_file
通过 stat/lstat 相关 api 来获取当前 文件(夹) 的元数据的信息
print_current_files
根据 输出格式 进行不同的输出, 我们这里是 print_long_format
print_long_format
函数内容较长, 这里不贴完了
使用 buf 向 stdout 输出 fmode, nlink
user, group 需要额外的系统调用来获取 uid, gid 对应的名称, 直接使用 stdout 输出
使用 buf 向 stdout 输出 size, mtime, fileName
呵呵 这些内置的命令 是否也没有那么神秘?
当然 这里仅仅是剖析的核心脉络, 真正的实现细节 还有很多设计
完
边栏推荐
- Spa in SDP
- How to dynamically cache components in Vue multi-level route nesting
- Leecode 122. Zuijia timing of buying and selling stocks ②
- Defensive programming skills
- Day05 錶格
- 【罗技】m720
- 1289_FreeRTOS中vTaskSuspend()接口实现分析
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- Want to do something in production? Then try these redis commands
- [book club issue 13] packaging format of video files
猜你喜欢
MySQL maxscale realizes read-write separation
Easy to win insert sort
AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
渗透实战-SQLServer提权
Package details_ Four access control characters_ Two details of protected
logistic regression
Session learning diary 1
PostgreSQL users cannot create table configurations by themselves
Nbear introduction and use diagram
随机推荐
华为云鲲鹏工程师培训(广西大学)
如何有效远程办公之我见 | 社区征文
【webrtc】m98 ninja 构建和编译指令
选择排序与冒泡排序模板
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
[untitled]
super_ Subclass object memory structure_ Inheritance tree traceability
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
还原窗口位置的微妙之处
Nbear introduction and use diagram
functools下的reduce函数
MySQL is dirty
智慧地铁| 云计算为城市地铁交通注入智慧
Aperçu du code source futur - série juc
Explain AI accelerator in detail: why is this the golden age of AI accelerator?
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
“软硬皆施”,助力建成新型云计算数据中心
[paddleseg source code reading] paddleseg calculation dice