annotate lib/popen.c @ 16511:2074f2bf7216

math code: Add comments. * lib/acosl.c: Add comment about related glibc source files. * lib/asinl.c: Likewise. * lib/atanl.c: Likewise. * lib/expl.c: Likewise. * lib/logl.c: Likewise. * lib/sincosl.c: Likewise. * lib/sinl.c: Likewise. * lib/tanl.c: Likewise. * lib/trigl.c: Likewise. * lib/cosl.c: Likewise. Fix comments.
author Bruno Haible <bruno@clisp.org>
date Wed, 29 Feb 2012 12:30:07 +0100
parents a712776b11ce
children e542fd46ad6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Open a stream to a sub-process.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15642
diff changeset
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 /* Specification. */
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22 #include <stdio.h>
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23
16339
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
24 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
25 /* Native Windows API. */
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
26
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
27 # include <string.h>
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
28
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
29 FILE *
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
30 popen (const char *filename, const char *mode)
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
31 {
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
32 /* Use binary mode by default. */
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
33 if (strcmp (mode, "r") == 0)
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
34 mode = "rb";
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
35 else if (strcmp (mode, "w") == 0)
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
36 mode = "wb";
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
37
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
38 return _popen (filename, mode);
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
39 }
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
40
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
41 #else
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42
15642
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43 # include <errno.h>
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
44 # include <fcntl.h>
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 # include <stdlib.h>
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
46 # include <unistd.h>
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
47
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
48 # undef popen
11857
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
49
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 FILE *
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
51 rpl_popen (const char *filename, const char *mode)
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 {
16339
993344da76d0 popen: Make more robust on Windows.
Bruno Haible <bruno@clisp.org>
parents: 16242
diff changeset
53 /* All other platforms have popen and fcntl.
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 The bug of the child clobbering its own file descriptors if stdin
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 or stdout was closed in the parent can be worked around by
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 opening those two fds as close-on-exec to begin with. */
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57 /* Cygwin 1.5.x also has a bug where the popen fd is improperly
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 marked close-on-exec, and if the application undoes this, then
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 the fd leaks into subsequent popen calls. We could work around
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 this by maintaining a list of all fd's opened by popen, and
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 temporarily marking them cloexec around the real popen call, but
16358
a712776b11ce maint: spelling fixes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16339
diff changeset
62 we would also have to override pclose, and the bookkeeping seems
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 extreme given that cygwin 1.7 no longer has the bug. */
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 FILE *result;
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 int cloexec0 = fcntl (STDIN_FILENO, F_GETFD);
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 int cloexec1 = fcntl (STDOUT_FILENO, F_GETFD);
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67 int saved_errno;
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68
11857
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
69 /* If either stdin or stdout was closed (that is, fcntl failed),
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
70 then we open a dummy close-on-exec fd to occupy that slot. That
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
71 way, popen's internal use of pipe() will not contain either fd 0
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
72 or 1, overcoming the fact that the child process blindly calls
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
73 close() on the parent's end of the pipe without first checking
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
74 whether it is clobbering the fd just placed there via dup2(); the
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
75 exec will get rid of the dummy fd's in the child. Fortunately,
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
76 closed stderr in the parent does not cause problems in the
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
77 child. */
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 if (cloexec0 < 0)
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 {
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 if (open ("/dev/null", O_RDONLY) != STDIN_FILENO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
81 || fcntl (STDIN_FILENO, F_SETFD,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
82 fcntl (STDIN_FILENO, F_GETFD) | FD_CLOEXEC) == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
83 abort ();
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 }
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 if (cloexec1 < 0)
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
86 {
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
87 if (open ("/dev/null", O_RDONLY) != STDOUT_FILENO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
88 || fcntl (STDOUT_FILENO, F_SETFD,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
89 fcntl (STDOUT_FILENO, F_GETFD) | FD_CLOEXEC) == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11857
diff changeset
90 abort ();
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
91 }
11857
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
92 result = popen (filename, mode);
f17a5ed578bd popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents: 11848
diff changeset
93 /* Now, close any dummy fd's created in the parent. */
11848
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
94 saved_errno = errno;
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
95 if (cloexec0 < 0)
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
96 close (STDIN_FILENO);
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
97 if (cloexec1 < 0)
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
98 close (STDOUT_FILENO);
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
99 errno = saved_errno;
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
100 return result;
9e3299dad578 popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff changeset
101 }
15642
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
102
5f0f5820c414 popen: Support for MSVC.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
103 #endif