339 lines
11 KiB
HTML
339 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>unlink</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="unlink"></a> <a name="tag_03_802"></a><!-- unlink -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_802_01"></a>NAME</h4>
|
|
|
|
<blockquote>unlink - remove a directory entry</blockquote>
|
|
|
|
<h4><a name="tag_03_802_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br>
|
|
<br>
|
|
int unlink(const char *</tt><i>path</i><tt>);<br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>unlink</i>() function shall remove a link to a file. If <i>path</i> names a symbolic link, <i>unlink</i>() shall remove
|
|
the symbolic link named by <i>path</i> and shall not affect any file or directory named by the contents of the symbolic link.
|
|
Otherwise, <i>unlink</i>() shall remove the link named by the pathname pointed to by <i>path</i> and shall decrement the link count
|
|
of the file referenced by the link.</p>
|
|
|
|
<p>When the file's link count becomes 0 and no process has the file open, the space occupied by the file shall be freed and the
|
|
file shall no longer be accessible. If one or more processes have the file open when the last link is removed, the link shall be
|
|
removed before <i>unlink</i>() returns, but the removal of the file contents shall be postponed until all references to the file
|
|
are closed.</p>
|
|
|
|
<p>The <i>path</i> argument shall not name a directory unless the process has appropriate privileges and the implementation
|
|
supports using <i>unlink</i>() on directories.</p>
|
|
|
|
<p>Upon successful completion, <i>unlink</i>() shall mark for update the <i>st_ctime</i> and <i>st_mtime</i> fields of the parent
|
|
directory. Also, if the file's link count is not 0, the <i>st_ctime</i> field of the file shall be marked for update.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and <i>errno</i> set to indicate the error. If
|
|
-1 is returned, the named file shall not be changed.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>unlink</i>() function shall fail and shall not unlink the file if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EACCES]</dt>
|
|
|
|
<dd>Search permission is denied for a component of the path prefix, or write permission is denied on the directory containing the
|
|
directory entry to be removed.</dd>
|
|
|
|
<dt>[EBUSY]</dt>
|
|
|
|
<dd>The file named by the <i>path</i> argument cannot be unlinked because it is being used by the system or another process and the
|
|
implementation considers this an error.</dd>
|
|
|
|
<dt>[ELOOP]</dt>
|
|
|
|
<dd>A loop exists in symbolic links encountered during resolution of the <i>path</i> argument.</dd>
|
|
|
|
<dt>[ENAMETOOLONG]</dt>
|
|
|
|
<dd>
|
|
The length of the <i>path</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.</dd>
|
|
|
|
<dt>[ENOENT]</dt>
|
|
|
|
<dd>A component of <i>path</i> does not name an existing file or <i>path</i> is an empty string.</dd>
|
|
|
|
<dt>[ENOTDIR]</dt>
|
|
|
|
<dd>A component of the path prefix is not a directory.</dd>
|
|
|
|
<dt>[EPERM]</dt>
|
|
|
|
<dd>The file named by <i>path</i> is a directory, and either the calling process does not have appropriate privileges, or the
|
|
implementation prohibits using <i>unlink</i>() on directories.</dd>
|
|
|
|
<dt>[EPERM] or [EACCES]</dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
|
|
"0"><br>
|
|
The S_ISVTX flag is set on the directory containing the file referred to by the <i>path</i> argument and the caller is not the file
|
|
owner, nor is the caller the directory owner, nor does the caller have appropriate privileges. <img src="../images/opt-end.gif"
|
|
alt="[Option End]" border="0"></dd>
|
|
|
|
<dt>[EROFS]</dt>
|
|
|
|
<dd>The directory entry to be unlinked is part of a read-only file system.</dd>
|
|
</dl>
|
|
|
|
<p>The <i>unlink</i>() function may fail and not unlink the file if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EBUSY]</dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
The file named by <i>path</i> is a named STREAM. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
|
|
|
|
<dt>[ELOOP]</dt>
|
|
|
|
<dd>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path</i> argument.</dd>
|
|
|
|
<dt>[ENAMETOOLONG]</dt>
|
|
|
|
<dd>
|
|
As a result of encountering a symbolic link in resolution of the <i>path</i> argument, the length of the substituted pathname
|
|
string exceeded {PATH_MAX}.</dd>
|
|
|
|
<dt>[ETXTBSY]</dt>
|
|
|
|
<dd>The entry to be unlinked is the last directory entry to a pure procedure (shared text) file that is being executed.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_802_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<h5><a name="tag_03_802_06_01"></a>Removing a Link to a File</h5>
|
|
|
|
<p>The following example shows how to remove a link to a file named <b>/home/cnd/mod1</b> by removing the entry named
|
|
<b>/modules/pass1</b>.</p>
|
|
|
|
<pre>
|
|
<tt>#include <unistd.h>
|
|
<br>
|
|
char *path = "/modules/pass1";
|
|
int status;
|
|
...
|
|
status = unlink(path);
|
|
</tt>
|
|
</pre>
|
|
|
|
<h5><a name="tag_03_802_06_02"></a>Checking for an Error</h5>
|
|
|
|
<p>The following example fragment creates a temporary password lock file named <b>LOCKFILE</b>, which is defined as
|
|
<b>/etc/ptmp</b>, and gets a file descriptor for it. If the file cannot be opened for writing, <i>unlink</i>() is used to remove
|
|
the link between the file descriptor and <b>LOCKFILE</b>.</p>
|
|
|
|
<pre>
|
|
<tt>#include <sys/types.h>
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
<br>
|
|
#define LOCKFILE "/etc/ptmp"
|
|
<br>
|
|
int pfd; /* Integer for file descriptor returned by open call. */
|
|
FILE *fpfd; /* File pointer for use in putpwent(). */
|
|
...
|
|
/* Open password Lock file. If it exists, this is an error. */
|
|
if ((pfd = open(LOCKFILE, O_WRONLY| O_CREAT | O_EXCL, S_IRUSR
|
|
| S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
|
|
fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
|
|
exit(1);
|
|
}
|
|
<br>
|
|
/* Lock file created; proceed with fdopen of lock file so that
|
|
putpwent() can be used.
|
|
*/
|
|
if ((fpfd = fdopen(pfd, "w")) == NULL) {
|
|
close(pfd);
|
|
unlink(LOCKFILE);
|
|
exit(1);
|
|
}
|
|
</tt>
|
|
</pre>
|
|
|
|
<h5><a name="tag_03_802_06_03"></a>Replacing Files</h5>
|
|
|
|
<p>The following example fragment uses <i>unlink</i>() to discard links to files, so that they can be replaced with new versions of
|
|
the files. The first call removes the link to <b>LOCKFILE</b> if an error occurs. Successive calls remove the links to
|
|
<b>SAVEFILE</b> and <b>PASSWDFILE</b> so that new links can be created, then removes the link to <b>LOCKFILE</b> when it is no
|
|
longer needed.</p>
|
|
|
|
<pre>
|
|
<tt>#include <sys/types.h>
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
<br>
|
|
#define LOCKFILE "/etc/ptmp"
|
|
#define PASSWDFILE "/etc/passwd"
|
|
#define SAVEFILE "/etc/opasswd"
|
|
...
|
|
/* If no change was made, assume error and leave passwd unchanged. */
|
|
if (!valid_change) {
|
|
fprintf(stderr, "Could not change password for user %s\n", user);
|
|
unlink(LOCKFILE);
|
|
exit(1);
|
|
}
|
|
<br>
|
|
/* Change permissions on new password file. */
|
|
chmod(LOCKFILE, S_IRUSR | S_IRGRP | S_IROTH);
|
|
<br>
|
|
/* Remove saved password file. */
|
|
unlink(SAVEFILE);
|
|
<br>
|
|
/* Save current password file. */
|
|
link(PASSWDFILE, SAVEFILE);
|
|
<br>
|
|
/* Remove current password file. */
|
|
unlink(PASSWDFILE);
|
|
<br>
|
|
/* Save new password file as current password file. */
|
|
link(LOCKFILE,PASSWDFILE);
|
|
<br>
|
|
/* Remove lock file. */
|
|
unlink(LOCKFILE);
|
|
<br>
|
|
exit(0);
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>Applications should use <a href="../functions/rmdir.html"><i>rmdir</i>()</a> to remove a directory.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>Unlinking a directory is restricted to the superuser in many historical implementations for reasons given in <a href=
|
|
"../functions/link.html"><i>link</i>()</a> (see also <a href="../functions/rename.html"><i>rename</i>()</a>).</p>
|
|
|
|
<p>The meaning of [EBUSY] in historical implementations is "mount point busy". Since this volume of
|
|
IEEE Std 1003.1-2001 does not cover the system administration concepts of mounting and unmounting, the description of the
|
|
error was changed to "resource busy". (This meaning is used by some device drivers when a second process tries to open an
|
|
exclusive use device.) The wording is also intended to allow implementations to refuse to remove a directory if it is the root or
|
|
current working directory of any process.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="close.html"><i>close</i>()</a> , <a href="link.html"><i>link</i>()</a> , <a href="remove.html"><i>remove</i>()</a> , <a
|
|
href="rmdir.html"><i>rmdir</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/unistd.h.html"><i><unistd.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_12"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>The [EBUSY] error is added to the "may fail" part of the ERRORS section.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_802_13"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>In the DESCRIPTION, the effect is specified if <i>path</i> specifies a symbolic link.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The [ELOOP] mandatory error condition is added.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A second [ENAMETOOLONG] is added as an optional error condition.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The [ETXTBSY] optional error condition is added.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The following changes were made to align with the IEEE P1003.1a draft standard:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The [ELOOP] optional error condition is added.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|