Chinaunix首页 | 论坛 | 博客
  • 博客访问: 752953
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: C/C++

2012-10-22 17:49:47

mode可以从stat获得:
struct stat info;
stat(filename_ptr, &info);
int mode = info.st_mode; //获得的是八进制的字符串

  1. void mode_to_letters( int mode, char str[] )
  2. {
  3.     strcpy( str, "----------" ); /* default=no perms */

  4.     if ( S_ISDIR(mode) ) str[0] = 'd'; /* directory? */
  5.     if ( S_ISCHR(mode) ) str[0] = 'c'; /* char devices */
  6.     if ( S_ISBLK(mode) ) str[0] = 'b'; /* block device */

  7.     if ( mode & S_IRUSR ) str[1] = 'r'; /* 3 bits for user */
  8.     if ( mode & S_IWUSR ) str[2] = 'w';
  9.     if ( (mode & S_ISUID) && (mode & S_IXUSR) )
  10.     str[3] = 's';
  11.     else if ( (mode & S_ISUID) && !(mode & S_IXUSR) )
  12.     str[3] = 'S';
  13.     else if ( mode & S_IXUSR )
  14.     str[3] = 'x';

  15.     if ( mode & S_IRGRP ) str[4] = 'r'; /* 3 bits for group */
  16.     if ( mode & S_IWGRP ) str[5] = 'w';
  17.     if ( (mode & S_ISGID) && (mode & S_IXGRP) )
  18.     str[6] = 's';
  19.     else if ( (mode & S_ISGID) && !(mode & S_IXGRP) )
  20.     str[6] = 'S';
  21.     else if ( mode & S_IXGRP )
  22.     str[6] = 'x';

  23.     if ( mode & S_IROTH ) str[7] = 'r'; /* 3 bits for other */
  24.     if ( mode & S_IWOTH ) str[8] = 'w';
  25.     if ( (mode & S_ISVTX) && (mode & S_IXOTH) )
  26.     str[9] = 't';
  27.     else if ( (mode & S_ISVTX) && !(mode & S_IXOTH) )
  28.     str[9] = 'T';
  29.     else if ( mode & S_IXOTH )
  30.     str[9] = 'x';
  31. }
类似于 S_IFDIR 这样的宏可以在 stat.h 中找到
stat.h:#define S_IFDIR  0040000
阅读(925) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~