自己动手写shell命令之ls -R1fF
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 ' ';
}
自己动手写shell命令之ls -R1fF的更多相关文章
- 自己动手写shell命令之ls
linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent. ...
- 自己动手写shell命令之write
Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...
- 自己动手写shell命令之more
unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...
- 自己动手写shell之chgrp,chown,chmod
1.chgrp实现 #include <grp.h> #include <unistd.h> void chgrp(char * groupname,char * filena ...
- 自己写shell命令pwd
思维:(1)得到"."的i节点号,叫n(使用stat) (2)chdir ..(使用chdir) (3)找到inode号为n的节点,得到其文件名称. 反复上述操作直到当前文件夹&q ...
- Android 的独特shell命令
Android本来就是一个linux操作系统,所以大部分都是linux的命令,如mkdir,ls,netstat,mount,ps 等,这里就不具体介绍了, 主要介绍几个Android特有的. get ...
- Ubuntu常用shell命令
目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...
- 自己动手写ls命令——Java版
自己动手写ls命令--Java版 介绍 在前面的文章Linux命令系列之ls--原来最简单的ls这么复杂当中,我们仔细的介绍了关于ls命令的使用和输出结果,在本篇文章当中我们用Java代码自己实现ls ...
- Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本
Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...
随机推荐
- Windows:32位程序运行在64位系统上注册表会重定向
参考资料 微软注册表英文文档 StackOverflow社区回答 1.注册表位置 64bit系统(Windows Server 2008 R2只有64bit系统)的注册表分32 位注册表项和64位注册 ...
- 15数据库与ADO.Net
数据库与ADO.Net 数据库与ADO.Net 8.1 数据库基本概念 数据库提供了一种将信息集合在一起的方法.数据库应用系统主要由三部分组成:数据库管理系统(DBMS),是针对所有应用的,例如A ...
- SQLServer:查询所有外键关联表信息
--从左到右分别是: 外键约束名,子表名,外键列名,父表名 --use demodtcms--外键信息select fk.name fkname , ftable.name ftablename, ...
- VIM基础操作方法汇总
学习自小甲鱼的视频,快速入门vim 目录: 1.光标移动 2.进入插入模式 3.进入普通模式 4.进入命令行模式 5.退出 6.光标跳跃 7.快速跳转行号 8.删除 9.利用数字重复操作 10.撤回 ...
- linux下nginx、php和mysql安装配置
一.安装nginx 安装nginx yum install -y epel-release yum install nginx -y 查看nginx软件包包括了哪些文件 rpm -ql nginx 启 ...
- virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenvwrapper.........(解决办法)
Linux(ubuntu)上python2与python3共存环境下,安装virtualenvwrapper后, 其环境变量被自动设置为VIRTUALENVWRAPPER_PYTHON=/usr/bi ...
- MapReduce架构与执行流程
一.MapReduce是用于解决什么问题的? 每一种技术的出现都是用来解决实际问题的,否则必将是昙花一现,那么MapReduce是用来解决什么实际的业务呢? 首先来看一下MapReduce官方定义: ...
- 完美解决了span的宽度设置
下 面代码的CSS定义完美解决了span的宽度设置问题.由于浏览器通常对不支持的CSS属性采取忽略处理的态度,所以最好将display:inline -block行写在后面,这样在Firefox里面, ...
- Linux下汇编语言学习笔记22 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Object-C 打开工程,选择模拟起时,提示"no scheme"
错误提示,如下图: 解决思路: