1 /* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 /* Written by Paul Eggert. */
19 /* derived from a function in touch.c */
33 /* Some systems (even some that do have <utime.h>) don't declare this
34 structure anywhere. */
35 #ifndef HAVE_STRUCT_UTIMBUF
43 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
44 # define __attribute__(x)
47 #ifndef ATTRIBUTE_UNUSED
48 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
51 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
52 TIMESPEC[0] and TIMESPEC[1], respectively.
53 FD must be either negative -- in which case it is ignored --
54 or a file descriptor that is open on FILE.
55 If TIMESPEC is null, set the time stamps to the current time. */
58 futimens (int fd ATTRIBUTE_UNUSED,
59 char const *file, struct timespec const timespec[2])
61 /* There's currently no interface to set file timestamps with
62 nanosecond resolution, so do the best we can, discarding any
63 fractional part of the timestamp. */
64 #if HAVE_WORKING_UTIMES
65 struct timeval timeval[2];
66 struct timeval const *t;
69 timeval[0].tv_sec = timespec[0].tv_sec;
70 timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
71 timeval[1].tv_sec = timespec[1].tv_sec;
72 timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
80 if (futimes (fd, t) == 0)
83 /* On GNU/Linux without the futimes syscall and without /proc
84 mounted, glibc futimes fails with errno == ENOENT. Fall back
85 on utimes if we get a weird error number like that. */
96 return utimes (file, t);
100 struct utimbuf utimbuf;
101 struct utimbuf const *t;
104 utimbuf.actime = timespec[0].tv_sec;
105 utimbuf.modtime = timespec[1].tv_sec;
110 return utime (file, t);
115 /* Set the access and modification time stamps of FILE to be
116 TIMESPEC[0] and TIMESPEC[1], respectively. */
118 utimens (char const *file, struct timespec const timespec[2])
120 return futimens (-1, file, timespec);