1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: process.c,v 2.7 2003/06/26 00:58:09 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Routines to communicate with separate process via dual pipes |
6 |
* |
7 |
* External symbols declared in standard.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
#include "rtprocess.h" |
13 |
|
14 |
/* |
15 |
|
16 |
The functions open_process() and close_process() exist in |
17 |
(currently) two versions, which are found in the files: |
18 |
|
19 |
win_process.c |
20 |
unix_process.c |
21 |
|
22 |
*/ |
23 |
|
24 |
int |
25 |
process( /* process data through pd */ |
26 |
SUBPROC *pd, |
27 |
char *recvbuf, char *sendbuf, |
28 |
int nbr, int nbs |
29 |
) |
30 |
{ |
31 |
if (nbs > PIPE_BUF) |
32 |
return(-1); |
33 |
if (!pd->running) |
34 |
return(-1); |
35 |
if (writebuf(pd->w, sendbuf, nbs) < nbs) |
36 |
return(-1); |
37 |
return(readbuf(pd->r, recvbuf, nbr)); |
38 |
} |
39 |
|
40 |
|
41 |
|
42 |
int |
43 |
readbuf( /* read all of requested buffer */ |
44 |
int fd, |
45 |
char *bpos, |
46 |
int siz |
47 |
) |
48 |
{ |
49 |
register int cc = 0, nrem = siz; |
50 |
retry: |
51 |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
52 |
bpos += cc; |
53 |
nrem -= cc; |
54 |
} |
55 |
if (cc < 0) { |
56 |
#ifndef BSD |
57 |
if (errno == EINTR) /* we were interrupted! */ |
58 |
goto retry; |
59 |
#endif |
60 |
return(cc); |
61 |
} |
62 |
return(siz-nrem); |
63 |
} |
64 |
|
65 |
|
66 |
int |
67 |
writebuf( /* write all of requested buffer */ |
68 |
int fd, |
69 |
char *bpos, |
70 |
int siz |
71 |
) |
72 |
{ |
73 |
register int cc = 0, nrem = siz; |
74 |
retry: |
75 |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
76 |
bpos += cc; |
77 |
nrem -= cc; |
78 |
} |
79 |
if (cc < 0) { |
80 |
#ifndef BSD |
81 |
if (errno == EINTR) /* we were interrupted! */ |
82 |
goto retry; |
83 |
#endif |
84 |
return(cc); |
85 |
} |
86 |
return(siz-nrem); |
87 |
} |