自己动手写shell命令之ls
linux下ls命令(支持-R參数)的c语言实现:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <string.h> void do_ls(char *);
void do_stat(char *,char *);
void show_file_info(char *,struct stat *,char *);
void mode_to_letters(int ,char []);
char * uid_to_name(uid_t);
char * gid_to_name(gid_t);
int recursive = 0; int main(int argc,char * argv[])
{
int i;
for(i = 1;i < argc;i++)
{
if(strcmp(argv[i],"-R") == 0)
{
recursive = 1;
break;
}
}
if(argc == 1 && recursive == 0)
do_ls(".");
else if(argc == 2 && recursive == 1)
do_ls(".");
else
{
int index = 1;
while(argc > 1)
{
if(strcmp(argv[index],"-R") != 0)
do_ls(argv[index]);
index++;
argc--;
}
}
return 0;
} void do_ls(char * path)
{
DIR * dir;
struct dirent * direntp;
if((dir = opendir(path)) != NULL)
{
while((direntp = readdir(dir)) != NULL)
{
char absolute_pathname[255];
strcpy(absolute_pathname,path);
strcat(absolute_pathname,"/");
strcat(absolute_pathname,direntp->d_name);
printf("%s\n",absolute_pathname);
do_stat(absolute_pathname,direntp->d_name);
}
closedir(dir);
}
else
fprintf(stderr,"can't open %s",path);
} void do_stat(char * absolute_filename,char * filename)
{
struct stat s;
if(lstat(absolute_filename,&s) == -1)
perror(absolute_filename);
else
show_file_info(absolute_filename,&s,filename);
} void show_file_info(char * absolute_filename,struct stat * info,char * filename)
{
char mode[11];
mode_to_letters(info->st_mode,mode);
printf("%s ",mode);
printf("%d ",info->st_nlink);
printf("%s ",uid_to_name(info->st_uid));
printf("%s ",gid_to_name(info->st_gid));
printf("%d ",info->st_size);
printf("%.12s ",4+ctime(&info->st_mtime));
printf("\n");
if(recursive == 1)
{
if(S_ISDIR(info->st_mode) && strcmp(filename,".") != 0 && strcmp(filename,"..") != 0)
do_ls(absolute_filename);
}
} void mode_to_letters(int mode,char * c_mode)
{
strcpy(c_mode,"----------"); if(S_ISDIR(mode))
c_mode[0] = 'd';
if(S_ISCHR(mode))
c_mode[0] = 'c';
if(S_ISBLK(mode))
c_mode[0] = 'b'; if(mode & S_IRUSR)
c_mode[1] = 'r';
if(mode & S_IWUSR)
c_mode[2] = 'w';
if(mode & S_IXUSR)
c_mode[3] = 'x'; if(mode & S_IRGRP)
c_mode[4] = 'r';
if(mode & S_IWGRP)
c_mode[5] = 'w';
if(mode & S_IXGRP)
c_mode[6] = 'x'; if(mode & S_IROTH)
c_mode[7] = 'r';
if(mode & S_IWOTH)
c_mode[8] = 'w';
if(mode & S_IXOTH)
c_mode[9] = 'x'; if(mode & S_ISUID)
c_mode[3] = 's';
if(mode & S_ISGID)
c_mode[6] = 's';
if(mode & S_ISVTX)
c_mode[9] = 's';
} char * uid_to_name(uid_t uid)
{
struct passwd * passwd_pointer;
passwd_pointer = getpwuid(uid);
return passwd_pointer->pw_name;
} char * gid_to_name(gid_t gid)
{ struct group * group_pointer;
static char numstr[10];
if((group_pointer = getgrgid(gid)) == NULL)
{
sprintf(numstr,"%d",gid);
return numstr;
}
return group_pointer->gr_name;
}
自己动手写shell命令之ls的更多相关文章
- 自己动手写shell命令之ls -R1fF
ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...
- 自己动手写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/ 文 ...
随机推荐
- Spark SQL概念学习系列之Spark SQL基本原理
Spark SQL基本原理 1.Spark SQL模块划分 2.Spark SQL架构--catalyst设计图 3.Spark SQL运行架构 4.Hive兼容性 1.Spark SQL模块划分 S ...
- python登录验证程序
自己写的一个python登录验证程序: 基础需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 升级需求: 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失 ...
- MFC单文档程序架构解析
MFC单文档程序架构解析 MFC单文档程序架构解析 这里我以科院杨老师的单文档程序来分析一下MFC单文档的程序架构,纯属个人见解,不当之处烦请指教! 首先我们了解到的是 图(一) theApp 是唯一 ...
- 【Codeforces Round #432 (Div. 1) A】 Five Dimensional Points
[链接]点击打开链接 [题意] 给你n个5维的点. 然后让你以其中的某一个点作为起点a. 另选两个点b,c. 组成向量a->b,a->c 如果所有的a->b和a->c的夹角都是 ...
- 51Nod——N1082 与7无关的数
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...
- vs2015 EF code first 问题待解决
在vs 2013 上可以成功ef 生成代码.EF power Tools 安装在vs 2015 :一般不可安装, 把扩展名改成zip,解压缩. 打开extension.vsixmanifest文件 找 ...
- Android 撕衣服(刮刮乐游戏)
项目简单介绍: 该项目为撕衣服,相似刮刮乐游戏 具体介绍: 用户启动项目后.载入一张图片,当用户点击图片的时候,点击的一片区域就会消失.从而显示出在这张图片以下的图片 这个小游戏相似与刮奖一样,刮开涂 ...
- amazeui学习笔记--css(常用组件12)--面板Panel
amazeui学习笔记--css(常用组件12)--面板Panel 一.总结 1.面板基本样式:默认的 .am-panel 提供基本的阴影和边距,默认边框添加 .am-panel-default,内容 ...
- Day2:数据类型
一.数字 1.整型(int),无长整型.python3.x,不论多大的数都是int #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuh ...
- 微服务实战(三):深入微服务架构的进程间通信 - DockOne.io
原文:微服务实战(三):深入微服务架构的进程间通信 - DockOne.io [编者的话]这是采用微服务架构创建自己应用系列第三篇文章.第一篇介绍了微服务架构模式,和单体式模式进行了比较,并且讨论了使 ...