rmdir()--Remove Directory


  Syntax
 #include <unistd.h>

 int rmdir(const char *path);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The rmdir() function removes a directory, path, provided that the directory is empty; that is, the directory contains no entries other than "dot" (.) or "dot-dot" (..). path must not end in dot (.) or dot-dot (..).

If no job currently has the directory open, rmdir() deletes the directory itself. The space occupied by the directory is freed for new use. If one or more jobs have the directory open, rmdir() removes the link and the dot (.) or dot-dot (..). entries. The directory itself is not removed until the last job closes the directory. New files cannot be created under a directory after the last link is removed, even if the directory is still open.

rmdir() does not remove a directory that still contains files or subdirectories. If path refers to a directory that is not empty, the [ENOTEMPTY] error is returned. If path refers to the current directory of the current job, to the "root" (/) directory, or to a directory that cannot be removed, the [EBUSY] error is returned.

If path refers to a symbolic link, rmdir() does not affect any file or directory named by the contents of the symbolic link.

If rmdir() is successful, the change and modification times for the parent directory are updated.


Parameters

path
(Input) A pointer to the null-terminated path name of the directory to be removed.

This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.

See QlgRmdir()--Remove Directory (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.


Authorities

Note: Adopted authority is not used.

Authorization Required for rmdir() (excluding QSYS.LIB, independent ASP QSYS.LIB, and QDLS)

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be removed *X EACCES
Parent directory of the directory to be removed *WX EACCES
Directory to be removed *OBJEXIST EACCES
Parent directory of the directory to be removed has the S_ISVTX mode bit set to binary one (see Note). *ALLOBJ, or owner of the directory to be removed, or owner of the parent directory of the directory to be removed EPERM
Note: The S_ISVTX mode bit (which is equivalent to the 'Restricted rename and unlink' object attribute) restriction only applies to objects in the "root" (/), QOpenSys, and user-defined file systems.

Authorization Required for rmdir() in the QSYS.LIB and independent ASP QSYS.LIB File Systems

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be removed *X EACCES
Parent directory of the directory to be removed *X EACCES
Directory to be removed, if it is a library *OBJEXIST, *RX EACCES
Directory to be removed, if it is a database file *OBJEXIST, *OBJOPR EACCES

Authorization Required for rmdir() in the QDLS File System

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be removed *X EACCES
Parent directory of the directory to be removed *X EACCES
Directory to be removed *OBJEXIST, *X EACCES

Authorization Required for rmdir() in the QOPT File System

Object Referred to Authority Required errno
Volume authorization list *CHANGE EACCES
Each directory in the path name preceding the directory to be removed if volume media format is Universal Disk Format (UDF) *X EACCES
Parent directory of the directory to be removed if volume media format is Universal Disk Format (UDF) *WX EACCES
Directory to be removed if volume media format is Universal Disk Format (UDF) *W EACCES
Directory and parent directories if volume media format is not Universal Disk Format (UDF) None None

Return Value

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

Error Conditions

If rmdir() 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]  
[EBADFID]  
[EBADNAME]  
[EBUSY]

The path cannot be removed because it is the current working directory of the current process, or it is currently being used by the system.

[ECONVERT]  
[EDAMAGE]  
[EFAULT]  
[EFILECVT]  
[EINTR]  
[EINVAL]

For example, the last component of path is 'dot' or 'dot-dot'.

[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ELOOP]  
[ENAMETOOLONG]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOENT]  
[ENOMEM]  
[ENOSPC]  
[ENOTAVAIL]  
[ENOTDIR]  
[ENOTEMPTY]  
[ENOTSAFE]  
[ENOTSUP]  
[EPERM]  
[EROOBJ]  
[EUNKNOWN]  
[ESTALE]

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


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. QSYS.LIB and Independent ASP QSYS.LIB File System Differences

    If one or more jobs have the library or file open, rmdir() returns [EBUSY].

    If rmdir() is successful, the change and modification times for the parent library are updated only if the "directory" being removed is a database file.


  3. QDLS File System Differences

    If one or more jobs have the folder open, or are using the folder as their current directory, rmdir() returns [EBUSY].


  4. QOPT File System Differences

    The change and modification times of the parent directory are not updated.

    If path refers to a directory that any job has open, the [EBUSY] error is returned.


  5. QNTC File System Differences

    The change and modification times of the parent directory are not updated.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example removes a directory:

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

main() {
  char new_dir[]="new_dir";
  char new_file[]="new_dir/new_file";
  int  file_descriptor;

  if (mkdir(new_dir, S_IRWXU|S_IRGRP|S_IXGRP) != 0)
    perror("mkdir() error");
  else if ((file_descriptor = creat(new_file, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    close(file_descriptor);
    unlink(new_file);
  }

  if (rmdir(new_dir) != 0)
    perror("rmdir() error");
  else
    puts("removed!");
}


API introduced: V3R1
Top | UNIX-Type APIs | APIs by category