将白天提取到的文件的属性格式进行整理,贴近为ls -l的格式,代码,现象上交
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<time.h>
#include<pwd.h>
#include<grp.h>
void modex(mode_t mode)
{
int a=0400;
int flag=0;
while(a!=0)
{
if((mode&a)==0)
{
putchar('-');
}
else
{
switch(flag%3)
{
case 0:putchar('r');break;
case 1:putchar('w');break;
case 2:putchar('x');break;
}
}
a=a>>1;
flag++;
}
}
int main(int argc, const char *argv[])
{
//获取文件的所有属性
struct stat buf;
if(stat("./fileno.c",&buf)<0)
{
perror("stat");
return -1;
}
//文件类型及文件权限
printf("-");
modex(buf.st_mode);
printf(" %ld ",buf.st_nlink);
struct passwd *pwd=getpwuid(getuid());
struct group *grp = getgrgid(getgid());
printf("%s %s ",pwd->pw_name,grp->gr_name);
printf("%ld ",buf.st_size);
struct tm* info = localtime(&buf.st_ctime);
if(NULL==info)
{
perror("localtime");
return -2;
}
printf("%d月 %d %d:%d ",info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min);
printf(" fileno.c");
printf("\n");
return 0;
}
功能测试