2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins.
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
12 #include "mutt_signal.h"
15 /* Invokes a commmand on a pipe and optionally connects its stdin and stdout
16 * to the specified handles.
19 mutt_create_filter_fd (const char *cmd, FILE ** in, FILE ** out, FILE ** err,
20 int fdin, int fdout, int fderr)
22 int pin[2], pout[2], perr[2], thepid;
32 if (pipe (pout) == -1) {
43 if (pipe (perr) == -1) {
56 mutt_block_signals_system ();
58 if ((thepid = fork ()) == 0) {
59 mutt_unblock_signals_system (0);
66 else if (fdin != -1) {
76 else if (fdout != -1) {
86 else if (fderr != -1) {
91 execl ("/bin/sh", "sh", "-c", cmd, NULL);
94 else if (thepid == -1) {
95 mutt_unblock_signals_system (1);
117 *out = fdopen (pout[0], "r");
122 *in = fdopen (pin[1], "w");
127 *err = fdopen (perr[0], "r");
133 pid_t mutt_create_filter (const char *s, FILE ** in, FILE ** out, FILE ** err)
135 return (mutt_create_filter_fd (s, in, out, err, -1, -1, -1));
138 int mutt_wait_filter (pid_t pid)
142 waitpid (pid, &rc, 0);
143 mutt_unblock_signals_system (1);
144 rc = WIFEXITED (rc) ? WEXITSTATUS (rc) : -1;
149 /* This function allows the user to specify a command to read stdout from in
150 place of a normal file. If the last character in the string is a pipe (|),
151 then we assume it is a commmand to run instead of a normal file. */
152 FILE *mutt_open_read (const char *path, pid_t * thepid)
154 int len = m_strlen(path);
157 if (path[len - 1] == '|') {
158 char *s = m_strdup(path);
160 /* read from a pipe */
164 *thepid = mutt_create_filter (s, NULL, &f, NULL);
167 f = fopen (path, "r");