Mercurial > hg > octave-kai > gnulib-hg
annotate lib/tmpfile-safer.c @ 11049:8398d9f607b4
unistd: guarantee STDIN_FILENO here, for OS/2 EMX
* lib/unistd.in.h (STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO):
Guarantee a definition.
* doc/posix-headers/unistd.texi (unistd.h): Document the bug.
* modules/unistd-safer (Depends-on): Add dependency on unistd.
* lib/c-stack.c (STDERR_FILENO): Rely on <unistd.h>.
* lib/dup-safer.c (STDERR_FILENO): Likewise.
* lib/execute.c (STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO):
Likewise.
* lib/fd-safer.c (STDIN_FILENO, STDERR_FILENO): Likewise.
* lib/fopen-safer.c (STDERR_FILENO): Likewise.
* lib/pipe.c (STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO):
Likewise.
* lib/tmpfile-safer.c (STDERR_FILENO): Likewise.
* tests/test-posix_spawn1.c (STDIN_FILENO, STDOUT_FILENO)
(STDERR_FILENO): Likewise.
* tests/test-posix_spawn2.c (STDIN_FILENO, STDOUT_FILENO)
(STDERR_FILENO): Likewise.
* tests/test-posix_spawn3.c (STDIN_FILENO, STDOUT_FILENO)
(STDERR_FILENO): Likewise.
Reported by Elbert Pol.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Mon, 19 Jan 2009 09:27:47 -0700 (2009-01-19) |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
7036 | 1 /* Invoke tmpfile, but avoid some glitches. |
11049
8398d9f607b4
unistd: guarantee STDIN_FILENO here, for OS/2 EMX
Eric Blake <ebb9@byu.net>
parents:
9309
diff
changeset
|
2 Copyright (C) 2006, 2009 Free Software Foundation, Inc. |
7036 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
7036 | 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:
7302
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:
7302
diff
changeset
|
7 (at your option) any later version. |
7036 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
7036 | 16 |
17 /* Written by Eric Blake, based on ideas from Paul Eggert. */ | |
18 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7036
diff
changeset
|
19 #include <config.h> |
7036 | 20 |
21 #include "stdio-safer.h" | |
22 | |
23 #include <errno.h> | |
24 #include <unistd.h> | |
25 #include "unistd-safer.h" | |
26 | |
27 #include "binary-io.h" | |
28 | |
29 /* Like tmpfile, but do not return stdin, stdout, or stderr. | |
30 | |
31 Remember that tmpfile can leave files behind if your program calls _exit, | |
32 so this function should not be mixed with the close_stdout module. */ | |
33 | |
34 FILE * | |
35 tmpfile_safer (void) | |
36 { | |
37 FILE *fp = tmpfile (); | |
38 | |
39 if (fp) | |
40 { | |
41 int fd = fileno (fp); | |
42 | |
43 if (0 <= fd && fd <= STDERR_FILENO) | |
44 { | |
45 int f = dup_safer (fd); | |
46 | |
47 if (f < 0) | |
48 { | |
49 int e = errno; | |
50 fclose (fp); | |
51 errno = e; | |
52 return NULL; | |
53 } | |
54 | |
55 /* Keep the temporary file in binary mode, on platforms | |
56 where that matters. */ | |
57 if (fclose (fp) != 0 | |
58 || ! (fp = fdopen (f, O_BINARY ? "wb+" : "w+"))) | |
59 { | |
60 int e = errno; | |
61 close (f); | |
62 errno = e; | |
63 return NULL; | |
64 } | |
65 } | |
66 } | |
67 | |
68 return fp; | |
69 } |