文件和目录

UNIX 环境高级编程第三章
本章描述了 UNIX 文件系统的其他特性和文件的性质

函数 stat, fstat, fstatat 和 lstat

1
2
3
4
5
6
7
8
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *__restrict pathname, struct stat *__restrict buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *__restrict pathname, struct stat *__restrict buf);
int fstatat(int fd, const char *__restrict pathname, struct stat *__restrict buf);

返回值(四个函数):
1). 若成功,返回0
2). 若出错,返回-1

stat 结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
struct timespec st_atime;
struct timespec st_mtime;
struct timespec st_ctime;
blksize_t st_blksize;
blkcnt_t st_blocks;
};

文件类型

  1. 普通文件(regular file)
  2. 目录文件(directory file)
  3. 块特殊文件(block special file)
  4. 字符特殊文件(character special file)
  5. FIFO
  6. 套接字(socket)
  7. 符号链接(symbolic link)

文件类型信息包含在 stat 结构中的 st_mode 成员中。

# UNIX

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×