CodeCogs
HomeForumsCart
Login



Forgotten Password?

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCStdio.h

rename

Change the name of a file

Interface

#include <stdio.h>
int rename (const char *from, const char *to)

Description

Rename causes the link named from to be renamed as to. If to exists, it is first removed. Both from and to must be of the same type (that is, both directories or both non-directories), and must reside on the same file system.

Rename guarantees that an instance of to will always exist, even if the system should crash in the middle of the operation.

If the final component of from is a symbolic link, the symbolic link is renamed, not the file or directory to which it points.

The following code attempts to rename the file "fred.txt" to "wilma.txt".

Example 1

#include <stdio.h>
 
int main()
{
  rename("fred.txt", "wilma.txt");
  return 0;
}

Caveat

The system can deadlock if a loop in the file system graph is present. This loop takes the form of an entry in directory '<span class="Qlq"><span class="Pa">a</span>,</span>' say '<span class="Qlq"><span class="Pa">a/foo</span>,</span>' being a hard link to directory '<span class="Qlq"><span class="Pa">b</span>,</span>' and an entry in directory '<span class="Qlq"><span class="Pa">b</span>,</span>' say '<span class="Qlq"><span class="Pa">b/bar</span>,</span>' being a hard link to directory '<span class="Qlq"><span class="Pa">a</span>.</span>' When such a loop exists and two separate processes attempt to perform '<span class="Qlq">rename</span> <span class="Qlq">a/foo</span> <span class="Qlq">b/bar</span>' and '<span class="Qlq">rename</span> <span class="Qlq">b/bar</span> <span class="Qlq">a/foo</span>', respectively, the system may deadlock attempting to lock both directories for modification. Hard links to directories should be replaced by symbolic links by the system administrator.

Return Values

A 0 value is returned if the operation succeeds, otherwise rename returns -1 and the global variable <span class="Va">errno</span> indicates the reason for the failure.

Errors

Rename will fail and neither of the argument files will be affected if: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENAMETOOLONG</span></span>] </td> <td valign="top"> A component of a pathname exceeded <span class="Dv">{NAME_MAX}</span> characters, or an entire path name exceeded <span class="Dv">{PATH_MAX}</span> characters. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOENT</span></span>] </td> <td valign="top"> A component of the \c from path does not exist, or a path prefix of \c to does not exist. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EACCES</span></span>] </td> <td valign="top"> A component of either path prefix denies search permission. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EACCES</span></span>] </td> <td valign="top"> The requested link requires writing in a directory with a mode that denies write permission. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EPERM</span></span>] </td> <td valign="top"> The directory containing \c from is marked sticky, and neither the containing directory nor \c from are owned by the effective user ID. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EPERM</span></span>] </td> <td valign="top"> The \c to file exists, the directory containing \c to is marked sticky, and neither the containing directory nor \c to are owned by the effective user ID. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ELOOP</span></span>] </td> <td valign="top"> Too many symbolic links were encountered in translating either pathname. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOTDIR</span></span>] </td> <td valign="top"> A component of either path prefix is not a directory. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOTDIR</span></span>] </td> <td valign="top"> \c from is a directory, but \c to is not a directory. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EISDIR</span></span>] </td> <td valign="top"> \c to is a directory, but \c from is not a directory. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EXDEV</span></span>] </td> <td valign="top"> The link named by \c to and the file named by \c from are on different logical devices (file systems). Note that this error code will not be returned if the implementation permits cross-device links. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOSPC</span></span>] </td> <td valign="top"> The directory in which the entry for the new name is being placed cannot be extended because there is no space left on the file system containing the directory. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EDQUOT</span></span>] </td> <td valign="top"> The directory in which the entry for the new name is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EIO</span></span>] </td> <td valign="top"> An I/O error occurred while making or updating a directory entry. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EROFS</span></span>] </td> <td valign="top"> The requested link requires writing in a directory on a read-only file system. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EFAULT</span></span>] </td> <td valign="top"> <span class="Em">Path</span> points outside the process's allocated address space. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EINVAL</span></span>] </td> <td valign="top"> \c From is a parent directory of \c to, or an attempt is made to rename '<span class="Qlq">.</span>' or '<span class="Qlq">..</span>'. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOTEMPTY</span></span>] </td> <td valign="top"> \c To is a directory and is not empty. </td> </tr> </table>

Last Modified: 18 Dec 11 @ 13:13     Page Rendered: 2022-03-14 15:45:57