Node:Deleting files at a low level, Next:Renaming files at a low level, Previous:Finding file positions at a low level, Up:Low-level file routines
Deleting files at a low level
If you want to delete a file, you can use the low-level file routine
unlink, as declared in the file unistd.h. Simply pass
this routine the name of the file you wish to delete. If this is the
only name the file has (that is, if no one has created a hard link to
the file with the link function, the GNU command ln, or
something similar), then the file itself will be deleted; otherwise,
only that name will be deleted. (See the section "Hard Links" in the
GNU C Library manual for more information on hard links.) If the file
is open when unlink is called, unlink will wait for the
file to be closed before it deletes it.
The unlink function returns 0 if the file or file name was
successfully deleted. If there was an error, unlink returns -1.
In addition to the usual file name errors, unlink can set
errno to the following values. (See Usual file name errors, for a list of the usual file name errors.)
EACCES- Your program does not have permission to delete the file from the
directory that contains it.
EBUSY- The file is currently being used by the system and cannot be deleted.
ENOENT- The file name to be deleted does not exist.
EPERM- Your program tried to delete a directory with
unlink; this is not permitted under GNU. (Seeremovebelow.) EROFS- The file name is on a read-only file system and cannot be deleted.
If you wish to delete a directory rather than an ordinary file, use the
rmdir function. Simply pass it the name of an empty directory
you wish to delete. It acts like unlink in most respects, except
that it can return an extra error code in the system variable
errno:
ENOTEMPTY- The directory was not empty, so cannot be deleted. This code is synonymous
with
EEXIST, but GNU always returnsENOTEMPTY.