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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines