Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <unistd.h>
int unlink(const char *pathname);
Removes a directory entry. This unlink() deletes the link named by pathname and decrements the link count for the file itself.
pathname can refer to a pathname, a link, or a symbolic link. If the pathname refers to a symbolic link, unlink() removes the symbolic link but not any file or directory named by the contents of the symbolic link.
If the link count becomes 0 and no process currently has the file open, the file itself is deleted. The space occupied by the file is freed for new use, and the current contents of the file are lost. If one or more processes have the file open when the last link is removed, unlink() removes the link, but the file itself is not removed until the last process closes the file.
unlink() cannot be used to remove a directory; use rmdir() instead.
If unlink() succeeds, the change and modification times for the parent directory are updated. If the file's link count is not 0, the change time for the file is also updated. If unlink() fails, the link is not removed.
If successful, unlink() returns 0.
⁄* CELEBU06
This example removes a directory entry, using unlink().
*⁄
#define _POSIX_SOURCE
#include <fcntl.h>
#include <sys⁄stat.h>
#include <sys⁄types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>
main() {
int fd;
char fn[]="unlink.file";
if ((fd = creat(fn, S_IWUSR)) < 0)
perror("creat() error");
else {
close(fd);
if (unlink(fn) != 0)
perror("unlink() error");
}
}