]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/futimesat.c
* io/Makefile (routines): Add fstatat, fstatat64, fxstatat, fxstatat64,
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / futimesat.c
CommitLineData
26cec518
UD
1/* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19#include <errno.h>
20#include <fcntl.h>
21#include <stddef.h>
22#include <stdio.h>
23#include <utime.h>
24#include <sys/time.h>
25#include <sysdep.h>
26#include "kernel-features.h"
27
28
29/* Change the access time of FILE relative to FD to TVP[0] and
30 the modification time of FILE to TVP[1]. */
31int
32futimesat (fd, file, tvp)
33 int fd;
34 const char *file;
35 const struct timeval tvp[2];
36{
37 char *buf = NULL;
38
39 if (fd != AT_FDCWD && file[0] != '/')
40 {
41 size_t filelen = strlen (file);
42 static const char procfd[] = "/proc/self/fd/%d/%s";
43 /* Buffer for the path name we are going to use. It consists of
44 - the string /proc/self/fd/
45 - the file descriptor number
46 - the file name provided.
47 The final NUL is included in the sizeof. A bit of overhead
48 due to the format elements compensates for possible negative
49 numbers. */
50 size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
51 buf = alloca (buflen);
52
53 __snprintf (buf, buflen, procfd, fd, file);
54 file = buf;
55 }
56
57 int result;
58 INTERNAL_SYSCALL_DECL (err);
59
60#ifdef __NR_utimes
61 result = INTERNAL_SYSCALL (utimes, err, 2, file, tvp);
62 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
63 return result;
64
65# ifndef __ASSUME_UTIMES
66 if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
67 goto fail;
68# endif
69#endif
70
71 /* The utimes() syscall does not exist or is not available in the
72 used kernel. Use utime(). For this we have to convert to the
73 data format utime() expects. */
74#ifndef __ASSUME_UTIMES
75 struct utimbuf tmp;
76 struct utimbuf *times;
77
78 if (tvp != NULL)
79 {
80 times = &tmp;
81 tmp.actime = tvp[0].tv_sec + tvp[0].tv_usec / 1000000;
82 tmp.modtime = tvp[1].tv_sec + tvp[1].tv_usec / 1000000;
83 }
84 else
85 times = NULL;
86
87 result = INTERNAL_SYSCALL (utime, err, 2, file, times);
88 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
89 return result;
90
91 fail:
92#endif
93
94 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
95
96 return -1;
97}