1RENAME(P)                  POSIX Programmer's Manual                 RENAME(P)
2
3
4

NAME

6       rename - rename a file
7

SYNOPSIS

9       #include <stdio.h>
10
11       int rename(const char *old, const char *new);
12
13

DESCRIPTION

15       The rename() function shall change the name of a file. The old argument
16       points to the pathname of the file to  be  renamed.  The  new  argument
17       points to the new pathname of the file.
18
19       If either the old or new argument names a symbolic link, rename() shall
20       operate on the symbolic link itself, and shall  not  resolve  the  last
21       component  of  the  argument.  If the old argument and the new argument
22       resolve to the same existing file, rename() shall  return  successfully
23       and perform no other action.
24
25       If  the  old  argument  points  to the pathname of a file that is not a
26       directory, the new argument shall not point to the pathname of a direc‐
27       tory. If the link named by the new argument exists, it shall be removed
28       and old renamed to new. In this case, a link  named  new  shall  remain
29       visible  to other processes throughout the renaming operation and refer
30       either to the file referred to by  new  or  old  before  the  operation
31       began.  Write access permission is required for both the directory con‐
32       taining old and the directory containing new.
33
34       If the old argument points to the pathname  of  a  directory,  the  new
35       argument shall not point to the pathname of a file that is not a direc‐
36       tory. If the directory named by the new argument exists,  it  shall  be
37       removed  and  old  renamed to new. In this case, a link named new shall
38       exist throughout the renaming operation and shall refer either  to  the
39       directory  referred to by new or old before the operation began. If new
40       names an existing directory, it shall be required to be an empty direc‐
41       tory.
42
43       If  the  old argument points to a pathname of a symbolic link, the sym‐
44       bolic link shall be renamed. If the new argument points to  a  pathname
45       of a symbolic link, the symbolic link shall be removed.
46
47       The  new pathname shall not contain a path prefix that names old. Write
48       access permission is required for the directory containing old and  the
49       directory  containing  new.  If the old argument points to the pathname
50       of a directory, write access permission may be required for the  direc‐
51       tory named by old, and, if it exists, the directory named by new.
52
53       If  the link named by the new argument exists and the file's link count
54       becomes 0 when it is removed and no process  has  the  file  open,  the
55       space  occupied by the file shall be freed and the file shall no longer
56       be accessible. If one or more processes have the  file  open  when  the
57       last  link  is  removed,  the  link  shall  be  removed before rename()
58       returns, but the removal of the file contents shall be postponed  until
59       all references to the file are closed.
60
61       Upon successful completion, rename() shall mark for update the st_ctime
62       and st_mtime fields of the parent directory of each file.
63
64       If the rename() function fails for any reason  other  than  [EIO],  any
65       file named by new shall be unaffected.
66

RETURN VALUE

68       Upon  successful  completion,  rename()  shall  return 0; otherwise, -1
69       shall be returned,   errno shall be set to  indicate  the  error,   and
70       neither  the  file  named  by  old  nor  the file named by new shall be
71       changed or created.
72

ERRORS

