2023/2/23 提取文件相关属性
#include "head.h"
void get_filePermiss(mode_t mode)
{
int i=0;
int temp = 0400;
while(i<3)
{
(mode & temp)?putchar('r'):putchar('-');
temp=temp >> 1;
(mode & temp)?putchar('w'):putchar('-');
temp=temp >> 1;
(mode & temp)?putchar('x'):putchar('-');
temp=temp >> 1;
i++;
}
}
char *get_filemode(mode_t mode,char *p)
{
int i=0;
int n=0;
int temp = 0400;
while(i<3)
{
(mode & temp)?(p[n++]='r'):(p[n++]='-');
temp=temp >> 1;
(mode & temp)?(p[n++]='w'):(p[n++]='-');
temp=temp >> 1;
(mode & temp)?(p[n++]='x'):(p[n++]='-');
temp=temp >> 1;
i++;
}
p[n]='\0';
return p;
}
int main(int argc, const char *argv[])
{
struct stat buf;
if(stat(argv[1],&buf) < 0)
{
ERR_MSG("stat");
return -1;
}
get_filePermiss(buf.st_mode);
char str[20]="";
char *m=get_filemode(buf.st_mode,str);
printf("%s\n",m);
printf("mode:0%o\n",buf.st_mode);
printf("nlink:%lu\n",buf.st_nlink);
struct passwd *pw = getpwuid(buf.st_uid);
struct group *gr = getgrgid(buf.st_gid);
if(pw != NULL && gr != NULL)
{
printf("%s %s\n",pw->pw_name,gr->gr_name);
}
printf("uid:%d,gid:%d\n",buf.st_uid,buf.st_gid);
printf("size:%ld\n",buf.st_size);
char *mon[12]={"一月","二月","三月","四月","五月","六月",\
"七月","八月","九月","十月","十一月","十二月"};
struct tm *t1 = localtime(&buf.st_ctime);
printf("%s %02d %02d:%02d %s\n",mon[t1->tm_mon],t1->tm_mday,t1->tm_hour,\
t1->tm_min,argv[1]);
printf("time:%ld\n",buf.st_ctime);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c4a76f5957cd594e1ddae9d2608dc1e7.png)