ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.53
Committed: Sat Jan 21 22:04:02 2012 UTC (12 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.52: +20 -3 lines
Log Message:
Added warning about altered output resolution

File Contents

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