]>
diplodocus.org Git - nmh/blob - sbr/m_popen.c
1 /* m_popen.c -- Interface for a popen() call that redirects the current
2 * process standard output to the popen()d process.
4 * This code is Copyright (c) 2014, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/signals.h>
13 static int m_pid
= NOTOK
; /* Process we're waiting for */
14 static int sd
= NOTOK
; /* Original standard output */
17 * Fork a process and redirect our standard output to that process
21 m_popen (char *name
, int savestdout
)
27 if (savestdout
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
28 adios ("standard output", "unable to dup()");
30 if (pipe (pd
) == NOTOK
)
31 adios ("pipe", "unable to");
33 switch (m_pid
= fork()) {
35 adios ("fork", "unable to");
38 SIGNAL (SIGINT
, SIG_DFL
);
39 SIGNAL (SIGQUIT
, SIG_DFL
);
42 if (pd
[0] != fileno (stdin
)) {
43 dup2 (pd
[0], fileno (stdin
));
46 arglist
= argsplit(name
, &file
, NULL
);
47 execvp (file
, arglist
);
48 fprintf (stderr
, "unable to exec ");
54 if (pd
[1] != fileno (stdout
)) {
55 dup2 (pd
[1], fileno (stdout
));
70 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
71 adios ("standard output", "unable to dup2()");