open a directory file
#include <dirent.h> DIR *opendir( const char *dirname );
The opendir() function is used in conjunction with the functions readdir() and closedir() to obtain the list of file names contained in the directory specified by dirname. The path indicated by dirname can be either relative to the current working directory, or it can be an absolute path name.
The file <dirent.h> contains definitions for the structure dirent and the DIR type.
In QNX the dirent structure contains a stat structure in the d_stat member. To speed up applications, which often want both the name and the stat data, a resource manager may return the stat information at the same time the readdir() function is called.
However, since the support of this feature is left to the discretion of various resource managers, every program must use the following test to determine if the d_stat member contains valid data:
d_stat.st_status & _FILE_USED
This test must be performed after every readdir() call.
If the d_stat member doesn't contain valid data, and the data is needed then the application should construct the file's pathname and call stat() or lstat(), as appropriate.
More than one directory can be read at the same time using the opendir(), readdir(), rewinddir() and closedir() functions.
The result of using a directory stream after one of the exec... or spawn ... family of functions is undefined. After a call to the fork() function, either the parent or the child (but not both) may continue processing the directory stream using readdir() or rewinddir() or both. If both the parent and child processes use these functions, the result is undefined. Either or both processes may use closedir(). |
A pointer to a DIR structure if successful. This pointer is required for subsequent calls to readdir() to retrieve the file names specified by dirname.
The opendir() function returns NULL if dirname is not a valid pathname.
To get a list of files contained in the directory /home/fred of your node:
#include <stdio.h> #include <dirent.h> void main() { DIR *dirp; struct dirent *direntp; dirp = opendir( "/home/fred" ); if( dirp == NULL ) { perror( "" ); } else { for(;;) { direntp = readdir( dirp ); if( direntp == NULL ) break; printf( "%s\n", direntp->d_name ); } closedir( dirp ); } }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
closedir(), errno, readdir(), rewinddir()