实现输出当前目录下的文件名

ls功能:

方法一:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8. #include <string>
  9. using namespace std;
  10.  
  11. bool cmp( string s1,string s2){
  12. return s1<s2;
  13. }
  14.  
  15. int main()
  16. {
  17. DIR *dir;
  18. char s[];
  19. string data[];
  20. int tot=;
  21. struct dirent *rent;
  22. dir =opendir(".");
  23. while(rent=readdir(dir))
  24. {
  25. strcpy(s,rent->d_name);
  26. if(s[]!='.'){
  27. data[tot]=s;
  28. tot++;
  29. }
  30. }
  31. sort(data,data+tot,cmp);
  32. for(int i=;i<tot;i++)
  33. cout<<data[i]<<" ";
  34. puts("");
  35. closedir(dir);
  36. return ;
  37. }

方法二:

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <pwd.h>
  8. #include <grp.h>
  9. #include <time.h>
  10. #include <dirent.h>
  11. int do_ls(char *dir,char *filename,int lflag)
  12. {
  13. int n;
  14. struct stat buf;
  15. char out[];
  16. struct passwd *pw;
  17. struct group *gr;
  18. struct tm *t;
  19. if(lflag == ) //如果不带l参数,直接显示文件/目录名
  20. {
  21. printf("%s\t",filename);
  22. return ;
  23. }
  24. if(lstat(dir,&buf)<)
  25. {
  26. fprintf(stderr,"stat error:%s\n",strerror(errno));
  27. return -;
  28. }
  29. switch(buf.st_mode & S_IFMT) //获取字符串的属性:普通文件-、目录d、字符设备c、块设备b、管道文件p、连接文件l、套接字文件s
  30. {
  31. case S_IFREG:
  32. printf("-");
  33. break;
  34. case S_IFDIR:
  35. printf("d");
  36. break;
  37. case S_IFCHR:
  38. printf("c");
  39. break;
  40. case S_IFBLK:
  41. printf("b");
  42. break;
  43. case S_IFIFO:
  44. printf("p");
  45. break;
  46. case S_IFLNK:
  47. printf("l");
  48. break;
  49. case S_IFSOCK:
  50. printf("s");
  51. break;
  52. }
  53. for(n=; n>=; n--) //打印文件的读写属性:读r、写w、执行x、无权限-
  54. {
  55. if(buf.st_mode&(<<n))
  56. {
  57. switch(n%)
  58. {
  59. case :
  60. printf("r");
  61. break;
  62. case :
  63. printf("w");
  64. break;
  65. case :
  66. printf("x");
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. printf("-");
  75. }
  76. }
  77. printf(" %d",buf.st_nlink); //硬链接数,此链接非彼链接,指(包含)目录的个数,文件为1,目录起始为2,再加上目录里包含的目录个数(不递归,只一层)
  78. pw = getpwuid(buf.st_uid); //所属用户名
  79. printf(" %s",pw->pw_name);
  80. gr = getgrgid(buf.st_gid); //所属组名
  81. printf(" %s",gr->gr_name);
  82. printf(" %ld",buf.st_size); //字节计总大小
  83. t = localtime(&buf.st_atime); //最后一次访问时间
  84. printf(" %d-%d-%d %d:%d"
  85. ,t->tm_year+
  86. ,t->tm_mon+
  87. ,t->tm_mday
  88. ,t->tm_hour
  89. ,t->tm_min);
  90. printf(" %s ",filename);
  91. if(S_ISLNK(buf.st_mode)) //判断是否为链接,是返回真
  92. {
  93. printf(" -> ");
  94. if(readlink(filename,out,)==-)
  95. {
  96. //printf("readlink error\n");
  97. }
  98. printf("%s",out);
  99. }
  100. printf("\n");
  101. return ;
  102. }
  103. int ls_prepare(char *w,int aflag,int lflag) //ls的准备工作
  104. {
  105. struct stat buf; //man lstat可以看到此结构
  106. char name[];
  107. DIR *dir; //类似打开文件的fd描述符
  108. struct dirent *pdr; //man readdir可以看到此结构
  109. if(lstat(w,&buf)<) //获取文件/目录属性并赋值给buf,该函数和lstat一样,只是当w为链接时,指代他本身,并不存在文件
  110. {
  111. fprintf(stderr,"stat error:%s\n",strerror(errno));
  112. return -;
  113. }
  114. if(S_ISDIR(buf.st_mode)) //判断是否为目录,是返回真
  115. {
  116. dir = opendir(w); //打开目录
  117. while ((pdr = readdir(dir))!=NULL) //读/遍历目录
  118. {
  119. if(aflag==) //如果不带a参数,越过以.开头的所有文件/目录
  120. {
  121. if(pdr->d_name[]=='.')
  122. continue;
  123. memset(name,,);
  124. strcpy(name,w); //拷贝
  125. strcat(name,"/"); //追加
  126. strcat(name,pdr->d_name);
  127. do_ls(name,pdr->d_name,lflag);
  128. }
  129. else //有a参数显示所有
  130. {
  131. memset(name,,);
  132. strcpy(name,w);
  133. strcat(name,"/");
  134. strcat(name,pdr->d_name);
  135. do_ls(name,pdr->d_name,lflag);
  136. }
  137. }
  138. closedir(dir);
  139. }
  140. else //为文件则直接显示
  141. {
  142. do_ls(w,w,lflag);
  143. }
  144. return ;
  145. }
  146. int main(int argc,char **argv)
  147. {
  148. int aflag =;
  149. int lflag =;
  150. char c;
  151. int i;
  152. while((c = getopt(argc,argv,"al"))!=-) //解析命令行参数,即-/--后面的字符串和给定的字符串匹配,有未解析字母返回字母或问号(取决于第3个参数),否则返回-1
  153. {
  154. switch(c) //此处仅匹配a(所有)和l(列表),即只支持参数a、l
  155. {
  156. case 'a':
  157. aflag =;
  158. break;
  159. case 'l':
  160. lflag =;
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. if(argc == optind )
  167. {
  168. ls_prepare("./",aflag,lflag);
  169. }
  170. else
  171. {
  172. for(i=optind; i<argc; i++) //所有目录都传进去
  173. ls_prepare(argv[i],aflag,lflag);
  174. }
  175. printf("\n");
  176. return ;
  177. }

ls -l功能:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <pwd.h>
  6. #include <grp.h>
  7.  
  8. void show_file_info(char* filename, struct stat* info_p) {
  9. char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
  10. void mode_to_letters();
  11. char modestr[11];
  12.  
  13. mode_to_letters(info_p->st_mode, modestr);
  14.  
  15. printf("%s", modestr);
  16. printf(" %4d", (int) info_p->st_nlink);
  17. printf(" %-8s", uid_to_name(info_p->st_uid));
  18. printf(" %-8s", gid_to_name(info_p->st_gid));
  19. printf(" %8ld", (long) info_p->st_size);
  20. printf(" %.12s", 4 + ctime(&info_p->st_mtime));
  21. printf(" %s\n", filename);
  22.  
  23. }
  24.  
  25. void mode_to_letters(int mode, char str[]) {
  26. strcpy(str, "----------");
  27.  
  28. if (S_ISDIR(mode)) {
  29. str[0] = 'd';
  30. }
  31.  
  32. if (S_ISCHR(mode)) {
  33. str[0] = 'c';
  34. }
  35.  
  36. if (S_ISBLK(mode)) {
  37. str[0] = 'b';
  38. }
  39.  
  40. if ((mode & S_IRUSR)) {
  41. str[1] = 'r';
  42. }
  43.  
  44. if ((mode & S_IWUSR)) {
  45. str[2] = 'w';
  46. }
  47.  
  48. if ((mode & S_IXUSR)) {
  49. str[3] = 'x';
  50. }
  51.  
  52. if ((mode & S_IRGRP)) {
  53. str[4] = 'r';
  54. }
  55.  
  56. if ((mode & S_IWGRP)) {
  57. str[5] = 'w';
  58. }
  59.  
  60. if ((mode & S_IXGRP)) {
  61. str[6] = 'x';
  62. }
  63.  
  64. if ((mode & S_IROTH)) {
  65. str[7] = 'r';
  66. }
  67.  
  68. if ((mode & S_IWOTH)) {
  69. str[8] = 'w';
  70. }
  71.  
  72. if ((mode & S_IXOTH)) {
  73. str[9] = 'x';
  74. }
  75. }
  76.  
  77. char* uid_to_name(uid_t uid){
  78. struct passwd* getpwuid(),* pw_ptr;
  79. static char numstr[10];
  80.  
  81. if((pw_ptr = getpwuid(uid)) == NULL){
  82. sprintf(numstr,"%d",uid);
  83.  
  84. return numstr;
  85. }else{
  86. return pw_ptr->pw_name;
  87. }
  88. }
  89.  
  90. char* gid_to_name(gid_t gid){
  91. struct group* getgrgid(),* grp_ptr;
  92. static char numstr[10];
  93.  
  94. if(( grp_ptr = getgrgid(gid)) == NULL){
  95. sprintf(numstr,"%d",gid);
  96. return numstr;
  97. }else{
  98. return grp_ptr->gr_name;
  99. }
  100. }
  101. void do_ls(char dirname[]) {
  102. DIR* dir_ptr;
  103. struct dirent* direntp;
  104.  
  105. if ((dir_ptr = opendir(dirname)) == NULL) {
  106. fprintf(stderr, "ls2: cannot open %s \n", dirname);
  107. } else {
  108. while ((direntp = readdir(dir_ptr)) != NULL) {
  109. dostat(direntp->d_name);
  110. }
  111.  
  112. close(dir_ptr);
  113. }
  114. }
  115.  
  116. void dostat(char* filename) {
  117. struct stat info;
  118.  
  119. if (stat(filename, &info) == -1) {
  120. perror(filename);
  121. } else {
  122. show_file_info(filename, &info);
  123. }
  124. }
  125.  
  126. int main(int ac,char* av[]){
  127. if(ac == 1){
  128. do_ls(".");
  129. }else{
  130. while(--ac){
  131. printf("%s: \n",*++av);
  132. do_ls(*av);
  133. }
  134. }
  135. }

  

用C++实现Linux中shell的ls功能的更多相关文章

  1. Linux中Shell

    Linux中Shell Shell是什么 ​ Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...

  2. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  3. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释:  变量说明:  $$  Shell本身的PID(ProcessID ...

  4. Linux中shell变量$0,$?等含义

    linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  5. 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释

    原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...

  6. 【Shell】linux中shell变量$#,$@,$0,$1,$2的含义解释 && set 关键字使用

    linux中shell变量$#,$@,$0,$1,$2的含义解释   摘抄自:ABS_GUIDE 下载地址:http://www.tldp.org/LDP/abs/abs-guide.pdf linu ...

  7. linux中shell变量$#,$@,$0,$1,$2的含义

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  8. linux中shell变量$#,$@,$0,$1,$2

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  9. 浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释

    浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释 下面小编就为大家带来一篇浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释.小编觉得挺不错的,现在就分享给 ...

随机推荐

  1. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  2. 再讲IQueryable<T>,揭开表达式树的神秘面纱

    接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个风生水起,感觉不整个自己的orm都不好意思继续混博客园了(开个玩笑).那么 ...

  3. 学习ASP.NET Core, 怎能不了解请求处理管道[4]: 应用的入口——Startup

    一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间件构成的,所以应用启动过程中一 ...

  4. MVC如何使用开源分页插件shenniu.pager.js

    最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...

  5. WebApi基于Token和签名的验证

    最近一段时间在学习WebApi,涉及到验证部分的一些知识觉得自己并不是太懂,所以来博客园看了几篇博文,发现一篇讲的特别好的,读了几遍茅塞顿开(都闪开,我要装逼了),刚开始读有些地方不理解,所以想了很久 ...

  6. MediatorPattern(中介者模式)

    /** * 中介者模式 * @author TMAC-J * 研究了这么多设计模式,觉得无非就是几点: * 1.若两个类有耦合关系,设立一个中间类,处理两个类的关系,把两个类的耦合降低 * 2.面向接 ...

  7. Atitit.如何建立研发体系

    Atitit.如何建立研发体系 组织,流程,prj..Mana  oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...

  8. Web前端需要熟悉大学里【高大上】的计算机专业课吗?

    作为一名刚刚大学毕业,进入新的学习阶段的研究生,我必须说大学的专业课非常重要!不管你信不信,事实就是如此! 一.大学学习的专业课非常重要,它决定了我们能走到什么高度 前端的发展非常快,我常常觉得刚刚关 ...

  9. 第14章 Linux启动管理(1)_系统运行级别

    1. CentOS 6.x 启动管理 (1)系统运行级别 ①运行级别 运行级别 含义 0 关机 1 单用户模式,可以想象为Windows的安全模式,主要用于系统修复.(但不是Linux的安全模式) 2 ...

  10. JAVA通信系列二:mina入门总结

    一.学习资料 Mina入门实例(一) http://www.cnblogs.com/juepei/p/3939119.html Mina入门教程(二)----Spring4 集成Mina http:/ ...