(__attribute__, ATTRIBUTE_UNUSED): New macros.
[pspp] / lib / utimens.c
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2
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
6    later version.
7
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.
12
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
16
17 /* Written by Paul Eggert.  */
18
19 /* derived from a function in touch.c */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "utimens.h"
26
27 #if HAVE_UTIME_H
28 # include <utime.h>
29 #endif
30
31 /* Some systems (even some that do have <utime.h>) don't declare this
32    structure anywhere.  */
33 #ifndef HAVE_STRUCT_UTIMBUF
34 struct utimbuf
35 {
36   long actime;
37   long modtime;
38 };
39 #endif
40
41 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
42 # define __attribute__(x)
43 #endif
44
45 #ifndef ATTRIBUTE_UNUSED
46 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
47 #endif
48
49 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
50    TIMESPEC[0] and TIMESPEC[1], respectively.
51    FD must be either negative -- in which case it is ignored --
52    or a file descriptor that is open on FILE.
53    If TIMESPEC is null, set the time stamps to the current time.  */
54
55 int
56 futimens (int fd ATTRIBUTE_UNUSED,
57           char const *file, struct timespec const timespec[2])
58 {
59   /* There's currently no interface to set file timestamps with
60      nanosecond resolution, so do the best we can, discarding any
61      fractional part of the timestamp.  */
62 #if HAVE_WORKING_UTIMES
63   struct timeval timeval[2];
64   struct timeval const *t;
65   if (timespec)
66     {
67       timeval[0].tv_sec = timespec[0].tv_sec;
68       timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
69       timeval[1].tv_sec = timespec[1].tv_sec;
70       timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
71       t = timeval;
72     }
73   else
74     t = NULL;
75 # if HAVE_FUTIMES
76   if (0 <= fd)
77     return futimes (fd, t);
78 # endif
79   return utimes (file, t);
80
81 #else
82
83   struct utimbuf utimbuf;
84   struct utimbuf const *t;
85   if (timespec)
86     {
87       utimbuf.actime = timespec[0].tv_sec;
88       utimbuf.modtime = timespec[1].tv_sec;
89       t = &utimbuf;
90     }
91   else
92     t = NULL;
93   return utime (file, t);
94
95 #endif
96 }
97
98 /* Set the access and modification time stamps of FILE to be
99    TIMESPEC[0] and TIMESPEC[1], respectively.  */
100 int
101 utimens (char const *file, struct timespec const timespec[2])
102 {
103   return futimens (-1, file, timespec);
104 }