]> diplodocus.org Git - nmh/blob - sbr/showfile.c
703132bdc5fb2fb5db8db9434779dca8df1fa579
[nmh] / sbr / showfile.c
1 /* showfile.c -- invoke the `lproc' command
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include "error.h"
10
11
12 int
13 showfile (char **arg, char *file)
14 {
15 pid_t pid;
16 int vecp;
17 char **vec, *program;
18 int retval = 1;
19
20 context_save(); /* save the context file */
21 fflush(stdout);
22
23 /*
24 * If you have your lproc listed as "mhl",
25 * then really invoked the mhlproc instead
26 * (which is usually mhl anyway).
27 */
28 if (!strcmp (r1bindex (lproc, '/'), "mhl"))
29 lproc = mhlproc;
30
31 switch (pid = fork()) {
32 case -1:
33 /* fork error */
34 advise ("fork", "unable to");
35 break;
36
37 case 0:
38 /* child */
39 vec = argsplit(lproc, &program, &vecp);
40 bool isdraft = true;
41 if (arg) {
42 while (*arg) {
43 if (**arg != '-')
44 isdraft = false;
45 vec[vecp++] = *arg++;
46 }
47 }
48 if (isdraft) {
49 if (!strcmp (vec[0], "show"))
50 vec[vecp++] = "-file";
51 vec[vecp++] = file;
52 }
53 vec[vecp] = NULL;
54
55 execvp (program, vec);
56 fprintf (stderr, "unable to exec ");
57 perror (lproc);
58 _exit(1);
59
60 default:
61 /* parent */
62 retval = pidwait (pid, -1) & 0377 ? 1 : 0;
63 }
64
65 return retval;
66 }