#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void get_filePermissing(mode_t m){
int a = 256;
for(int i = 1; i < 10;i++){
if(0 == (m & a)){
printf("%c",'-');
a = a / 2;
continue;
}
if(1 == i%3) {
if(m & a){
printf("%c",'r');
}
}
else if(2 == i%3) {
if(m & a){
printf("%c",'w');
}
}
else if(0 == i%3) {
if(m & a){
printf("%c",'x');
}
}
a = a/2;
}
}
int main(int argc,const char * argv[])
{
struct stat st;
struct tm *info = NULL;
if(-1 == stat("/home/ubuntu/xwf/IOday2",&st)){
perror("stat");
return -1;
}
//printf("type = %#o\n",st.st_mode&0170000);
//判断文件的的类型
if( 0100000 == (st.st_mode&0170000)){
printf("%c",'-');
}else if(0140000 == (st.st_mode&0170000)){
printf("%c",'s');
}else if(0120000 == (st.st_mode&0170000)){
printf("%c",'l');
}else if(0060000 == (st.st_mode&0170000)){
printf("%c",'b');
}else if(0040000 == (st.st_mode&0170000)){
printf("%c",'d');
}else if(0020000 == (st.st_mode&0170000)){
printf("%c",'c');
}else if(0010000 == (st.st_mode&0170000)){
printf("FIFO");
}
get_filePermissing(st.st_mode);//文件的权限
printf(" %ld ",st.st_nlink);//文件的硬链接数
printf(" %s ",getpwuid(st.st_uid)->pw_name);//本用户的用户名
printf(" %s ",getgrgid(st.st_gid)->gr_name);//组用户名
printf(" %ld ",st.st_size);//文件大小
/*
printf("\n");
get_filePermissing(st.st_mode);
printf("\n");
printf("ino = %ld\n",st.st_ino);
printf("gid = %d\n",st.st_gid);
printf("gid name= %s\n",getgrgid(st.st_gid)->gr_name);
printf("uid = %d\n",st.st_uid);
printf("uid name= %s\n",getpwuid(st.st_uid)->pw_name);
printf("mode = %d\n",st.st_mode);
//printf("type = %#o\n",st.st_mode&0170000);
printf("st.mode = %#o\n",st.st_mode&0777);
printf("nlink = %ld\n",st.st_nlink);
printf("size = %ld\n",st.st_size);
printf("atime = %ld\n",st.st_atime);
*/
time_t t = st.st_atime;
info = localtime(&t);
printf (" %2d月 %02d %02d:%02d",info ->tm_mon+1, info ->tm_mday, \
info ->tm_hour, info -> tm_min);//时间
printf(" %s\n","IOday2");//文件名
return 0;
}
1.