Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / utime.c
1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004, 2006 Free Software
2    Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 2, or (at your option) any
7    later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* derived from a function in touch.c */
19
20 #include <config.h>
21 #undef utime
22
23 #include <sys/types.h>
24
25 #ifdef HAVE_UTIME_H
26 # include <utime.h>
27 #endif
28
29 #if !HAVE_UTIMES_NULL
30 # include <sys/stat.h>
31 # include <fcntl.h>
32 #endif
33
34 #include <unistd.h>
35 #include <errno.h>
36
37 #include "full-write.h"
38 #include "safe-read.h"
39
40 /* Some systems (even some that do have <utime.h>) don't declare this
41    structure anywhere.  */
42 #ifndef HAVE_STRUCT_UTIMBUF
43 struct utimbuf
44 {
45   long actime;
46   long modtime;
47 };
48 #endif
49
50 /* The results of open() in this file are not used with fchdir,
51    therefore save some unnecessary work in fchdir.c.  */
52 #undef open
53 #undef close
54
55 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
56    interpret it to set the access and modification times of FILE to
57    the current time.  Return 0 if successful, -1 if not. */
58
59 static int
60 utime_null (const char *file)
61 {
62 #if HAVE_UTIMES_NULL
63   return utimes (file, 0);
64 #else
65   int fd;
66   char c;
67   int status = 0;
68   struct stat st;
69   int saved_errno = 0;
70
71   fd = open (file, O_RDWR);
72   if (fd < 0
73       || fstat (fd, &st) < 0
74       || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR
75       || lseek (fd, (off_t) 0, SEEK_SET) < 0
76       || full_write (fd, &c, sizeof c) != sizeof c
77       /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination
78          of patches, but that system doesn't use this code: it has utimes.
79          || fsync (fd) < 0
80       */
81       || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0))
82     {
83       saved_errno = errno;
84       status = -1;
85     }
86
87   if (0 <= fd)
88     {
89       if (close (fd) < 0)
90         status = -1;
91
92       /* If there was a prior failure, use the saved errno value.
93          But if the only failure was in the close, don't change errno.  */
94       if (saved_errno)
95         errno = saved_errno;
96     }
97
98   return status;
99 #endif
100 }
101
102 int
103 rpl_utime (const char *file, const struct utimbuf *times)
104 {
105   if (times)
106     return utime (file, times);
107
108   return utime_null (file);
109 }