[Previous]
[Contents]
[Next]

closedir()

close a directory

Synopsis:

#include <dirent.h>
int closedir( DIR *dirp );

Description:

The closedir() function closes the directory specified by dirp, and frees the memory allocated by opendir().


Note: 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 the closedir() function.

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EBADF
The argument dirp does not refer to an open directory stream.
EINTR
The closedir() function was interrupted by a signal.

Examples:

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 ) {

      for(;;) {
        direntp = readdir( dirp );
        if( direntp == NULL )
          break;
        printf( "%s\n", direntp->d_name );
      }

      closedir( dirp );
    }
  }

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, opendir(), readdir(), rewinddir()


[Previous]
[Contents]
[Next]