(mempcpy): Don't define if a system header defines it.
[gnulib.git] / lib / utime.c
1 /* Copyright (C) 1998, 2001, 2002, 2003 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 /* derived from a function in touch.c */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #undef utime
23
24 #include <sys/types.h>
25
26 #ifdef HAVE_UTIME_H
27 # include <utime.h>
28 #endif
29
30 #if !HAVE_UTIMES_NULL
31 # include <sys/stat.h>
32 # include <fcntl.h>
33 #endif
34
35 #include <unistd.h>
36 #include <errno.h>
37 #ifndef errno
38 extern int errno;
39 #endif
40
41 #include "full-write.h"
42 #include "safe-read.h"
43
44 /* Some systems (even some that do have <utime.h>) don't declare this
45    structure anywhere.  */
46 #ifndef HAVE_STRUCT_UTIMBUF
47 struct utimbuf
48 {
49   long actime;
50   long modtime;
51 };
52 #endif
53
54 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
55    interpret it to set the access and modification times of FILE to
56    the current time.  Return 0 if successful, -1 if not. */
57
58 static int
59 utime_null (const char *file)
60 {
61 #if HAVE_UTIMES_NULL
62   return utimes (file, 0);
63 #else
64   int fd;
65   char c;
66   int status = 0;
67   struct stat st;
68   int saved_errno = 0;
69
70   fd = open (file, O_RDWR);
71   if (fd < 0
72       || fstat (fd, &st) < 0
73       || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR
74       || lseek (fd, (off_t) 0, SEEK_SET) < 0
75       || full_write (fd, &c, sizeof c) != sizeof c
76       /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination
77          of patches, but that system doesn't use this code: it has utimes.
78          || fsync (fd) < 0
79       */
80       || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
81     {
82       saved_errno = errno;
83       status = -1;
84     }
85
86   if (0 <= fd)
87     {
88       if (close (fd) < 0)
89         status = -1;
90
91       /* If there was a prior failure, use the saved errno value.
92          But if the only failure was in the close, don't change errno.  */
93       if (saved_errno)
94         errno = saved_errno;
95     }
96
97   return status;
98 #endif
99 }
100
101 int
102 rpl_utime (const char *file, const struct utimbuf *times)
103 {
104   if (times)
105     return utime (file, times);
106
107   return utime_null (file);
108 }