函数原型

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. int stat(const char *path, struct stat *buf);
  5. int fstat(int fd, struct stat *buf);
  6. int lstat(const char *path, struct stat *buf);

三个函数的关系与chown,fchown,lchown的关系相同。

  • stat检查符号链接时,实际检查的是符号链接所引用的文件
  • lstat检查符号链接时,检查的是符号链接本身
  • fstat功能与stat相同,不过它的参数是文件的描述符

返回值

成功返回0;失败返回-1,并设置相应errno的值。

结构体stat的内容

  1. struct stat {
  2. dev_t st_dev; /* 设备号(主设备号,次设备号)*/
  3. ino_t st_ino; /* inode的数量 */
  4. mode_t st_mode; /* 文件的类型和存取的权限 */
  5. nlink_t st_nlink; /* 硬链接的数量 */
  6. uid_t st_uid; /* 所用者的uid */
  7. gid_t st_gid; /* 所有者的组id */
  8. dev_t st_rdev; /* device ID (if special file) */
  9. off_t st_size; /* 总大小,字节数 */
  10. blksize_t st_blksize; /* blocksize for filesystem I/O */
  11. blkcnt_t st_blocks; /* number of 512B blocks allocated */
  12. time_t st_atime; /* 最后访问时间(access)*/
  13. time_t st_mtime; /* 最后修改时间(modification)文件内容的改动时间 */
  14. time_t st_ctime; /* 最后改动时间(change)文件属性的改动时间 */
  15. };