15 |
|
#include "view.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
+ |
/* set the following to 0 to forgo forking */ |
19 |
+ |
#ifndef MAXFORK |
20 |
+ |
#define MAXFORK 3 /* allotment of duped processes */ |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
/* rpict command */ |
24 |
|
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
25 |
|
int rpargc = 9; |
35 |
|
int outfd; |
36 |
|
long scanorig; |
37 |
|
int syncfd = -1; /* lock file descriptor */ |
38 |
+ |
int nforked = 0; |
39 |
|
|
40 |
|
char *progname; |
41 |
|
int verbose = 0; |
123 |
|
} |
124 |
|
init(argc, argv); |
125 |
|
rpiece(); |
126 |
< |
rval = cleanup(); |
126 |
> |
rval = cleanup(0); |
127 |
|
exit(rval); |
128 |
|
} |
129 |
|
|
244 |
|
if (sscanf(buf, "%d %d", xp, yp) == 2) |
245 |
|
return(1); |
246 |
|
fprintf(stderr, "%s: input format error\n", progname); |
247 |
< |
exit(1); |
247 |
> |
exit(cleanup(1)); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
int |
252 |
< |
cleanup() /* close rpict process and clean up */ |
252 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
253 |
> |
int rstat; |
254 |
|
{ |
255 |
< |
register int rpstat; |
255 |
> |
int status; |
256 |
|
|
257 |
|
free((char *)pbuf); |
258 |
|
fclose(torp); |
259 |
|
fclose(fromrp); |
260 |
< |
rpstat = close_process(rpd); |
261 |
< |
if (rpstat == -1) |
262 |
< |
rpstat = 0; |
263 |
< |
return(rpstat); |
260 |
> |
while (wait(&status) != -1) |
261 |
> |
if (rstat == 0) |
262 |
> |
rstat = status>>8 & 0xff; |
263 |
> |
return(rstat); |
264 |
|
} |
265 |
|
|
266 |
|
|
292 |
|
default: |
293 |
|
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
294 |
|
progname, ourview.type); |
295 |
< |
exit(1); |
295 |
> |
exit(cleanup(1)); |
296 |
|
} |
297 |
|
pview.hoff += xorg - 0.5*(hmult-1); |
298 |
|
pview.voff += yorg - 0.5*(vmult-1); |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
+ |
int |
313 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
314 |
|
int xpos, ypos; |
315 |
|
{ |
316 |
|
struct flock fls; |
317 |
+ |
int pid, status; |
318 |
|
int hr, vr; |
319 |
|
register int y; |
320 |
|
/* check bounds */ |
321 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
322 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
323 |
|
progname, xpos, ypos); |
324 |
< |
exit(1); |
324 |
> |
exit(cleanup(1)); |
325 |
|
} |
326 |
|
/* check header from rpict */ |
327 |
|
getheader(fromrp, NULL); |
328 |
|
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
329 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
330 |
|
progname, rpargv[0]); |
331 |
< |
exit(1); |
331 |
> |
exit(cleanup(1)); |
332 |
|
} |
333 |
|
/* load new piece into buffer */ |
334 |
|
for (y = 0; y < vr; y++) |
335 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
336 |
|
fprintf(stderr, "%s: read error from %s\n", |
337 |
|
progname, rpargv[0]); |
338 |
< |
exit(1); |
338 |
> |
exit(cleanup(1)); |
339 |
|
} |
340 |
+ |
#if MAXFORK |
341 |
+ |
/* fork so we don't slow rpict down */ |
342 |
+ |
if ((pid = fork()) > 0) { |
343 |
+ |
if (++nforked >= MAXFORK) { |
344 |
+ |
wait(&status); /* reap a child */ |
345 |
+ |
if (status) |
346 |
+ |
exit(cleanup(status>>8 & 0xff)); |
347 |
+ |
nforked--; |
348 |
+ |
} |
349 |
+ |
return(pid); |
350 |
+ |
} |
351 |
+ |
#else |
352 |
+ |
pid = -1; /* no forking */ |
353 |
+ |
#endif |
354 |
|
/* lock file section so NFS doesn't mess up */ |
355 |
|
fls.l_whence = 0; |
356 |
|
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
360 |
|
/* write new piece to file */ |
361 |
|
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
362 |
|
goto seekerr; |
363 |
< |
for (y = 0; y < vr; y++) { |
364 |
< |
if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) != |
365 |
< |
hr*sizeof(COLR)) { |
366 |
< |
fprintf(stderr, "%s: write error on file \"%s\"\n", |
367 |
< |
progname, outfile); |
368 |
< |
exit(1); |
363 |
> |
if (hmult == 1) { |
364 |
> |
if (writebuf(outfd, (char *)pbuf, |
365 |
> |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
366 |
> |
goto writerr; |
367 |
> |
} else |
368 |
> |
for (y = 0; y < vr; y++) { |
369 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
370 |
> |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
371 |
> |
goto writerr; |
372 |
> |
if (y < vr-1 && lseek(outfd, |
373 |
> |
(long)(hmult-1)*hr*sizeof(COLR), |
374 |
> |
1) == -1) |
375 |
> |
goto seekerr; |
376 |
|
} |
377 |
< |
if (hmult != 1 && y < vr-1 && lseek(outfd, |
378 |
< |
(long)(hmult-1)*hr*sizeof(COLR), 1) == -1) |
379 |
< |
goto seekerr; |
377 |
> |
if (pid == -1) { /* fork failed */ |
378 |
> |
fls.l_type = F_UNLCK; /* release lock */ |
379 |
> |
fcntl(outfd, F_SETLKW, &fls); |
380 |
> |
return(0); |
381 |
|
} |
382 |
< |
/* unlock file section */ |
352 |
< |
fls.l_type = F_UNLCK; |
353 |
< |
fcntl(outfd, F_SETLKW, &fls); |
354 |
< |
return; |
382 |
> |
_exit(0); /* else exit child process (releasing lock) */ |
383 |
|
seekerr: |
384 |
|
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
385 |
< |
exit(1); |
385 |
> |
_exit(1); |
386 |
> |
writerr: |
387 |
> |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
388 |
> |
_exit(1); |
389 |
|
} |