46 |
|
|
47 |
|
extern long ftell(), lseek(); |
48 |
|
static int initambfile(), avsave(), avinsert(); |
49 |
+ |
#ifdef F_SETLKW |
50 |
+ |
static aflock(); |
51 |
+ |
#endif |
52 |
|
|
53 |
|
|
54 |
|
setambres(ar) /* set ambient resolution */ |
76 |
|
AMBVAL amb; |
77 |
|
/* init ambient limits */ |
78 |
|
setambres(ambres); |
79 |
+ |
if (afile == NULL) |
80 |
+ |
return; |
81 |
|
/* open ambient file */ |
82 |
< |
if (afile != NULL) { |
83 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
84 |
< |
initambfile(0); |
85 |
< |
headlen = ftell(ambfp); |
86 |
< |
while (readambval(&amb, ambfp)) |
87 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
88 |
< |
thescene.cusize); |
89 |
< |
/* align */ |
90 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
91 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
92 |
< |
initambfile(1); |
93 |
< |
else { |
94 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
90 |
< |
afile); |
91 |
< |
error(SYSTEM, errmsg); |
92 |
< |
} |
93 |
< |
nunflshed++; /* lie */ |
94 |
< |
ambsync(); |
82 |
> |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
83 |
> |
initambfile(0); |
84 |
> |
headlen = ftell(ambfp); |
85 |
> |
while (readambval(&amb, ambfp)) |
86 |
> |
avinsert(&amb, &atrunk, thescene.cuorg, |
87 |
> |
thescene.cusize); |
88 |
> |
/* align */ |
89 |
> |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
90 |
> |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
91 |
> |
initambfile(1); |
92 |
> |
else { |
93 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
94 |
> |
error(SYSTEM, errmsg); |
95 |
|
} |
96 |
+ |
nunflshed++; /* lie */ |
97 |
+ |
ambsync(); |
98 |
|
} |
99 |
|
|
100 |
|
|
310 |
|
{ |
311 |
|
extern char *progname, *octname, VersionID[]; |
312 |
|
|
313 |
+ |
#ifdef F_SETLKW |
314 |
+ |
aflock(creat ? F_WRLCK : F_RDLCK); |
315 |
+ |
#endif |
316 |
|
#ifdef MSDOS |
317 |
|
setmode(fileno(ambfp), O_BINARY); |
318 |
|
#endif |
391 |
|
|
392 |
|
#ifdef F_SETLKW |
393 |
|
|
394 |
+ |
static |
395 |
+ |
aflock(typ) /* lock/unlock ambient file */ |
396 |
+ |
int typ; |
397 |
+ |
{ |
398 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
399 |
+ |
|
400 |
+ |
fls.l_type = typ; |
401 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
402 |
+ |
error(SYSTEM, "cannot (un)lock ambient file"); |
403 |
+ |
} |
404 |
+ |
|
405 |
+ |
|
406 |
|
int |
407 |
|
ambsync() /* synchronize ambient file */ |
408 |
|
{ |
409 |
|
static FILE *ambinp = NULL; |
410 |
|
static long lastpos = -1; |
394 |
– |
struct flock fls; |
411 |
|
long flen; |
412 |
|
AMBVAL avs; |
413 |
|
register int n; |
414 |
|
|
415 |
|
if (nunflshed == 0) |
416 |
|
return(0); |
417 |
< |
/* gain exclusive access */ |
402 |
< |
fls.l_type = F_WRLCK; |
403 |
< |
fls.l_whence = 0; |
404 |
< |
fls.l_start = 0L; |
405 |
< |
fls.l_len = 0L; |
406 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
407 |
< |
error(SYSTEM, "cannot lock ambient file"); |
408 |
< |
if (lastpos < 0) /* initializing */ |
417 |
> |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
418 |
|
goto syncend; |
419 |
+ |
/* gain exclusive access */ |
420 |
+ |
aflock(F_WRLCK); |
421 |
|
/* see if file has grown */ |
422 |
|
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
423 |
|
error(SYSTEM, "cannot seek on ambient file"); |
440 |
|
syncend: |
441 |
|
n = fflush(ambfp); /* calls write() at last */ |
442 |
|
lastpos = lseek(fileno(ambfp), 0L, 1); |
443 |
< |
fls.l_type = F_UNLCK; /* release file */ |
433 |
< |
fcntl(fileno(ambfp), F_SETLKW, &fls); |
443 |
> |
aflock(F_UNLCK); /* release file */ |
444 |
|
nunflshed = 0; |
445 |
|
return(n); |
446 |
|
} |