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