Files
2024-02-19 00:21:47 -05:00

360 lines
17 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>rename</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="rename"></a> <a name="tag_03_608"></a><!-- rename -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_608_01"></a>NAME</h4>
<blockquote>rename - rename a file</blockquote>
<h4><a name="tag_03_608_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/stdio.h.html">stdio.h</a>&gt;<br>
<br>
int rename(const char *</tt><i>old</i><tt>, const char *</tt><i>new</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_608_03"></a>DESCRIPTION</h4>
<blockquote>
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between the
requirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers to
the ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
<p>The <i>rename</i>() function shall change the name of a file. The <i>old</i> argument points to the pathname of the file to be
renamed. The <i>new</i> argument points to the new pathname of the file.</p>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> If
either the <i>old</i> or <i>new</i> argument names a symbolic link, <i>rename</i>() shall operate on the symbolic link itself, and
shall not resolve the last component of the argument. If the <i>old</i> argument and the <i>new</i> argument resolve to the same
existing file, <i>rename</i>() shall return successfully and perform no other action.</p>
<p>If the <i>old</i> argument points to the pathname of a file that is not a directory, the <i>new</i> argument shall not point to
the pathname of a directory. If the link named by the <i>new</i> argument exists, it shall be removed and <i>old</i> renamed to
<i>new</i>. In this case, a link named <i>new</i> shall remain visible to other processes throughout the renaming operation and
refer either to the file referred to by <i>new</i> or <i>old</i> before the operation began. Write access permission is required
for both the directory containing <i>old</i> and the directory containing <i>new</i>.</p>
<p>If the <i>old</i> argument points to the pathname of a directory, the <i>new</i> argument shall not point to the pathname of a
file that is not a directory. If the directory named by the <i>new</i> argument exists, it shall be removed and <i>old</i> renamed
to <i>new</i>. In this case, a link named <i>new</i> shall exist throughout the renaming operation and shall refer either to the
directory referred to by <i>new</i> or <i>old</i> before the operation began. If <i>new</i> names an existing directory, it shall
be required to be an empty directory.</p>
<p>If the <i>old</i> argument points to a pathname of a symbolic link, the symbolic link shall be renamed. If the <i>new</i>
argument points to a pathname of a symbolic link, the symbolic link shall be removed.</p>
<p>The <i>new</i> pathname shall not contain a path prefix that names <i>old</i>. Write access permission is required for the
directory containing <i>old</i> and the directory containing <i>new</i>. If the <i>old</i> argument points to the pathname of a
directory, write access permission may be required for the directory named by <i>old</i>, and, if it exists, the directory named by
<i>new</i>.</p>
<p>If the link named by the <i>new</i> argument exists and the file's link count becomes 0 when it is removed 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>rename</i>() returns, but the removal of the
file contents shall be postponed until all references to the file are closed.</p>
<p>Upon successful completion, <i>rename</i>() shall mark for update the <i>st_ctime</i> and <i>st_mtime</i> fields of the parent
directory of each file.</p>
<p>If the <i>rename</i>() function fails for any reason other than [EIO], any file named by <i>new</i> shall be unaffected. <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_608_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>rename</i>() shall return 0; otherwise, -1 shall be returned, <sup>[<a href=
"javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> <i>errno</i> shall
be set to indicate the error, <img src="../images/opt-end.gif" alt="[Option End]" border="0"> and neither the file named by
<i>old</i> nor the file named by <i>new</i> shall be changed or created.</p>
</blockquote>
<h4><a name="tag_03_608_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>rename</i>() function shall fail if:</p>
<dl compact>
<dt>[EACCES]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A
component of either path prefix denies search permission; or one of the directories containing <i>old</i> or <i>new</i> denies
write permissions; or, write permission is required and is denied for a directory pointed to by the <i>old</i> or <i>new</i>
arguments. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[EBUSY]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The directory named by <i>old</i> or <i>new</i> is currently in use by the system or another process, and the implementation
considers this an error. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[EEXIST]&nbsp;or&nbsp;[ENOTEMPTY]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
"0"><br>
The link named by <i>new</i> is a directory that is not an empty directory. <img src="../images/opt-end.gif" alt="[Option End]"
border="0"></dd>
<dt>[EINVAL]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The <i>new</i> directory pathname contains a path prefix that names the <i>old</i> directory. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
<dt>[EIO]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A
physical I/O error has occurred. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[EISDIR]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The <i>new</i> argument points to a directory and the <i>old</i> argument points to a file that is not a directory. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ELOOP]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A
loop exists in symbolic links encountered during resolution of the <i>path</i> argument. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
<dt>[EMLINK]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The file named by <i>old</i> is a directory, and the link count of the parent directory of <i>new</i> would exceed {LINK_MAX}. <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ENAMETOOLONG]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
"0"><br>
The length of the <i>old</i> or <i>new</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ENOENT]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The link named by <i>old</i> does not name an existing file, or either <i>old</i> or <i>new</i> points to an empty string. <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ENOSPC]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The directory that would contain <i>new</i> cannot be extended. <img src="../images/opt-end.gif" alt="[Option End]" border=
"0"></dd>
<dt>[ENOTDIR]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> A
component of either path prefix is not a directory; or the <i>old</i> argument names a directory and <i>new</i> argument names a
non-directory file. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></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 <i>old</i> and the caller is not the file owner, nor is
the caller the directory owner, nor does the caller have appropriate privileges; or <i>new</i> refers to an existing file, the
S_ISVTX flag is set on the directory containing this file, 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><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The requested operation requires writing in a directory on a read-only file system. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
<dt>[EXDEV]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The links named by <i>new</i> and <i>old</i> are on different file systems and the implementation does not support links between
file systems. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
</dl>
<p>The <i>rename</i>() function may fail 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 the <i>old</i> or <i>new</i> arguments is a named STREAM. <img src="../images/opt-end.gif" alt="[Option End]"
border="0"></dd>
<dt>[ELOOP]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path</i> argument. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ENAMETOOLONG]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
"0"><br>
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}. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>[ETXTBSY]</dt>
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The file to be renamed is a pure procedure (shared text) file that is being executed. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_608_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_608_06_01"></a>Renaming a File</h5>
<p>The following example shows how to rename a file named <b>/home/cnd/mod1</b> to <b>/home/cnd/mod2</b>.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
<br>
int status;
...
status = rename("/home/cnd/mod1", "/home/cnd/mod2");
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_608_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>Some implementations mark for update the <i>st_ctime</i> field of renamed files and some do not. Applications which make use of
the <i>st_ctime</i> field may behave differently with respect to renamed files unless they are designed to allow for either
behavior.</p>
</blockquote>
<h4><a name="tag_03_608_08"></a>RATIONALE</h4>
<blockquote>
<p>This <i>rename</i>() function is equivalent for regular files to that defined by the ISO&nbsp;C standard. Its inclusion here
expands that definition to include actions on directories and specifies behavior when the <i>new</i> parameter names a file that
already exists. That specification requires that the action of the function be atomic.</p>
<p>One of the reasons for introducing this function was to have a means of renaming directories while permitting implementations to
prohibit the use of <a href="../functions/link.html"><i>link</i>()</a> and <a href="../functions/unlink.html"><i>unlink</i>()</a>
with directories, thus constraining links to directories to those made by <a href="../functions/mkdir.html"><i>mkdir</i>()</a>.</p>
<p>The specification that if <i>old</i> and <i>new</i> refer to the same file is intended to guarantee that:</p>
<pre>
<tt>rename("x", "x");
</tt>
</pre>
<p>does not remove the file.</p>
<p>Renaming dot or dot-dot is prohibited in order to prevent cyclical file system paths.</p>
<p>See also the descriptions of [ENOTEMPTY] and [ENAMETOOLONG] in <a href="rmdir.html"><i>rmdir</i>()</a> and [EBUSY] in <a href=
"unlink.html"><i>unlink</i>()</a> . For a discussion of [EXDEV], see <a href="link.html"><i>link</i>()</a> .</p>
</blockquote>
<h4><a name="tag_03_608_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_608_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="link.html"><i>link</i>()</a> , <a href="rmdir.html"><i>rmdir</i>()</a> , <a href="symlink.html"><i>symlink</i>()</a> ,
<a href="unlink.html"><i>unlink</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/stdio.h.html"><i>&lt;stdio.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_608_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 3. Included for alignment with the POSIX.1-1988 standard.</p>
</blockquote>
<h4><a name="tag_03_608_12"></a>Issue 5</h4>
<blockquote>
<p>The [EBUSY] error is added to the &quot;may fail&quot; part of the ERRORS section.</p>
</blockquote>
<h4><a name="tag_03_608_13"></a>Issue 6</h4>
<blockquote>
<p>Extensions beyond the ISO&nbsp;C standard are marked.</p>
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
<ul>
<li>
<p>The [EIO] mandatory error condition is added.</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&nbsp;P1003.1a draft standard:</p>
<ul>
<li>
<p>Details are added regarding the treatment of symbolic links.</p>
</li>
<li>
<p>The [ELOOP] optional error condition is added.</p>
</li>
</ul>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; 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 &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>