Mercurial > hg > octave-kai > gnulib-hg
annotate lib/utime.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 |
parents | a48fb0e98c8c |
children | cb2590895141 |
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 | |
50 /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not | |
51 interpret it to set the access and modification times of FILE to | |
52 the current time. Return 0 if successful, -1 if not. */ | |
53 | |
54 static int | |
55 utime_null (const char *file) | |
56 { | |
57 #if HAVE_UTIMES_NULL | |
58 return utimes (file, 0); | |
59 #else | |
60 int fd; | |
61 char c; | |
62 int status = 0; | |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
63 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
|
64 int saved_errno = 0; |
1442 | 65 |
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
|
66 fd = open (file, O_RDWR); |
1442 | 67 if (fd < 0 |
4629
9583f9cee522
[!HAVE_UTIMES_NULL]: Include <sys/stat.h>, <fcntl.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4474
diff
changeset
|
68 || fstat (fd, &st) < 0 |
4088
815386455680
Update for changed signature of safe_read.
Bruno Haible <bruno@clisp.org>
parents:
3407
diff
changeset
|
69 || safe_read (fd, &c, sizeof c) == SAFE_READ_ERROR |
1442 | 70 || lseek (fd, (off_t) 0, SEEK_SET) < 0 |
3406 | 71 || 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
|
72 /* Maybe do this -- it's necessary on SunOS 4.1.3 with some combination |
1442 | 73 of patches, but that system doesn't use this code: it has utimes. |
74 || fsync (fd) < 0 | |
75 */ | |
4873
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
76 || (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
|
77 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
78 saved_errno = errno; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
79 status = -1; |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
80 } |
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 if (0 <= fd) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
83 { |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
84 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
|
85 status = -1; |
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 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
|
88 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
|
89 if (saved_errno) |
8b1923c943a6
On systems without utime and without a utimes function capable of
Jim Meyering <jim@meyering.net>
parents:
4629
diff
changeset
|
90 errno = saved_errno; |
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 |
1442 | 93 return status; |
94 #endif | |
95 } | |
96 | |
97 int | |
98 rpl_utime (const char *file, const struct utimbuf *times) | |
99 { | |
100 if (times) | |
101 return utime (file, times); | |
102 | |
103 return utime_null (file); | |
104 } |