35 |
|
|
36 |
|
static AMBTREE atrunk; /* our ambient trunk node */ |
37 |
|
|
38 |
– |
static char *ambfname = NULL; /* ambient file name */ |
38 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
39 |
+ |
static int nunflshed = 0; /* number of unflushed ambient values */ |
40 |
|
|
41 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
42 |
|
|
45 |
|
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
|
|
47 |
|
extern long ftell(), lseek(); |
48 |
< |
static int initambfile(), avsave(), avinsert(), ambsync(); |
48 |
> |
static int initambfile(), avsave(), avinsert(); |
49 |
|
|
50 |
|
|
51 |
|
setambres(ar) /* set ambient resolution */ |
74 |
|
/* init ambient limits */ |
75 |
|
setambres(ambres); |
76 |
|
/* open ambient file */ |
77 |
< |
if ((ambfname = afile) != NULL) |
77 |
> |
if (afile != NULL) { |
78 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
79 |
|
initambfile(0); |
80 |
|
headlen = ftell(ambfp); |
90 |
|
afile); |
91 |
|
error(SYSTEM, errmsg); |
92 |
|
} |
93 |
+ |
nunflshed++; /* lie */ |
94 |
+ |
ambsync(); |
95 |
+ |
} |
96 |
|
} |
97 |
|
|
98 |
|
|
326 |
|
putambmagic(ambfp); |
327 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
328 |
|
error(USER, "bad ambient file"); |
326 |
– |
ambsync(); |
329 |
|
} |
330 |
|
|
331 |
|
|
333 |
|
avsave(av) /* insert and save an ambient value */ |
334 |
|
AMBVAL *av; |
335 |
|
{ |
334 |
– |
static int nunflshed = 0; |
335 |
– |
|
336 |
|
avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); |
337 |
|
if (ambfp == NULL) |
338 |
|
return; |
339 |
|
if (writambval(av, ambfp) < 0) |
340 |
|
goto writerr; |
341 |
< |
if (++nunflshed >= AMBFLUSH) { |
341 |
> |
if (++nunflshed >= AMBFLUSH) |
342 |
|
if (ambsync() == EOF) |
343 |
|
goto writerr; |
344 |
– |
nunflshed = 0; |
345 |
– |
} |
344 |
|
return; |
345 |
|
writerr: |
346 |
|
error(SYSTEM, "error writing ambient file"); |
386 |
|
|
387 |
|
#ifdef NIX |
388 |
|
|
389 |
< |
static |
389 |
> |
int |
390 |
|
ambsync() /* flush ambient file */ |
391 |
|
{ |
392 |
+ |
if (nunflshed == 0) |
393 |
+ |
return(0); |
394 |
+ |
nunflshed = 0; |
395 |
|
return(fflush(ambfp)); |
396 |
|
} |
397 |
|
|
398 |
|
#else |
399 |
|
|
400 |
< |
static |
400 |
> |
int |
401 |
|
ambsync() /* synchronize ambient file */ |
402 |
|
{ |
403 |
|
static FILE *ambinp = NULL; |
406 |
|
long flen; |
407 |
|
AMBVAL avs; |
408 |
|
register int n; |
409 |
+ |
|
410 |
+ |
if (nunflshed == 0) |
411 |
+ |
return(0); |
412 |
|
/* gain exclusive access */ |
413 |
|
fls.l_type = F_WRLCK; |
414 |
|
fls.l_whence = 0; |
422 |
|
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
423 |
|
error(SYSTEM, "cannot seek on ambient file"); |
424 |
|
if (n = flen - lastpos) { /* file has grown */ |
425 |
< |
if (ambinp == NULL) { |
426 |
< |
ambinp = fopen(ambfname, "r"); |
425 |
> |
if (ambinp == NULL) { /* use duplicate filedes */ |
426 |
> |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
427 |
|
if (ambinp == NULL) |
428 |
< |
error(SYSTEM, "fopen failed in ambsync"); |
428 |
> |
error(SYSTEM, "fdopen failed in ambsync"); |
429 |
|
} |
430 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
431 |
|
error(SYSTEM, "fseek failed in ambsync"); |
442 |
|
lastpos = lseek(fileno(ambfp), 0L, 1); |
443 |
|
fls.l_type = F_UNLCK; /* release file */ |
444 |
|
fcntl(fileno(ambfp), F_SETLKW, &fls); |
445 |
+ |
nunflshed = 0; |
446 |
|
return(n); |
447 |
|
} |
448 |
|
|