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. vue之Render函数

    (1)什么是Render函数 先来看一个场景,在博客网中,一般有一级标题.二级标题.三级标题...,为了方便分享url,它们都做成了锚点,点击后,会将内容加载网址后面,以#分隔. 例如‘特性’是一个& ...

  2. 【原】简单shell练习(二)

    1.查找awk # cat /etc/passwd |awk -F ':' 'BEGIN {print "name,shell"} {print $1","$7 ...

  3. B3. Cocurrent 线程的状态

    [概述] 1). java.lang.Thread 类中定义了一个枚举 State, 定义了线程的六种状态:NEW.RUNNABLE.BLOCKED.WAITING.TIMED_WAITING.TER ...

  4. 【C语言】控制台窗口图形界面编程(八):键盘事件

    目录 00. 目录 01. INPUT_RECORD结构 02. KEY_EVENT_RECORD结构 03. ReadConsoleInput函数 04. 示例程序 00. 目录 01. INPUT ...

  5. thinkphp5验证码处理

    1.确定项目目录>vendor>topthink>think-captcha目录存在 2.在config中添加验证码配置 //验证码配置 'captcha' => [ // 验 ...

  6. python compare with other language

    java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...

  7. python Matplotlib 系列教程(三)——绘制直方图和条形图

    在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...

  8. 1 SQL 数据库和SQL

    1 数据库和SQL 1-1 数据库是什么 将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合称为数据库(Database, DB). 法则 用来管理数据库的计算机系统称为数据库管理系统 ...

  9. [WPF自定义控件库]为Form和自定义Window添加FunctionBar

    1. 前言 我常常看到同一个应用程序中的表单的按钮----也就是"确定"."取消"那两个按钮----实现得千奇百怪,其实只要使用统一的Style起码就可以统一按 ...

  10. MySQL-Transfer2.2发布

    http://dinglin.iteye.com/blog/1888640 Transfer 2.2发布.下载地址 版本说明 1.  基于版本 Percona-5.5.31 ,简单用法是先安装好官方或 ...