1.\" Copyright (c) 1983, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" from: @(#)rename.2 6.7 (Berkeley) 3/10/91 33.\" $Id: rename.2,v 1.6 1994/04/22 00:13:46 jtc Exp $ 34.\" 35.Dd March 10, 1991 36.Dt RENAME 2 37.Os BSD 4.2 38.Sh NAME 39.Nm rename 40.Nd change the name of a file 41.Sh SYNOPSIS 42.Fd #include <stdio.h> 43.Ft int 44.Fn rename "const char *from" "const char *to" 45.Sh DESCRIPTION 46.Fn Rename 47causes the link named 48.Fa from 49to be renamed as 50.Fa to . 51If 52.Fa to 53exists, it is first removed. 54Both 55.Fa from 56and 57.Fa to 58must be of the same type (that is, both directories or both 59non-directories), and must reside on the same file system. 60.Pp 61.Fn Rename 62guarantees that an instance of 63.Fa to 64will always exist, even if the system should crash in 65the middle of the operation. 66.Pp 67If the final component of 68.Fa from 69is a symbolic link, 70the symbolic link is renamed, 71not the file or directory to which it points. 72.Sh CAVEAT 73The system can deadlock if a loop in the file system graph is present. 74This loop takes the form of an entry in directory 75.Ql Pa a , 76say 77.Ql Pa a/foo , 78being a hard link to directory 79.Ql Pa b , 80and an entry in 81directory 82.Ql Pa b , 83say 84.Ql Pa b/bar , 85being a hard link 86to directory 87.Ql Pa a . 88When such a loop exists and two separate processes attempt to 89perform 90.Ql rename a/foo b/bar 91and 92.Ql rename b/bar a/foo , 93respectively, 94the system may deadlock attempting to lock 95both directories for modification. 96Hard links to directories should be 97replaced by symbolic links by the system administrator. 98.Sh RETURN VALUES 99A 0 value is returned if the operation succeeds, otherwise 100.Fn rename 101returns -1 and the global variable 102.Va errno 103indicates the reason for the failure. 104.Sh ERRORS 105.Fn Rename 106will fail and neither of the argument files will be 107affected if: 108.Bl -tag -width Er 109.It Bq Er ENAMETOOLONG 110A component of a pathname exceeded 111.Dv {NAME_MAX} 112characters, or an entire path name exceeded 113.Dv {PATH_MAX} 114characters. 115.It Bq Er ENOENT 116A component of the 117.Fa from 118path does not exist, 119or a path prefix of 120.Fa to 121does not exist. 122.It Bq Er EACCES 123A component of either path prefix denies search permission. 124.It Bq Er EACCES 125The requested link requires writing in a directory with a mode 126that denies write permission. 127.It Bq Er EPERM 128The directory containing 129.Fa from 130is marked sticky, 131and neither the containing directory nor 132.Fa from 133are owned by the effective user ID. 134.It Bq Er EPERM 135The 136.Fa to 137file exists, 138the directory containing 139.Fa to 140is marked sticky, 141and neither the containing directory nor 142.Fa to 143are owned by the effective user ID. 144.It Bq Er ELOOP 145Too many symbolic links were encountered in translating either pathname. 146.It Bq Er ENOTDIR 147A component of either path prefix is not a directory. 148.It Bq Er ENOTDIR 149.Fa from 150is a directory, but 151.Fa to 152is not a directory. 153.It Bq Er EISDIR 154.Fa to 155is a directory, but 156.Fa from 157is not a directory. 158.It Bq Er EXDEV 159The link named by 160.Fa to 161and the file named by 162.Fa from 163are on different logical devices (file systems). Note that this error 164code will not be returned if the implementation permits cross-device 165links. 166.It Bq Er ENOSPC 167The directory in which the entry for the new name is being placed 168cannot be extended because there is no space left on the file 169system containing the directory. 170.It Bq Er EDQUOT 171The directory in which the entry for the new name 172is being placed cannot be extended because the 173user's quota of disk blocks on the file system 174containing the directory has been exhausted. 175.It Bq Er EIO 176An I/O error occurred while making or updating a directory entry. 177.It Bq Er EROFS 178The requested link requires writing in a directory on a read-only file 179system. 180.It Bq Er EFAULT 181.Em Path 182points outside the process's allocated address space. 183.It Bq Er EINVAL 184.Fa From 185is a parent directory of 186.Fa to , 187or an attempt is made to rename 188.Ql \&. 189or 190.Ql \&.. . 191.It Bq Er ENOTEMPTY 192.Fa To 193is a directory and is not empty. 194.El 195.Sh SEE ALSO 196.Xr open 2 197.Sh STANDARDS 198The 199.Fn rename 200function conforms to 201.St -p1003.1-88 . 202