busybox源码剖析(3)---cat.c

本文详细解析了BusyBox中cat命令的具体实现过程,包括如何处理标准输入与文件输入的不同情况,展示了核心函数如print_file及print_file_by_name的功能与工作流程,并解释了如何避免处理目录。
部署运行你感兴趣的模型镜像

       cat显示一个文件的内容。需要注意的是,cat并不能处理目录。

复制代码
 1 extern int cat_main(int argc, char **argv)
 2 {
 3     int status = EXIT_SUCCESS;
 4 
 5     if (argc == 1) {
 6         print_file(stdin);
 7         return status;
 8     }
 9 
10     while (--argc > 0) {
11         if(!(strcmp(*++argv, "-"))) {
12             print_file(stdin);
13         } else if (print_file_by_name(*argv) == FALSE) {
14             status = EXIT_FAILURE;
15         }
16     }
17     return status;
18 }
复制代码

      当输入cat或者是cat -时,都会调用print_file函数。

复制代码
1 extern void print_file(FILE *file)
2 {
3     fflush(stdout);
4     copyfd(fileno(file), fileno(stdout));//将file文件的内容拷贝到stdout
5     fclose(file);
6 }
复制代码

      显然,print_file(stdin)就是将stdin的内容拷贝到stdout。即输入什么,就输出什么。

 

V658tekiMacBook-Pro:busybox-0.60.3 neilhappy$ cat -
input--stdin
input--stdin

 

      当cat filename时,进入print_file_by_name函数。

      

复制代码

1
extern int print_file_by_name(char *filename) 2 { 3 struct stat statBuf; 4 int status = TRUE; 5 6 if(is_directory(filename, TRUE, &statBuf)==TRUE) { 7 error_msg("%s: Is directory", filename); 8 status = FALSE; 9 } else { 10 FILE *f = wfopen(filename, "r"); 11 if(f!=NULL) 12 print_file(f); 13 else 14 status = FALSE; 15 } 16 17 return status; 18 }
复制代码

      is_directory判断是否是目录。如果不是目录,就用pring_file函数打印。要注意,这里的TRUE参数决定了只使用stat(不考虑符号链接)。

复制代码
 1 int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
 2 {
 3     int status;
 4     int didMalloc = 0;
 5 
 6     if (statBuf == NULL) {
 7         statBuf = (struct stat *)xmalloc(sizeof(struct stat));
 8         ++didMalloc;
 9     }
10 
11     if (followLinks == TRUE)
12         status = stat(fileName, statBuf);
13     else
14         status = lstat(fileName, statBuf);
15 
16     if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
17         status = FALSE;
18     }
19     else status = TRUE;
20 
21     if (didMalloc) {
22         free(statBuf);
23         statBuf = NULL;
24     }
25     return status;
26 }
复制代码

       整个流程的结构非常清晰。

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

当用户尝试运行 `./busybox --install -s /bin` 时遇到 "not found" 错误,通常表明 BusyBox 可执行文件未被正确编译、放置或路径未设置正确。以下是对问题的详细分析和解决方法: 1. **确认 BusyBox 可执行文件是否存在** 首先,检查当前目录下是否存在 `busybox` 可执行文件。使用 `ls` 命令查看: ```bash ls -l busybox ``` 如果没有该文件,则说明 BusyBox 没有被正确编译或下载[^1]。 2. **编译 BusyBox** 如果源代码已经下载,需要进行配置和编译。进入 BusyBox 源码目录后,执行: ```bash make defconfig make ``` 编译完成后,应生成 `busybox` 可执行文件。如果编译失败,请检查交叉编译工具链是否配置正确,例如 `arm-linux-gnueabihf-gcc` 是否存在于系统路径中[^1]。 3. **确认路径设置** 如果 `busybox` 文件存在,但运行时仍然提示 "not found",可能是由于动态链接库缺失或路径配置错误。使用以下命令检查可执行文件的类型和链接情况: ```bash file busybox ldd busybox ``` 如果提示动态库缺失,需安装相应的库或使用静态编译选项。在 BusyBox 配置阶段,选择静态编译: ```bash make menuconfig ``` 在配置界面中选择 `Build static binary (no shared libs)` 选项,然后重新编译[^1]。 4. **使用正确的安装命令** 运行 `./busybox --install -s /bin` 时,确保 BusyBox 二进制文件具有可执行权限: ```bash chmod +x busybox ``` 此外,`/bin` 目录可能需要 root 权限才能写入,因此建议使用 `sudo`: ```bash sudo ./busybox --install -s /bin ``` 5. **验证安装** 安装完成后,可以通过运行 BusyBox 提供的命令来验证是否成功,例如: ```bash /bin/ls ``` 如果命令正常执行,则说明 BusyBox 已正确安装[^3]。 ### 示例代码 以下是一个完整的 BusyBox 编译和安装示例: ```bash # 下载 BusyBox 源码 wget https://busybox.net/downloads/busybox-1.29.0.tar.bz2 tar -xjf busybox-1.29.0.tar.bz2 cd busybox-1.29.0 # 配置和编译 make defconfig make menuconfig # 选择静态编译选项 make # 安装 BusyBox 命令到 /bin sudo ./busybox --install -s /bin ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值