2.5-18.1
[glibc.git] / sysdeps / unix / sysv / linux / futimes.c
blobb307c3ff64ae2b65f2bd5495822c1a7350477f8c
1 /* futimes -- change access and modification times of open file. Linux version.
2 Copyright (C) 2002,2003,2005,2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <string.h>
23 #include <utime.h>
24 #include <sys/time.h>
25 #include <stdio-common/_itoa.h>
26 #include <fcntl.h>
28 #include <kernel-features.h>
30 /* Change the access time of FILE to TVP[0] and
31 the modification time of FILE to TVP[1], but do not follow symlinks.
33 The Linux kernel has no futimes() syscall so we use the /proc
34 filesystem. */
35 int
36 __futimes (int fd, const struct timeval tvp[2])
38 static const char selffd[] = "/proc/self/fd/";
39 char fname[sizeof (selffd) + 3 * sizeof (int)];
40 fname[sizeof (fname) - 1] = '\0';
41 char *cp = _itoa_word ((unsigned int) fd, fname + sizeof (fname) - 1, 10, 0);
42 cp = memcpy (cp - sizeof (selffd) + 1, selffd, sizeof (selffd) - 1);
44 int result;
45 #ifdef __NR_utimes
46 result = INLINE_SYSCALL (utimes, 2, cp, tvp);
47 # ifndef __ASSUME_UTIMES
48 if (result == -1 && errno == ENOSYS)
49 # endif
50 #endif
52 /* The utimes() syscall does not exist or is not available in the
53 used kernel. Use utime(). For this we have to convert to the
54 data format utime() expects. */
55 #ifndef __ASSUME_UTIMES
56 struct utimbuf buf;
57 struct utimbuf *times;
59 if (tvp != NULL)
61 times = &buf;
62 buf.actime = tvp[0].tv_sec + (tvp[0].tv_usec + 500000) / 1000000;
63 buf.modtime = tvp[1].tv_sec + (tvp[1].tv_usec + 500000) / 1000000;
65 else
66 times = NULL;
68 result = INLINE_SYSCALL (utime, 2, cp, times);
69 #endif
72 if (result == -1)
73 /* Check for errors that result from failing to find /proc.
74 This means we can't do futimes at all, so return ENOSYS
75 rather than some confusing error. */
76 switch (errno)
78 case EACCES:
79 if (tvp == NULL) /* Could be a path problem or a file problem. */
80 break;
81 /*FALLTHROUGH*/
82 case ELOOP:
83 case ENAMETOOLONG:
84 case ENOTDIR:
85 __set_errno (ENOSYS);
86 break;
88 case ENOENT:
89 /* Validate the file descriptor by letting fcntl set errno to
90 EBADF if it's bogus. Otherwise it's a /proc issue. */
91 #if !defined __NR_fcntl && defined __NR_fcntl64
92 # define __NR_fcntl __NR_fcntl64
93 #endif
94 if (INLINE_SYSCALL (fcntl, 3, fd, F_GETFD, 0) != -1)
95 __set_errno (ENOSYS);
96 break;
99 return result;
101 weak_alias (__futimes, futimes)