ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/process.c
(Generate patch)

Comparing ray/src/common/process.c (file contents):
Revision 2.6 by greg, Tue Feb 25 02:47:21 2003 UTC vs.
Revision 2.11 by greg, Fri Jan 15 19:01:53 2021 UTC

# Line 4 | Line 4 | static const char      RCSid[] = "$Id$";
4   /*
5   * Routines to communicate with separate process via dual pipes
6   *
7 < * External symbols declared in standard.h
7 > * External symbols declared in rtprocess.h
8   */
9  
10   #include "copyright.h"
11  
12 <                                /* find pipe buffer limit */
13 < #include  <sys/param.h>
12 > #include "rtprocess.h"
13  
14 < #ifndef PIPE_BUF
16 < #ifdef PIPSIZ
17 < #define PIPE_BUF        PIPSIZ
18 < #else
19 < #ifdef PIPE_MAX
20 < #define PIPE_BUF        PIPE_MAX
21 < #else
22 < #define PIPE_BUF        512             /* hyperconservative */
23 < #endif
24 < #endif
25 < #endif
14 > /*
15  
16 < #include  "vfork.h"
16 > The functions open_process() and close_process() exist in
17 > (currently) two versions, which are found in the files:
18  
19 < #ifndef BSD
20 < #include  <errno.h>
31 < #endif
19 >        win_process.c
20 >        unix_process.c
21  
22 + */
23  
24   int
25 < open_process(pd, av)            /* open communication to separate process */
26 < int     pd[3];
27 < char    *av[];
25 > process(                /* process data through pd */
26 >        SUBPROC *pd,
27 >        char    *recvbuf, char *sendbuf,
28 >        int     nbr, int nbs
29 > )
30   {
31 <        extern char     *getpath(), *getenv();
40 <        char    *compath;
41 <        int     p0[2], p1[2];
42 <                                        /* find executable */
43 <        compath = getpath(av[0], getenv("PATH"), 1);
44 <        if (compath == 0)
45 <                return(0);
46 <        if (pipe(p0) < 0 || pipe(p1) < 0)
31 >        if (!(pd->flags & PF_RUNNING))
32                  return(-1);
33 <        if ((pd[2] = vfork()) == 0) {           /* if child */
49 <                close(p0[1]);
50 <                close(p1[0]);
51 <                if (p0[0] != 0) {       /* connect p0 to stdin */
52 <                        dup2(p0[0], 0);
53 <                        close(p0[0]);
54 <                }
55 <                if (p1[1] != 1) {       /* connect p1 to stdout */
56 <                        dup2(p1[1], 1);
57 <                        close(p1[1]);
58 <                }
59 <                execv(compath, av);     /* exec command */
60 <                perror(compath);
61 <                _exit(127);
62 <        }
63 <        if (pd[2] == -1)
33 >        if (writebuf(pd->w, sendbuf, nbs) < nbs)
34                  return(-1);
35 <        close(p0[0]);
66 <        close(p1[1]);
67 <        pd[0] = p1[0];
68 <        pd[1] = p0[1];
69 <        return(PIPE_BUF);
35 >        return(readbuf(pd->r, recvbuf, nbr));
36   }
37  
38  
73 int
74 process(pd, recvbuf, sendbuf, nbr, nbs)         /* process data through pd */
75 int     pd[3];
76 char    *recvbuf, *sendbuf;
77 int     nbr, nbs;
78 {
79        if (nbs > PIPE_BUF)
80                return(-1);
81        if (writebuf(pd[1], sendbuf, nbs) < nbs)
82                return(-1);
83        return(readbuf(pd[0], recvbuf, nbr));
84 }
39  
40 <
41 < int
42 < close_process(pd)               /* close pipes and wait for process */
43 < int     pd[3];
40 > ssize_t
41 > readbuf(                /* read all of requested buffer */
42 >        int     fd,
43 >        char    *bpos,
44 >        ssize_t siz
45 > )
46   {
47 <        int     pid, status;
92 <
93 <        close(pd[1]);
94 <        close(pd[0]);
95 <        while ((pid = wait(&status)) != -1)
96 <                if (pid == pd[2])
97 <                        return(status>>8 & 0xff);
98 <        return(-1);             /* ? unknown status */
99 < }
100 <
101 <
102 < int
103 < readbuf(fd, bpos, siz)          /* read all of requested buffer */
104 < int     fd;
105 < char    *bpos;
106 < int     siz;
107 < {
108 <        register int    cc = 0, nrem = siz;
47 >        ssize_t cc = 0, nrem = siz;
48   retry:
49          while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
50                  bpos += cc;
# Line 122 | Line 61 | retry:
61   }
62  
63  
64 < int
65 < writebuf(fd, bpos, siz)         /* write all of requested buffer */
66 < int     fd;
67 < char    *bpos;
68 < int     siz;
64 > ssize_t
65 > writebuf(               /* write all of requested buffer */
66 > int     fd,
67 > char    *bpos,
68 > ssize_t siz
69 > )
70   {
71 <        register int    cc = 0, nrem = siz;
71 >        ssize_t cc = 0, nrem = siz;
72   retry:
73          while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
74                  bpos += cc;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines