利用stat函数实现
int stat(const char *path, struct stat *buf)
我们来看程序
8 #define N_BITS 3
9
10 int main(int argc,char *argv[])
11 {
12 unsigned int i,mask = 0700;
13 struct stat buff;
W> 14 static char *perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
15 if(argc>1)
16 {
17 if((stat(argv[1],&buff)!=-1))
18 {
19 printf("permissions for %s\t",argv[1]);
20 for(i = 3;i;--i)
21 {
22 printf("%3s",perm[(buff.st_mode&mask)>>(i-1)*N_BITS]);
23 mask>>=N_BITS;
24 }
25 putchar('\n');
26 }
27 else{
28 perror(argv[1]);
29 exit(EXIT_FAILURE);
30
31 }
32 }
33 else{
34 fprintf(stderr,"Usage:%s file_name\n",argv[0]);
35 }
36
37 return 0;
38 }
测试程序
运行程序
可以看到实现效果相同。