--- ray/src/rt/ambient.c 1993/01/22 09:51:13 2.16 +++ ray/src/rt/ambient.c 1993/08/04 14:22:11 2.19 @@ -35,7 +35,6 @@ double minarad; /* minimum ambient radius */ static AMBTREE atrunk; /* our ambient trunk node */ -static char *ambfname = NULL; /* ambient file name */ static FILE *ambfp = NULL; /* ambient file pointer */ static int nunflshed = 0; /* number of unflushed ambient values */ @@ -47,6 +46,9 @@ static int nunflshed = 0; /* number of unflushed ambi extern long ftell(), lseek(); static int initambfile(), avsave(), avinsert(); +#ifdef F_SETLKW +static aflock(); +#endif setambres(ar) /* set ambient resolution */ @@ -74,26 +76,25 @@ char *afile; AMBVAL amb; /* init ambient limits */ setambres(ambres); + if (afile == NULL) + return; /* open ambient file */ - if ((ambfname = afile) != NULL) { - if ((ambfp = fopen(afile, "r+")) != NULL) { - initambfile(0); - headlen = ftell(ambfp); - while (readambval(&amb, ambfp)) - avinsert(&amb, &atrunk, thescene.cuorg, - thescene.cusize); - /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); - } else if ((ambfp = fopen(afile, "w+")) != NULL) - initambfile(1); - else { - sprintf(errmsg, "cannot open ambient file \"%s\"", - afile); - error(SYSTEM, errmsg); - } - nunflshed++; /* lie */ - ambsync(); + if ((ambfp = fopen(afile, "r+")) != NULL) { + initambfile(0); + headlen = ftell(ambfp); + while (readambval(&amb, ambfp)) + avinsert(&amb, &atrunk, thescene.cuorg, + thescene.cusize); + /* align */ + fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); + } else if ((ambfp = fopen(afile, "w+")) != NULL) + initambfile(1); + else { + sprintf(errmsg, "cannot open ambient file \"%s\"", afile); + error(SYSTEM, errmsg); } + nunflshed++; /* lie */ + ambsync(); } @@ -309,6 +310,9 @@ int creat; { extern char *progname, *octname, VersionID[]; +#ifdef F_SETLKW + aflock(creat ? F_WRLCK : F_RDLCK); +#endif #ifdef MSDOS setmode(fileno(ambfp), O_BINARY); #endif @@ -325,10 +329,8 @@ int creat; fputformat(AMBFMT, ambfp); putc('\n', ambfp); putambmagic(ambfp); - } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) { - sprintf(errmsg, "bad ambient file \"%s\"", ambfname); - error(USER, errmsg); - } + } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) + error(USER, "bad ambient file"); } @@ -346,8 +348,7 @@ AMBVAL *av; goto writerr; return; writerr: - sprintf(errmsg, "error writing ambient file \"%s\"", ambfname); - error(SYSTEM, errmsg); + error(SYSTEM, "error writing ambient file"); } @@ -388,48 +389,43 @@ memerr: } -#ifdef NIX +#ifdef F_SETLKW -int -ambsync() /* flush ambient file */ +static +aflock(typ) /* lock/unlock ambient file */ +int typ; { - if (nunflshed == 0) - return(0); - nunflshed = 0; - return(fflush(ambfp)); + static struct flock fls; /* static so initialized to zeroes */ + + fls.l_type = typ; + if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) + error(SYSTEM, "cannot (un)lock ambient file"); } -#else int ambsync() /* synchronize ambient file */ { static FILE *ambinp = NULL; static long lastpos = -1; - struct flock fls; long flen; AMBVAL avs; register int n; if (nunflshed == 0) return(0); - /* gain exclusive access */ - fls.l_type = F_WRLCK; - fls.l_whence = 0; - fls.l_start = 0L; - fls.l_len = 0L; - if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) - error(SYSTEM, "cannot lock ambient file"); - if (lastpos < 0) /* initializing */ + if (lastpos < 0) /* initializing (locked in initambfile) */ goto syncend; + /* gain exclusive access */ + aflock(F_WRLCK); /* see if file has grown */ if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) error(SYSTEM, "cannot seek on ambient file"); if (n = flen - lastpos) { /* file has grown */ - if (ambinp == NULL) { - ambinp = fopen(ambfname, "r"); + if (ambinp == NULL) { /* use duplicate filedes */ + ambinp = fdopen(dup(fileno(ambfp)), "r"); if (ambinp == NULL) - error(SYSTEM, "fopen failed in ambsync"); + error(SYSTEM, "fdopen failed in ambsync"); } if (fseek(ambinp, lastpos, 0) < 0) error(SYSTEM, "fseek failed in ambsync"); @@ -444,10 +440,20 @@ ambsync() /* synchronize ambient file */ syncend: n = fflush(ambfp); /* calls write() at last */ lastpos = lseek(fileno(ambfp), 0L, 1); - fls.l_type = F_UNLCK; /* release file */ - fcntl(fileno(ambfp), F_SETLKW, &fls); + aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); +} + +#else + +int +ambsync() /* flush ambient file */ +{ + if (nunflshed == 0) + return(0); + nunflshed = 0; + return(fflush(ambfp)); } #endif