ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/process.c
Revision: 1.1
Committed: Tue Jul 23 15:56:57 1991 UTC (32 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines to communicate with separate process via dual pipes
9 */
10
11 /* find pipe buffer limit */
12 #include <sys/param.h>
13
14 #ifndef PIPE_BUF
15 #ifdef PIPSIZ
16 #define PIPE_BUF PIPSIZ
17 #else
18 #ifdef PIPE_MAX
19 #define PIPE_BUF PIPE_MAX
20 #else
21 #define PIPE_BUF 512 /* hyperconservative */
22 #endif
23 #endif
24 #endif
25
26 #ifndef BSD
27 #define vfork fork
28 #endif
29
30
31 int
32 open_process(pd, av) /* open communication to separate process */
33 int pd[3];
34 char *av[];
35 {
36 extern char *getpath(), *getenv();
37 char *compath;
38 int p0[2], p1[2];
39 /* find executable */
40 compath = getpath(av[0], getenv("PATH"), 1);
41 if (compath == 0)
42 return(0);
43 if (pipe(p0) < 0 || pipe(p1) < 0)
44 return(-1);
45 if ((pd[2] = vfork()) == 0) { /* if child */
46 close(p0[1]);
47 close(p1[0]);
48 if (p0[0] != 0) { /* connect p0 to stdin */
49 dup2(p0[0], 0);
50 close(p0[0]);
51 }
52 if (p1[1] != 1) { /* connect p1 to stdout */
53 dup2(p1[1], 1);
54 close(p1[1]);
55 }
56 execv(compath, av); /* exec command */
57 perror(compath);
58 _exit(127);
59 }
60 if (pd[2] == -1)
61 return(-1);
62 close(p0[0]);
63 close(p1[1]);
64 pd[0] = p1[0];
65 pd[1] = p0[1];
66 return(PIPE_BUF);
67 }
68
69
70 int
71 process(pd, recvbuf, sendbuf, nbr, nbs) /* process data through pd */
72 int pd[3];
73 char *recvbuf, *sendbuf;
74 int nbr, nbs;
75 {
76 if (nbs > PIPE_BUF)
77 return(-1);
78 if (writebuf(pd[1], sendbuf, nbs) < nbs)
79 return(-1);
80 return(readbuf(pd[0], recvbuf, nbr));
81 }
82
83
84 int
85 close_process(pd) /* close pipes and wait for process */
86 int pd[3];
87 {
88 int pid, status;
89
90 close(pd[1]);
91 close(pd[0]);
92 while ((pid = wait(&status)) != -1)
93 if (pid == pd[2])
94 return(status>>8 & 0xff);
95 return(-1); /* ? unknown status */
96 }
97
98
99 int
100 readbuf(fd, bpos, siz) /* read all of requested buffer */
101 int fd;
102 char *bpos;
103 int siz;
104 {
105 register int cc, nrem = siz;
106
107 while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
108 bpos += cc;
109 nrem -= cc;
110 }
111 if (cc < 0)
112 return(cc);
113 return(siz-nrem);
114 }
115
116
117 int
118 writebuf(fd, bpos, siz) /* write all of requested buffer */
119 int fd;
120 char *bpos;
121 int siz;
122 {
123 register int cc, nrem = siz;
124
125 while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
126 bpos += cc;
127 nrem -= cc;
128 }
129 if (cc < 0)
130 return(cc);
131 return(siz-nrem);
132 }