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


呵呵 这些内置的命令 是否也没有那么神秘?
当然 这里仅仅是剖析的核心脉络, 真正的实现细节 还有很多设计
完
边栏推荐
- Management and thesis of job management system based on SSM
- Katalon框架测试web(二十六)自动发邮件
- Wechat official account web page authorization
- Katalon使用script实现查询List大小
- 2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
- MySQL backup notes
- Leecode 122. Zuijia timing of buying and selling stocks ②
- Easy to win insert sort
- If you have just joined a new company, don't be fired because of your mistakes
- Reduce function under functools
猜你喜欢

基于PHP的轻量企业销售管理系统

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

Reduce function under functools

Why is it recommended that technologists write blogs?

What kind of experience is it when the Institute earns 20000 yuan a month!

logistic regression

SQL语句加强练习(MySQL8.0为例)

深度优先搜索简要讲解(附带基础题)

Katalon框架测试web(二十六)自动发邮件

三年进账35.31亿,这个江西老表要IPO了
随机推荐
新型数据中心,助力加快构建以数据为关键要素的数字经济
Value transfer communication between components (parent to child, child to parent, brother component to value)
Management and thesis of job management system based on SSM
MySQL one master multiple slaves + linear replication
还原窗口位置的微妙之处
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
三年进账35.31亿,这个江西老表要IPO了
Spa in SDP
[paddleseg source code reading] paddleseg custom data class
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
CUDA basic knowledge
Object oriented -- encapsulation, inheritance, polymorphism
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
system information
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
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
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
Zigzag scan
Reduce function under functools