【发布时间】:2022-10-24 12:10:02
【问题描述】:
我正在探索getdents64 系统调用。生成的 struct linux_dirent64 未由相关标头定义。 related question 和 man 2 getdirents64 中的示例都在声明自己的结构。虽然我知道 Linux 系统调用的向后兼容性,但像这样在本地定义结构看起来像一个 hack。我是否需要包含另一个在内部定义了 struct linux_dirent64 的标题?
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
struct linux_dirent64 {
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
void test() {
char buf[1024];
const int procfs = open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
getdents64(procfs, buf, 1024);
printf("%lu\n", ((struct linux_dirent64 *) buf)->d_ino);
close(procfs);
}
【问题讨论】: