Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / futimesat.c
blobce7290e0f3fc20992be0a692c2827d20d8250d67
1 /* Copyright (C) 2005-2014 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <utime.h>
24 #include <sys/time.h>
25 #include <sysdep.h>
26 #include <kernel-features.h>
29 /* Change the access time of FILE relative to FD to TVP[0] and
30 the modification time of FILE to TVP[1]. */
31 int
32 futimesat (fd, file, tvp)
33 int fd;
34 const char *file;
35 const struct timeval tvp[2];
37 int result;
39 #ifdef __NR_futimesat
40 # ifndef __ASSUME_ATFCTS
41 if (__have_atfcts >= 0)
42 # endif
44 if (file == NULL)
45 return __futimes (fd, tvp);
47 result = INLINE_SYSCALL (futimesat, 3, fd, file, tvp);
48 # ifndef __ASSUME_ATFCTS
49 if (result == -1 && errno == ENOSYS)
50 __have_atfcts = -1;
51 else
52 # endif
53 return result;
55 #endif
57 #ifndef __ASSUME_ATFCTS
58 char *buf = NULL;
60 if (file == NULL)
62 static const char procfd[] = "/proc/self/fd/%d";
63 /* Buffer for the path name we are going to use. It consists of
64 - the string /proc/self/fd/
65 - the file descriptor number.
66 The final NUL is included in the sizeof. A bit of overhead
67 due to the format elements compensates for possible negative
68 numbers. */
69 size_t buflen = sizeof (procfd) + sizeof (int) * 3;
70 buf = alloca (buflen);
72 __snprintf (buf, buflen, procfd, fd);
73 file = buf;
75 else if (fd != AT_FDCWD && file[0] != '/')
77 size_t filelen = strlen (file);
78 if (__builtin_expect (filelen == 0, 0))
80 __set_errno (ENOENT);
81 return -1;
84 static const char procfd[] = "/proc/self/fd/%d/%s";
85 /* Buffer for the path name we are going to use. It consists of
86 - the string /proc/self/fd/
87 - the file descriptor number
88 - the file name provided.
89 The final NUL is included in the sizeof. A bit of overhead
90 due to the format elements compensates for possible negative
91 numbers. */
92 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
93 buf = alloca (buflen);
95 __snprintf (buf, buflen, procfd, fd, file);
96 file = buf;
99 INTERNAL_SYSCALL_DECL (err);
101 # ifdef __NR_utimes
102 result = INTERNAL_SYSCALL (utimes, err, 2, file, tvp);
103 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
104 return result;
106 # ifndef __ASSUME_UTIMES
107 if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
108 goto fail;
109 # endif
110 # endif
112 /* The utimes() syscall does not exist or is not available in the
113 used kernel. Use utime(). For this we have to convert to the
114 data format utime() expects. */
115 # ifndef __ASSUME_UTIMES
116 struct utimbuf tmp;
117 struct utimbuf *times;
119 if (tvp != NULL)
121 times = &tmp;
122 tmp.actime = tvp[0].tv_sec + tvp[0].tv_usec / 1000000;
123 tmp.modtime = tvp[1].tv_sec + tvp[1].tv_usec / 1000000;
125 else
126 times = NULL;
128 result = INTERNAL_SYSCALL (utime, err, 2, file, times);
129 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
130 return result;
132 fail:
133 # endif
135 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
137 return -1;
138 #endif