【问题标题】:What is the header for the struct linux_dirent64?struct linux_dirent64 的标头是什么?
【发布时间】:2022-10-24 12:10:02
【问题描述】:

我正在探索getdents64 系统调用。生成的 struct linux_dirent64 未由相关标头定义。 related questionman 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);
}

【问题讨论】:

    标签: c linux


    【解决方案1】:

    struct linux_dirent64 更改为 struct dirent64。这适用于 glibc 2.36。没有必要包含另一个标题,因为#include &lt;dirent.h&gt; 为它提供了#define _GNU_SOURCE。原始代码如下所示:

    #define _GNU_SOURCE
    #include <dirent.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void test() {
      char buf[1024];
      const int procfs = open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
      getdents64(procfs, buf, 1024);
      printf("%lu
    ", ((struct dirent64 *) buf)->d_ino);
      close(procfs);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多