#include"head.h"
// vsp xxx
int main(int argc, const char *argv[])
{
struct stat buf;
if(stat("./1.c",&buf)<0)
{
perror("stat");
return -1;
}
//见 man 2 stat
//文件类型以及权限
printf("mode:0%o\n",buf.st_mode);
# define MODE buf.st_mode //定义一个 图省事
int a=0400;
int i;
char str[10]="";
for(i=0;i<9;i++)
{
if((a&MODE)!=0) //这里要括号,否则有优先级问题,先算MODE!=0得1或者0,再与a
{
switch (i%3)
{
case 0:str[i]='r';break;
case 1:str[i]='w';break;
case 2:str[i]='x';break;
}
}
else str[i]='-';
a=a>>1;
}
puts(str);
}#ifndef __head_h__
#define __head_h__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
该代码示例使用C语言的stat函数检查文件1.c的权限和类型,然后将文件模式转化为可读的rwx表示形式。程序首先定义结构体statbuf,通过stat函数获取文件信息,接着分析文件模式并打印出来。
737

被折叠的 条评论
为什么被折叠?



