#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
int main(int argc, char const *argv[])
{
struct stat s;
if (stat("ccc.c", &s) < 0)
{
perror("stat err");
return -1;
}
printf("%lu\n", s.st_ino);
printf("%lu\n", s.st_size);
printf("%#o\n", s.st_mode);
switch (s.st_mode & S_IFMT) //判断文件类型
{
case S_IFSOCK:
printf("s");
break;
case S_IFCHR:
printf("c");
break;
case S_IFLNK:
printf("l");
break;
case S_IFREG:
printf("-");
break;
case S_IFBLK:
printf("b");
break;
case S_IFDIR:
printf("d");
break;
case S_IFIFO:
printf("p");
break;
}
for (int n = 8; n >= 0; n--) //判断文件权限
if (s.st_mode & (1 << n))
switch (n % 3)
{
case 2:
printf("r");
break;
case 1:
printf("w");
break;
case 0:
printf("x");
break;
}
else
{
printf("-");
}
char buf[4]="rwx-";//判断文件权限
for(int i =0;i<9;i++)
{
printf("%c",(s.st_mode& (0400>>i))? buf[i%3]:buf[3])
}
printf(" %d", s.st_nlink); //硬链接数
//用户和组
struct passwd *p; //用户
p = getpwuid(s.st_uid);
printf(" %s", p->pw_name);
struct group *q; //组
q = getgrgid(s.st_gid);
printf(" %s", q->gr_name);
printf(" %ld", s.st_size); //查看大小
struct tm *now_time;//最后访问时间
now_time = localtime(&s.st_mtime);
printf(" %d-%d-%d %d:%d", now_time->tm_year + 1900,
now_time->tm_mon + 1,
now_time->tm_mday, now_time->tm_hour,
now_time->tm_min);
printf("%s "," ccc.c");//打印文件名
return 0;
}