Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define_POSIX_SOURCE
#include <unistd.h>
int rmdir(const char *pathname);
Removes a directory, pathname, provided that the directory is empty. pathname must not end in . (dot) or .. (dot-dot).
If pathname refers to a symbolic link, rmdir() does not affect any file or directory named by the contents of the symbolic link. rmdir() does not remove a directory that still contains files or subdirectories.
Special behavior for XPG4.2: If pathname refers to a symbolic link, rmdir() fails and sets errno to ENOTDIR.
If no process 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 processes have the directory open when it is removed, the directory itself is not removed until the last process 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() removes the directory even if it is the working directory of a process.
If rmdir() is successful, the change and modification times for the parent directory are updated.
If successful, rmdir() returns 0.
⁄* CELEBR16
This example removes a directory.
*⁄
#define _OPEN_SYS
#include <fcntl.h>
#include <sys⁄stat.h>
#include <sys⁄stat.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#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!");
}