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.21 by greg, Fri Jun 4 17:03:29 1993 UTC vs.
Revision 2.66 by greg, Thu Sep 12 23:14:32 2024 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines