]>
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>
12 static int m_pid
= NOTOK
; /* Process we're waiting for */
13 static int sd
= NOTOK
; /* Original standard output */
16 * Fork a process and redirect our standard output to that process
20 m_popen (char *name
, int savestdout
)
26 if (savestdout
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
27 adios ("standard output", "unable to dup()");
29 if (pipe (pd
) == NOTOK
)
30 adios ("pipe", "unable to");
32 switch (m_pid
= fork()) {
34 adios ("fork", "unable to");
37 SIGNAL (SIGINT
, SIG_DFL
);
38 SIGNAL (SIGQUIT
, SIG_DFL
);
41 if (pd
[0] != fileno (stdin
)) {
42 dup2 (pd
[0], fileno (stdin
));
45 arglist
= argsplit(name
, &file
, NULL
);
46 execvp (file
, arglist
);
47 fprintf (stderr
, "unable to exec ");
53 if (pd
[1] != fileno (stdout
)) {
54 dup2 (pd
[1], fileno (stdout
));
69 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
70 adios ("standard output", "unable to dup2()");