188 |
|
} |
189 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
|
initambfile(1); /* else create new file */ |
191 |
+ |
fflush(ambfp); |
192 |
|
lastpos = ftell(ambfp); |
193 |
|
} else { |
194 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
195 |
|
error(SYSTEM, errmsg); |
196 |
|
} |
197 |
< |
ambsync(); /* load previous values */ |
197 |
> |
#ifdef F_SETLKW |
198 |
> |
aflock(F_UNLCK); /* release file */ |
199 |
> |
#endif |
200 |
|
} |
201 |
|
|
202 |
|
|
511 |
|
|
512 |
|
static void |
513 |
|
initambfile( /* initialize ambient file */ |
514 |
< |
int creat |
514 |
> |
int cre8 |
515 |
|
) |
516 |
|
{ |
517 |
|
extern char *progname, *octname; |
518 |
|
static char *mybuf = NULL; |
519 |
|
|
520 |
|
#ifdef F_SETLKW |
521 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
521 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
522 |
|
#endif |
523 |
|
SET_FILE_BINARY(ambfp); |
524 |
|
if (mybuf == NULL) |
525 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
526 |
|
setbuf(ambfp, mybuf); |
527 |
< |
if (creat) { /* new file */ |
527 |
> |
if (cre8) { /* new file */ |
528 |
|
newheader("RADIANCE", ambfp); |
529 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
530 |
|
progname, colval(ambval,RED), |
533 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
534 |
|
ambdiv, ambssamp, ambres); |
535 |
|
if (octname != NULL) |
536 |
< |
printargs(1, &octname, ambfp); |
537 |
< |
else |
535 |
< |
fputc('\n', ambfp); |
536 |
> |
fputs(octname, ambfp); |
537 |
> |
fputc('\n', ambfp); |
538 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
539 |
|
fputnow(ambfp); |
540 |
|
fputformat(AMBFMT, ambfp); |
541 |
< |
putc('\n', ambfp); |
541 |
> |
fputc('\n', ambfp); |
542 |
|
putambmagic(ambfp); |
543 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
544 |
|
error(USER, "bad ambient file"); |
888 |
|
/* see if file has grown */ |
889 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
890 |
|
goto seekerr; |
891 |
< |
if ( (n = flen - lastpos) ) { /* file has grown */ |
891 |
> |
if ((n = flen - lastpos) > 0) { /* file has grown */ |
892 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
893 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
894 |
|
if (ambinp == NULL) |
913 |
|
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
914 |
|
goto seekerr; |
915 |
|
} |
914 |
– |
#ifdef DEBUG |
915 |
– |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
916 |
– |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
917 |
– |
ambfp->_ptr - ambfp->_base, |
918 |
– |
nunflshed*AMBVALSIZ); |
919 |
– |
error(CONSISTENCY, errmsg); |
920 |
– |
} |
921 |
– |
#endif |
916 |
|
n = fflush(ambfp); /* calls write() at last */ |
917 |
|
if (n != EOF) |
918 |
|
lastpos += (long)nunflshed*AMBVALSIZ; |