gtpc2m60C/C++ Language Support User's Guide

rmdir-Remove a Directory

This function removes a directory.

TPF deviation from POSIX

The TPF system does not support the ctime (change time) time stamp.

Format

#include <unistd.h>
int rmdir(const char *pathname);

pathname
A pointer to the path name of the directory to be deleted.

This function removes a directory, pathname, provided that the directory is empty. pathname must not end in dot (.) or dot-dot (..).

The rmdir function does not remove a directory that still contains files or subdirectories.

If pathname refers to a symbolic link, the rmdir function fails and sets errno to ENOTDIR.

If no process currently has the directory open, the rmdir function deletes the directory itself. The space occupied by the directory is freed for new use. If one or more processes have the directory open when it is removed, the directory remains accessible to those processes. New files cannot be created under a directory after the last link is removed even if the directory is still open.

The rmdir function removes the directory even if it is the working directory of a process.

If a rmdir function is successful, the modification time for the parent directory is updated.

Normal Return

If successful, the rmdir function returns a value of 0.

Error Return

If unsuccessful, the rmdir function returns a value of -1 and sets errno to one of the following:

EACCES
The process did not have search permission for some component of pathname or it did not have write permission for the directory containing the directory to be removed.

EBUSY
pathname cannot be removed because it is currently being used by the system or a process.

EINVAL
The last component of pathname contains a dot (.) or a dot-dot (..).

ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links found while resolving the pathname argument is greater than POSIX_SYMLOOP.

ENAMETOOLONG
pathname is longer than PATH_MAX characters or some component of pathname is longer than NAME_MAX characters. For symbolic links, the length of the path name string substituted for a symbolic link exceeds PATH_MAX.

ENOENT
pathname does not exist or it is an empty string.

ENOTDIR
Some component of the pathname prefix is not a directory.

ENOTEMPTY
The directory still contains files or subdirectories.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Because the rmdir function requires an update to a directory file and the deletion of a second directory file, directories cannot be removed in 1052 or UTIL state.

Examples

The following example removes a directory.

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
 
main() {
  char new_dir[]="new.dir";
  char new_file[]="new.dir/new.file";
  int  fd;
 
  if (mkdir(new_dir, S_IRWXU|S_IRGRP|S_IXGRP) != 0)
    perror("mkdir() error");
  else if ((fd = creat(new_file, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    close(fd);
    unlink(new_file);
  }
 
  if (rmdir(new_dir) != 0)
    perror("rmdir() error");
  else
    puts("removed!");
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.