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

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.2 by greg, Thu Aug 6 17:16:29 1992 UTC vs.
Revision 2.49 by schorsch, Thu Apr 24 10:28:25 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Generate sections of a picture.
6   */
7  
8 +
9 + #include <stdio.h>
10 + #include <signal.h>
11 + #include <sys/types.h>
12 +
13 + #include "platform.h"
14 + #ifndef NON_POSIX /* XXX need abstraction for process management */
15 + #include <sys/wait.h>
16 + #endif
17 +
18   #include "standard.h"
12 #include <fcntl.h>
19   #include "color.h"
20   #include "view.h"
21 < #include "resolu.h"
21 > #include "rtprocess.h"
22  
23 + #ifndef F_SETLKW
24 +
25 + int
26 + main(
27 +        int argc,
28 +        char *argv[]
29 + )
30 + {
31 +        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
32 +        exit(1);
33 + }
34 +
35 + #else
36 +
37 + #ifndef NFS
38 + #define  NFS                    1
39 + #endif
40 +                                /* set the following to 0 to forgo forking */
41 + #ifndef MAXFORK
42 + #if NFS
43 + #define  MAXFORK                3       /* allotment of duped processes */
44 + #else
45 + #define  MAXFORK                0
46 + #endif
47 + #endif
48 +                                        /* protection from SYSV signals(!) */
49 + #if defined(sgi)
50 + #define guard_io()      sighold(SIGALRM)
51 + #define unguard()       sigrelse(SIGALRM)
52 + #endif
53 + #ifndef guard_io
54 + #define guard_io()      
55 + #define unguard()      
56 + #endif
57 +
58 + extern char  *strerror();
59 +
60                                  /* rpict command */
61 < char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", };
62 < int  rpargc = 7;
20 < int  rpd[3];
61 > char  *rpargv[128] = {"rpict", "-S", "1"};
62 > int  rpargc = 3;
63   FILE  *torp, *fromrp;
64 < COLR  *scanline;
64 > COLR  *pbuf;
65                                  /* our view parameters */
66   VIEW  ourview = STDVIEW;
67 < double  pixaspect = 0.0;
68 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
67 > double  pixaspect = 1.0;
68 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
69                                  /* output file */
70   char  *outfile = NULL;
71   int  outfd;
72   long  scanorig;
73 + FILE  *syncfp = NULL;           /* synchronization file pointer */
74 + int  synclst = F_UNLCK;         /* synchronization file lock status */
75 + int  nforked = 0;
76  
77 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
78 +
79   char  *progname;
80   int  verbose = 0;
81 + unsigned  timelim = 0;
82 + int  rvrlim = -1;
83  
84 < extern long  lseek(), ftell();
84 > int  gotalrm = 0;
85 > void  onalrm(int i) { gotalrm++; }
86  
87 + static void dolock(int  fd, int  ltyp);
88 + static void init(int  ac, char  **av);
89 + static int nextpiece(int        *xp, int        *yp);
90 + static int rvrpiece(int *xp, int        *yp);
91 + static int cleanup(int  rstat);
92 + static void rpiece(void);
93 + static int putpiece(int xpos, int       ypos);
94 + static void filerr(char  *t);
95  
96 < main(argc, argv)
97 < int  argc;
98 < char  *argv[];
96 >
97 > int
98 > main(
99 >        int  argc,
100 >        char  *argv[]
101 > )
102   {
103          register int  i, rval;
104          
105          progname = argv[0];
106          for (i = 1; i < argc; i++) {
107 +                                                /* expand arguments */
108 +                while ((rval = expandarg(&argc, &argv, i)) > 0)
109 +                        ;
110 +                if (rval < 0) {
111 +                        fprintf(stderr, "%s: cannot expand '%s'",
112 +                                        argv[0], argv[i]);
113 +                        exit(1);
114 +                }
115                  if (argv[i][0] == '-')
116                          switch (argv[i][1]) {
117                          case 'v':
# Line 67 | Line 136 | char  *argv[];
136                                  }
137                                  break;
138                          case 'p':               /* pixel aspect ratio? */
139 <                                if (argv[i][2] == 'a' && !argv[i][3])
140 <                                        pixaspect = atof(argv[i+1]);
139 >                                if (argv[i][2] != 'a' || argv[i][3])
140 >                                        break;
141 >                                pixaspect = atof(argv[++i]);
142 >                                continue;
143 >                        case 'T':               /* time limit (hours) */
144 >                                if (argv[i][2])
145 >                                        break;
146 >                                timelim = atof(argv[++i])*3600. + .5;
147                                  break;
148 <                        case 'x':               /* piece x resolution */
149 <                                if (!argv[i][2])
150 <                                        hres = atoi(argv[i+1]);
151 <                                break;
152 <                        case 'y':               /* piece y resolution */
153 <                                if (!argv[i][2])
154 <                                        vres = atoi(argv[i+1]);
155 <                                break;
148 >                        case 'x':               /* overall x resolution */
149 >                                if (argv[i][2])
150 >                                        break;
151 >                                hres = atoi(argv[++i]);
152 >                                continue;
153 >                        case 'y':               /* overall y resolution */
154 >                                if (argv[i][2])
155 >                                        break;
156 >                                vres = atoi(argv[++i]);
157 >                                continue;
158                          case 'X':               /* horizontal multiplier */
159                                  if (argv[i][2])
160                                          break;
# Line 88 | Line 165 | char  *argv[];
165                                          break;
166                                  vmult = atoi(argv[++i]);
167                                  continue;
168 +                        case 'R':               /* recover */
169 +                                if (argv[i][2])
170 +                                        break;
171 +                                rvrlim = 0;
172 +                        /* fall through */
173 +                        case 'F':               /* syncronization file */
174 +                                if (argv[i][2])
175 +                                        break;
176 +                                if ((syncfp =
177 +                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
178 +                                        fprintf(stderr, "%s: cannot open\n",
179 +                                                        argv[i]);
180 +                                        exit(1);
181 +                                }
182 +                                continue;
183 +                        case 'z':               /* z-file ist verbotten */
184 +                                fprintf(stderr, "%s: -z option not allowed\n",
185 +                                                argv[0]);
186 +                                exit(1);
187                          case 'o':               /* output file */
188                                  if (argv[i][2])
189                                          break;
190                                  outfile = argv[++i];
191                                  continue;
192 <                        }
192 >                        } else if (i >= argc-1)
193 >                                break;
194                  rpargv[rpargc++] = argv[i];
195          }
196 <        rpargv[rpargc] = NULL;
196 >        if (i >= argc) {
197 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
198 >                exit(1);
199 >        }
200          if (outfile == NULL) {
201                  fprintf(stderr, "%s: missing output file\n", argv[0]);
202                  exit(1);
203          }
204          init(argc, argv);
205          rpiece();
206 <        rval = cleanup();
107 <        exit(rval);
206 >        exit(cleanup(0));
207   }
208  
209  
210 < init(ac, av)                    /* set up output file and start rpict */
211 < int  ac;
212 < char  **av;
210 > static void
211 > dolock(         /* lock or unlock a file */
212 >        int  fd,
213 >        int  ltyp
214 > )
215   {
216 +        static struct flock  fls;       /* static so initialized to zeroes */
217 +
218 +        fls.l_type = ltyp;
219 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
220 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
221 +                                progname, strerror(errno));
222 +                exit(1);
223 +        }
224 + }
225 +
226 +
227 + static void
228 + init(                   /* set up output file and start rpict */
229 +        int  ac,
230 +        char  **av
231 + )
232 + {
233 +        static char  hrbuf[16], vrbuf[16];
234          extern char  VersionID[];
235          char  *err;
236          FILE  *fp;
237          int  hr, vr;
238 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
239                                          /* set up view */
240          if ((err = setview(&ourview)) != NULL) {
241                  fprintf(stderr, "%s: %s\n", progname, err);
242                  exit(1);
243          }
244 +        if (syncfp != NULL) {
245 +                sflock(F_RDLCK);
246 +                fscanf(syncfp, "%d %d", &hmult, &vmult);
247 +                sflock(F_UNLCK);
248 +        }
249 +                                        /* compute piece size */
250 +        hres /= hmult;
251 +        vres /= vmult;
252 +        if (hres <= 0 || vres <= 0) {
253 +                fprintf(stderr, "%s: illegal resolution/subdivision\n", progname);
254 +                exit(1);
255 +        }
256          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
257 +        sprintf(hrbuf, "%d", hres);
258 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
259 +        sprintf(vrbuf, "%d", vres);
260 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
261 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
262 +        rpargv[rpargc++] = av[ac-1];
263 +        rpargv[rpargc] = NULL;
264                                          /* open output file */
265          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
266 +                dolock(outfd, F_WRLCK);
267                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
268                          goto filerr;
269 <                printargs(ac, av, fp);          /* write header */
269 >                newheader("RADIANCE", fp);      /* create header */
270 >                printargs(ac, av, fp);
271                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
272                  fputs(VIEWSTR, fp);
273                  fprintview(&ourview, fp);
# Line 137 | Line 278 | char  **av;
278                  putc('\n', fp);
279                  fprtresolu(hres*hmult, vres*vmult, fp);
280          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
281 <                sleep(30);                      /* wait for header */
281 >                dolock(outfd, F_RDLCK);
282                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
283                          goto filerr;
284 <                getheader(fp, NULL);            /* skip header */
285 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
284 >                getheader(fp, NULL, NULL);      /* skip header */
285 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
286                                  hr != hres*hmult || vr != vres*vmult) {
287 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
288 <                                        outfile);
287 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
288 >                                        progname, outfile);
289                          exit(1);
290                  }
291          } else {
292 <                fprintf(stderr, "%s: cannot open\n", outfile);
292 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
293 >                                progname, outfile);
294                  exit(1);
295          }
296          scanorig = ftell(fp);           /* record position of first scanline */
297          if (fclose(fp) == -1)           /* done with stream i/o */
298                  goto filerr;
299 +        dolock(outfd, F_UNLCK);
300                                          /* start rpict process */
301 <        if (open_process(rpd, rpargv) <= 0) {
302 <                fprintf(stderr, "%s: cannot start\n", rpargv[0]);
301 >        if (open_process(&rpd, rpargv) <= 0) {
302 >                fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
303                  exit(1);
304          }
305 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
306 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
305 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
306 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
307                  fprintf(stderr, "%s: cannot open stream to %s\n",
308                                  progname, rpargv[0]);
309                  exit(1);
310          }
311 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
311 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
312                  fprintf(stderr, "%s: out of memory\n", progname);
313                  exit(1);
314          }
315 +        signal(SIGALRM, onalrm);
316 +        if (timelim)
317 +                alarm(timelim);
318          return;
319   filerr:
320 <        fprintf(stderr, "%s: file i/o error\n", outfile);
320 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
321          exit(1);
322   }
323  
324  
325 < int
326 < cleanup()                       /* close rpict process and clean up */
325 > static int
326 > nextpiece(              /* get next piece assignment */
327 >        int     *xp,
328 >        int     *yp
329 > )
330   {
331 <        register int  rpstat;
331 >        if (gotalrm)                    /* someone wants us to quit */
332 >                return(0);
333 >        if (syncfp != NULL) {           /* use sync file */
334 >                /*
335 >                 * So we don't necessarily have to lock and unlock the file
336 >                 * multiple times (very slow), we establish an exclusive
337 >                 * lock at the beginning on our synchronization file and
338 >                 * maintain it in the subroutine rvrpiece().
339 >                 */
340 >                sflock(F_WRLCK);
341 >                fseek(syncfp, 0L, 0);           /* read position */
342 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
343 >                        *xp = hmult-1;
344 >                        *yp = vmult;
345 >                }
346 >                if (rvrlim == 0)                /* initialize recovery limit */
347 >                        rvrlim = *xp*vmult + *yp;
348 >                if (rvrpiece(xp, yp)) {         /* do stragglers first */
349 >                        sflock(F_UNLCK);
350 >                        return(1);
351 >                }
352 >                if (--(*yp) < 0) {              /* decrement position */
353 >                        *yp = vmult-1;
354 >                        if (--(*xp) < 0) {      /* all done */
355 >                                sflock(F_UNLCK);
356 >                                return(0);
357 >                        }
358 >                }
359 >                fseek(syncfp, 0L, 0);           /* write new position */
360 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
361 >                fflush(syncfp);
362 >                sflock(F_UNLCK);                /* release sync file */
363 >                return(1);
364 >        }
365 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
366 > }
367  
368 <        free((char *)scanline);
368 >
369 > static int
370 > rvrpiece(               /* check for recoverable pieces */
371 >        register int    *xp,
372 >        register int    *yp
373 > )
374 > {
375 >        static char  *pdone = NULL;     /* which pieces are done */
376 >        static long  readpos = -1;      /* how far we've read */
377 >        register int  i;
378 >        /*
379 >         * This routine is called by nextpiece() with an
380 >         * exclusive lock on syncfp and the file pointer at the
381 >         * appropriate position to read in the finished pieces.
382 >         */
383 >        if (rvrlim < 0)
384 >                return(0);              /* only check if asked */
385 >        if (pdone == NULL)              /* first call */
386 >                pdone = calloc(hmult*vmult, sizeof(char));
387 >        if (pdone == NULL) {
388 >                fprintf(stderr, "%s: out of memory\n", progname);
389 >                exit(1);
390 >        }
391 >        if (readpos != -1)              /* mark what's been done */
392 >                fseek(syncfp, readpos, 0);
393 >        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
394 >                pdone[*xp*vmult+*yp] = 1;
395 >        if (!feof(syncfp)) {
396 >                fprintf(stderr, "%s: format error in sync file\n", progname);
397 >                exit(1);
398 >        }
399 >        readpos = ftell(syncfp);
400 >        i = hmult*vmult;                /* find an unaccounted for piece */
401 >        while (i-- > rvrlim)
402 >                if (!pdone[i]) {
403 >                        *xp = i / vmult;
404 >                        *yp = i % vmult;
405 >                        pdone[i] = 1;   /* consider it done */
406 >                        return(1);
407 >                }
408 >        rvrlim = -1;                    /* nothing left to recover */
409 >        free(pdone);
410 >        pdone = NULL;
411 >        return(0);
412 > }
413 >
414 >
415 > static int
416 > cleanup(                        /* close rpict process and clean up */
417 >        int  rstat
418 > )
419 > {
420 >        int  status;
421 >
422 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
423          fclose(torp);
424          fclose(fromrp);
425 <        rpstat = close_process(rpd);
426 <        if (rpstat == -1)
427 <                rpstat = 0;
428 <        return(rpstat);
425 >        while (wait(&status) != -1)
426 >                if (rstat == 0)
427 >                        rstat = status>>8 & 0xff;
428 >        return(rstat);
429   }
430  
431  
432 < rpiece()                        /* render picture piece by piece */
432 > static void
433 > rpiece(void)                    /* render picture piece by piece */
434   {
196        extern char  *gets(), *strcat();
435          VIEW  pview;
198        char  buf[48];
436          int  xorg, yorg;
437 <        
438 <        while (gets(buf) != NULL) {
439 <                if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) {
440 <                        fprintf(stderr, "%s: input format error\n", progname);
441 <                        exit(1);
442 <                }
443 <                copystruct(&pview, &ourview);   /* compute view for piece */
444 <                switch (ourview.type) {
445 <                case VT_PER:
446 <                        pview.horiz = 2.*180./PI*atan(
447 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
448 <                        pview.vert = 2.*180./PI*atan(
449 <                                        tan(PI/180./2.*ourview.vert)/vmult );
450 <                        break;
451 <                case VT_PAR:
452 <                case VT_ANG:
453 <                        pview.horiz = ourview.horiz / hmult;
454 <                        pview.vert = ourview.vert / vmult;
455 <                        break;
456 <                case VT_HEM:
457 <                default:
458 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
459 <                                        progname, ourview.type);
460 <                        exit(1);
461 <                }
462 <                pview.hoff += xorg - 0.5*(hmult-1);
463 <                pview.voff += yorg - 0.5*(vmult-1);
437 >                                        /* compute view parameters */
438 >        pview = ourview;
439 >        switch (ourview.type) {
440 >        case VT_PER:
441 >                pview.horiz = (2.*180./PI)*atan(
442 >                                tan((PI/180./2.)*ourview.horiz)/hmult );
443 >                pview.vert = (2.*180./PI)*atan(
444 >                                tan((PI/180./2.)*ourview.vert)/vmult );
445 >                break;
446 >        case VT_PAR:
447 >        case VT_ANG:
448 >                pview.horiz = ourview.horiz / hmult;
449 >                pview.vert = ourview.vert / vmult;
450 >                break;
451 >        case VT_CYL:
452 >                pview.horiz = ourview.horiz / hmult;
453 >                pview.vert = (2.*180./PI)*atan(
454 >                                tan((PI/180./2.)*ourview.vert)/vmult );
455 >                break;
456 >        case VT_HEM:
457 >                pview.horiz = (2.*180./PI)*asin(
458 >                                sin((PI/180./2.)*ourview.horiz)/hmult );
459 >                pview.vert = (2.*180./PI)*asin(
460 >                                sin((PI/180./2.)*ourview.vert)/vmult );
461 >                break;
462 >        case VT_PLS:
463 >                pview.horiz = sin((PI/180./2.)*ourview.horiz) /
464 >                                (1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult;
465 >                pview.horiz *= pview.horiz;
466 >                pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) /
467 >                                                (1. + pview.horiz));
468 >                pview.vert = sin((PI/180./2.)*ourview.vert) /
469 >                                (1.0 + cos((PI/180./2.)*ourview.vert)) / vmult;
470 >                pview.vert *= pview.vert;
471 >                pview.vert = (2.*180./PI)*acos((1. - pview.vert) /
472 >                                                (1. + pview.vert));
473 >                break;
474 >        default:
475 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
476 >                                progname, ourview.type);
477 >                exit(cleanup(1));
478 >        }
479 >                                        /* render each piece */
480 >        while (nextpiece(&xorg, &yorg)) {
481 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
482 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
483                  fputs(VIEWSTR, torp);
484                  fprintview(&pview, torp);
485                  putc('\n', torp);
486 <                fflush(torp);                   /* assign piece to rpict */
486 >                fflush(torp);                   /* assigns piece to rpict */
487                  putpiece(xorg, yorg);           /* place piece in output */
232                if (verbose) {                  /* notify caller */
233                        strcat(buf, " done\n");
234                        writebuf(fileno(stdout), buf, strlen(buf));
235                }
488          }
489   }
490  
491  
492 < putpiece(xpos, ypos)            /* get next piece from rpict */
493 < int  xpos, ypos;
492 > static int
493 > putpiece(               /* get next piece from rpict */
494 > int     xpos,
495 > int     ypos
496 > )
497   {
498 +        struct flock  fls;
499 +        int  pid, status;
500          int  hr, vr;
501 <        int  y;
502 <
503 <        getheader(fromrp, NULL);        /* discard header info. */
504 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
505 <                        hr != hres || vr != vres) {
506 <                fprintf(stderr, "%s: resolution mismatch from rpict\n",
250 <                                progname);
251 <                exit(1);
501 >        register int  y;
502 >                                /* check bounds */
503 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
504 >                fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
505 >                                progname, xpos, ypos);
506 >                exit(cleanup(1));
507          }
508 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
509 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
510 <                        fprintf(stderr, "%s: read error from rpict\n",
511 <                                        progname);
512 <                        exit(1);
508 >                                /* check header from rpict */
509 >        guard_io();
510 >        getheader(fromrp, NULL, NULL);
511 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
512 >                fprintf(stderr, "%s: resolution mismatch from %s\n",
513 >                                progname, rpargv[0]);
514 >                exit(cleanup(1));
515 >        }
516 >        if (verbose) {                          /* notify caller */
517 >                printf("%d %d begun\n", xpos, ypos);
518 >                fflush(stdout);
519 >        }
520 >        unguard();
521 >                                /* load new piece into buffer */
522 >        for (y = 0; y < vr; y++) {
523 >                guard_io();
524 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
525 >                        fprintf(stderr, "%s: read error from %s\n",
526 >                                        progname, rpargv[0]);
527 >                        exit(cleanup(1));
528                  }
529 <                if ((y == 0 || hmult != 1) && lseek(outfd,
530 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
531 <                                0) == -1) {
532 <                        fprintf(stderr, "%s: seek error\n", outfile);
533 <                        exit(1);
529 >                unguard();
530 >        }
531 > #if MAXFORK
532 >                                /* fork so we don't slow rpict down */
533 >        if ((pid = fork()) > 0) {
534 >                if (++nforked >= MAXFORK) {
535 >                        wait(&status);          /* reap a child */
536 >                        if (status)
537 >                                exit(cleanup(status>>8 & 0xff));
538 >                        nforked--;
539                  }
540 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
541 <                                hr*sizeof(COLR)) {
542 <                        fprintf(stderr, "%s: write error\n", outfile);
543 <                        exit(1);
540 >                return(pid);
541 >        }
542 > #else
543 >        pid = -1;               /* no forking */
544 > #endif
545 >        fls.l_start = scanorig +
546 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
547 > #if NFS
548 >        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
549 >                                /* lock file section so NFS doesn't mess up */
550 >        fls.l_whence = 0;
551 >        fls.l_type = F_WRLCK;
552 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
553 >                filerr("lock");
554 > #endif
555 >                                /* write new piece to file */
556 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
557 >                filerr("seek");
558 >        if (hmult == 1) {
559 >                if (writebuf(outfd, (char *)pbuf,
560 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
561 >                        filerr("write");
562 >        } else
563 >                for (y = 0; y < vr; y++) {
564 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
565 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
566 >                                filerr("write");
567 >                        if (y < vr-1 && lseek(outfd,
568 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
569 >                                        SEEK_CUR) < 0)
570 >                                filerr("seek");
571                  }
572 + #if NFS
573 +        fls.l_type = F_UNLCK;           /* release lock */
574 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
575 +                filerr("lock");
576 + #endif
577 +        if (verbose) {                          /* notify caller */
578 +                printf("%d %d done\n", xpos, ypos);
579 +                fflush(stdout);
580          }
581 +        if (syncfp != NULL) {                   /* record what's been done */
582 +                sflock(F_WRLCK);
583 +                fseek(syncfp, 0L, 2);           /* append index */
584 +                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
585 +                fflush(syncfp);
586 +                                /*** Unlock not necessary, since
587 +                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
588 +        }
589 +        if (pid == -1)          /* didn't fork or fork failed */
590 +                return(0);
591 +        _exit(0);               /* else exit child process (releasing locks) */
592   }
593 +
594 +
595 + static void
596 + filerr(                 /* report file error and exit */
597 +        char  *t
598 + )
599 + {
600 +        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
601 +                        progname, t, outfile, strerror(errno));
602 +        _exit(1);
603 + }
604 +
605 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines