stat函数的文件状态查询代码示例
#include<stdio.h>
#include<time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
struct stat buf;
int n;
time_t t;
struct tm *tp;
if(argc!=2)
{
printf("Usage %s <str_file>\n",argv[0]);
return -1;
}
if(stat(argv[1],&buf)==-1)
{
perror("stat");
return -1;
}
switch(buf.st_mode & S_IFMT)
{
case S_IFREG:
printf("-");
break;
case S_IFDIR:
printf("d");
break;
}
for(n=8;n>=0;n--)
{
if(buf.st_mode & (1<<n))
{
switch(n%3)
{
case 2:
printf("r");
break;
case 1:
printf("w");
break;
case 0:
printf("x");
break;
}
}
else
{
printf("-");
}
}
printf("%lu",buf.st_size);
printf(" ");
tp = localtime(&buf.st_mtime);
printf("%d-%02d-%02d %02d:%02d:%02d\n",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
return 0;