annotate lib/freopen.c @ 17907:0a1c2535cad9

euidaccess: Fix Android build * modules/euidaccess (Depends-on): Add fcntl-h to ensure that AT_EACCESS gets declared.
author Kevin Cernekee <cernekee@google.com>
date Wed, 11 Feb 2015 15:22:54 -0800
parents ab58d4870664
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.
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17826
diff changeset
2 Copyright (C) 2007-2015 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_freopen 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_freopen. */
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
10532
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
25 /* Get the original definition of freopen. It might be defined as a macro. */
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
26 #include <stdio.h>
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
27 #undef __need_FILE
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
28
17826
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
29 #include <errno.h>
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
30
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
31 static FILE *
10532
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
32 orig_freopen (const char *filename, const char *mode, FILE *stream)
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
33 {
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
34 return freopen (filename, mode, stream);
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
35 }
81789aaf2d0b Override freopen more carefully.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
36
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 /* Specification. */
15583
716e67b8d5a9 openat: Work around compilation error with OSF/1 5.1 DTK cc.
Bruno Haible <bruno@clisp.org>
parents: 15540
diff changeset
38 /* 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: 15540
diff changeset
39 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: 15540
diff changeset
40 #include "stdio.h"
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include <string.h>
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 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
45 rpl_freopen (const char *filename, const char *mode, FILE *stream)
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 {
17826
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
47 FILE *result;
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
48
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
15540
eff2fc71e9c1 freopen: Don't crash if the filename argument is NULL.
Claudio Bley <claudio.bley@gmail.com>
parents: 15538
diff changeset
50 if (filename != NULL && strcmp (filename, "/dev/null") == 0)
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 filename = "NUL";
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 #endif
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
17826
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
54 /* Clear errno to check the success of freopen() with it */
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
55 errno = 0;
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
56
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
57 result = orig_freopen (filename, mode, stream);
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
58
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
59 #ifdef __KLIBC__
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
60 /* On OS/2 kLIBC, freopen() returns NULL even if it is successful
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
61 if filename is NULL. */
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
62 if (!filename && !result && !errno)
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
63 result = stream;
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
64 #endif
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
65
68470530f583 freopen: workaround freopen() on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 17587
diff changeset
66 return result;
9301
98e1721b4e40 New modules 'fopen' and 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 }