K&安培; R接口阅读目录:多余DIR结构? [英] K&R interface for reading directories: superfluous DIR structure?
问题描述
在第2版的C程序设计语言的由Kernighan和Ritchie他们实现了UNIX命令 LS
(段的简化版本8.6的示例 - 列出目录的,第179页)。为此,他们建立以下界面,提供对存储在目录中的文件名和inode号的系统无关的访问。
的#define NAME_MAX 14 / *最长的文件名部分; * /
/ *系统相关的* /typedef结构{/ *便携式主任入境* /
长井野; / *索引节点号* /
焦炭名[NAME_MAX + 1]; / *名称+'\\ 0'终止* /
}的dirent;typedef结构{/ *最小的DIR:无缓冲等* /
INT的fd; / *文件描述符目录* /
的dirent D组; / *目录项* /
} DIR;DIR *执行opendir(字符*目录名);
*的dirent READDIR(DIR * DFD);
无效closedir(DIR * DFD);
然后,他们实现了这个接口的版本7和System V UNIX系统。
-
执行opendir()
基本上都采用该系统
叫的open()
来打开一个目录,的malloc()
来的分配空间DIR
结构。文件描述符
通过返回的open()
然后存储
在的变量
。FD
DIR
没有什么是存储在的dirent
组件。 -
READDIR()
使用系统调用阅读()
来得到下一个
(系统相关的)的目录项
一个打开的目录和副本等等
获得inode编号和文件名
为静态的dirent
结构(以
这是返回指针)。该
只有需要的信息READDIR()
是文件描述符
存储在DIR
结构。
现在我的问题:什么是具有 DIR
结构的意义呢?如果我的这个计划的理解是正确的, DIR
的的dirent
分量从来没有使用过,那么为什么不更换整个结构文件描述符并直接使用的open()
和的close()
?
感谢。
诗:据我所知,在现代Unix系统阅读()
不能再上的目录(我已经尝试了这个程序在Ubuntu 10.04)使用,但我还是想确保我没有忽略一些重要的东西在这个例子中。
从K&安培; R:
令人遗憾的是,一个目录的格式和precise内容不是在所有同
该系统的版本。因此,我们将划分任务分成两部分来尝试隔离
非便携式部件。外部级别定义的结构称为的dirent 和三个程序的执行opendir 的 READDIR 和 closedir 的提供系统 - 无关的访问的名称和节点号码的目录条目。
块引用>所以,原因是便携性。他们希望能够定义上有不同的统计结构或系统的非标准
的open()
和的close()
。他们去建立一堆围绕它可重复使用的工具,如果他们是一个类Unix系统上都不在乎。这是包装的地步。也许这不是使用,因为他们开始了通过定义数据结构(带的dirent内DIR),但最终不使用它。保持数据结构的分组喜欢就是好的设计。
In the 2nd edition of "The C Programming Language" by Kernighan and Ritchie they implement a simplified version of the UNIX command
ls
(section 8.6 "Example - Listing Directories", p. 179). For this purpose they create the following interface which provides a system-independent access to the name and inode number of the files stored in a directory.#define NAME_MAX 14 /* longest filename component; */ /* system dependent */ typedef struct { /* portable director-entry */ long ino; /* inode number */ char name[NAME_MAX+1]; /* name + '\0' terminator */ } Dirent; typedef struct { /* minimal DIR: no buffering, etc. */ int fd; /* file descriptor for directory */ Dirent d; /* the directory entry */ } DIR; DIR *opendir(char *dirname); Dirent *readdir(DIR *dfd); void closedir(DIR *dfd);
Then they implement this interface for Version 7 and System V UNIX systems.
opendir()
basically uses the system callopen()
to open a directory andmalloc()
to allocate space for aDIR
structure. The file descriptor returned byopen()
is then stored in the variablefd
of thatDIR
. Nothing is stored in theDirent
component.
readdir()
uses the system callread()
to get the next (system-dependent) directory entry of an opened directory and copies the so obtained inode number and filename into a staticDirent
structure (to which a pointer is returned). The only information needed byreaddir()
is the file descriptor stored in theDIR
structure.Now to my question: What is the point of having a
DIR
structure? If my understanding of this program is correct, theDirent
component ofDIR
is never used, so why not replace the whole structure with a file descriptor and directly useopen()
andclose()
?Thanks.
Ps: I am aware that on modern UNIX systems
read()
can no longer be used on directories (I have tried out this program on Ubuntu 10.04), but I still want to make sure that I have not overlooked something important in this example.解决方案From K&R:
Regrettably, the format and precise contents of a directory are not the same on all versions of the system. So we will divide the task into two pieces to try to isolate the non-portable parts. The outer level defines a structure called a Dirent and three routines opendir, readdir, and closedir to provide system-independent access to the name and inode number in a directory entry.So the reason is portability. They want to define an interface that can survive on systems that have different stat structs or nonstandard
open()
andclose()
. They go on to build a bunch of reusable tools around it, which don't even care if they're on a Unix-like system. That's the point of wrappers.Maybe it's not used because they started out by defining their data structures (with a Dirent inside DIR) but ended up not using it. Keeping data structures grouped like that is good design.
这篇关于K&安培; R接口阅读目录:多余DIR结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!