In the second edition of Kernigan and Ritchie's Programming Language C, they implement a simplified version of the UNIX command ls(section 8.6, “Example - Directory Directories,” p. 179). To this end, they create the following interface, which provides system independent access to the name and number of inode files stored in the directory.
#define NAME_MAX 14
typedef struct {
long ino;
char name[NAME_MAX+1];
} Dirent;
typedef struct {
int fd;
Dirent d;
} DIR;
DIR *opendir(char *dirname);
Dirent *readdir(DIR *dfd);
void closedir(DIR *dfd);
They then implement this interface for version 7 and System V UNIX systems.
opendir()mainly uses the call system open()to open the directory and
malloc()to make room for the
DIRstructure. The file descriptor is returned open(), then stored in a variable of fdthis DIR. Nothing is saved to the Dirent
component.
readdir()
read(),
( )
inode
Dirent (to
).
,
readdir() -
DIR.
: DIR? , Dirent DIR , open() close()?
.
Ps: , UNIX- read() ( Ubuntu 10.04), , - .