fstat
- 功能:获取文件的元数据
- 原型:
int fstat(int fd, struct stat *statbuf);
- 参数:
1)fd
2)struct stat* statbuf,传出参数,保存文件元数据信息 - 返回值:
成功:返回0
失败:返回-1,并会设置errno - DO
#include <func.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
error(1, 0, "Usage: %s file", argv[0]);
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
error(1, errno, "open %s", argv[1]);
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
error(1, errno, "fstat %d", fd);
}
printf("st_ino = %ld\nst_mode = %lo\nst_nlink = %ld\nst_size = %ld\nst_blocks = %ld\n",
(long)sb.st_ino,
(long)sb.st_mode,
(long)sb.st_nlink,
(long)sb.st_size,
(long)sb.st_blocks);
return 0;
}