实现自己的ls

来源:互联网 发布:上海淘宝公司 编辑:程序博客网 时间:2024/05/29 19:29

该程序实现的是ls -al

[cpp] view plaincopy
  1. #include    <stdio.h>  
  2. #include    <stdlib.h>  
  3. #include    <unistd.h>  
  4. #include    <sys/types.h>  
  5. #include    <sys/stat.h>  
  6. #include    <grp.h>  
  7. #include    <pwd.h>  
  8. #include    <time.h>  
  9. #include    <string.h>  
  10. #include    <dirent.h>  
  11. //从path中获取文件名  
  12. void getFileName(char* fileName, const char* path)  
  13. {  
  14.     if( NULL==fileName || NULL==path)  
  15.     {  
  16.         printf("in getFileName, args error!/n");  
  17.         exit(-1) ;  
  18.     }  
  19.     int len=strlen(path);  
  20.     int pos=len-1;  
  21.     while(pos>=0 && path[pos]!='/')  
  22.     {  
  23.         pos--;  
  24.     }  
  25.     strcpy(fileName, path+pos+1 );  
  26.       
  27. }  
  28.   
  29. //显示某个文件的详细信息  
  30. void showFileInfo(const char* path)  
  31. {  
  32.     struct stat buf;  
  33.     int ret=lstat(path, &buf);  
  34.     if( -1==ret )  
  35.     {  
  36.         perror("in showFileInfo, lstat error:");  
  37.         printf("%s/n", path);  
  38.         return ;  
  39.     }  
  40.       
  41.     //输出文件类型  
  42.     //文件类型只判断普通、目录、软链接,其它类似  
  43.     if( S_ISDIR(buf.st_mode) )  
  44.     {  
  45.         printf("d");  
  46.     }  
  47.     else if( S_ISLNK(buf.st_mode) )  
  48.     {  
  49.         printf("l");  
  50.     }  
  51.     else  
  52.     {  
  53.         printf("-");  
  54.     }  
  55.     //输出权限:owner  
  56.     if(S_IRUSR&buf.st_mode )  
  57.     {  
  58.         printf("r");  
  59.     }  
  60.     else  
  61.     {  
  62.         printf("-");  
  63.     }  
  64.     if(S_IWUSR&buf.st_mode)  
  65.     {  
  66.         printf("w");  
  67.     }  
  68.     else  
  69.     {  
  70.         printf("-");  
  71.     }  
  72.     if(S_IXUSR&buf.st_mode)  
  73.     {  
  74.         printf("x");  
  75.     }  
  76.     else  
  77.     {  
  78.         printf("-");  
  79.     }  
  80.     //输出权限:group  
  81.     if(S_IRGRP&buf.st_mode)  
  82.     {  
  83.         printf("r");  
  84.     }  
  85.     else  
  86.     {  
  87.         printf("-");  
  88.     }  
  89.     if(S_IWGRP&buf.st_mode)  
  90.     {  
  91.         printf("w");  
  92.     }  
  93.     else  
  94.     {  
  95.         printf("-");  
  96.     }  
  97.     if(S_IXGRP&buf.st_mode)  
  98.     {  
  99.         printf("x");  
  100.     }  
  101.     else  
  102.     {  
  103.         printf("-");  
  104.     }  
  105.     //输出权限:others  
  106.     if(S_IROTH&buf.st_mode)  
  107.     {  
  108.         printf("r");  
  109.     }  
  110.     else  
  111.     {  
  112.         printf("-");  
  113.     }  
  114.     if(S_IWOTH&buf.st_mode)  
  115.     {  
  116.         printf("w");  
  117.     }  
  118.     else  
  119.     {  
  120.         printf("-");  
  121.     }  
  122.     if(S_IXOTH&buf.st_mode)  
  123.     {  
  124.         printf("x");  
  125.     }  
  126.     else  
  127.     {  
  128.         printf("-");  
  129.     }  
  130.     //输出一个空格  
  131.     printf(" ");  
  132.       
  133.     //输出硬链接数  
  134.     printf("%d ", buf.st_nlink);  
  135.       
  136.     //输出用户  
  137.     struct passwd* pwd=getpwuid(buf.st_uid);  
  138.     printf("%s ", pwd->pw_name);  
  139.     //输出组  
  140.     struct group* grp=getgrgid(buf.st_gid);  
  141.     printf("%s ",grp->gr_name);  
  142.     //输出文件大小  
  143.     printf("%d ", (int)buf.st_size);  
  144.       
  145.     //输出最后一次修改时间  
  146.     struct tm* t=localtime(&(buf.st_mtime));  
  147.     printf("%4d-%02d-%02d %02d:%02d ", t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min);  
  148.     //输出文件名  
  149.     //如果文件是软链接,请同学们自己完善文件名的输出  
  150.     int len=strlen(path);  
  151.     char* fileName=(char*)malloc(len+1);  
  152.     getFileName(fileName, path);  
  153.     printf("%s", fileName);  
  154.     free(fileName);  
  155.     printf("/n");  
  156. }  
  157. //实现myls的功能  
  158. void myls(const char* path )  
  159. {  
  160.     DIR* pDir=NULL;  
  161.     struct dirent* pde=NULL;  
  162.     pDir=opendir(path);  
  163.     if( NULL==pDir )  
  164.     {  
  165.         showFileInfo(path);  
  166.     }  
  167.     else  
  168.     {  
  169.         int ret=chdir(path);  
  170.         if(ret!=0)  
  171.         {  
  172.             perror("in myls, chdir error:");  
  173.             exit(-1);  
  174.         }  
  175.         while( (pde=readdir(pDir))!=NULL )  
  176.         {  
  177.             showFileInfo(pde->d_name);  
  178.         }  
  179.     }  
  180.     closedir(pDir);  
  181.     pDir=NULL;  
  182. }  
  183. int main(int argc, char *argv[])  
  184. {  
  185.     if(argc!=2)  
  186.     {  
  187.         printf("in main, args error!/n");  
  188.         exit(-1);  
  189.     }  
  190.     myls(argv[1]);  
  191.   
  192.     return 0;  

0 0
原创粉丝点击