First of all, we will talk about the structure of DIR. Following is the definition of the structure of DIR:
- struct __dirstream
- {
- void *__fd;
- char *__data;
- int __entry_data;
- char *__ptr;
- int __entry_ptr;
- size_t __allocation;
- size_t __size;
- __libc_lock_define (, __lock)
- };
- typedef struct __dirstream DIR;
The DIR structure, similar to FILE, is an internal structure. The following functions use this internal structure to store information about the directory currently being read (excerpted from) Advanced Programming for UNIX Environment (Second Edition) ) The function DIR *opendir(const char *pathname), which opens the file directory, returns a pointer to the DIR structure, which is used by the following functions:
- struct dirent *readdir(DIR *dp);
- void rewinddir(DIR *dp);
- int closedir(DIR *dp);
- long telldir(DIR *dp);
- void seekdir(DIR *dp,long loc);
As far as DIR structure is concerned, we know so much about it. There is no need to study its members.
Next comes the dirent structure. First, we need to understand the concept of directory file: this file contains the names of other files and pointers to the information related to these files (extract) Advanced Programming for UNIX Environment (Second Edition) ) As can be seen from the definition, dirent points not only to directories, but also to specific files in directories. The readdir function also reads files in directories, which is the evidence. The following is the definition of the dirent structure:
- struct dirent
- {
- long d_ino; /* inode number Index Node Number*/
- off_t d_off; /* offset to this dirent Offset in a directory file*/
- unsigned short d_reclen; /* length of this d_name File Name Length*/
- unsigned char d_type; /* the type of d_name File type*/
- char d_name [NAME_MAX+1]; /* file name (null-terminated) File name, up to 255 characters*/
- }
As can be seen from the above definition, the dirent structure stores little information about files, so dirent also plays an index role. If you want to get file information with the effect similar to ls-l, you must rely on the stat function.
The file name read through the readdir function is stored in the d_name member of the structure dirent, while the function
int stat(const char *file_name, struct stat *buf);
The function is to get the details of the file name D d_name and store them in the stat structure. The following is the definition of the stat structure:
- struct stat {
- mode_t st_mode; //File access rights
- ino_t st_ino; //Index Node Number
- dev_t st_dev; //The device number used in the document
- dev_t st_rdev; //Equipment Number of Equipment Documents
- nlink_t st_nlink; //Number of hard connections to files
- uid_t st_uid; //Owner User Identification Number
- gid_t st_gid; //Group identification number
- off_t st_size; //File capacity in bytes
- time_t st_atime; //The last time the file was accessed
- time_t st_mtime; //The last time the file was modified
- time_t st_ctime; //The last time to change the state of the file
- blksize_t st_blksize; //The size of the disk block containing the file
- blkcnt_t st_blocks; //The disk block occupied by the file
- };
The information of this record is very detailed, ha ha ha.
Finally, to sum up, what should we do if we want to get detailed information about a directory (e.g. a directory) b file?
First, we use the opendir function to open directory a and return the DIR structure c pointing to directory a.
Next, we call the readdir (c) function to read all files (including directories) under directory a and return the dirent structure d pointing to all files under directory a.
Then, we traverse D and call stat (d - > name, stat * e) to get the details of each file and store them in the stat structure E.
Overall, it is such a process of gradual refinement, in which the three structures play different roles.
In this program, we only use the DIR structure to declare the pointer of the structure, so the DIR structure should be the same as the FILE structure, when opening a directory, the kernel helps us allocate the memory of the structure.