针对文件属性

在linux中获取任意一个文件的属性内容

1、int stat(const char *pathname, struct stat *statbuf) ---获取文件的属性

#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 


int stat(const char *pathname, struct stat *statbuf); //传一个文件名字,返回一个结构体信息

参数:
    参数1:const char *pathname//:指针,文件路径字符串地址,表示获取哪个文件信息 

    参数2:struct stat *statbuf//:结构体指针,文件信息存储的地址,把文件信息存储到这个地址对应的空间中(需要有空间)
     
    struct stat { 
        dev_t st_dev; /* ID of device containing file */         设备id 
        ino_t st_ino; /* Inode number */                         文件的唯一标识inode号       
        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) */         设备id 
        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*/ 最后修改时间 

        #define st_atime st_atim.tv_sec /* Backward compatibility */ 
                                            秒数。最后访问时间的秒数(1970年开始的秒数) 
        #define st_mtime st_mtim.tv_sec #define st_ctime st_ctim.tv_sec 
                                            秒数,最后修改时间的秒数 
    };


返回值: int
        
        成功:返回0,表示获取到文件属性
        失败:返回 -1,设置错误码


文件属性结构体:struct stat 类型         成员: mode_t   st_mode; 文件类型与文件权限

        文件权限:rwx rwx rwx:最少需要9位就可以表示(8-0位,9位来表示)

        文件类型:7种类型:最少需要3位就可以表示(15-12位,4位来表示)

        就在成员变量 st_mode 中选择哪些位来表示权限,哪些位来表示类型文件类型:只用查看st_mode 变量中对应文件类型的那 4 位就可以得到类型,一个值代表一种类型 st_mode & S_IFMT 0170000 ):得到的结果中把其他位都设置为 0 12 - 15 位不变。
结果: 
    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               管道文件
文件权限:文件权限判断每一位的值,得到每一位是为 1 还是 0 ,得到一个对应的权限st_mode & ( 判断的权限值 )
                S_IRWXU 00700 owner has read, write, and execute permission      
    用户        S_IRUSR 00400 owner has read permission 
                S_IWUSR 00200 owner has write permission 
                S_IXUSR 00100 owner has execute permission 

                S_IRWXG 00070 group has read, write, and execute permission 
     组         S_IRGRP 00040 group has read permission 
                S_IWGRP 00020 group has write permission 
                S_IXGRP 00010 group has execute permission 

                S_IRWXO 00007 others have read, write, and execute permission         
     其他       S_IROTH 00004 others have read permission 
                S_IWOTH 00002 others have write permission 
                S_IXOTH 00001 others have execute permission

如下例程是通过获取文件信息,通过st_mode&对应的变量看是否相等,如果相等,说明就是该文件。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
	
	struct stat sb;
	stat("a.out",&sb);

	if((sb.st_mode & S_IFMT) == S_IFSOCK)
	{
		printf("socket\n");
	}
	else if((sb.st_mode & S_IFMT) == S_IFREG)
	{
		printf("regular file\n");
	}
	else if((sb.st_mode & S_IFMT) == S_IFDIR)
	{
		printf("directory\n");
	}


	if( (sb.st_mode & S_IRUSR) ==  S_IRUSR )
	{
		printf("owner have read\n");
	}
	
	if( (sb.st_mode & S_IWUSR) == S_IWUSR )
	{
		printf("owner have write\n");
	}

}

 获取已经打开的文件的文件属性

int fstat(int fd, struct stat *statbuf);   

如果获取的文件为符号链接件,则获取符号链接文件的属性

int lstat(const char *pathname, struct stat *statbuf);

        根据用户id,得到用户信息,返回值就是用户信息结构体的地址,通过地址访问用户信息

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,得到用户组信息,返回值就是用户组信息结构体的地址,通过地址可以访问到用户组信息 

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 */ 
    };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啵啵520520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值