This page contains information about my dirent implementation for
Microsoft Windows. However, the information you find from this page is
mostly applicable to any POSIX compliant dirent implementation.
You can download my implementation from
here.
Contents:
Other sources of information:
DIR *opendir( const char *dirname);
The opendir() function opens named directory for reading and returns pointer to internal working area that can be used to retrieve individual directory entries. The internal working area has no fields of your interest.
Pointer to internal working area or NULL if directory stream could not be opened. Global errno variable is set in case of error as follows:
struct dirent *readdir( DIR *dir);
Read individual directory entry and return pointer to a structure containing the name of the entry. Individual entries returned include normal files, sub-directories, "." and ".." entries, and also volume labels, hidden- and system files in MS-DOS and MS-Windows platforms. You may wish to use stat() function to determinate which one are you dealing with. (You can find information about stat() for example from http://menger.eecs.stevens-tech.edu/. The information seems to be a bit UNIX specific.)
The dirent structure that is returned by the readdir() function contains only one portable field: char d_name[]. The field contains the name of the directory entry without leading path. By the way, the length of d_name is unspecified; POSIX standard says it is at least NAME_MAX characters (without terminating null character), in MS-DOS it is exactly 13 characters (16-bit applications), in Windows 32-bit it is equal to MAX_PATH characters. The message is this: there is no portable way to find out length of d_name. Use sizeof(ptr->d_name) whenever you need to find out true length of d_name. Unfortunately, not all compilers allow the use of sizeof() in variable declarations.
Pointer to structure containing name of the directory entry in d_name field or NULL if there was an error. In case of an error, the global errno variable is set as follows:
int closedir(DIR *dir);
Close directory stream opened by the opendir() function. Close of directory stream invalidates the DIR structure as well as previously returned dirent structure that was read from the directory stream.
0 on success and -1 on failure, but may return void in some other implementations. Void is returned at least in Borland C/C++.
The global errno variable is set to
EBADF in case of error.
void rewinddir(DIR *dir);
Reset the directory stream to it's beginning so that next readdir() call returns the first directory entry. Note that next readdir() call may not return the same directory entry as it did in first time; the directory stream may be affected by newly created files.
Nothing. If something went wrong while rewinding, you will notice it
later when you try to retrieve next entry.