]>
diplodocus.org Git - nmh/blob - sbr/m_popen.c
3 * m_popen.c -- Interface for a popen() call that redirects the current
4 * process standard output to the popen()d process.
6 * This code is Copyright (c) 2014, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
12 #include <h/signals.h>
14 static int m_pid
= NOTOK
; /* Process we're waiting for */
15 static int sd
= NOTOK
; /* Original standard output */
18 * Fork a process and redirect our standard output to that process
22 m_popen (char *name
, int savestdout
)
28 if (savestdout
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
29 adios ("standard output", "unable to dup()");
31 if (pipe (pd
) == NOTOK
)
32 adios ("pipe", "unable to");
34 switch (m_pid
= fork()) {
36 adios ("fork", "unable to");
39 SIGNAL (SIGINT
, SIG_DFL
);
40 SIGNAL (SIGQUIT
, SIG_DFL
);
43 if (pd
[0] != fileno (stdin
)) {
44 dup2 (pd
[0], fileno (stdin
));
47 arglist
= argsplit(name
, &file
, NULL
);
48 execvp (file
, arglist
);
49 fprintf (stderr
, "unable to exec ");
55 if (pd
[1] != fileno (stdout
)) {
56 dup2 (pd
[1], fileno (stdout
));
71 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
72 adios ("standard output", "unable to dup2()");