C语言文件的属性获取

先了解下面的这几个结构体

以下三个函数可以获取文件/目录的属性信息:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int stat(const char *path, struct stat *buf);//如果文件是符号链接文件,它会返回该链接文件的源文件
int fstat(int fd, struct stat *buf);//需要先打开文件得到文件描述符
int lstat(const char *path, struct stat *buf);//如果文件是符号链接文件,它会返回链接文件的信息(建议用这个)

struct stat {
dev_t st_dev; /* ID of device containing file 设备文件 /
ino_t st_ino; /
Inode number 索引码*/
mode_t st_mode; /* File type and mode 文件类型和权限*/
nlink_t st_nlink; /* Number of hard links 硬链接数*/
uid_t st_uid; /* User ID of owner 文件拥有者的ID*/
gid_t st_gid; /* Group ID of owner 文件所属组的ID*/
dev_t st_rdev; /* Device ID (if special file) 设备号*/
off_t st_size; /* Total size, in bytes 文件大小*/
blksize_t st_blksize; /* Block size for filesystem I/O 块大小*/
blkcnt_t st_blocks; /* Number of 512B blocks allocated 块数*/
struct timespec st_atim; /* Time of last access 最后访问时间*/
struct timespec st_mtim; /* Time of last modification 最后修改时间*/
struct timespec st_ctim; /* Time of last status change 文件状态最后修改时间*/
}
//通过文件拥有者ID获取用户的属性
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd {
char pw_name; / username */
char pw_passwd; / user password /
uid_t pw_uid; /
user ID /
gid_t pw_gid; /
group ID */
char pw_gecos; / user information */
char pw_dir; / home directory */
char pw_shell; / shell program */
};
//通过组ID获取组的信息
#include <sys/types.h>
#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group {
char gr_name; / group name */
char gr_passwd; / group password /
gid_t gr_gid; /
group ID */
char *gr_mem; / NULL-terminated array of pointers
to names of group members */
};
可以用以下的宏确定文件类型。这些宏的参数都是struct stat结构中的st_mode成员。
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission

以上就了解一下,下面看代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
#include <time.h>

void Get_file_properties(const char *path)
{
    struct stat st;
    char *str="xwr-";
    int i=0;
	char *month[]={"Jan","Feb","Mar","Apr","May","jun","Jul","Aug","Sept","Oct","Nov","Dec"};
    if(lstat(path,&st)<0)
    {
	   perror("lstat");
	   exit(1);
    }
    ///仿写库函数里面的使用 man 2 stat 指令 往下拉可以看到
    switch(st.st_mode & S_IFMT)
    {
	case S_IFSOCK:printf("socket ");                                        break;
	case S_IFLNK :printf("symbolic link ");                                 break;
	case S_IFREG :printf("regular file ");                                  break;
	case S_IFBLK :printf("block device ");                                  break;
	case S_IFDIR :printf("directory ");                                     break;
	case S_IFCHR :printf("character device ");                              break;
	case S_IFIFO :printf("FIFO ");                                          break;
	case S_ISUID :printf("set UID bit ");                                   break;
	case S_ISGID :printf("set-group-ID bit (see below) ");                  break;
	case S_ISVTX :printf("sticky bit (see below) ");                        break;
	case S_IRWXU :printf("mask for file owner permissions ");               break;
	case S_IRUSR :printf("owner has read permission ");                     break;
	case S_IWUSR :printf("owner has write permission ");                    break;
	case S_IXUSR :printf("owner has execute permission ");                  break;
	case S_IRWXG :printf("mask for group permissions ");                    break;
	case S_IRGRP :printf("group has read permission ");                     break;
	case S_IWGRP :printf("group has write permission ");                    break;
	case S_IXGRP :printf("group has execute permission ");                  break;
	case S_IRWXO :printf("mask for permissions for others(not in group) "); break;
	case S_IROTH :printf("others have read permission ");                   break;
	case S_IWOTH :printf("others have write permission ");                  break;
	case S_IXOTH :printf("others have execute permission ");                break;
	default:printf("unknown ");                                             break;
    }
    for(i=8;i>=0;i--)
    {
	if(st.st_mode & 1<< i )   	//权限
	    printf("%c",str[i%3]);
	else
	    printf("%c",str[3]);
    }
	printf(" %lu ",st.st_nlink);//硬链接数
    printf(" %s ",getpwuid(st.st_uid)->pw_name);//通过用户ID得到用户名
    printf(" %s ",getgrgid(st.st_gid)->gr_name);//通过所属组ID得到组名
	printf("%ld\n",st.st_size);//获取文件大小
	
	
	
	struct tm *tp = localtime(&st.st_mtime);
    printf("Time of last modification:%s %d ",month[tp->tm_mon],tp->tm_mday);//获取文件修改时间
	printf(" %d:%d ",tp->tm_hour,tp->tm_min);
	printf(" %d\n",tp->tm_year+1900);
}

int main()
{
    Get_file_properties("1.c");

    return 0;
}
  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值