Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/utime.c @ 9268:a553c18c572d
Rename sys_time_.h to sys_time.in.h.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 02 Oct 2007 00:37:40 +0200 |
parents | cb2590895141 |
children | bbbbbf4cd1c5 |
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 |
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, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
1442 | 17 |
18 /* derived from a function in touch.c */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
20 #include <config.h> |
1442 | 21 #undef utime |
22 | |
23 #include <sys/types.h> | |
24 | |
25 #ifdef HAVE_UTIME_H | |
26 # include <utime.h> | |
27 #endif | |
28 | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
29 #if !HAVE_UTIMES_NULL |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
30 # include <sys/stat.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
31 # include <fcntl.h> |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
32 #endif |
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
33 |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
34 #include <unistd.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
35 #include <errno.h> |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
36 |
3406 | 37 #include "full-write.h" |
1442 | 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 | |
7863 | 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 | |
1442 | 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; | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
68 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
|
69 int saved_errno = 0; |
1442 | 70 |
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
|
71 fd = open (file, O_RDWR); |
1442 | 72 if (fd < 0 |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
73 || fstat (fd, &st) < 0 |
4088
815386455680
Update for changed signature of safe_read.
Bruno Haible <bruno@clisp.org>
parents:
3407
diff
changeset
|
74 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR |
1442 | 75 || lseek (fd, (off_t) 0, SEEK_SET) < 0 |
3406 | 76 || 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
|
77 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination |
1442 | 78 of patches, but that system doesn't use this code: it has utimes. |
79 || fsync (fd) < 0 | |
80 */ | |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
81 || (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
|
82 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
83 saved_errno = errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
84 status = -1; |
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 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
87 if (0 <= fd) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
88 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
89 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
|
90 status = -1; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
91 |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
92 /* 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
|
93 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
|
94 if (saved_errno) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
95 errno = saved_errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
96 } |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
97 |
1442 | 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 } |