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> |
9 |
> |
|
10 |
> |
#ifndef F_SETLKW |
11 |
> |
|
12 |
> |
main(argc, argv) |
13 |
> |
int argc; |
14 |
> |
char *argv[]; |
15 |
> |
{ |
16 |
> |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
17 |
> |
exit(1); |
18 |
> |
} |
19 |
> |
|
20 |
> |
#else |
21 |
> |
|
22 |
|
#include <signal.h> |
23 |
+ |
|
24 |
|
#include "color.h" |
25 |
|
#include "view.h" |
26 |
< |
#include "resolu.h" |
26 |
> |
#include "rtprocess.h" |
27 |
|
|
28 |
|
#ifndef NFS |
29 |
|
#define NFS 1 |
36 |
|
#define MAXFORK 0 |
37 |
|
#endif |
38 |
|
#endif |
39 |
+ |
/* protection from SYSV signals(!) */ |
40 |
+ |
#if defined(sgi) |
41 |
+ |
#define guard_io() sighold(SIGALRM) |
42 |
+ |
#define unguard() sigrelse(SIGALRM) |
43 |
+ |
#endif |
44 |
+ |
#ifndef guard_io |
45 |
+ |
#define guard_io() 0 |
46 |
+ |
#define unguard() 0 |
47 |
+ |
#endif |
48 |
|
|
49 |
+ |
extern char *strerror(); |
50 |
+ |
|
51 |
|
/* rpict command */ |
52 |
< |
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
53 |
< |
int rpargc = 9; |
33 |
< |
int rpd[3]; |
52 |
> |
char *rpargv[128] = {"rpict", "-S", "1"}; |
53 |
> |
int rpargc = 3; |
54 |
|
FILE *torp, *fromrp; |
55 |
|
COLR *pbuf; |
56 |
|
/* our view parameters */ |
57 |
|
VIEW ourview = STDVIEW; |
58 |
|
double pixaspect = 1.0; |
59 |
< |
int hres = 512, vres = 512, hmult = 2, vmult = 2; |
59 |
> |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
60 |
|
/* output file */ |
61 |
|
char *outfile = NULL; |
62 |
|
int outfd; |
63 |
|
long scanorig; |
64 |
< |
int syncfd = -1; /* lock file descriptor */ |
64 |
> |
FILE *syncfp = NULL; /* synchronization file pointer */ |
65 |
> |
int synclst = F_UNLCK; /* synchronization file lock status */ |
66 |
|
int nforked = 0; |
67 |
|
|
68 |
+ |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
69 |
+ |
|
70 |
|
char *progname; |
71 |
|
int verbose = 0; |
72 |
+ |
unsigned timelim = 0; |
73 |
+ |
int rvrlim = -1; |
74 |
|
|
50 |
– |
extern long lseek(), ftell(); |
51 |
– |
|
75 |
|
int gotalrm = 0; |
76 |
< |
int onalrm() { gotalrm++; } |
76 |
> |
void onalrm(int i) { gotalrm++; } |
77 |
|
|
78 |
|
|
79 |
|
main(argc, argv) |
84 |
|
|
85 |
|
progname = argv[0]; |
86 |
|
for (i = 1; i < argc; i++) { |
87 |
+ |
/* expand arguments */ |
88 |
+ |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
89 |
+ |
; |
90 |
+ |
if (rval < 0) { |
91 |
+ |
fprintf(stderr, "%s: cannot expand '%s'", |
92 |
+ |
argv[0], argv[i]); |
93 |
+ |
exit(1); |
94 |
+ |
} |
95 |
|
if (argv[i][0] == '-') |
96 |
|
switch (argv[i][1]) { |
97 |
|
case 'v': |
116 |
|
} |
117 |
|
break; |
118 |
|
case 'p': /* pixel aspect ratio? */ |
119 |
< |
if (argv[i][2] == 'a' && !argv[i][3]) |
120 |
< |
pixaspect = atof(argv[i+1]); |
119 |
> |
if (argv[i][2] != 'a' || argv[i][3]) |
120 |
> |
break; |
121 |
> |
pixaspect = atof(argv[++i]); |
122 |
> |
continue; |
123 |
> |
case 'T': /* time limit (hours) */ |
124 |
> |
if (argv[i][2]) |
125 |
> |
break; |
126 |
> |
timelim = atof(argv[++i])*3600. + .5; |
127 |
|
break; |
128 |
< |
case 'x': /* piece x resolution */ |
129 |
< |
if (!argv[i][2]) |
130 |
< |
hres = atoi(argv[i+1]); |
131 |
< |
break; |
132 |
< |
case 'y': /* piece y resolution */ |
133 |
< |
if (!argv[i][2]) |
134 |
< |
vres = atoi(argv[i+1]); |
135 |
< |
break; |
128 |
> |
case 'x': /* overall x resolution */ |
129 |
> |
if (argv[i][2]) |
130 |
> |
break; |
131 |
> |
hres = atoi(argv[++i]); |
132 |
> |
continue; |
133 |
> |
case 'y': /* overall y resolution */ |
134 |
> |
if (argv[i][2]) |
135 |
> |
break; |
136 |
> |
vres = atoi(argv[++i]); |
137 |
> |
continue; |
138 |
|
case 'X': /* horizontal multiplier */ |
139 |
|
if (argv[i][2]) |
140 |
|
break; |
145 |
|
break; |
146 |
|
vmult = atoi(argv[++i]); |
147 |
|
continue; |
148 |
+ |
case 'R': /* recover */ |
149 |
+ |
if (argv[i][2]) |
150 |
+ |
break; |
151 |
+ |
rvrlim = 0; |
152 |
+ |
/* fall through */ |
153 |
|
case 'F': /* syncronization file */ |
154 |
|
if (argv[i][2]) |
155 |
|
break; |
156 |
< |
if ((syncfd = open(argv[++i], |
157 |
< |
O_RDWR|O_CREAT, 0666)) < 0) { |
156 |
> |
if ((syncfp = |
157 |
> |
fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) { |
158 |
|
fprintf(stderr, "%s: cannot open\n", |
159 |
|
argv[i]); |
160 |
|
exit(1); |
169 |
|
break; |
170 |
|
outfile = argv[++i]; |
171 |
|
continue; |
172 |
< |
} |
172 |
> |
} else if (i >= argc-1) |
173 |
> |
break; |
174 |
|
rpargv[rpargc++] = argv[i]; |
175 |
|
} |
176 |
< |
rpargv[rpargc] = NULL; |
176 |
> |
if (i >= argc) { |
177 |
> |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
178 |
> |
exit(1); |
179 |
> |
} |
180 |
|
if (outfile == NULL) { |
181 |
|
fprintf(stderr, "%s: missing output file\n", argv[0]); |
182 |
|
exit(1); |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
+ |
dolock(fd, ltyp) /* lock or unlock a file */ |
191 |
+ |
int fd; |
192 |
+ |
int ltyp; |
193 |
+ |
{ |
194 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
195 |
+ |
|
196 |
+ |
fls.l_type = ltyp; |
197 |
+ |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
198 |
+ |
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
199 |
+ |
progname, strerror(errno)); |
200 |
+ |
exit(1); |
201 |
+ |
} |
202 |
+ |
} |
203 |
+ |
|
204 |
+ |
|
205 |
|
init(ac, av) /* set up output file and start rpict */ |
206 |
|
int ac; |
207 |
|
char **av; |
208 |
|
{ |
209 |
+ |
static char hrbuf[16], vrbuf[16]; |
210 |
|
extern char VersionID[]; |
211 |
|
char *err; |
212 |
|
FILE *fp; |
213 |
|
int hr, vr; |
214 |
+ |
SUBPROC rpd; /* since we don't close_process(), this can be local */ |
215 |
|
/* set up view */ |
216 |
|
if ((err = setview(&ourview)) != NULL) { |
217 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
218 |
|
exit(1); |
219 |
|
} |
220 |
< |
if (syncfd != -1) { |
221 |
< |
char buf[32]; |
222 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
223 |
< |
sscanf(buf, "%d %d", &hmult, &vmult); |
220 |
> |
if (syncfp != NULL) { |
221 |
> |
sflock(F_RDLCK); |
222 |
> |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
223 |
> |
sflock(F_UNLCK); |
224 |
|
} |
225 |
+ |
/* compute piece size */ |
226 |
+ |
hres /= hmult; |
227 |
+ |
vres /= vmult; |
228 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
229 |
+ |
sprintf(hrbuf, "%d", hres); |
230 |
+ |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
231 |
+ |
sprintf(vrbuf, "%d", vres); |
232 |
+ |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
233 |
+ |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
234 |
+ |
rpargv[rpargc++] = av[ac-1]; |
235 |
+ |
rpargv[rpargc] = NULL; |
236 |
|
/* open output file */ |
237 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
238 |
+ |
dolock(outfd, F_WRLCK); |
239 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
240 |
|
goto filerr; |
241 |
< |
printargs(ac, av, fp); /* write header */ |
241 |
> |
newheader("RADIANCE", fp); /* create header */ |
242 |
> |
printargs(ac, av, fp); |
243 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
244 |
|
fputs(VIEWSTR, fp); |
245 |
|
fprintview(&ourview, fp); |
250 |
|
putc('\n', fp); |
251 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
252 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
253 |
+ |
dolock(outfd, F_RDLCK); |
254 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
255 |
|
goto filerr; |
256 |
|
getheader(fp, NULL, NULL); /* skip header */ |
268 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
269 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
270 |
|
goto filerr; |
271 |
< |
#if NFS |
194 |
< |
sync(); /* flush NFS buffers */ |
195 |
< |
#endif |
271 |
> |
dolock(outfd, F_UNLCK); |
272 |
|
/* start rpict process */ |
273 |
< |
if (open_process(rpd, rpargv) <= 0) { |
273 |
> |
if (open_process(&rpd, rpargv) <= 0) { |
274 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
275 |
|
exit(1); |
276 |
|
} |
277 |
< |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
278 |
< |
(torp = fdopen(rpd[1], "w")) == NULL) { |
277 |
> |
if ((fromrp = fdopen(rpd.r, "r")) == NULL || |
278 |
> |
(torp = fdopen(rpd.w, "w")) == NULL) { |
279 |
|
fprintf(stderr, "%s: cannot open stream to %s\n", |
280 |
|
progname, rpargv[0]); |
281 |
|
exit(1); |
282 |
|
} |
283 |
< |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
283 |
> |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
284 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
285 |
|
exit(1); |
286 |
|
} |
287 |
|
signal(SIGALRM, onalrm); |
288 |
+ |
if (timelim) |
289 |
+ |
alarm(timelim); |
290 |
|
return; |
291 |
|
filerr: |
292 |
|
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
298 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
299 |
|
int *xp, *yp; |
300 |
|
{ |
223 |
– |
extern char *fgets(); |
224 |
– |
struct flock fls; |
225 |
– |
char buf[64]; |
226 |
– |
|
301 |
|
if (gotalrm) /* someone wants us to quit */ |
302 |
|
return(0); |
303 |
< |
if (syncfd != -1) { /* use sync file */ |
304 |
< |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
305 |
< |
fls.l_whence = 0; |
306 |
< |
fls.l_start = 0L; |
307 |
< |
fls.l_len = 0L; |
308 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
309 |
< |
lseek(syncfd, 0L, 0); |
310 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
311 |
< |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
303 |
> |
if (syncfp != NULL) { /* use sync file */ |
304 |
> |
/* |
305 |
> |
* So we don't necessarily have to lock and unlock the file |
306 |
> |
* multiple times (very slow), we establish an exclusive |
307 |
> |
* lock at the beginning on our synchronization file and |
308 |
> |
* maintain it in the subroutine rvrpiece(). |
309 |
> |
*/ |
310 |
> |
sflock(F_WRLCK); |
311 |
> |
fseek(syncfp, 0L, 0); /* read position */ |
312 |
> |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
313 |
|
*xp = hmult-1; |
314 |
|
*yp = vmult; |
315 |
|
} |
316 |
+ |
if (rvrlim == 0) /* initialize recovery limit */ |
317 |
+ |
rvrlim = *xp*vmult + *yp; |
318 |
+ |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
319 |
+ |
sflock(F_UNLCK); |
320 |
+ |
return(1); |
321 |
+ |
} |
322 |
|
if (--(*yp) < 0) { /* decrement position */ |
323 |
|
*yp = vmult-1; |
324 |
< |
if (--(*xp) < 0) { /* all done! */ |
325 |
< |
close(syncfd); |
324 |
> |
if (--(*xp) < 0) { /* all done */ |
325 |
> |
sflock(F_UNLCK); |
326 |
|
return(0); |
327 |
|
} |
328 |
|
} |
329 |
< |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
330 |
< |
lseek(syncfd, 0L, 0); /* write new position */ |
331 |
< |
write(syncfd, buf, strlen(buf)); |
332 |
< |
fls.l_type = F_UNLCK; /* release sync file */ |
252 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
329 |
> |
fseek(syncfp, 0L, 0); /* write new position */ |
330 |
> |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
331 |
> |
fflush(syncfp); |
332 |
> |
sflock(F_UNLCK); /* release sync file */ |
333 |
|
return(1); |
334 |
|
} |
335 |
< |
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)); |
335 |
> |
return(scanf("%d %d", xp, yp) == 2); /* use stdin */ |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
int |
340 |
+ |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
341 |
+ |
register int *xp, *yp; |
342 |
+ |
{ |
343 |
+ |
static char *pdone = NULL; /* which pieces are done */ |
344 |
+ |
static long readpos = -1; /* how far we've read */ |
345 |
+ |
register int i; |
346 |
+ |
/* |
347 |
+ |
* This routine is called by nextpiece() with an |
348 |
+ |
* exclusive lock on syncfp and the file pointer at the |
349 |
+ |
* appropriate position to read in the finished pieces. |
350 |
+ |
*/ |
351 |
+ |
if (rvrlim < 0) |
352 |
+ |
return(0); /* only check if asked */ |
353 |
+ |
if (pdone == NULL) /* first call */ |
354 |
+ |
pdone = calloc(hmult*vmult, sizeof(char)); |
355 |
+ |
if (pdone == NULL) { |
356 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
357 |
+ |
exit(1); |
358 |
+ |
} |
359 |
+ |
if (readpos != -1) /* mark what's been done */ |
360 |
+ |
fseek(syncfp, readpos, 0); |
361 |
+ |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
362 |
+ |
pdone[*xp*vmult+*yp] = 1; |
363 |
+ |
if (!feof(syncfp)) { |
364 |
+ |
fprintf(stderr, "%s: format error in sync file\n", progname); |
365 |
+ |
exit(1); |
366 |
+ |
} |
367 |
+ |
readpos = ftell(syncfp); |
368 |
+ |
i = hmult*vmult; /* find an unaccounted for piece */ |
369 |
+ |
while (i-- > rvrlim) |
370 |
+ |
if (!pdone[i]) { |
371 |
+ |
*xp = i / vmult; |
372 |
+ |
*yp = i % vmult; |
373 |
+ |
pdone[i] = 1; /* consider it done */ |
374 |
+ |
return(1); |
375 |
+ |
} |
376 |
+ |
rvrlim = -1; /* nothing left to recover */ |
377 |
+ |
free(pdone); |
378 |
+ |
pdone = NULL; |
379 |
+ |
return(0); |
380 |
+ |
} |
381 |
+ |
|
382 |
+ |
|
383 |
+ |
int |
384 |
|
cleanup(rstat) /* close rpict process and clean up */ |
385 |
|
int rstat; |
386 |
|
{ |
387 |
|
int status; |
388 |
|
|
389 |
< |
free((char *)pbuf); |
389 |
> |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
390 |
|
fclose(torp); |
391 |
|
fclose(fromrp); |
392 |
|
while (wait(&status) != -1) |
401 |
|
VIEW pview; |
402 |
|
int xorg, yorg; |
403 |
|
/* compute view parameters */ |
404 |
< |
copystruct(&pview, &ourview); |
404 |
> |
pview = ourview; |
405 |
|
switch (ourview.type) { |
406 |
|
case VT_PER: |
407 |
|
pview.horiz = 2.*180./PI*atan( |
414 |
|
pview.horiz = ourview.horiz / hmult; |
415 |
|
pview.vert = ourview.vert / vmult; |
416 |
|
break; |
417 |
+ |
case VT_CYL: |
418 |
+ |
pview.horiz = ourview.horiz / hmult; |
419 |
+ |
pview.vert = 2.*180./PI*atan( |
420 |
+ |
tan(PI/180./2.*ourview.vert)/vmult ); |
421 |
+ |
break; |
422 |
|
case VT_HEM: |
423 |
|
pview.horiz = 2.*180./PI*asin( |
424 |
|
sin(PI/180./2.*ourview.horiz)/hmult ); |
432 |
|
} |
433 |
|
/* render each piece */ |
434 |
|
while (nextpiece(&xorg, &yorg)) { |
435 |
< |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
436 |
< |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
435 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
436 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
437 |
|
fputs(VIEWSTR, torp); |
438 |
|
fprintview(&pview, torp); |
439 |
|
putc('\n', torp); |
440 |
|
fflush(torp); /* assigns piece to rpict */ |
441 |
|
putpiece(xorg, yorg); /* place piece in output */ |
318 |
– |
if (verbose) { /* notify caller */ |
319 |
– |
printf("%d %d done\n", xorg, yorg); |
320 |
– |
fflush(stdout); |
321 |
– |
} |
442 |
|
} |
443 |
|
} |
444 |
|
|
452 |
|
int hr, vr; |
453 |
|
register int y; |
454 |
|
/* check bounds */ |
455 |
< |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
455 |
> |
if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { |
456 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
457 |
|
progname, xpos, ypos); |
458 |
|
exit(cleanup(1)); |
459 |
|
} |
460 |
|
/* check header from rpict */ |
461 |
+ |
guard_io(); |
462 |
|
getheader(fromrp, NULL, NULL); |
463 |
< |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
463 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) { |
464 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
465 |
|
progname, rpargv[0]); |
466 |
|
exit(cleanup(1)); |
467 |
|
} |
468 |
+ |
if (verbose) { /* notify caller */ |
469 |
+ |
printf("%d %d begun\n", xpos, ypos); |
470 |
+ |
fflush(stdout); |
471 |
+ |
} |
472 |
+ |
unguard(); |
473 |
|
/* load new piece into buffer */ |
474 |
< |
for (y = 0; y < vr; y++) |
474 |
> |
for (y = 0; y < vr; y++) { |
475 |
> |
guard_io(); |
476 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
477 |
|
fprintf(stderr, "%s: read error from %s\n", |
478 |
|
progname, rpargv[0]); |
479 |
|
exit(cleanup(1)); |
480 |
|
} |
481 |
+ |
unguard(); |
482 |
+ |
} |
483 |
|
#if MAXFORK |
484 |
|
/* fork so we don't slow rpict down */ |
485 |
|
if ((pid = fork()) > 0) { |
494 |
|
#else |
495 |
|
pid = -1; /* no forking */ |
496 |
|
#endif |
497 |
< |
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
498 |
< |
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
497 |
> |
fls.l_start = scanorig + |
498 |
> |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
499 |
|
#if NFS |
500 |
+ |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
501 |
|
/* lock file section so NFS doesn't mess up */ |
502 |
|
fls.l_whence = 0; |
503 |
|
fls.l_type = F_WRLCK; |
504 |
< |
fcntl(outfd, F_SETLKW, &fls); |
504 |
> |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
505 |
> |
filerr("lock"); |
506 |
|
#endif |
507 |
|
/* write new piece to file */ |
508 |
< |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
509 |
< |
goto seekerr; |
508 |
> |
if (lseek(outfd, (off_t)fls.l_start, 0) < 0) |
509 |
> |
filerr("seek"); |
510 |
|
if (hmult == 1) { |
511 |
|
if (writebuf(outfd, (char *)pbuf, |
512 |
|
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
513 |
< |
goto writerr; |
513 |
> |
filerr("write"); |
514 |
|
} else |
515 |
|
for (y = 0; y < vr; y++) { |
516 |
|
if (writebuf(outfd, (char *)(pbuf+y*hr), |
517 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
518 |
< |
goto writerr; |
518 |
> |
filerr("write"); |
519 |
|
if (y < vr-1 && lseek(outfd, |
520 |
< |
(long)(hmult-1)*hr*sizeof(COLR), |
521 |
< |
1) == -1) |
522 |
< |
goto seekerr; |
520 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
521 |
> |
1) < 0) |
522 |
> |
filerr("seek"); |
523 |
|
} |
393 |
– |
if (pid == -1) { /* fork failed */ |
524 |
|
#if NFS |
525 |
< |
fls.l_type = F_UNLCK; /* release lock */ |
526 |
< |
fcntl(outfd, F_SETLKW, &fls); |
525 |
> |
fls.l_type = F_UNLCK; /* release lock */ |
526 |
> |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
527 |
> |
filerr("lock"); |
528 |
|
#endif |
529 |
< |
return(0); |
529 |
> |
if (verbose) { /* notify caller */ |
530 |
> |
printf("%d %d done\n", xpos, ypos); |
531 |
> |
fflush(stdout); |
532 |
|
} |
533 |
< |
_exit(0); /* else exit child process (releasing lock) */ |
534 |
< |
seekerr: |
535 |
< |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
533 |
> |
if (syncfp != NULL) { /* record what's been done */ |
534 |
> |
sflock(F_WRLCK); |
535 |
> |
fseek(syncfp, 0L, 2); /* append index */ |
536 |
> |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
537 |
> |
fflush(syncfp); |
538 |
> |
/*** Unlock not necessary, since |
539 |
> |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
540 |
> |
} |
541 |
> |
if (pid == -1) /* didn't fork or fork failed */ |
542 |
> |
return(0); |
543 |
> |
_exit(0); /* else exit child process (releasing locks) */ |
544 |
> |
} |
545 |
> |
|
546 |
> |
|
547 |
> |
filerr(t) /* report file error and exit */ |
548 |
> |
char *t; |
549 |
> |
{ |
550 |
> |
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
551 |
> |
progname, t, outfile, strerror(errno)); |
552 |
|
_exit(1); |
404 |
– |
writerr: |
405 |
– |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
406 |
– |
_exit(1); |
553 |
|
} |
554 |
+ |
|
555 |
+ |
#endif |