74       The rename() function shall fail if:
75
76       EACCES A component of either path prefix denies search  permission;  or
77              one  of  the directories containing old or new denies write per‐
78              missions; or, write permission is required and is denied  for  a
79              directory pointed to by the old or new arguments.
80
81       EBUSY  The  directory  named  by  old or new is currently in use by the
82              system or another process, and the implementation considers this
83              an error.
84
85       EEXIST or ENOTEMPTY
86
87              The link named by new is a directory that is not an empty direc‐
88              tory.
89
90       EINVAL The new directory pathname contains a path prefix that names the
91              old directory.
92
93       EIO    A physical I/O error has occurred.
94
95       EISDIR The  new  argument  points  to  a directory and the old argument
96              points to a file that is not a directory.
97
98       ELOOP  A loop exists in symbolic links encountered during resolution of
99              the path argument.
100
101       EMLINK The  file named by old is a directory, and the link count of the
102              parent directory of new would exceed {LINK_MAX}.
103
104       ENAMETOOLONG
105
106              The length of the old or new argument exceeds  {PATH_MAX}  or  a
107              pathname component is longer than {NAME_MAX}.
108
109       ENOENT The  link named by old does not name an existing file, or either
110              old or new points to an empty string.
111
112       ENOSPC The directory that would contain new cannot be extended.
113
114       ENOTDIR
115              A component of either path prefix is not a directory; or the old
116              argument  names  a directory and new argument names a non-direc‐
117              tory file.
118
119       EPERM or EACCES
120
121              The S_ISVTX flag is set on the  directory  containing  the  file
122              referred  to by old and the caller is not the file owner, nor is
123              the caller the directory owner, nor does the caller have  appro‐
124              priate  privileges;  or  new  refers  to  an  existing file, the
125              S_ISVTX flag is set on the directory containing this  file,  and
126              the  caller  is not the file owner, nor is the caller the direc‐
127              tory owner, nor does the caller have appropriate privileges.
128
129       EROFS  The requested operation requires writing in  a  directory  on  a
130              read-only file system.
131
132       EXDEV  The links named by new and old are on different file systems and
133              the implementation does not support links between file systems.
134
135
136       The rename() function may fail if:
137
138       EBUSY  The file named by the old or new arguments is a named STREAM.
139
140       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
141              resolution of the path argument.
142
143       ENAMETOOLONG
144
145              As a result of encountering a symbolic link in resolution of the
146              path argument, the length of  the  substituted  pathname  string
147              exceeded {PATH_MAX}.
148
149       ETXTBSY
150              The  file  to  be renamed is a pure procedure (shared text) file
151              that is being executed.
152
153
154       The following sections are informative.
155

EXAMPLES

157   Renaming a File
158       The following example shows how to rename a file  named  /home/cnd/mod1
159       to /home/cnd/mod2.
160
161
162              #include <stdio.h>
163
164
165              int status;
166              ...
167              status = rename("/home/cnd/mod1", "/home/cnd/mod2");
168

APPLICATION USAGE

170       Some  implementations  mark  for  update  the st_ctime field of renamed
171       files and some do not. Applications which  make  use  of  the  st_ctime
172       field  may behave differently with respect to renamed files unless they
173       are designed to allow for either behavior.
174

RATIONALE

176       This rename() function is equivalent for regular files to that  defined
177       by  the  ISO C  standard. Its inclusion here expands that definition to
178       include actions on directories and  specifies  behavior  when  the  new
179       parameter names a file that already exists. That specification requires
180       that the action of the function be atomic.
181
182       One of the reasons for introducing this function was to have a means of
183       renaming  directories  while permitting implementations to prohibit the
184       use of link() and unlink() with directories, thus constraining links to
185       directories to those made by mkdir().
186
187       The  specification  that  if  old  and  new  refer  to the same file is
188       intended to guarantee that:
189
190
191              rename("x", "x");
192
193       does not remove the file.
194
195       Renaming dot or dot-dot is prohibited in order to prevent cyclical file
196       system paths.
197
198       See  also the descriptions of [ENOTEMPTY] and [ENAMETOOLONG] in rmdir()
199       and [EBUSY] in unlink() . For a discussion of [EXDEV], see link() .
200

FUTURE DIRECTIONS

202       None.
203

SEE ALSO

205       link() , rmdir() , symlink() , unlink() , the Base  Definitions  volume
206       of IEEE Std 1003.1-2001, <stdio.h>
207
209       Portions  of  this text are reprinted and reproduced in electronic form
210       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
211       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
212       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
213       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
214       event of any discrepancy between this version and the original IEEE and
215       The  Open Group Standard, the original IEEE and The Open Group Standard
216       is the referee document. The original Standard can be  obtained  online
217       at http://www.opengroup.org/unix/online.html .
218
219
220
221IEEE/The Open Group                  2003                            RENAME(P)
Impressum