ls -l需要列出文件类型和许可权限、文件链接数、用户ID、所属组ID、所占空间大小、文件修改时间和文件名称。 而我们可以通过readdir函数读取到的文件名,再通过调用函数 int stat(const char *file_name, struct stat *buf)获取文件名为d_name的文件的详细信息,存储在stat结构体中。 以下为stat结构体的定义:
structstat { dev_t st_dev; /* [XSI] ID of device containing file */ ino_t st_ino; /* [XSI] File serial number */ mode_t st_mode; /* [XSI] Mode of file (see below) */ nlink_t st_nlink; /* [XSI] Number of hard links */ uid_t st_uid; /* [XSI] User ID of the file */ gid_t st_gid; /* [XSI] Group ID of the file */ dev_t st_rdev; /* [XSI] Device ID */ #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE) structtimespecst_atimespec;/* time of last access */ structtimespecst_mtimespec;/* time of last data modification */ structtimespecst_ctimespec;/* time of last status change */ #else time_t st_atime; /* [XSI] Time of last access */ long st_atimensec; /* nsec of last access */ time_t st_mtime; /* [XSI] Last data modification time */ long st_mtimensec; /* last data modification nsec */ time_t st_ctime; /* [XSI] Time of last status change */ long st_ctimensec; /* nsec of last status change */ #endif off_t st_size; /* [XSI] file size, in bytes */ blkcnt_t st_blocks; /* [XSI] blocks allocated for file */ blksize_t st_blksize; /* [XSI] optimal blocksize for I/O */ __uint32_t st_flags; /* user defined flags for file */ __uint32_t st_gen; /* file generation number */ __int32_t st_lspare; /* RESERVED: DO NOT USE! */ __int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */ };
structpasswd { 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 */ };
#include<grp.h> struct group *getgrgid(gid_t gid);
structgroup { 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 */ };