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. android 4.4 添加物理按键

    kernel下添加 Linux-3.4/drivers/input/keyboard/Makefile linux-3.4/drivers/input/keyboard/sw-keyboard.c s ...

  2. Java Web学习总结(13)——JSP入门

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  3. COGS——C66. [HAOI2004模拟] 数列问题

    http://www.cogs.pro/cogs/problem/problem.php?pid=66 ★☆   输入文件:dfs3.in   输出文件:dfs3.out   简单对比 时间限制:1 ...

  4. 硬件——STM32 , 软件框架

    单片机应用程序的框架大概有三种: 1,简单的前后台顺序执行程序. 2,时间片轮询法. 3,应用操作系统. 下面我们主要来讲解时间片轮询法: 在这里我们先介绍一下定时器的复用功能.就是使用1个定时器,可 ...

  5. python3 turtle 画国际象棋棋盘

    python3 turtle 画国际象棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan import turt ...

  6. LoadRunner--HTML与URL录制方式区别

    Recording录制选项 这里提供了两个大类的录制方式: 1. HTML-based script基于HTML的脚本 这种方式录制出来的脚本是基于HTML基础的,为每个用户操作生成单独的步骤,这种脚 ...

  7. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

  8. VMware linux虚拟机在线识别新添加磁盘

    登录进虚拟机linux系统中执行以下命令,识别新增加的硬盘 echo "- - -" > /sys/class/scsi_host/host0/scan # ls /sys/ ...

  9. [CSS] Use CSS Counters to Create Pure CSS Dynamic Lists

    CSS counters let you create dynamic lists without JavaScript. In this lesson, we will create a multi ...

  10. php对象和数组的相互转换(还是可以去找没有没php的高阶课程看看看)(要不别人分析一下重点要点,要不自己来,不然 效果真的不好)

    php对象和数组的相互转换(还是可以去找没有没php的高阶课程看看看)(要不别人分析一下重点要点,要不自己来,不然 效果真的不好) 一.总结 都是自己实现的函数 算法: 1.先判断类型,gettype ...