Mercurial > hg > octave-shane > gnulib-hg
annotate lib/fopen-safer.c @ 17632:86af85d364e1 default tip
unistd: port readlink to Mac OS X 10.3.9
* lib/unistd.in.h (_GL_INCLUDING_UNISTD_H): New macro, to work
around self-include problem in Mac OS X 10.3.9 when combined with
readlink module. Problem reported by Klaus Zietler in
<http://bugs.gnu.org/16825>.
author | Paul Eggert <eggert@penguin.cs.ucla.edu> |
---|---|
date | Tue, 25 Feb 2014 11:16:27 -0800 |
parents | 344018b6e5d7 |
children |
rev | line source |
---|---|
3196 | 1 /* Invoke fopen, but avoid some glitches. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 |
17587 | 3 Copyright (C) 2001, 2004-2006, 2009-2014 Free Software Foundation, Inc. |
3196 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
3196 | 6 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:
7302
diff
changeset
|
7 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:
7302
diff
changeset
|
8 (at your option) any later version. |
3196 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
3196 | 17 |
18 /* Written by Paul Eggert. */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
20 #include <config.h> |
3196 | 21 |
6150
882f5823cabb
Merge minor changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
5848
diff
changeset
|
22 #include "stdio-safer.h" |
882f5823cabb
Merge minor changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
5848
diff
changeset
|
23 |
882f5823cabb
Merge minor changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
5848
diff
changeset
|
24 #include <errno.h> |
882f5823cabb
Merge minor changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
5848
diff
changeset
|
25 #include <unistd.h> |
882f5823cabb
Merge minor changes from coreutils.
Jim Meyering <jim@meyering.net>
parents:
5848
diff
changeset
|
26 #include "unistd-safer.h" |
3196 | 27 |
28 /* Like fopen, but do not return stdin, stdout, or stderr. */ | |
29 | |
30 FILE * | |
31 fopen_safer (char const *file, char const *mode) | |
32 { | |
33 FILE *fp = fopen (file, mode); | |
34 | |
35 if (fp) | |
36 { | |
37 int fd = fileno (fp); | |
38 | |
39 if (0 <= fd && fd <= STDERR_FILENO) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
40 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
41 int f = dup_safer (fd); |
3196 | 42 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
43 if (f < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
44 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
45 int e = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
46 fclose (fp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
47 errno = e; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
48 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
49 } |
3196 | 50 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
51 if (fclose (fp) != 0 |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
52 || ! (fp = fdopen (f, mode))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
53 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
54 int e = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
55 close (f); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
56 errno = e; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
57 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
58 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11049
diff
changeset
|
59 } |
3196 | 60 } |
61 | |
62 return fp; | |
63 } |