K & R directory reading interface: redundant DIR structure?

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   /* 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);

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), , - .

+5
2

K & R:

, . , , . , Dirent, opendir, readdir closedir - inode .

, . , stat open() close(). , , Unix- . .

, , ( Dirent DIR), . , , .

+4

, Dirent, readdir. , Dirent readdir.

0

All Articles