Linux 是如何隐藏`DIR`结构体定义的

32次阅读

共计 521 个字符,预计需要花费 2 分钟才能阅读完成。

最近在看 APUE 时遇到了DIR,提到了由于不同的实现,解析目录文件内容方式不统一,通常会阻止直接读取目录文件的内容

DIR *opendir (const char *name)

跳转到 DIR 的定义

/* This is the data type of directory stream objects.
   The actual structure is opaque to users.  */
typedef struct __dirstream DIR;

__dirstream定义找不到
注释说 DIR 结构体对用户透明,那么它是如何隐藏这个定义的呢?

源文件

#include 
#include 

int main() {DIR* dir = opendir(".");
    struct dirent *d = readdir(dir);
    while (d) {printf("%sn", d->d_name);
        d = readdir(dir);
    }
    return 0;
}

使用 gcc 预处理源文件

➜  test gcc -E ls.c | grep __dirstream -A2 -B2
  };
# 127 "/usr/include/dirent.h" 3 4
typedef struct __dirstream DIR;


并没有找到 __dirstream 的定义

所以是如何实现隐藏定义的?

正文完
 0