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. COGS——C2274. [HEOI 2016] tree

    http://www.cogs.pro/cogs/problem/problem.php?pid=2274 ★☆   输入文件:heoi2016_tree.in   输出文件:heoi2016_tre ...

  2. 洛谷 P1691 有重复元素的排列问题

    P1691 有重复元素的排列问题 题目描述 设R={r1,r2,……,rn}是要进行排列的n个元素.其中元素r1,r2,……,rn可能相同.使设计一个算法,列出R的所有不同排列. 给定n以及待排列的n ...

  3. JQuery EasyUI Combobox 实现省市二级联动菜单

    //编辑改动或新增页面联动能够这样写 jQuery(function(){ // 省级 $('#province').combobox({ valueField:'itemvalue', //值字段 ...

  4. leetcode笔记:Word Break

    一. 题目描写叙述 Given a string s and a dictionary of words dict, determine if s can be segmented into a sp ...

  5. Altium Designer如何对齐原件

    右边那个图标是排列菜单

  6. 写PPT的先扬后抑的思路

    近期给一个客户做IT战略规划. 基本结束了,客户要求写点有高度的东西.我想也是,尽管眼下的PPT也触及到战略和行业的问题,可是没有总结出来.于是就挖空心思,琢磨了三天.写了4页PPT.改动了几遍.还算 ...

  7. 10.1、android输入系统_必备Linux编程知识_inotify和epoll

    1. inotify和epoll 怎么监测键盘接入与拔出? (1)hotplug机制:内核发现键盘接入/拔出==>启动hotplug进程==>发消息给输入系统 (2)inotify机制:输 ...

  8. 【习题 5-2 UVA-1594】Ducci Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map加vector轻松搞定. [代码] #include <bits/stdc++.h> using namespac ...

  9. android权限详细

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

  10. Spring的资源抽象Resource2实体类

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0UAAAGkCAIAAABxYhnsAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWX ...