简体   繁体   English

是否可以在Ubuntu 12.10(内核3.5)中实现readdir()?

[英]Is it possible to implement readdir() in Ubuntu 12.10 (kernel 3.5)?

In 8.6 of K & R , the authors implemented a simple version of readdir() . K & R 8.6中,作者实现了readdir()的简单版本。 The code is as follows: 代码如下:

#include <sys/dir.h>   /* local directory structure */
/* readdir:  read directory entries in sequence */
Dirent *readdir(DIR *dp)
{
   struct direct dirbuf;  /* local directory structure */
   static Dirent  d;      /* return: portable structure */
   while (read(dp->fd, (char *) &dirbuf, sizeof(dirbuf))
                   == sizeof(dirbuf)) {
       if (dirbuf.d_ino == 0) /* slot not in use */
           continue;
       d.ino = dirbuf.d_ino;
       strncpy(d.name, dirbuf.d_name, DIRSIZ);
       d.name[DIRSIZ] = '\0';  /* ensure termination */
       return &d;
   }
   return NULL;
}

In my opinion, in the line with read() , dp->fd is the file descriptor of the directory. 我认为,与read()dp->fd是目录的文件描述符。 The authors used read() to get struct direct directly from the directory file. 作者使用read()直接从目录文件中直接获取struct direct

However, in Ubuntu, it is not possible to read a directory file. 但是,在Ubuntu中,无法读取目录文件。 When I tried to read a directory, I just got something strange. 当我尝试读取目录时,我感到有些奇怪。

I read in APUE that in some systems, this action is not allowed. 我在APUE中读到,在某些系统中,不允许执行此操作。 So is there any other ways to realize my own readdir() ? 那么还有其他方法可以实现我自己的readdir()吗?

You are looking at code from 40 years ago. 您正在查看40年前的代码。 Directories are simply not implemented like that on any modern platform. 目录根本没有像在任何现代平台上那样实现。 Read the documentation for your filesystem (ext4 if you are on Linux) if you really need to write code to manipulate it. 如果确实需要编写代码来操作文件系统,请阅读文件系统的文档(如果在Linux上,请阅读ext4)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM