I have never understood how to operate directories in Linux. I recently studied dirent. h. The following are the results of the study:
There are two dirent. h:
/Usr/include/dirent. h
Defines the directory stream structure Dir and some operation directory node interfaces, including opening, reading, closing, and the operation of the pointer position in the stream; very similar to file operations, where DIR is not actually defined, the estimation is complicated, but in the process of use, he just transmits and does not need to modify him. It is a bit similar to the file handle. Common interfaces include:
Extern dir * opendir (_ const char * _ name) _ nonnull (1 ));
Extern int scandir (_ const char * _ restrict _ DIR ,...)
Extern int closedir (dir * _ dirp) _ nonnull (1 ));
Extern struct dirent * readdir (dir * _ dirp) _ nonnull (1 ));
/Usr/include/bits/dirent. h
Defines the data structure of the next file node in Dir, including attributes such as name and file type. After obtaining this structure through readdir, you can use the stat function to further parse these attributes, the following are various file types:
Enum
{
Dt_unknown = 0,
# Define dt_unknown
Dt_fifo = 1,
# Define dt_fifo
Dt_chr = 2,
# Define dt_chr
Dt_dir = 4,
# Define dt_dir
Dt_blk = 6,
# Define dt_blk
Dt_reg = 8,
# Define dt_reg
Dt_lnk = 10,
# Define dt_lnk
Dt_sock = 12,
# Define dt_sock
Dt_wht = 14
# Define dt_wht
};