structstat { 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 */ gid_t st_gid; /* Group ID of owner */ 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 512 B blocks allocated */
/* Since POSIX.1-2008, this structure supports nanosecond precision for the following timestamp fields. For the details before POSIX.1-2008, see VERSIONS. */
structtimespecst_atim;/* Time of last access */ structtimespecst_mtim;/* Time of last modification */ structtimespecst_ctim;/* Time of last status change */
// 所属用户权限 S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission // 所属组权限 S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission // 其他人权限 S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission
如果这个文件所属用户拥有读的权利 l, st_mode & S_IRUSR 为 true
完整的[文件类型及文件权限]就得到了。
所属用户与所属组
所属用户名可以通过 getpwuid() 来获得。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<pwd.h>
struct passwd *getpwuid(uid_t uid);
structpasswd { char *pw_name; /* user name */ char *pw_passwd; /* encrypted password */
__uid_t pw_uid; /* user uid */ __gid_t pw_gid; /* user gid */
char *pw_gecos; /* Real name */ char *pw_dir; /* home directory */ char *pw_shell; /* default shell */ };
所属组可以通过 getgrgid() 来获得。
1 2 3 4 5 6 7 8 9 10
#include<grp.h>
struct group *getgrgid(gid_t);
structgroup { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group id */ char **gr_mem; /* members list */ };
最后修改时间
st_mtime 可以通过 ctime() 来转变为字符串。
硬连接数与文件大小
st_nlink 是一个 short int , st_size 是一个 long int, 这两个都是一个数字类型,可以直接拿来用。
遍历目录
遍历目录的方法是使用 C 语言的库函数 opendir, readdir, closedir。
opendir
1 2 3 4
#include<sys/types.h> #include<dirent.h>
DIR *opendir(constchar* name);
The opendir() function opens a directory stream corresponding to the directory name, and returns a pointer to the directory stream. The stream is positioned at the first entry in the directory.
opendir() 函数通过参数文件夹的名字返回一个 DIR 的文件夹流指针, 函数执行失败会返回一个 NULL。
Filename entries can be read from a directory stream using readdir(3).
使用 DIR * 获取每一个文件需要使用 readdir()
readdir
1 2 3
#include<dirent.h>
struct dirent *readdir(DIR *dirp);
The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occurred.
The closedir() function closes the directory stream associated with dirp. A successful call to closedir() also closes the underlying file descriptor associated with dirp. The directory stream descriptor dirp is not available after this call.