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.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

[EAGAIN]  
[EBADF]  
[EBADFID]  
[EBUSY]  
[EDAMAGE]  
[EFAULT]  
[EINTR]  
[EINVAL]  
[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOSPC]  
[ENOSYS]  
[ENOTAVAIL]  
[ENOTSAFE]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[EUNKNOWN]  

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

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

The following messages may be sent from this function:

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPFA0D4 E File system error occurred. Error number &1.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:

  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

See Code disclaimer information for information pertaining to code examples.

The following example closes a directory:

#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
Top | UNIX-Type APIs | APIs by category