closedir()--Close Directory


  Syntax
 #include <sys/types.h>
 #include <dirent.h>

 int closedir(DIR *dirp);  

  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The closedir() function closes the directory stream indicated by dirp. It frees the buffer that readdir() uses when reading the directory stream.

A file descriptor is used for type DIR; closedir() closes the file descriptor.


Parameters

dirp
(Input) A pointer to a DIR that refers to the open directory stream to be closed. This pointer is returned by the opendir() function.

Authorities

No authorization is required. Authorization is verified during opendir().


Return Value

0
closedir() was successful.
-1
closedir() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If closedir() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

If interaction with a file server is required to access the object, errno could indicate one of the following errors:



Error Messages

The following messages may be sent from this function:



Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:
    • Where multiple threads exist in the job.
    • The object on which this function is operating resides in a file system that is not threadsafe. Only the following file systems are threadsafe for this function:
      • "Root" (/)
      • QOpenSys
      • User-defined
      • QNTC
      • QSYS.LIB
      • Independent ASP QSYS.LIB
      • QOPT
      • Network File System
      • QFileSvr.400


  2. If the dirp argument passed to closedir() does not refer to an open directory, closedir() returns the [EBADF] or [EFAULT] error.

  3. After a call to closedir() the dirp will not point to a valid directory.

Related Information


Example

The following example closes a directory.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

main() {
  DIR *dir;
  struct dirent *entry;
  int count;

  if ((dir = opendir("/")) == NULL)
    perror("opendir() error");
  else {
    count = 0;
    while ((entry = readdir(dir)) != NULL) {
      printf("directory entry %03d: %s\n", ++count, entry->d_name);
    }
    closedir(dir);
  }
}

Output:

directory entry 001: .
directory entry 002: ..
directory entry 003: QSYS.LIB
directory entry 004: QDLS
directory entry 005: QOpenSys
directory entry 006: home


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]