annotate lib/fopen.c @ 17463:203c036eb0c6

bootstrap: support checksum utils without a --status option * build-aux/bootstrap: Only look for sha1sum if updating po files. Add sha1 to the list of supported checksum utils since it's now supported through adjustments below. (update_po_files): Remove the use of --status in a way that will suppress all error messages, but since this is only used to minimize updates, it shouldn't cause an issue. Exit early if there is a problem updating the po file checksums. (find_tool): Remove the check for --version support as this is optional as per commit 86186b17. Don't even check for the presence of the command as if that is needed, it's supported through configuring prerequisites in bootstrap.conf. Prompt that when a tool isn't found, one can define an environment variable to add to the hardcoded search list.
author Pádraig Brady <P@draigBrady.com>
date Thu, 08 Aug 2013 11:08:49 +0100
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Open a stream to a file.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 17185
diff changeset
2 Copyright (C) 2007-2013 Free Software Foundation, Inc.
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9301
diff changeset
4 This program is free software: you can redistribute it and/or modify
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9301
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9301
diff changeset
7 (at your option) any later version.
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9301
diff changeset
14 You should have received a copy of the GNU General Public License
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9301
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
15538
9fd857e43955 Avoid endless recursions if config.h includes some header files.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
19 /* If the user's config.h happens to include <stdio.h>, let it include only
9fd857e43955 Avoid endless recursions if config.h includes some header files.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
20 the system's <stdio.h> here, so that orig_fopen doesn't recurse to
9fd857e43955 Avoid endless recursions if config.h includes some header files.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
21 rpl_fopen. */
9fd857e43955 Avoid endless recursions if config.h includes some header files.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
22 #define __need_FILE
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <config.h>
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
10531
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
25 /* Get the original definition of fopen. It might be defined as a macro. */
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
26 #include <stdio.h>
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
27 #undef __need_FILE
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
28
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
29 static FILE *
10531
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
30 orig_fopen (const char *filename, const char *mode)
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
31 {
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
32 return fopen (filename, mode);
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
33 }
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
34
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* Specification. */
15583
716e67b8d5a9 openat: Work around compilation error with OSF/1 5.1 DTK cc.
Bruno Haible <bruno@clisp.org>
parents: 15538
diff changeset
36 /* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
716e67b8d5a9 openat: Work around compilation error with OSF/1 5.1 DTK cc.
Bruno Haible <bruno@clisp.org>
parents: 15538
diff changeset
37 this include because of the preliminary #include <stdio.h> above. */
716e67b8d5a9 openat: Work around compilation error with OSF/1 5.1 DTK cc.
Bruno Haible <bruno@clisp.org>
parents: 15538
diff changeset
38 #include "stdio.h"
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
10376
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
40 #include <errno.h>
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
41 #include <fcntl.h>
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include <string.h>
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
43 #include <unistd.h>
11031
f33575c28e6c Fix compilation error on HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 10531
diff changeset
44 #include <sys/types.h>
f33575c28e6c Fix compilation error on HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 10531
diff changeset
45 #include <sys/stat.h>
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 FILE *
9349
0c6048a8aaed Use rpl_ prefix for functions, so as to avoid endless recursions in weird cases.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
48 rpl_fopen (const char *filename, const char *mode)
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
50 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
51 if (strcmp (filename, "/dev/null") == 0)
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
52 filename = "NUL";
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
53 #endif
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
54
10376
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
55 #if FOPEN_TRAILING_SLASH_BUG
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
56 /* If the filename ends in a slash and a mode that requires write access is
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
57 specified, then fail.
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
58 Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
59 says that
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
60 "A pathname that contains at least one non-slash character and that
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
61 ends with one or more trailing slashes shall be resolved as if a
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
62 single dot character ( '.' ) were appended to the pathname."
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
63 and
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
64 "The special filename dot shall refer to the directory specified by
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
65 its predecessor."
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
66 If the named file already exists as a directory, then if a mode that
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
67 requires write access is specified, fopen() must fail because POSIX
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
68 <http://www.opengroup.org/susv3/functions/fopen.html> says that it
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
69 fails with errno = EISDIR in this case.
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
70 If the named file does not exist or does not name a directory, then
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
71 fopen() must fail since the file does not contain a '.' directory. */
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
72 {
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
73 size_t len = strlen (filename);
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
74 if (len > 0 && filename[len - 1] == '/')
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
75 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
76 int fd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
77 struct stat statbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
78 FILE *fp;
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
79
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
80 if (mode[0] == 'w' || mode[0] == 'a')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
81 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
82 errno = EISDIR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
83 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
84 }
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
85
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
86 fd = open (filename, O_RDONLY);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
87 if (fd < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
88 return NULL;
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
89
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
90 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
91 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
92 close (fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
93 errno = ENOTDIR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
94 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
95 }
10376
8532ecccedda Work around bug with trailing slash on Solaris 9 and HP-UX 11.00.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
96
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
97 fp = fdopen (fd, mode);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
98 if (fp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
99 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
100 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
101 close (fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
102 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
103 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11031
diff changeset
104 return fp;
10472
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
105 }
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
106 }
a4dc39a18d54 Ensure that a filename ending in a slash cannot be used to access a non-directory.
Bruno Haible <bruno@clisp.org>
parents: 10376
diff changeset
107 # endif
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
10531
e83a90adf243 Override fopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 10473
diff changeset
109 return orig_fopen (filename, mode);
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 }