自己动手写shell命令之ls -R1fF
来源:互联网 发布:内网演示软件 编辑:程序博客网 时间:2024/06/11 00:06
ls命令的R参数代表递归的列出所有子文件夹中的所有文件,1表示每一行只显示一个文件或文件夹,f表示不排序即输出,F表示在每项的输出的最后根据其文件类型相应的加上*/=>@|字符。通过c语言实现ls -R1fF命令的效果,其源代码如下:
#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <pwd.h>#include <grp.h>#include <string.h>void listdir(char *);char get_type(struct stat *);int main(int argc,char * argv[]){if(argc == 1)listdir(".");else{int index = 1;while(argc > 1){listdir(argv[index]);index++;argc--;}}return 0;}void listdir(char * dirname){DIR * dir;struct stat info;//char pointer[];struct dirent * direntp;if((dir = opendir(dirname)) != NULL){printf("%s:\n",dirname);while((direntp = readdir(dir)) != NULL){char absolute_pathname[255];strcpy(absolute_pathname,dirname);strcat(absolute_pathname,"/");strcat(absolute_pathname,direntp->d_name);lstat(absolute_pathname,&info);printf("%s",direntp->d_name);printf("%c\n",get_type(&info));}printf("\n");rewinddir(dir);while((direntp = readdir(dir)) != NULL){if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0)continue;char absolute_pathname[255];strcpy(absolute_pathname,dirname);strcat(absolute_pathname,"/");strcat(absolute_pathname,direntp->d_name);lstat(absolute_pathname,&info);if(S_ISDIR((&info)->st_mode))listdir(absolute_pathname);}}}char get_type(struct stat * info){if(S_ISCHR(info->st_mode))return '*';if(S_ISDIR(info->st_mode))return '/';if(S_ISSOCK(info->st_mode))return '=';if(S_ISBLK(info->st_mode))return '>';if(S_ISLNK(info->st_mode))return '@';if(S_ISFIFO(info->st_mode))return '|';return ' ';}
0 0
- 自己动手写shell命令之ls -R1fF
- 自己动手写shell命令之ls
- 自己动手写shell命令之more
- 自己动手写shell命令之who
- 自己动手写shell命令之pwd
- 自己动手写shell命令之du
- 自己动手写shell命令之write
- 自己动手写shell之chgrp,chown,chmod
- 自己动手写shell脚本
- 自己动手写PWD命令
- 自己动手写cp命令
- 自己动手写一个简单的Shell之二:运行程序
- 自己动手写一个简单的Shell之三:命令行解析
- 自己动手写一个简单的Shell之四:流程控制
- Linux shell: ls命令详解
- Linux shell: ls命令详解
- 写自己的ls命令
- 写自己的ls命令
- android File类的用法:File.exists()错误、File.mkdir()错误等创建删除的解决办法
- ValueStack
- 多态类中的虚函数表是Compile-Time,还是Run-Time时建立的
- MySQL报错:ERROR 1045 (28000) (mot de passe: OUI)
- ubuntu12.04LTS输入法无反应,系统应用无响应
- 自己动手写shell命令之ls -R1fF
- Ubuntu添加用户操作
- java实现的Trie树数据结构
- iOS 8 and iPhone 6 for web developers and designers: next evolution for Safari and native webapps
- Fragment的使用简介【Android】
- C#Form打开一个新窗口的同时关闭这个窗体
- POJ 3237 Tree (树链剖分)
- android--查看keystore文件签名信息
- message sent to deallocated instance