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
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
EBUSY
ENOENT
EPERM
unlink
;
this is not permitted under GNU. (See remove
below.)
EROFS
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
EEXIST
, but GNU always returns ENOTEMPTY
.