1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines to communicate with separate process via dual pipes |
9 |
*/ |
10 |
|
11 |
/* find pipe buffer limit */ |
12 |
#include <sys/param.h> |
13 |
|
14 |
#ifndef PIPE_BUF |
15 |
#ifdef PIPSIZ |
16 |
#define PIPE_BUF PIPSIZ |
17 |
#else |
18 |
#ifdef PIPE_MAX |
19 |
#define PIPE_BUF PIPE_MAX |
20 |
#else |
21 |
#define PIPE_BUF 512 /* hyperconservative */ |
22 |
#endif |
23 |
#endif |
24 |
#endif |
25 |
|
26 |
#include "vfork.h" |
27 |
|
28 |
|
29 |
int |
30 |
open_process(pd, av) /* open communication to separate process */ |
31 |
int pd[3]; |
32 |
char *av[]; |
33 |
{ |
34 |
extern char *getpath(), *getenv(); |
35 |
char *compath; |
36 |
int p0[2], p1[2]; |
37 |
/* find executable */ |
38 |
compath = getpath(av[0], getenv("PATH"), 1); |
39 |
if (compath == 0) |
40 |
return(0); |
41 |
if (pipe(p0) < 0 || pipe(p1) < 0) |
42 |
return(-1); |
43 |
if ((pd[2] = vfork()) == 0) { /* if child */ |
44 |
close(p0[1]); |
45 |
close(p1[0]); |
46 |
if (p0[0] != 0) { /* connect p0 to stdin */ |
47 |
dup2(p0[0], 0); |
48 |
close(p0[0]); |
49 |
} |
50 |
if (p1[1] != 1) { /* connect p1 to stdout */ |
51 |
dup2(p1[1], 1); |
52 |
close(p1[1]); |
53 |
} |
54 |
execv(compath, av); /* exec command */ |
55 |
perror(compath); |
56 |
_exit(127); |
57 |
} |
58 |
if (pd[2] == -1) |
59 |
return(-1); |
60 |
close(p0[0]); |
61 |
close(p1[1]); |
62 |
pd[0] = p1[0]; |
63 |
pd[1] = p0[1]; |
64 |
return(PIPE_BUF); |
65 |
} |
66 |
|
67 |
|
68 |
int |
69 |
process(pd, recvbuf, sendbuf, nbr, nbs) /* process data through pd */ |
70 |
int pd[3]; |
71 |
char *recvbuf, *sendbuf; |
72 |
int nbr, nbs; |
73 |
{ |
74 |
if (nbs > PIPE_BUF) |
75 |
return(-1); |
76 |
if (writebuf(pd[1], sendbuf, nbs) < nbs) |
77 |
return(-1); |
78 |
return(readbuf(pd[0], recvbuf, nbr)); |
79 |
} |
80 |
|
81 |
|
82 |
int |
83 |
close_process(pd) /* close pipes and wait for process */ |
84 |
int pd[3]; |
85 |
{ |
86 |
int pid, status; |
87 |
|
88 |
close(pd[1]); |
89 |
close(pd[0]); |
90 |
while ((pid = wait(&status)) != -1) |
91 |
if (pid == pd[2]) |
92 |
return(status>>8 & 0xff); |
93 |
return(-1); /* ? unknown status */ |
94 |
} |
95 |
|
96 |
|
97 |
int |
98 |
readbuf(fd, bpos, siz) /* read all of requested buffer */ |
99 |
int fd; |
100 |
char *bpos; |
101 |
int siz; |
102 |
{ |
103 |
register int cc = 0, nrem = siz; |
104 |
|
105 |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
106 |
bpos += cc; |
107 |
nrem -= cc; |
108 |
} |
109 |
if (cc < 0) |
110 |
return(cc); |
111 |
return(siz-nrem); |
112 |
} |
113 |
|
114 |
|
115 |
int |
116 |
writebuf(fd, bpos, siz) /* write all of requested buffer */ |
117 |
int fd; |
118 |
char *bpos; |
119 |
int siz; |
120 |
{ |
121 |
register int cc = 0, nrem = siz; |
122 |
|
123 |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
124 |
bpos += cc; |
125 |
nrem -= cc; |
126 |
} |
127 |
if (cc < 0) |
128 |
return(cc); |
129 |
return(siz-nrem); |
130 |
} |