2023/2/23 提取文件相关属性

2023/2/23 提取文件相关属性

#include "head.h"

void get_filePermiss(mode_t mode)
{
	int i=0;
	int temp = 0400;
	while(i<3)
	{
		(mode & temp)?putchar('r'):putchar('-');
		temp=temp >> 1;	
		(mode & temp)?putchar('w'):putchar('-');
		temp=temp >> 1;	
		(mode & temp)?putchar('x'):putchar('-');
		temp=temp >> 1;
		i++;
	}
}

char *get_filemode(mode_t mode,char *p)
{
	int i=0;
	int n=0;
	int temp = 0400;
	while(i<3)
	{
		(mode & temp)?(p[n++]='r'):(p[n++]='-');
		temp=temp >> 1;	
		(mode & temp)?(p[n++]='w'):(p[n++]='-');
		temp=temp >> 1;
		(mode & temp)?(p[n++]='x'):(p[n++]='-');
		temp=temp >> 1;
		i++;
	}
	p[n]='\0';
	return p;

}

int main(int argc, const char *argv[])
{
	struct stat buf;
	if(stat(argv[1],&buf) < 0)
	{
		ERR_MSG("stat");
		return -1;
	}

	//文件类型以及权限
	get_filePermiss(buf.st_mode);
	char str[20]="";
	char *m=get_filemode(buf.st_mode,str);
	printf("%s\n",m);
	printf("mode:0%o\n",buf.st_mode);

	//硬链接数
	printf("nlink:%lu\n",buf.st_nlink);

	//文件所属用户、组用户
	struct passwd *pw = getpwuid(buf.st_uid);
	struct group *gr = getgrgid(buf.st_gid);
	if(pw != NULL && gr != NULL)
	{
		printf("%s %s\n",pw->pw_name,gr->gr_name);
	}
	printf("uid:%d,gid:%d\n",buf.st_uid,buf.st_gid);

	//文件大小
	printf("size:%ld\n",buf.st_size);

	//日期
	char *mon[12]={"一月","二月","三月","四月","五月","六月",\
					"七月","八月","九月","十月","十一月","十二月"};
	struct tm *t1 = localtime(&buf.st_ctime);
	printf("%s %02d %02d:%02d %s\n",mon[t1->tm_mon],t1->tm_mday,t1->tm_hour,\
			t1->tm_min,argv[1]);
	printf("time:%ld\n",buf.st_ctime);

	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值