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. vue使用jsonp

    axios不支持jsonp,所以需使用其他插件:vue-jsonp npm i vue-jsonp -S 然后在 src/main.js : import Vue from 'vue' import ...

  2. 【例题 6-20 UVA - 1599】Ideal Path

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 逆向做一遍bfs. 得到终点到某个点的最短距离. 这样,我们从起点顺序的时候. 就能知道最短路的下一步是要走哪里了. 这样,我们从起 ...

  3. Linux 从core信息中找到TLS信息

    背景 我们在查core问题时,有时候须要查看某个TLS变量的值.可是GDB没有提供直接的命令,或者我不知道.这篇文字的目的.就是想办法从core文件里找出某个线程存放TLS变量的内容. 依据 Linu ...

  4. 忍者无敌-实例解说Cocos2d-x瓦片地图

    实例比較简单,如图所看到的,地图上有一个忍者精灵,玩家点击他周围的上.下.左.右,他能够向这个方向行走. 当他遇到障碍物后是无法穿越的,障碍物是除了草地以为部分,包含了:树.山.河流等. 忍者实例地图 ...

  5. git stash备份当前工作区内容,回到最近一次commit提交

    git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致.同时,将当前的工作区内容保存到Git栈中.git stash pop: 从Git栈中读取 ...

  6. LeetCode Algorithm 133_Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. JS预解释的总结

    预解释阶段发生在创建了堆内存,让代码执行之前,对当前作用域中带var和function的进行预解释 在浏览器解析执行代码的时候,会提前把带var和function的代码声明或定义,提前放在作用域的最前 ...

  8. apache与IIS共用80端口冲突解决方法

    如果同一台电脑安装了apache和iis,会提示80端口冲突,如何解决apache与iis 80端口冲突的问题呢,并且同时使用apache和iis 将apache设为使用80端口,IIS使用其它端口, ...

  9. java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: student is not mapped

    Spring 5.0 +Jpa,使用@Query实现 自定义查询报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.a ...

  10. 一文看懂AI芯片竞争五大维度

    下一波大趋势和大红利从互联网+让位于人工智能+,已成业界共识.在AI的数据.算法和芯片之三剑客中,考虑到AI算法开源的发展趋势,数据与芯片将占据越来越重要的地位,而作为AI发展支柱的芯片更是AI业的竞 ...