1: #include "hd.h"
2: #include <signal.h>
3:
4: /* F_exec forks and executes a process. It then waits for the
5: forked process to finish.
6: Use f_exec like execl, except f_exec waits for termination of the
7: called program and then falls through.
8: */
9:
10: extern leave ();
11:
12: /*VARARGS1*/
13: f_exec (a, b) char *a, *b; {
14:
15: int retval;
16: int p;
17:
18: tty_push (COOKEDMODE);
19: if ((p = myfork ()) == 0) myexecv (a, &b);
20: else retval = join (p);
21: tty_pop ();
22: return retval;
23: }
24:
25: /* Exec takes parameters like a command and interfaces properly
26: to the command processor in command.c. */
27:
28: exec (argv) char **argv; {
29:
30: register p;
31:
32: if (*argv == CNULL) {
33: putmsg ("Execute command must have parameter");
34: return NOREPLOT;
35: }
36: tty_push (COOKEDMODE);
37:
38: if ((p = myfork ()) == 0) myexecv (*argv, argv);
39: else join (p);
40:
41: getrtn ();
42: tty_pop ();
43: return REPLOT;
44: }
45: /* Mysystem is similar to system, except the bugs are fixed. In
46: addition, the tty is set to cooked mode and the command is printed.
47: */
48: mysystem (name) char *name; {
49:
50: int pipefile[2];
51: # define pipein pipefile [0]
52: # define pipeout pipefile [1]
53:
54: int p; FILE *stream;
55:
56: tty_push (COOKEDMODE); pipe (pipefile);
57:
58: if ((p = myfork()) == 0) {
59: close (infile); dup (pipein);
60: myexecl (envshell, "+", 0);
61: }
62: else {
63: stream = fdopen (pipeout, "w");
64: close (pipein);
65: printf ("%s\n", name);
66: fprintf (stream, "%s\n", name);
67: fclose (stream);
68: join (p);
69: }
70: tty_pop ();
71: }
72:
73: /* p_exec is just like f_exec except output is paged */
74:
75: /*VARARGS1*/
76: p_exec (a, b) char *a, *b; {
77:
78: int pipefile [2];
79:
80: int p;
81: FILE *stream;
82:
83: pipe (pipefile);
84: if ((p = myfork ()) == 0) {
85: close (outfile); dup (pipeout);
86: close (pipein); close (pipeout);
87: myexecv (a, &b);
88: }
89: else {
90: stream = fdopen (pipein, "r");
91: close (pipeout);
92: page (stream);
93: fclose (stream);
94: join (p);
95: }
96: }
97:
98: /* Special interfaces to exec */
99: /* Myexecl and myexecv close files numbered > 3 and
100: print their arguments. */
101:
102: /*VARARGS1*/
103: myexecl (a, b) char *a, *b; {
104: myexecv (a, &b);
105: }
106:
107: myexecv (a, b) char *a, **b; {
108:
109: register char **sp; register i;
110:
111: if (**b != '+') {
112: for (sp = b; *sp; sp++) printf ("%s ", *sp);
113: printf ("\r\n");
114: }
115: for (i = 3; i <= _NFILE; i++) close (i);
116: execv (a, b);
117: myperror (a); getrtn (); exit (1);
118: }
119:
120: /* Myfork acts like fork but never fails */
121: #define MAXTRIES 10 /* Max tries of a fork */
122: myfork () {
123: int p; /* process number */
124: int tries; /* number of tries */
125:
126: for (tries = 1; tries <= MAXTRIES; tries++) {
127: p = fork ();
128: if (p > 0) signal (SIGINT, SIG_IGN);
129: if (p == 0) signal (SIGINT, SIG_DFL);
130: if (p != -1) return p;
131: myperror ("Cannot fork");
132: sleep (tries);
133: clearmsg (0);
134: }
135: putmsg ("Fatal error -- cannot fork\n");
136: leave ();
137: return -1;
138: }
139:
140: /* Join is the compliment of fork */
141:
142: join (p) int p; {
143:
144: int status [2]; int w;
145:
146: do {
147: w = wait (status);
148: } while (p != -1 && w != p);
149:
150: signal (SIGINT, leave);
151:
152: return (status [0]);
153: }
Defined functions
exec
defined in line
28; used 6 times
join
defined in line
142; used 7 times
Defined macros