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的更多相关文章

  1. 自己动手写shell命令之ls

    linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent. ...

  2. 自己动手写shell命令之write

    Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...

  3. 自己动手写shell命令之more

    unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...

  4. 自己动手写shell之chgrp,chown,chmod

    1.chgrp实现 #include <grp.h> #include <unistd.h> void chgrp(char * groupname,char * filena ...

  5. 自己写shell命令pwd

    思维:(1)得到"."的i节点号,叫n(使用stat) (2)chdir ..(使用chdir) (3)找到inode号为n的节点,得到其文件名称. 反复上述操作直到当前文件夹&q ...

  6. Android 的独特shell命令

    Android本来就是一个linux操作系统,所以大部分都是linux的命令,如mkdir,ls,netstat,mount,ps 等,这里就不具体介绍了, 主要介绍几个Android特有的. get ...

  7. Ubuntu常用shell命令

    目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...

  8. 自己动手写ls命令——Java版

    自己动手写ls命令--Java版 介绍 在前面的文章Linux命令系列之ls--原来最简单的ls这么复杂当中,我们仔细的介绍了关于ls命令的使用和输出结果,在本篇文章当中我们用Java代码自己实现ls ...

  9. Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本

    Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...

随机推荐

  1. 20面向对象三特征 之继承 方法重写 super

    继承是:多个类有重复内容,把重复内容放到一个新类中,就可以通过extends关键词去让原来的类和新类产生继承关系,子类只能拿到父类一部分信息.通过extends关键词去指明类与类之间的关系,一个父类可 ...

  2. 编程规范:allocator

    一.作用 标准库allocator类定义在头文件memory中,它帮助我们将内存分配和对象构造分离开来 allocator<T> a //定义一个名为a的allocator对象,它可以为类 ...

  3. 【原】CentosDocker安装(一)

    CentosDocker安装 来源:https://www.runoob.com/docker/centos-docker-install.html 1.前提条件 目前,CentOS 仅发行版本中的内 ...

  4. Oracle中如何插入特殊字符: &amp;amp; 和 &amp;#39; (多种解决方案)

    Oracle中如何插入特殊字符:& 和 ' (多种解决方案)今天在导入一批数据到Oracle时,碰到了一个问题:Toad提示要给一个自定义变量AMP赋值,一开始我很纳闷,数据是一系列的Inse ...

  5. android ListView几个比较特别的属性

    由于这两天在做listView的东西,所以整理出来一些我个人认为比较特别的属性,通过设置这样的属性可以做出更加美观的列表 首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你 ...

  6. CF792E Colored Balls

    题目大意:将n个数分解成若干组,如4 = 2+2, 7 = 2+2+3,保证所有组中数字之差<=1. 首先我们能想到找一个最小值x,然后从x+1到1枚举并check,找到了就输出.这是40分做法 ...

  7. Map集合遍历的方式(以HashMap为例)

    环境:jdk1.8 HashMap的遍历方式有多种,下面将会一一列出. 首先我们先在HashMap中添加几个键值对. HashMap<Integer, String> map = new ...

  8. ubuntu(linux)占领小米平板2(mipad2)

    昨天 2014年,媳妇坐月子,给媳妇买了mi6和一个小米平板2(16G).是我们人生拥有的第一个平板,激动不已. 买之前看了小米平板1的口碑不错,arm构架,NVIDIA的主板好像,图形处理做得当然没 ...

  9. 怎样提高DB2存储过程性能

    高性能的SQL过程是数据库开发人员所追求的,我将不断把学到的,或在实际开发中用到的一些提高SQL过程性能的技巧整理出来,温故而知新. 1,在只使用一条语句即可做到时避免使用多条语句 让我们从一个简单的 ...

  10. jQuery的ready与js的load事件的区别

    摘自:http://www.cnblogs.com/see7di/archive/2011/07/15/2239677.html 为了理解这两个事件的异同,读者应该先了解HTML文档加载的顺序. DO ...