当前位置:网站首页>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


呵呵 这些内置的命令 是否也没有那么神秘?
当然 这里仅仅是剖析的核心脉络, 真正的实现细节 还有很多设计
完
边栏推荐
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
- Defensive programming skills
- 渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
- ctf-pikachu-XSS
- Pytest multi process / multi thread execution test case
- Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
- 渗透实战-SQLServer提权
- 还原窗口位置的微妙之处
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
猜你喜欢

【罗技】m720

postgresql 用户不能自己创建表格配置

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡

1289_ Implementation analysis of vtask suspend() interface in FreeRTOS

Storage of MySQL database

Don't disagree, this is the most powerful "language" of the Internet

Getting started with the go language is simple: go implements the Caesar password

EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?

MySQL one master multiple slaves + linear replication

pytest多进程/多线程执行测试用例
随机推荐
Which product is better if you want to go abroad to insure Xinguan?
ctf-pikachu-XSS
[paddleseg source code reading] paddleseg calculates Miou
思考的小记录
Monitoring - Prometheus introduction
postgresql 用户不能自己创建表格配置
Object oriented -- encapsulation, inheritance, polymorphism
EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
MySQL maxscale realizes read-write separation
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
MySQL is dirty
[paddleseg source code reading] normalize operation of paddleseg transform
Reduce function under functools
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Es network layer
Objective-C string class, array class
JDBC 进阶
“软硬皆施”,助力建成新型云计算数据中心
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Leetcode51.n queen