Back to index

radiance  4R0+20100331
process.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: process.c,v 2.8 2004/09/17 21:43:49 greg Exp $";
00003 #endif
00004 /*
00005  * Routines to communicate with separate process via dual pipes
00006  *
00007  * External symbols declared in standard.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include "rtprocess.h"
00013 
00014 /*
00015 
00016 The functions open_process() and close_process() exist in
00017 (currently) two versions, which are found in the files:
00018 
00019        win_process.c
00020        unix_process.c
00021 
00022 */
00023 
00024 int
00025 process(             /* process data through pd */
00026 SUBPROC *pd,
00027 char   *recvbuf, char *sendbuf,
00028 int    nbr, int nbs
00029 )
00030 {
00031        if (nbs > PIPE_BUF)
00032               return(-1);
00033        if (!pd->running)
00034               return(-1);
00035        if (writebuf(pd->w, sendbuf, nbs) < nbs)
00036               return(-1);
00037        return(readbuf(pd->r, recvbuf, nbr));
00038 }
00039 
00040 
00041 
00042 int
00043 readbuf(             /* read all of requested buffer */
00044 int    fd,
00045 char   *bpos,
00046 int    siz
00047 )
00048 {
00049        register int  cc = 0, nrem = siz;
00050 retry:
00051        while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
00052               bpos += cc;
00053               nrem -= cc;
00054        }
00055        if (cc < 0) {
00056 #ifndef BSD
00057               if (errno == EINTR)  /* we were interrupted! */
00058                      goto retry;
00059 #endif
00060               return(cc);
00061        }
00062        return(siz-nrem);
00063 }
00064 
00065 
00066 int
00067 writebuf(            /* write all of requested buffer */
00068 int    fd,
00069 char   *bpos,
00070 int    siz
00071 )
00072 {
00073        register int  cc = 0, nrem = siz;
00074 retry:
00075        while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
00076               bpos += cc;
00077               nrem -= cc;
00078        }
00079        if (cc < 0) {
00080 #ifndef BSD
00081               if (errno == EINTR)  /* we were interrupted! */
00082                      goto retry;
00083 #endif
00084               return(cc);
00085        }
00086        return(siz-nrem);
00087 }