Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388953
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2015-11-27 22:47:13


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>

  6. #include <pwd.h>
  7. #include <grp.h>
  8. #include <time.h>

  9. void print_file_type(struct stat buf);
  10. void print_access_permission(struct stat buf);
  11. void print_access_permission_data(struct stat buf);

  12. int main(int argc,char **argv)
  13. {
  14.     if(2 !=argc)
  15.     {
  16.         printf("usage:%s file\n",argv[0]);
  17.         exit(1);
  18.     }

  19.     int ret_val = -1;
  20.     struct stat buf;
  21.     ret_val = lstat(argv[1],&buf);
  22.     if(-1 == ret_val)
  23.     {
  24.         perror("lstat");
  25.         exit(1);
  26.     }

  27.     print_file_type(buf);
  28.     print_access_permission(buf);
  29.     printf(" ");
  30.     /*打印链接数*/
  31.     printf("%d ",buf.st_nlink);

  32.     /*获取uid*/
  33.     printf("%s ",getpwuid(buf.st_uid)->pw_name);

  34.     /*获取gid*/
  35.     printf("%s ",getgrgid(buf.st_gid)->gr_name);

  36.     /*获取大小*/
  37.     printf("%ld ",buf.st_size);
  38.     

  39.     /*获取时间*/
  40.     struct tm *ptime;
  41.     ptime = localtime(&(buf.st_mtime));
  42.     printf("%d-%d-%d %d:%d",ptime->tm_year + 1900,ptime->tm_mon,
  43.             ptime->tm_mday,ptime->tm_hour,ptime->tm_min);

  44.     printf(" ");
  45.     printf("%s\n",argv[1]);
  46.     return 0;
  47.     
  48. }


  49. void print_file_type(struct stat buf)
  50. {
  51.     if(S_ISREG(buf.st_mode))
  52.     {
  53.         printf("-");
  54.     }
  55.     else if(S_ISDIR(buf.st_mode))
  56.     {
  57.         printf("d");
  58.     }
  59.     else if(S_ISCHR(buf.st_mode))
  60.     {
  61.         printf("c");
  62.     }
  63.     else if(S_ISBLK(buf.st_mode))
  64.     {
  65.         printf("b");
  66.     }
  67.     else if(S_ISFIFO(buf.st_mode))
  68.     {
  69.         printf("p");
  70.     }
  71.     else if(S_ISLNK(buf.st_mode))
  72.     {
  73.         printf("l");
  74.     }
  75.     else if(S_ISSOCK(buf.st_mode))
  76.     {
  77.         printf("s");
  78.     }
  79. }

  80. void print_access_permission(struct stat buf)
  81. {
  82.     /*用户的读写执行权限*/
  83.     if(S_IRUSR & buf.st_mode)
  84.     {
  85.         printf("r");
  86.     }
  87.     else
  88.     {
  89.         printf("-");
  90.     }

  91.     if(S_IWUSR & buf.st_mode)
  92.     {
  93.         printf("w");
  94.     }
  95.     else
  96.     {
  97.         printf("-");
  98.     }

  99.     if(S_IXUSR & buf.st_mode)
  100.     {
  101.         printf("x");
  102.     }
  103.     else
  104.     {
  105.         printf("-");
  106.     }

  107.     
  108.     /*组读写执行权限*/
  109.     if(S_IRGRP & buf.st_mode)
  110.     {
  111.         printf("r");
  112.     }
  113.     else
  114.     {
  115.         printf("-");
  116.     }
  117.     
  118.     if(S_IWGRP & buf.st_mode)
  119.     {
  120.         printf("w");
  121.     }
  122.     else
  123.     {
  124.         printf("-");
  125.     }

  126.     if(S_IXGRP & buf.st_mode)
  127.     {
  128.         printf("x");
  129.     }
  130.     else
  131.     {
  132.         printf("-");
  133.     }

  134.     /*其他的读写执行权限*/
  135.     if(S_IROTH & buf.st_mode)
  136.     {
  137.         printf("r");
  138.     }
  139.     else
  140.     {
  141.         printf("-");
  142.     }
  143.     
  144.     if(S_IWOTH & buf.st_mode)
  145.     {
  146.         printf("w");
  147.     }
  148.     else
  149.     {
  150.         printf("-");
  151.     }

  152.     if(S_IXOTH & buf.st_mode)
  153.     {
  154.         printf("x");
  155.     }
  156.     else
  157.     {
  158.         printf("-");
  159.     }        
  160. }

  161. /*
  162. 通过buf.st_mode的0-9位屏蔽字来
  163. 确定该文件的访问权限
  164. */
  165. void print_access_permission_data(struct stat buf)
  166. {
  167.     printf("buf:%x\n",buf.st_mode);
  168.     printf("S_IRUSR:%x\n",S_IRUSR);
  169.     printf("S_IWUSR:%x\n",S_IWUSR);
  170.     printf("S_IXUSR:%x\n",S_IXUSR);

  171.     printf("S_IRGRP:%x\n",S_IRGRP);
  172.     printf("S_IWGRP:%x\n",S_IWGRP);
  173.     printf("S_IXGRP:%x\n",S_IXGRP);

  174.     printf("S_IROTH:%x\n",S_IROTH);
  175.     printf("S_IWOTH:%x\n",S_IWOTH);
  176.     printf("S_IXOTH:%x\n",S_IXOTH);
  177. }
阅读(1212) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~