要求:
获取一个文件的属性,结果类似
-rw-rw-r-- 1 ubuntu ubuntu 524 一月 30 16:14 01_fileno.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <time.h>
//char ptr[]等价与char* ptr
//char ptr[10]与char ptr[]的区别:这两功能上个都等价于char* ptr
//但是,char ptr[10]放在形参位置不代表数组,它代表指针,
//且还有另外一个功能用于提示用户,需要传入一个数组容量为10
char* get_filePermission(mode_t m, char ptr[10], int size)
{
if(size < 10) //存储权限,权限为9个字符,加上'\0',所以容量不必须大于等于10
{
fprintf(stderr, "line:%d 数组的容量必须大于等于10\n", __LINE__);
return NULL;
}
char per[] = "rwx";
int i = 0;
for(i=0; i<9; i++)
{
if((m & (0400>>i)) == 0)
{
// putchar('-');
ptr[i] = '-';
}
else
{
ptr[i] = per[i%3];
}
}
return ptr;
}
//提取文件类型
#if 0
void get_fileType(mode_t m)
{
if(S_ISREG(m))
putchar('-');
else if(S_ISDIR(m))
putchar('d');
else if(S_ISCHR(m))
putchar('c');
else if(S_ISBLK(m))
putchar('b');
else if(S_ISFIFO(m))
putchar('p');
else if(S_ISLNK(m))
putchar('l');
else if(S_ISSOCK(m))
putchar('s');
}
#endif
void get_fileType(mode_t m)
{
switch(m & S_IFMT)
{
case S_IFSOCK: putchar('s'); break;
case S_IFLNK: putchar('l'); break;
case S_IFREG: putchar('-'); break;
case S_IFBLK: putchar('b'); break;
case S_IFDIR: putchar('d'); break;
case S_IFCHR: putchar('c'); break;
case S_IFIFO: putchar('p'); break;
default: printf("mode错误\n");
}
}
char *get_usrName(uid_t uid,char *usrname)
{
struct passwd *usr = getpwuid(uid);
if(NULL == usr){
perror("getpwuid");
return NULL;
}
strcpy(usrname,usr->pw_name);
return usrname;
}
char *get_grpName(gid_t gid,char *grpname)
{
struct group *gr = getgrgid(gid);
if(NULL == gr){
perror("get_grpName");
return NULL;
}
strcpy(grpname,gr->gr_name);
return grpname;
}
char *get_month(int mon)
{
switch(mon)
{
case 0:
return "一月";
break;
case 1:
return "二月";
break;
case 2:
return "三月";
break;
case 3:
return "四月";
break;
case 4:
return "五月";
break;
case 5:
return "六月";
break;
case 6:
return "七月";
break;
case 7:
return "八月";
break;
case 8:
return "九月";
break;
case 9:
return "十月";
break;
case 10:
return "十一月";
break;
case 11:
return "十二月";
break;
default:
break;
}
}
void print_time(time_t t)
{
struct tm *info = localtime(&t);
if (NULL == info)
{
perror("localtime");
return;
}
printf(" %s %02d %02d:%02d",\
get_month(info->tm_mon),info->tm_mday,\
info->tm_hour,info->tm_min);
fflush(stdout);
}
int main(int argc, const char *argv[])
{
if(argc < 2)
{
fprintf(stderr, "请在命令行输入文件名\n");
return -1;
}
//获取文件属性
struct stat buf;
if(stat(argv[1], &buf) < 0)
{
perror("stat");
return -1;
}
//文件的类型以及权限
//printf("mode:%d : 八进制:%o\n", buf.st_mode, buf.st_mode);
get_fileType(buf.st_mode);
char str[10] = "";
if(get_filePermission(buf.st_mode, str, sizeof(str)) == NULL)
{
fprintf(stderr, "文件权限获取失败\n");
return -1;
}
printf("%s", str);
//文件的硬链接数
//printf("link:%ld\n", buf.st_nlink);
fprintf(stdout," %ld",buf.st_nlink);
//文件的所属用户名
//printf("uid:%d\n", buf.st_uid);
struct passwd* usr = getpwuid(buf.st_uid);
if(NULL == usr)
{
perror("getpwuid");
return -1;
}
//printf("usrname=%s", usr->pw_name);
char usrname[32] = "";
printf(" %s",get_usrName(buf.st_uid,usrname));
//文件的所属组用户名
//printf("gid:%d\n", buf.st_gid);
struct group* grp = getgrgid(buf.st_gid);
if(NULL == grp)
{
perror("getgrgid");
return -1;
}
//printf("grpname = %s", grp->gr_name);
char grpname[32] = "";
printf(" %s",get_grpName(buf.st_gid,grpname));
//文件的大小
//printf("size=%ld\n", buf.st_size);
fprintf(stdout," %ld",buf.st_size);
//文件的时间
//printf("ctime:%ld\n", buf.st_ctime);
print_time(buf.st_ctime);
//文件的名字
fprintf(stdout," %s\n",argv[1]);
return 0;
}
结果为
![](https://i-blog.csdnimg.cn/blog_migrate/6cbfaed3b86ef46e19c80ba5deb9162d.png)