15 |
|
#include "view.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
+ |
/* set the following to 0 to forgo forking */ |
19 |
+ |
#ifndef MAXFORK |
20 |
+ |
#define MAXFORK 2 /* max. unreaped child 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; |
251 |
|
int |
252 |
|
cleanup() /* close rpict process and clean up */ |
253 |
|
{ |
254 |
< |
register int rpstat; |
254 |
> |
register int pid; |
255 |
> |
int status, rstat = 0; |
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 ((pid = wait(&status)) != -1) |
261 |
> |
if (rstat == 0) |
262 |
> |
rstat = status>>8 & 0xff; |
263 |
> |
return(rstat); |
264 |
|
} |
265 |
|
|
266 |
|
|
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 */ |
337 |
|
progname, rpargv[0]); |
338 |
|
exit(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(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 |
|
} |