1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: process.c,v 2.10 2020/04/03 17:06:16 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Routines to communicate with separate process via dual pipes |
6 |
* |
7 |
* External symbols declared in rtprocess.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 (!(pd->flags & PF_RUNNING)) |
32 |
return(-1); |
33 |
if (writebuf(pd->w, sendbuf, nbs) < nbs) |
34 |
return(-1); |
35 |
return(readbuf(pd->r, recvbuf, nbr)); |
36 |
} |
37 |
|
38 |
|
39 |
|
40 |
ssize_t |
41 |
readbuf( /* read all of requested buffer */ |
42 |
int fd, |
43 |
char *bpos, |
44 |
ssize_t siz |
45 |
) |
46 |
{ |
47 |
ssize_t cc = 0, nrem = siz; |
48 |
retry: |
49 |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
50 |
bpos += cc; |
51 |
nrem -= cc; |
52 |
} |
53 |
if (cc < 0) { |
54 |
#ifndef BSD |
55 |
if (errno == EINTR) /* we were interrupted! */ |
56 |
goto retry; |
57 |
#endif |
58 |
return(cc); |
59 |
} |
60 |
return(siz-nrem); |
61 |
} |
62 |
|
63 |
|
64 |
ssize_t |
65 |
writebuf( /* write all of requested buffer */ |
66 |
int fd, |
67 |
char *bpos, |
68 |
ssize_t siz |
69 |
) |
70 |
{ |
71 |
ssize_t cc = 0, nrem = siz; |
72 |
retry: |
73 |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
74 |
bpos += cc; |
75 |
nrem -= cc; |
76 |
} |
77 |
if (cc < 0) { |
78 |
#ifndef BSD |
79 |
if (errno == EINTR) /* we were interrupted! */ |
80 |
goto retry; |
81 |
#endif |
82 |
return(cc); |
83 |
} |
84 |
return(siz-nrem); |
85 |
} |