annotate lib/fopen.c @ 17185:dd46d4e6beea

dup, execute, fatal-signal, etc.: no 'static inline' * lib/dup.c (dup_nothrow): * lib/execute.c (nonintr_close, nonintr_open): * lib/fatal-signal.c (uninstall_handlers, install_handlers): * lib/fopen.c (orig_fopen): * lib/freadseek.c (freadptrinc): * lib/freopen.c (orig_freopen): * lib/fstat.c (orig_fstat, fstat_nothrow): * lib/get-rusage-as.c (get_rusage_as_via_setrlimit) (get_rusage_as_via_iterator): * lib/get-rusage-data.c (get_rusage_data_via_setrlimit): * lib/getdtablesize.c (_setmaxstdio_nothrow): * lib/isatty.c (_isatty_nothrow): * lib/open.c (orig_open): * lib/read.c (read_nothrow): * lib/sigprocmask.c (signal_nothrow): * lib/spawn-pipe.c (nonintr_close, nonintr_open): * lib/vasnprintf.c (MAX_ROOM_NEEDED): * lib/wait-process.c (unregister_slave_subprocess): * lib/write.c (write_nothrow): Now static, not static inline. * lib/spawn-pipe.c (nonintr_open): Define only if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__. * m4/dup.m4 (gl_PREREQ_DUP): * m4/execute.m4 (gl_EXECUTE): * m4/fatal-signal.m4 (gl_FATAL_SIGNAL): * m4/fopen.m4 (gl_PREREQ_FOPEN): * m4/freadseek.m4 (gl_FUNC_FREADSEEK): * m4/freopen.m4 (gl_PREREQ_FREOPEN): * m4/fstat.m4 (gl_PREREQ_FSTAT): * m4/getdtablesize.m4 (gl_PREREQ_GETDTABLESIZE): * m4/isatty.m4 (gl_PREREQ_ISATTY): * m4/open.m4 (gl_PREREQ_OPEN): * m4/read.m4 (gl_PREREQ_READ): * m4/signalblocking.m4 (gl_PREREQ_SIGPROCMASK): * m4/spawn-pipe.m4 (gl_SPAWN_PIPE): * m4/vasnprintf.m4 (gl_PREREQ_VASNPRINTF): * m4/wait-process.m4 (gl_WAIT_PROCESS): * m4/write.m4 (gl_PREREQ_WRITE): * modules/get-rusage-as, modules/get-rusage-data (configure.ac): Do not require AC_C_INLINE.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 20 Nov 2012 22:25:09 -0800
parents 8250f2777afc
children e542fd46ad6f
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.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15583
diff changeset
2 Copyright (C) 2007-2012 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 }