Name

rename - rename files

Library

libc.lib

Synopsis

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

Return values

The rename() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The rename system call 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.

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

If a file with a symbolic link pointing to it is renamed, then a subsequent open call on the symbolic link file would automatically remove the link file, i.e consider a symbolic file link.x pointing to a file abc.x. If the file abc.x is renamed to abcd.x then, a subsequent open call on link.x file would automatically remove link.x file.

Note: 1>rename call doesn’t differenciate between hard and soft links. 2>If the specified file is a dangling link file, then this link file will be automatically removed.

Limitation: rename call fails if from and to are files, and are in use(i.e., if any one of these files is kept open by a process). Parent directory time stamps remain uneffected if rename creates a new entry in the directory, this new entry has only two time stamps, modification and access time stamps, here access time stamp is equal to modification time stamp.


Examples

/****************************************************************************************
* Detailed description: This sample code demonstrates usage of rename system call.
*
* Preconditions: Example.cfg file should be present in the current working directory.
****************************************************************************************/
#include <stdio.h>
int main()
{
  if(rename("Example.txt" , "Example2.txt") < 0 )  {
     printf("Failed to rename Example.txt\n");
     return -1;
  }
  printf("Rename successful \n");
  return 0;
}

         

Output

Rename successful

         


Errors

The rename system call will fail and neither of the argument files will be affected if:
[ENAMETOOLONG]
  A component or an entire path name exceeded 255 characters.
[ENOENT]
  A component of the from path does not exist, or a path prefix of to does not exist.
[EACCES]
  A component of either path prefix denies search permission(Not supported).
[EACCES]
  The requested link requires writing in a directory with a mode that denies write permission(Not supported).

[EPERM]
  The directory containing from is marked sticky, and neither the containing directory nor from are owned by the effective user ID.

Too many symbolic links were encountered in translating either pathname(Currenlty not supported).

[ENOTDIR]
  A component of either path prefix is not a directory.
[ENOTDIR]
  The from argument is a directory, but to is not a directory.
[EISDIR]
  The to argument is a directory, but from is not a directory.
[EXDEV]
  The link named by to and the file named by from are on different logical devices (file systems). Note that this error code will not be returned if the implementation permits cross-device links(Not supported).
[ENOSPC]
  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(Currently not supported).
[EDQUOT]
  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(Currently not supported).
[EIO] An I/O error occurred while making or updating a directory entry(Not supported).
[EROFS]
  The requested link requires writing in a directory on a read-only file system(Not supported).
[EFAULT]
  Path points outside the process’s allocated address space(Currently not supported).
[EINVAL]
  The from argument is a parent directory of to, or an attempt is made to rename .’ or ..’.
[ENOTEMPTY]
  The to argument is a directory and is not empty.

See also

open, symlink
The rename system call is expected to conform to -p1003.1-96.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top