* io/Makefile (routines): Add fstatat, fstatat64, fxstatat, fxstatat64,
[glibc.git] / sysdeps / unix / sysv / linux / renameat.c
blob31662ea05313594999ae7e3a1244544557dcfe6f
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <sysdep.h>
25 /* Rename the file OLD relative to OLDFD to NEW relative to NEWFD. */
26 int
27 renameat (oldfd, old, newfd, new)
28 int oldfd;
29 const char *old;
30 int newfd;
31 const char *new;
33 static const char procfd[] = "/proc/self/fd/%d/%s";
34 char *bufold = NULL;
36 if (oldfd != AT_FDCWD && old[0] != '/')
38 size_t filelen = strlen (old);
39 /* Buffer for the path name we are going to use. It consists of
40 - the string /proc/self/fd/
41 - the file descriptor number
42 - the file name provided.
43 The final NUL is included in the sizeof. A bit of overhead
44 due to the format elements compensates for possible negative
45 numbers. */
46 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
47 bufold = alloca (buflen);
49 __snprintf (bufold, buflen, procfd, oldfd, old);
50 old = bufold;
53 char *bufnew = NULL;
55 if (newfd != AT_FDCWD && new[0] != '/')
57 size_t filelen = strlen (new);
58 /* Buffer for the path name we are going to use. It consists of
59 - the string /proc/self/fd/
60 - the file descriptor number
61 - the file name provided.
62 The final NUL is included in the sizeof. A bit of overhead
63 due to the format elements compensates for possible negative
64 numbers. */
65 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
66 bufnew = alloca (buflen);
68 __snprintf (bufnew, buflen, procfd, newfd, new);
69 new = bufnew;
72 INTERNAL_SYSCALL_DECL (err);
74 int result = INTERNAL_SYSCALL (rename, err, 2, old, new);
76 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
78 int errval = INTERNAL_SYSCALL_ERRNO (result, err);
79 if (errval == ENOTDIR && (bufnew != NULL || bufold != NULL))
81 /* This can mean either the file descriptor is invalid or
82 /proc is not mounted. */
83 struct stat64 st;
85 if (bufnew != NULL)
87 if (__fxstat64 (_STAT_VER, newfd, &st) != 0)
88 /* errno is already set correctly. */
89 return -1;
91 /* If /proc is not mounted there is nothing we can do. */
92 if (S_ISDIR (st.st_mode)
93 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
94 || !S_ISDIR (st.st_mode)))
96 errval = ENOSYS;
97 goto out;
101 if (bufold != NULL)
103 if (__fxstat64 (_STAT_VER, oldfd, &st) != 0)
104 /* errno is already set correctly. */
105 return -1;
107 /* If /proc is not mounted there is nothing we can do. */
108 if (S_ISDIR (st.st_mode)
109 && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
110 || !S_ISDIR (st.st_mode)))
111 errval = ENOSYS;
115 out:
116 __set_errno (errval);
117 result = -1;
120 return result;