| 1 | 
#ifndef lint | 
| 2 | 
static const char       RCSid[] = "$Id: process.c,v 2.12 2024/06/03 18:55:51 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 | 
        void    *recvbuf, void *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 | 
        void    *buf, | 
| 44 | 
        ssize_t siz | 
| 45 | 
) | 
| 46 | 
{ | 
| 47 | 
        char    *bpos = (char *)buf; | 
| 48 | 
        ssize_t cc = 0, nrem = siz; | 
| 49 | 
retry: | 
| 50 | 
        while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { | 
| 51 | 
                bpos += cc; | 
| 52 | 
                nrem -= cc; | 
| 53 | 
        } | 
| 54 | 
        if (cc < 0) { | 
| 55 | 
#ifndef BSD | 
| 56 | 
                if (errno == EINTR)     /* we were interrupted! */ | 
| 57 | 
                        goto retry; | 
| 58 | 
#endif | 
| 59 | 
                return(cc); | 
| 60 | 
        } | 
| 61 | 
        return(siz-nrem); | 
| 62 | 
} | 
| 63 | 
 | 
| 64 | 
 | 
| 65 | 
ssize_t | 
| 66 | 
writebuf(               /* write all of requested buffer */ | 
| 67 | 
int     fd, | 
| 68 | 
const void      *buf, | 
| 69 | 
ssize_t siz | 
| 70 | 
) | 
| 71 | 
{ | 
| 72 | 
        const char      *bpos = (const char *)buf; | 
| 73 | 
        ssize_t         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 | 
} |