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

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

    ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...

  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. rocketmq事务消息入门介绍

    说明 周五的时候发了篇:Rocketmq4.3支持事务啦!!!,趁着周末的时候把相关内容看了下,下面的主要内容就是关于RocketMQ事务相关内容介绍了. 说明: 今天这篇仅仅是入门介绍,并没有涉及到 ...

  2. Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

    package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...

  3. 最小二乘法,python3实现

    https://www.cnblogs.com/BlogOfMr-Leo/p/8627311.html                      [用的是库函数] https://blog.csdn. ...

  4. HDU 4508 湫湫系列故事——减肥记I (2013腾讯编程马拉松初赛第一场)

    http://acm.hdu.edu.cn/showproblem.php?pid=4508 题目大意: 给定一些数据. 每组数据以一个整数n开始,表示每天的食物清单有n种食物.  接下来n行,每行两 ...

  5. 【前端统计图】echarts实现属性修改

    原图: 原代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  6. 项目太大tomcat启动不起来

    双击server,Open launch configuration Arguments VM arguments增加参数: -Xms512m -Xmx1024m -Xss4m -XX:PermSiz ...

  7. R语言-回归

    定义: 回归是统计学的核心,它其实是一个广义的概念,通常指那些用一个或多个预测变量来预测响应变量.既:从一堆数据中获取最优模型参数 1.线性回归 1.1简单线性回归 案例:女性预测身高和体重的关系 结 ...

  8. t_user is not mapped [from t_user as u where u.loginname = :loginname and u.password =:password]

    转自:https://blog.csdn.net/u010876380/article/details/52714539 错误: Struts Problem Report Struts has de ...

  9. 重新启动IIS不重启电脑

      有时候我们在WEB程序如:ASP,中无意中使用到了一个死循环,或者在测试 DLL组件时,挂了.这时候IIS就停止了响应,我们要继续我们的工作啊,重启IIS服务吧. 然而这个进程还在执行,Inter ...

  10. angular 设置全局常量

    一:在项目核心文件core.module.ts中设置全局静态常量 解释:相当于自动注入到inject中. providers:[ { provide:'BASE_CONFIG', useValue:' ...