Mercurial > hg > octave-kai > gnulib-hg
annotate lib/utime.c @ 11583:dc80f2cc1327
Second attempt to work around an AIX 5.3, 6.1 compiler bug with include_next.
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Thu, 21 May 2009 16:48:12 +0200 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
1 /* Copyright (C) 1998, 2001, 2002, 2003, 2004, 2006 Free Software |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
2 Foundation, Inc. |
1442 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify it |
1442 | 5 under the terms of the GNU General Public License as published by the |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
6 Free Software Foundation; either version 3 of the License, or any |
1442 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
1442 | 16 |
17 /* derived from a function in touch.c */ | |
18 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
19 #include <config.h> |
1442 | 20 #undef utime |
21 | |
22 #include <sys/types.h> | |
23 | |
24 #ifdef HAVE_UTIME_H | |
25 # include <utime.h> | |
26 #endif | |
27 | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
28 #if !HAVE_UTIMES_NULL |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
29 # include <sys/stat.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
30 # include <fcntl.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
31 #endif |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
32 |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
33 #include <unistd.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
34 #include <errno.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
35 |
3406 | 36 #include "full-write.h" |
1442 | 37 #include "safe-read.h" |
38 | |
39 /* Some systems (even some that do have <utime.h>) don't declare this | |
40 structure anywhere. */ | |
41 #ifndef HAVE_STRUCT_UTIMBUF | |
42 struct utimbuf | |
43 { | |
44 long actime; | |
45 long modtime; | |
46 }; | |
47 #endif | |
48 | |
7863 | 49 /* The results of open() in this file are not used with fchdir, |
50 therefore save some unnecessary work in fchdir.c. */ | |
51 #undef open | |
52 #undef close | |
53 | |
1442 | 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; | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
67 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
|
68 int saved_errno = 0; |
1442 | 69 |
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
|
70 fd = open (file, O_RDWR); |
1442 | 71 if (fd < 0 |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
72 || fstat (fd, &st) < 0 |
4088
815386455680
Update for changed signature of safe_read.
Bruno Haible <bruno@clisp.org>
parents:
3407
diff
changeset
|
73 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR |
1442 | 74 || lseek (fd, (off_t) 0, SEEK_SET) < 0 |
3406 | 75 || 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
|
76 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination |
1442 | 77 of patches, but that system doesn't use this code: it has utimes. |
78 || fsync (fd) < 0 | |
79 */ | |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
80 || (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
|
81 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
82 saved_errno = errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
83 status = -1; |
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 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
86 if (0 <= fd) |
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 (close (fd) < 0) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
89 status = -1; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
90 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
91 /* 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
|
92 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
|
93 if (saved_errno) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
94 errno = saved_errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
95 } |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
96 |
1442 | 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 } |