当前位置:网站首页>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
呵呵 这些内置的命令 是否也没有那么神秘?
当然 这里仅仅是剖析的核心脉络, 真正的实现细节 还有很多设计
完
边栏推荐
- 选择排序与冒泡排序模板
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
- [paddleseg source code reading] normalize operation of paddleseg transform
- How much does it cost to open a futures account in China? Where is it safe to open an account at present?
- Eh, the log time of MySQL server is less than 8h?
- CUDA basic knowledge
- A review of reverse reinforcement learning at Virginia Tech (VT)
- Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
- [.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
猜你喜欢
Add IDM to Google browser
Consul of distributed service registration discovery and unified configuration management
ctf-pikachu-CSRF
Objective-C description method and type method
[paddleseg source code reading] paddleseg custom data class
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Audio and video technology development weekly | 232
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
Defensive programming skills
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
随机推荐
Epidemic strikes -- Thinking about telecommuting | community essay solicitation
warning: LF will be replaced by CRLF in XXXXXX
深度优先搜索简要讲解(附带基础题)
vim正确加区间注释
如何有效远程办公之我见 | 社区征文
Management and thesis of job management system based on SSM
JSON string conversion in unity
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
ctf-pikachu-XSS
Why is it recommended that technologists write blogs?
Simple dialogue system -- text classification using transformer
【webrtc】m98 ninja 构建和编译指令
渗透实战-SQLServer提权
用于TCP协议交互的TCPClientDemo
Reduce function under functools
'2'>' 10'==true? How does JS perform implicit type conversion?
PostgreSQL users cannot create table configurations by themselves
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Leecode 122. Zuijia timing of buying and selling stocks ②