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