Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/utime.c @ 5159:a535859efd14
Merge from coreutils.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sat, 07 Aug 2004 00:09:38 +0000 |
parents | 8b1923c943a6 |
children | a48fb0e98c8c |
rev | line source |
---|---|
5159 | 1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
1442 | 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 | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
30 #if !HAVE_UTIMES_NULL |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
31 # include <sys/stat.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
32 # include <fcntl.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
33 #endif |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
34 |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
35 #include <unistd.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
36 #include <errno.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
37 |
3406 | 38 #include "full-write.h" |
1442 | 39 #include "safe-read.h" |
40 | |
41 /* Some systems (even some that do have <utime.h>) don't declare this | |
42 structure anywhere. */ | |
43 #ifndef HAVE_STRUCT_UTIMBUF | |
44 struct utimbuf | |
45 { | |
46 long actime; | |
47 long modtime; | |
48 }; | |
49 #endif | |
50 | |
51 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not | |
52 interpret it to set the access and modification times of FILE to | |
53 the current time. Return 0 if successful, -1 if not. */ | |
54 | |
55 static int | |
56 utime_null (const char *file) | |
57 { | |
58 #if HAVE_UTIMES_NULL | |
59 return utimes (file, 0); | |
60 #else | |
61 int fd; | |
62 char c; | |
63 int status = 0; | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
64 struct stat st; |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
65 int saved_errno = 0; |
1442 | 66 |
1820
6908e7ed16b7
(utime_null): Don't pass 0666 to open; it's not needed and isn't
Jim Meyering <jim@meyering.net>
parents:
1442
diff
changeset
|
67 fd = open (file, O_RDWR); |
1442 | 68 if (fd < 0 |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
69 || fstat (fd, &st) < 0 |
4088
815386455680
Update for changed signature of safe_read.
Bruno Haible <bruno@clisp.org>
parents:
3407
diff
changeset
|
70 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR |
1442 | 71 || lseek (fd, (off_t) 0, SEEK_SET) < 0 |
3406 | 72 || full_write (fd, &c, sizeof c) != sizeof c |
4474
f1650b772bb6
Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4105
diff
changeset
|
73 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination |
1442 | 74 of patches, but that system doesn't use this code: it has utimes. |
75 || fsync (fd) < 0 | |
76 */ | |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
77 || (st.st_size == 0 && ftruncate (fd, st.st_size) < 0)) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
78 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
79 saved_errno = errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
80 status = -1; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
81 } |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
82 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
83 if (0 <= fd) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
84 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
85 if (close (fd) < 0) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
86 status = -1; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
87 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
88 /* If there was a prior failure, use the saved errno value. |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
89 But if the only failure was in the close, don't change errno. */ |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
90 if (saved_errno) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
91 errno = saved_errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
92 } |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
93 |
1442 | 94 return status; |
95 #endif | |
96 } | |
97 | |
98 int | |
99 rpl_utime (const char *file, const struct utimbuf *times) | |
100 { | |
101 if (times) | |
102 return utime (file, times); | |
103 | |
104 return utime_null (file); | |
105 } |