自己动手写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/ 文 ...
随机推荐
- 将Jar安装到本地仓库和Jar上传到私服
举例 1. 依赖如下: <dependency> <groupId>org.quartz-scheduler.internal</groupId> <arti ...
- SQL Server中 sysobjects、sysolumns、systypes
1.sysobjects 系统对象表. 保存当前数据库的对象,如约束.默认值.日志.规则.存储过程等 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.x ...
- baidu让用户更快看到首页
//让用户更快看到首页 if(!location.hash.match(/[^a-zA-Z0-9]wd=/)) { document.getElementById("wrapper" ...
- Linux CentOS7.5静默安装Oracle11gR2
网上有很多安装教程,但大多不够完整,参照了一些教程,实测安装成功,整理出来分享给大家! 一.官方最低要求配置 内存:1G(官方最低要求1G) 硬盘:40G(企业版安装所需4.29G和1.7G数据文件) ...
- assert.deepStrictEqual()
assert.deepStrictEqual(actual, expected[, message]) 一般情况下等同于 assert.deepEqual(),但有两个例外.首先,原始值是使用全等运算 ...
- 【02】HTML5与CSS3基础教程(第8版)(全)
[02]HTML5与CSS3基础教程(第8版)(全) 共392页. (魔芋:大体上扫了一遍.没有什么新东西,都是入门的一些基础知识.) 已看完. [美]elizabeth cast ...
- Python安装配置
Python下载 官网下载地址:https://www.python.org/downloads/windows/ 下载安装包: python-3.5.0-amd64(64位).exe python- ...
- BNUOJ 5629 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 1 ...
- 587. Erect the Fence
Problem statement: There are some trees, where each tree is represented by (x,y) coordinate in a two ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...