Updated: October 28, 2024 |
Read a directory entry
#include <dirent.h> struct dirent *readdir( DIR *dirp ); struct dirent64 *readdir64( DIR *dirp );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The readdir() and readdir64() functions read the next directory entry from the directory specified by dirp, which is the value returned by a call to opendir(). The readdir64() function is a large-file support version of readdir().
You can call readdir() repeatedly to list all of the entries contained in the directory specified by the pathname given to opendir(). You must call closedir() to close the directory stream and free the memory allocated by opendir().
The <dirent.h> file defines the dirent and dirent64 structures, and the DIR type used by the readdir() family of functions.
The <dirent.h> file also defines the following macros for accessing extra data associated with the dirent and dirent64 structures:
The blocks of data are dirent_extra or dirent_extra_stat structures:
struct dirent_extra { uint16_t d_datalen; /* data size (without header) */ uint16_t d_type; uint32_t d_reserved; /* unsigned char d_data[d_datalen] */ }; struct dirent_extra_stat { uint16_t d_datalen; /* sizeof(struct stat) */ uint16_t d_type; uint32_t d_reserved; struct stat d_stat; };
The d_type member identifies the type of extra information. Its value is from the dirent_extra_type enumerated type, and it depends on whether you're using a 32- or 64-bit architecture, and on the version of POSIX (see the entry for struct stat).
enum dirent_extra_type { _DTYPE_NONE, _DTYPE_STAT_UNSET, _DTYPE_LSTAT_UNSET, _DTYPE_STAT_T32_2001, _DTYPE_LSTAT_T32_2001, _DTYPE_STAT_T32_2008, _DTYPE_LSTAT_T32_2008, _DTYPE_STAT_T64_2008, _DTYPE_LSTAT_T64_2008, #if __PTR_BITS__ == 32 _DTYPE_STAT = _DTYPE_STAT_T32_2001, _DTYPE_LSTAT = _DTYPE_LSTAT_T32_2001, #else _DTYPE_STAT = _DTYPE_STAT_T64_2008, _DTYPE_LSTAT = _DTYPE_LSTAT_T64_2008, #endif };
You can use these macros to traverse the data associated with the dirent structure like this:
for( extra = _DEXTRA_FIRST(dirent); _DEXTRA_VALID(extra, dirent); extra = _DEXTRA_NEXT(extra)) { switch(extra->d_type) { case _DTYPE_NONE : /* No data */ break; case _DTYPE_STAT : /* Data includes information as returned by stat() */ break; case _DTYPE_LSTAT : /* Data includes information as returned by lstat() */ break; … } }
If the value of the d_type member is _DTYPE_STAT or _DTYPE_LSTAT, the d_stat member is a struct stat. You can use the dircntl() function to request that the filesystem include this extra information.
A pointer to a struct dirent or struct dirent64 object for success, or NULL if the end of the directory stream is encountered or an error occurs (errno is set).
Get a list of files contained in the directory /home/fred:
#include <stdio.h> #include <dirent.h> #include <stdlib.h> int main( void ) { DIR* dirp; struct dirent* direntp; dirp = opendir( "/home/fred" ); if( dirp != NULL ) { for(;;) { direntp = readdir( dirp ); if( direntp == NULL ) break; printf( "%s\n", direntp->d_name ); } closedir( dirp ); return EXIT_SUCCESS; } return EXIT_FAILURE; }
Get stat data while reading a directory:
/* * This function gets stat information from the dirent. * If it isn't supported (ramdisk, symlinks, etc.), we fall back to * a generic stat and return that instead. * * @returns 0 on success (statp structure populated); * @returns -1 on error; */ static int sync_stat( const char *path, struct dirent *ent, struct stat *statp ) { struct dirent_extra *dex; struct dirent_extra_stat *dex_stat; // Get the dirent stat for ( dex = _DEXTRA_FIRST(ent); _DEXTRA_VALID(dex, ent); dex = _DEXTRA_NEXT(dex) ) { if ( dex->d_type == _DTYPE_STAT || dex->d_type == _DTYPE_LSTAT ) { dex_stat = (struct dirent_extra_stat *) dex; memcpy( statp, &(dex_stat->d_stat), sizeof(struct stat) ); return 0; } } // We failed the dirent stat. Return the normal one. return stat(path, statp); } // The volume label should be a name-special file at the root folder. // Depending on the way fs-dos is used, that name special file will be prefixed // by an '=' character which will need to be stripped off. dir = opendir( mountpath ); if ( dir == NULL ) { log_error( 1, "Could not open %s to find the volume name\n", mountpath ); return -1; } // Turn on stat info in the dirent. if ( (dirflags = dircntl(dir, D_GETFLAG)) == -1 ) { log_error( 1, "Could not request stat information for dir %s. (%s)\n", mountpath, strerror(errno) ); } else if (dircntl(dir, D_SETFLAG, dirflags | D_FLAG_STAT) == -1) { log_error( 1, "Could not request stat information for dir %s. (%s)\n", mountpath, strerror(errno)); } while ((ent = readdir(dir))) { strcpy( statpath, mountpath ); strcat( statpath, "/" ); strcat( statpath, ent->d_name ); if ( sync_stat(statpath, ent, &buf) == 0 ) { Do something with the data. } else { log_error( 1, "Could not stat %s. (%s)\n", statpath, strerror(errno) ); } } log_error( 1, "Could not find volume name in %s\n", mountpath ); closedir(dir);
readdir() is POSIX 1003.1; readdir64() is Large-file support
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Read the Caveats |
The readdir() function is safe to use in multithreaded environments as long as the directory stream, DIR *dirp, isn't shared between threads.