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.15 by greg, Thu Jan 21 17:46:31 1993 UTC vs.
Revision 2.51 by greg, Mon May 17 17:03:53 2010 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 < #include "standard.h"
9 < #include <fcntl.h>
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"
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
# Line 26 | Line 45 | static char SCCSid[] = "$SunId$ LBL";
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", "-pa", "1"};
62 < int  rpargc = 9;
33 < int  rpd[3];
61 > char  *rpargv[128] = {"rpict", "-S", "1"};
62 > int  rpargc = 3;
63   FILE  *torp, *fromrp;
64   COLR  *pbuf;
65                                  /* our view parameters */
66   VIEW  ourview = STDVIEW;
67   double  pixaspect = 1.0;
68 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
68 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
69                                  /* output file */
70   char  *outfile = NULL;
71   int  outfd;
72   long  scanorig;
73 < int  syncfd = -1;               /* lock file descriptor */
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  
50 extern long  lseek(), ftell();
51
84   int  gotalrm = 0;
85 < int  onalrm() { gotalrm++; }
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'\n",
112 +                                        argv[0], argv[i]);
113 +                        exit(1);
114 +                }
115                  if (argv[i][0] == '-')
116                          switch (argv[i][1]) {
117                          case 'v':
# Line 85 | 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]);
141 <                                break;
142 <                        case 'x':               /* piece x resolution */
143 <                                if (!argv[i][2])
144 <                                        hres = atoi(argv[i+1]);
145 <                                break;
146 <                        case 'y':               /* piece y resolution */
147 <                                if (!argv[i][2])
148 <                                        vres = atoi(argv[i+1]);
149 <                                break;
139 >                                if (argv[i][2] == 'm') {
140 >                                        fprintf(stderr, "%s: -pm unsupported\n",
141 >                                                argv[0]);
142 >                                        ++i;
143 >                                        continue;
144 >                                }
145 >                                if (argv[i][2] != 'a' || argv[i][3])
146 >                                        break;
147 >                                pixaspect = atof(argv[++i]);
148 >                                continue;
149 >                        case 'T':               /* time limit (hours) */
150 >                                if (argv[i][2])
151 >                                        break;
152 >                                timelim = atof(argv[++i])*3600. + .5;
153 >                                continue;
154 >                        case 'x':               /* overall x resolution */
155 >                                if (argv[i][2])
156 >                                        break;
157 >                                hres = atoi(argv[++i]);
158 >                                continue;
159 >                        case 'y':               /* overall y resolution */
160 >                                if (argv[i][2])
161 >                                        break;
162 >                                vres = atoi(argv[++i]);
163 >                                continue;
164                          case 'X':               /* horizontal multiplier */
165                                  if (argv[i][2])
166                                          break;
# Line 106 | Line 171 | char  *argv[];
171                                          break;
172                                  vmult = atoi(argv[++i]);
173                                  continue;
174 +                        case 'R':               /* recover */
175 +                                if (argv[i][2])
176 +                                        break;
177 +                                rvrlim = 0;
178 +                        /* fall through */
179                          case 'F':               /* syncronization file */
180                                  if (argv[i][2])
181                                          break;
182 <                                if ((syncfd = open(argv[++i],
183 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
182 >                                if ((syncfp =
183 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
184                                          fprintf(stderr, "%s: cannot open\n",
185                                                          argv[i]);
186                                          exit(1);
# Line 125 | Line 195 | char  *argv[];
195                                          break;
196                                  outfile = argv[++i];
197                                  continue;
198 <                        }
198 >                        } else if (i >= argc-1)
199 >                                break;
200                  rpargv[rpargc++] = argv[i];
201          }
202 <        rpargv[rpargc] = NULL;
202 >        if (i >= argc) {
203 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
204 >                exit(1);
205 >        }
206          if (outfile == NULL) {
207                  fprintf(stderr, "%s: missing output file\n", argv[0]);
208                  exit(1);
# Line 139 | Line 213 | char  *argv[];
213   }
214  
215  
216 < init(ac, av)                    /* set up output file and start rpict */
217 < int  ac;
218 < char  **av;
216 > static void
217 > dolock(         /* lock or unlock a file */
218 >        int  fd,
219 >        int  ltyp
220 > )
221   {
222 +        static struct flock  fls;       /* static so initialized to zeroes */
223 +
224 +        fls.l_type = ltyp;
225 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
226 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
227 +                                progname, strerror(errno));
228 +                exit(1);
229 +        }
230 + }
231 +
232 +
233 + static void
234 + init(                   /* set up output file and start rpict */
235 +        int  ac,
236 +        char  **av
237 + )
238 + {
239 +        static char  hrbuf[16], vrbuf[16];
240          extern char  VersionID[];
241          char  *err;
242          FILE  *fp;
243          int  hr, vr;
244 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
245                                          /* set up view */
246          if ((err = setview(&ourview)) != NULL) {
247                  fprintf(stderr, "%s: %s\n", progname, err);
248                  exit(1);
249          }
250 <        if (syncfd != -1) {
251 <                char  buf[32];
252 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
253 <                sscanf(buf, "%d %d", &hmult, &vmult);
250 >        if (syncfp != NULL) {
251 >                sflock(F_RDLCK);
252 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
253 >                sflock(F_UNLCK);
254          }
255 +                                        /* compute piece size */
256 +        hres /= hmult;
257 +        vres /= vmult;
258 +        if (hres <= 0 || vres <= 0) {
259 +                fprintf(stderr, "%s: illegal resolution/subdivision\n", progname);
260 +                exit(1);
261 +        }
262          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
263 +        sprintf(hrbuf, "%d", hres);
264 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
265 +        sprintf(vrbuf, "%d", vres);
266 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
267 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
268 +        rpargv[rpargc++] = av[ac-1];
269 +        rpargv[rpargc] = NULL;
270                                          /* open output file */
271          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
272 +                dolock(outfd, F_WRLCK);
273                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
274                          goto filerr;
275 <                printargs(ac, av, fp);          /* write header */
275 >                newheader("RADIANCE", fp);      /* create header */
276 >                printargs(ac, av, fp);
277                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
278                  fputs(VIEWSTR, fp);
279                  fprintview(&ourview, fp);
# Line 173 | Line 284 | char  **av;
284                  putc('\n', fp);
285                  fprtresolu(hres*hmult, vres*vmult, fp);
286          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
287 +                dolock(outfd, F_RDLCK);
288                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
289                          goto filerr;
290                  getheader(fp, NULL, NULL);      /* skip header */
# Line 190 | Line 302 | char  **av;
302          scanorig = ftell(fp);           /* record position of first scanline */
303          if (fclose(fp) == -1)           /* done with stream i/o */
304                  goto filerr;
305 < #if NFS
194 <        sync();                         /* flush NFS buffers */
195 < #endif
305 >        dolock(outfd, F_UNLCK);
306                                          /* start rpict process */
307 <        if (open_process(rpd, rpargv) <= 0) {
307 >        if (open_process(&rpd, rpargv) <= 0) {
308                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
309                  exit(1);
310          }
311 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
312 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
311 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
312 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
313                  fprintf(stderr, "%s: cannot open stream to %s\n",
314                                  progname, rpargv[0]);
315                  exit(1);
316          }
317 <        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
317 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
318                  fprintf(stderr, "%s: out of memory\n", progname);
319                  exit(1);
320          }
321          signal(SIGALRM, onalrm);
322 +        if (timelim)
323 +                alarm(timelim);
324          return;
325   filerr:
326          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 216 | Line 328 | filerr:
328   }
329  
330  
331 < int
332 < nextpiece(xp, yp)               /* get next piece assignment */
333 < int  *xp, *yp;
331 > static int
332 > nextpiece(              /* get next piece assignment */
333 >        int     *xp,
334 >        int     *yp
335 > )
336   {
223        extern char  *fgets();
224        struct flock  fls;
225        char  buf[64];
226
337          if (gotalrm)                    /* someone wants us to quit */
338                  return(0);
339 <        if (syncfd != -1) {             /* use sync file */
340 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
341 <                fls.l_whence = 0;
342 <                fls.l_start = 0L;
343 <                fls.l_len = 0L;
344 <                fcntl(syncfd, F_SETLKW, &fls);
345 <                lseek(syncfd, 0L, 0);
346 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
347 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
339 >        if (syncfp != NULL) {           /* use sync file */
340 >                /*
341 >                 * So we don't necessarily have to lock and unlock the file
342 >                 * multiple times (very slow), we establish an exclusive
343 >                 * lock at the beginning on our synchronization file and
344 >                 * maintain it in the subroutine rvrpiece().
345 >                 */
346 >                sflock(F_WRLCK);
347 >                fseek(syncfp, 0L, 0);           /* read position */
348 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
349                          *xp = hmult-1;
350                          *yp = vmult;
351                  }
352 +                if (rvrlim == 0)                /* initialize recovery limit */
353 +                        rvrlim = *xp*vmult + *yp;
354 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
355 +                        sflock(F_UNLCK);
356 +                        return(1);
357 +                }
358                  if (--(*yp) < 0) {              /* decrement position */
359                          *yp = vmult-1;
360 <                        if (--(*xp) < 0) {      /* all done! */
361 <                                close(syncfd);
360 >                        if (--(*xp) < 0) {      /* all done */
361 >                                sflock(F_UNLCK);
362                                  return(0);
363                          }
364                  }
365 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
366 <                lseek(syncfd, 0L, 0);           /* write new position */
367 <                write(syncfd, buf, strlen(buf));
368 <                fls.l_type = F_UNLCK;           /* release sync file */
252 <                fcntl(syncfd, F_SETLKW, &fls);
365 >                fseek(syncfp, 0L, 0);           /* write new position */
366 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
367 >                fflush(syncfp);
368 >                sflock(F_UNLCK);                /* release sync file */
369                  return(1);
370          }
371 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
256 <                return(0);
257 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
258 <                return(1);
259 <        fprintf(stderr, "%s: input format error\n", progname);
260 <        exit(cleanup(1));
371 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
372   }
373  
374  
375 < int
376 < cleanup(rstat)                  /* close rpict process and clean up */
377 < int  rstat;
375 > static int
376 > rvrpiece(               /* check for recoverable pieces */
377 >        register int    *xp,
378 >        register int    *yp
379 > )
380   {
381 +        static char  *pdone = NULL;     /* which pieces are done */
382 +        static long  readpos = -1;      /* how far we've read */
383 +        register int  i;
384 +        /*
385 +         * This routine is called by nextpiece() with an
386 +         * exclusive lock on syncfp and the file pointer at the
387 +         * appropriate position to read in the finished pieces.
388 +         */
389 +        if (rvrlim < 0)
390 +                return(0);              /* only check if asked */
391 +        if (pdone == NULL)              /* first call */
392 +                pdone = calloc(hmult*vmult, sizeof(char));
393 +        if (pdone == NULL) {
394 +                fprintf(stderr, "%s: out of memory\n", progname);
395 +                exit(1);
396 +        }
397 +        if (readpos != -1)              /* mark what's been done */
398 +                fseek(syncfp, readpos, 0);
399 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
400 +                pdone[*xp*vmult+*yp] = 1;
401 +        if (!feof(syncfp)) {
402 +                fprintf(stderr, "%s: format error in sync file\n", progname);
403 +                exit(1);
404 +        }
405 +        readpos = ftell(syncfp);
406 +        i = hmult*vmult;                /* find an unaccounted for piece */
407 +        while (i-- > rvrlim)
408 +                if (!pdone[i]) {
409 +                        *xp = i / vmult;
410 +                        *yp = i % vmult;
411 +                        pdone[i] = 1;   /* consider it done */
412 +                        return(1);
413 +                }
414 +        rvrlim = -1;                    /* nothing left to recover */
415 +        free(pdone);
416 +        pdone = NULL;
417 +        return(0);
418 + }
419 +
420 +
421 + static int
422 + cleanup(                        /* close rpict process and clean up */
423 +        int  rstat
424 + )
425 + {
426          int  status;
427  
428 <        free((char *)pbuf);
428 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
429          fclose(torp);
430          fclose(fromrp);
431          while (wait(&status) != -1)
# Line 277 | Line 435 | int  rstat;
435   }
436  
437  
438 < rpiece()                        /* render picture piece by piece */
438 > static void
439 > rpiece(void)                    /* render picture piece by piece */
440   {
441          VIEW  pview;
442          int  xorg, yorg;
443                                          /* compute view parameters */
444 <        copystruct(&pview, &ourview);
444 >        pview = ourview;
445          switch (ourview.type) {
446          case VT_PER:
447 <                pview.horiz = 2.*180./PI*atan(
448 <                                tan(PI/180./2.*ourview.horiz)/hmult );
449 <                pview.vert = 2.*180./PI*atan(
450 <                                tan(PI/180./2.*ourview.vert)/vmult );
447 >                pview.horiz = (2.*180./PI)*atan(
448 >                                tan((PI/180./2.)*ourview.horiz)/hmult );
449 >                pview.vert = (2.*180./PI)*atan(
450 >                                tan((PI/180./2.)*ourview.vert)/vmult );
451                  break;
452          case VT_PAR:
453          case VT_ANG:
454                  pview.horiz = ourview.horiz / hmult;
455                  pview.vert = ourview.vert / vmult;
456                  break;
457 +        case VT_CYL:
458 +                pview.horiz = ourview.horiz / hmult;
459 +                pview.vert = (2.*180./PI)*atan(
460 +                                tan((PI/180./2.)*ourview.vert)/vmult );
461 +                break;
462          case VT_HEM:
463 <                pview.horiz = 2.*180./PI*asin(
464 <                                sin(PI/180./2.*ourview.horiz)/hmult );
465 <                pview.vert = 2.*180./PI*asin(
466 <                                sin(PI/180./2.*ourview.vert)/vmult );
463 >                pview.horiz = (2.*180./PI)*asin(
464 >                                sin((PI/180./2.)*ourview.horiz)/hmult );
465 >                pview.vert = (2.*180./PI)*asin(
466 >                                sin((PI/180./2.)*ourview.vert)/vmult );
467                  break;
468 +        case VT_PLS:
469 +                pview.horiz = sin((PI/180./2.)*ourview.horiz) /
470 +                                (1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult;
471 +                pview.horiz *= pview.horiz;
472 +                pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) /
473 +                                                (1. + pview.horiz));
474 +                pview.vert = sin((PI/180./2.)*ourview.vert) /
475 +                                (1.0 + cos((PI/180./2.)*ourview.vert)) / vmult;
476 +                pview.vert *= pview.vert;
477 +                pview.vert = (2.*180./PI)*acos((1. - pview.vert) /
478 +                                                (1. + pview.vert));
479 +                break;
480          default:
481                  fprintf(stderr, "%s: unknown view type '-vt%c'\n",
482                                  progname, ourview.type);
# Line 308 | Line 484 | rpiece()                       /* render picture piece by piece */
484          }
485                                          /* render each piece */
486          while (nextpiece(&xorg, &yorg)) {
487 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
488 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
487 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
488 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
489                  fputs(VIEWSTR, torp);
490                  fprintview(&pview, torp);
491                  putc('\n', torp);
492                  fflush(torp);                   /* assigns piece to rpict */
493                  putpiece(xorg, yorg);           /* place piece in output */
318                if (verbose) {                  /* notify caller */
319                        printf("%d %d done\n", xorg, yorg);
320                        fflush(stdout);
321                }
494          }
495   }
496  
497  
498 < int
499 < putpiece(xpos, ypos)            /* get next piece from rpict */
500 < int  xpos, ypos;
498 > static int
499 > putpiece(               /* get next piece from rpict */
500 > int     xpos,
501 > int     ypos
502 > )
503   {
504          struct flock  fls;
505          int  pid, status;
506          int  hr, vr;
507          register int  y;
508                                  /* check bounds */
509 <        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
509 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
510                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
511                                  progname, xpos, ypos);
512                  exit(cleanup(1));
513          }
514                                  /* check header from rpict */
515 +        guard_io();
516          getheader(fromrp, NULL, NULL);
517 <        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
517 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
518                  fprintf(stderr, "%s: resolution mismatch from %s\n",
519                                  progname, rpargv[0]);
520                  exit(cleanup(1));
521          }
522 +        if (verbose) {                          /* notify caller */
523 +                printf("%d %d begun\n", xpos, ypos);
524 +                fflush(stdout);
525 +        }
526 +        unguard();
527                                  /* load new piece into buffer */
528 <        for (y = 0; y < vr; y++)
528 >        for (y = 0; y < vr; y++) {
529 >                guard_io();
530                  if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
531                          fprintf(stderr, "%s: read error from %s\n",
532                                          progname, rpargv[0]);
533                          exit(cleanup(1));
534                  }
535 +                unguard();
536 +        }
537   #if MAXFORK
538                                  /* fork so we don't slow rpict down */
539          if ((pid = fork()) > 0) {
# Line 365 | Line 548 | int  xpos, ypos;
548   #else
549          pid = -1;               /* no forking */
550   #endif
551 <        fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
552 <        fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
551 >        fls.l_start = scanorig +
552 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
553   #if NFS
554 +        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
555                                  /* lock file section so NFS doesn't mess up */
556          fls.l_whence = 0;
557          fls.l_type = F_WRLCK;
558 <        fcntl(outfd, F_SETLKW, &fls);
558 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
559 >                filerr("lock");
560   #endif
561                                  /* write new piece to file */
562 <        if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
563 <                goto seekerr;
562 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
563 >                filerr("seek");
564          if (hmult == 1) {
565                  if (writebuf(outfd, (char *)pbuf,
566                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
567 <                        goto writerr;
567 >                        filerr("write");
568          } else
569                  for (y = 0; y < vr; y++) {
570                          if (writebuf(outfd, (char *)(pbuf+y*hr),
571                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
572 <                                goto writerr;
572 >                                filerr("write");
573                          if (y < vr-1 && lseek(outfd,
574 <                                        (long)(hmult-1)*hr*sizeof(COLR),
575 <                                        1) == -1)
576 <                                goto seekerr;
574 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
575 >                                        SEEK_CUR) < 0)
576 >                                filerr("seek");
577                  }
393        if (pid == -1) {        /* fork failed */
578   #if NFS
579 <                fls.l_type = F_UNLCK;           /* release lock */
580 <                fcntl(outfd, F_SETLKW, &fls);
579 >        fls.l_type = F_UNLCK;           /* release lock */
580 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
581 >                filerr("lock");
582   #endif
583 <                return(0);
583 >        if (verbose) {                          /* notify caller */
584 >                printf("%d %d done\n", xpos, ypos);
585 >                fflush(stdout);
586          }
587 <        _exit(0);               /* else exit child process (releasing lock) */
588 < seekerr:
589 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
587 >        if (syncfp != NULL) {                   /* record what's been done */
588 >                sflock(F_WRLCK);
589 >                fseek(syncfp, 0L, 2);           /* append index */
590 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
591 >                fflush(syncfp);
592 >                                /*** Unlock not necessary, since
593 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
594 >        }
595 >        if (pid == -1)          /* didn't fork or fork failed */
596 >                return(0);
597 >        _exit(0);               /* else exit child process (releasing locks) */
598 > }
599 >
600 >
601 > static void
602 > filerr(                 /* report file error and exit */
603 >        char  *t
604 > )
605 > {
606 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
607 >                        progname, t, outfile, strerror(errno));
608          _exit(1);
404 writerr:
405        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
406        _exit(1);
609   }
610 +
611 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines