FAT | RFS | XFS
rename()
PROTOTYPE
#include <stdio.h>
int rename(const char *old, const char *new);
DESCRIPTION
rename() changes a file’s path name from old to new. Neither path may end with “.” or “..”. Paths starting with ‘/’ are absolute. Otherwise they are relative to the CWD. Both paths must resolve to the same volume.
If the file or directory specified by new already exists, it is deleted. The operation of rename() is atomic: either it completes successfully or-if a reset occurs-the original files are present with their original names.
The caller needs write-access to both parent directories and execute-access to every directory in the path prefix. The status change and data modification timestamps of both parent directories are updated.
If successful, rename() returns 0. Otherwise, it sets errno and returns -1.
ERROR CODES
| EACCES | No execute access to a directory in either path prefix or no write access to either parent directory. |
| EBUSY | new is a task’s CWD. |
| EFAULT | old or new equals NULL. |
| EINVAL | old is a regular file and new ends with ‘/’, old is a parent of new, or either path ends with “.” or “..”. |
| EISDIR | old is a regular file and new is a directory. |
| ENAMETOOLONG | A path component exceeds the effective maximum file name length. |
| ENOENT | old or new is the empty string, either old or the new prefix includes a file that was not found. |
| ENOSPC | No space is left on volume to rename a file or directory. |
| ENOTEMPTY | new specifies a directory that is not empty. |
| ENOTDIR | A non-leaf path component is not a directory or old is but new is not. |
| EROFS | The volume is read-only. |
| EXDEV | new is on a different volume than old. |
EXAMPLE
/*-------------------------------------------------------------------*/
/* Atomically delete initial file and rename second to first's name. */
/*-------------------------------------------------------------------*/
if (rename("second.txt", "initial.txt"))
error("rename() failed");
if (vsync("initial.txt"))
error("vsync() failed");