ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.92 by greg, Fri Nov 21 00:30:11 2014 UTC vs.
Revision 2.109 by greg, Tue Mar 10 15:57:52 2020 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 14 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  "platform.h"
13   #include  "ray.h"
14   #include  "otypes.h"
15 + #include  "otspecial.h"
16   #include  "resolu.h"
17   #include  "ambient.h"
18   #include  "random.h"
19 + #include  "pmapamb.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 77 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
80 #define  freeav(av)     free((void *)av);
80  
81 + #define  tfunc(lwr, x, upr)     (((x)-(lwr))/((upr)-(lwr)))
82 +
83   static void initambfile(int creat);
84   static void avsave(AMBVAL *av);
85   static AMBVAL *avstore(AMBVAL  *aval);
# Line 95 | Line 96 | static int aposcmp(const void *avp1, const void *avp2)
96   static int avlmemi(AMBVAL *avaddr);
97   static void sortambvals(int always);
98  
99 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
100 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
101 +                                AMBTREE *at, FVECT c0, double s);
102 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
103 + static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
104 +                                FVECT uvw[3]);
105 +
106   #ifdef  F_SETLKW
107   static void aflock(int  typ);
108   #endif
# Line 133 | Line 141 | setambacc(                             /* set ambient accuracy */
141          newa *= (newa > 0);
142          if (fabs(newa - olda) >= .05*(newa + olda)) {
143                  ambacc = newa;
144 <                if (nambvals > 0)
144 >                if (ambacc > FTINY && nambvals > 0)
145                          sortambvals(1);         /* rebuild tree */
146          }
147   }
# Line 185 | Line 193 | setambient(void)                               /* initialize calculation */
193                                          (flen - lastpos)/AMBVALSIZ);
194                          error(WARNING, errmsg);
195                          fseek(ambfp, lastpos, SEEK_SET);
188 #ifndef _WIN32 /* XXX we need a replacement for that one */
196                          ftruncate(fileno(ambfp), (off_t)lastpos);
190 #endif
197                  }
198          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
199                  initambfile(1);                 /* else create new file */
# Line 197 | Line 203 | setambient(void)                               /* initialize calculation */
203                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
204                  error(SYSTEM, errmsg);
205          }
200 #ifdef getc_unlocked
201        flockfile(ambfp);                       /* application-level lock */
202 #endif
206   #ifdef  F_SETLKW
207          aflock(F_UNLCK);                        /* release file */
208   #endif
# Line 220 | Line 223 | ambdone(void)                  /* close ambient file and free memory
223                  lastpos = -1;
224          }
225                                          /* free ambient tree */
226 <        unloadatree(&atrunk, &avfree);
226 >        unloadatree(&atrunk, avfree);
227                                          /* reset state variables */
228          avsum = 0.;
229          navsum = 0;
# Line 261 | Line 264 | ambnotify(                     /* record new modifier */
264                  }
265   }
266  
264 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
267  
266 #ifndef OLDAMB
267
268 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
269
270 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
271 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
272                                AMBTREE *at, FVECT c0, double s);
273 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
274 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
275                                FVECT uvw[3]);
276
268   void
269   multambient(            /* compute ambient component & multiply by coef. */
270          COLOR  aval,
# Line 282 | Line 273 | multambient(           /* compute ambient component & multiply
273   )
274   {
275          static int  rdepth = 0;                 /* ambient recursion */
276 <        COLOR   acol;
277 <        int     ok;
276 >        COLOR   acol, caustic;
277 >        int     i, ok;
278          double  d, l;
279  
280 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
281 +         * ambient. Return as all ambient components accounted for, else
282 +         * continue. */
283 +        if (ambPmap(aval, r, rdepth))
284 +                return;
285 +
286 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
287 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
288 +         * with RADIANCE ambient calculation */
289 +        copycolor(caustic, aval);
290 +        ambPmapCaustic(caustic, r, rdepth);
291 +        
292          if (ambdiv <= 0)                        /* no ambient calculation */
293                  goto dumbamb;
294                                                  /* check number of bounces */
# Line 297 | Line 300 | multambient(           /* compute ambient component & multiply
300                  goto dumbamb;
301  
302          if (ambacc <= FTINY) {                  /* no ambient storage */
303 +                FVECT   uvd[2];
304 +                float   dgrad[2], *dgp = NULL;
305 +
306 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
307 +                        dgp = dgrad;            /* compute rotational grad. */
308                  copycolor(acol, aval);
309                  rdepth++;
310                  ok = doambient(acol, r, r->rweight,
311 <                                NULL, NULL, NULL, NULL, NULL);
311 >                                uvd, NULL, NULL, dgp, NULL);
312                  rdepth--;
313                  if (!ok)
314                          goto dumbamb;
315 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
316 +                        FVECT   v1;
317 +                        VCROSS(v1, r->ron, nrm);
318 +                        d = 1.0;
319 +                        for (i = 3; i--; )
320 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
321 +                        if (d >= 0.05)
322 +                                scalecolor(acol, d);
323 +                }
324                  copycolor(aval, acol);
325 +
326 +                /* PMAP: add in caustic */
327 +                addcolor(aval, caustic);
328                  return;
329          }
330  
# Line 314 | Line 334 | multambient(           /* compute ambient component & multiply
334          setcolor(acol, 0.0, 0.0, 0.0);
335          d = sumambient(acol, r, nrm, rdepth,
336                          &atrunk, thescene.cuorg, thescene.cusize);
337 +                        
338          if (d > FTINY) {
339                  d = 1.0/d;
340                  scalecolor(acol, d);
341                  multcolor(aval, acol);
342 +
343 +                /* PMAP: add in caustic */
344 +                addcolor(aval, caustic);
345                  return;
346          }
347 +        
348          rdepth++;                               /* need to cache new value */
349          ok = makeambient(acol, r, nrm, rdepth-1);
350          rdepth--;
351 +        
352          if (ok) {
353                  multcolor(aval, acol);          /* computed new value */
354 +
355 +                /* PMAP: add in caustic */
356 +                addcolor(aval, caustic);
357                  return;
358          }
359 +        
360   dumbamb:                                        /* return global value */
361          if ((ambvwt <= 0) | (navsum == 0)) {
362                  multcolor(aval, ambval);
363 +                
364 +                /* PMAP: add in caustic */
365 +                addcolor(aval, caustic);
366                  return;
367          }
368 <        l = bright(ambval);                     /* average in computations */
368 >        
369 >        l = bright(ambval);                     /* average in computations */  
370          if (l > FTINY) {
371                  d = (log(l)*(double)ambvwt + avsum) /
372                                  (double)(ambvwt + navsum);
# Line 379 | Line 413 | plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
413          VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
414          rtst.rmax = normalize(rtst.rdir);       /* short ray test */
415          while (localhit(&rtst, &thescene)) {    /* check for occluder */
416 <                if (rtst.ro->omod != OVOID &&
416 >                OBJREC  *m = findmaterial(rtst.ro);
417 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
418                                  (rtst.clipset == NULL ||
419                                          !inset(rtst.clipset, rtst.ro->omod)))
420                          return(1);              /* plug light leak */
# Line 547 | Line 582 | extambient(            /* extrapolate value at pv, nv */
582          FVECT  uvw[3]
583   )
584   {
585 +        const double    min_d = 0.05;
586          static FVECT    my_uvw[3];
587          FVECT           v1;
588          int             i;
# Line 566 | Line 602 | extambient(            /* extrapolate value at pv, nv */
602          for (i = 3; i--; )
603                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
604          
605 <        if (d <= 0.0) {
606 <                setcolor(cr, 0.0, 0.0, 0.0);
571 <                return(0);              /* should not use if we can avoid it */
572 <        }
605 >        if (d < min_d)                  /* should not use if we can avoid it */
606 >                d = min_d;
607          copycolor(cr, ap->val);
608          scalecolor(cr, d);
609 <        return(1);
609 >        return(d > min_d);
610   }
611  
612  
# Line 619 | Line 653 | avinsert(                              /* insert ambient value in our tree */
653   }
654  
655  
622 #else /* ! NEWAMB */
623
624 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
625                                AMBTREE *at, FVECT c0, double s);
626 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
627 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
628
629
630 void
631 multambient(            /* compute ambient component & multiply by coef. */
632        COLOR  aval,
633        RAY  *r,
634        FVECT  nrm
635 )
636 {
637        static int  rdepth = 0;                 /* ambient recursion */
638        COLOR   acol;
639        double  d, l;
640
641        if (ambdiv <= 0)                        /* no ambient calculation */
642                goto dumbamb;
643                                                /* check number of bounces */
644        if (rdepth >= ambounce)
645                goto dumbamb;
646                                                /* check ambient list */
647        if (ambincl != -1 && r->ro != NULL &&
648                        ambincl != inset(ambset, r->ro->omod))
649                goto dumbamb;
650
651        if (ambacc <= FTINY) {                  /* no ambient storage */
652                copycolor(acol, aval);
653                rdepth++;
654                d = doambient(acol, r, r->rweight, NULL, NULL);
655                rdepth--;
656                if (d <= FTINY)
657                        goto dumbamb;
658                copycolor(aval, acol);
659                return;
660        }
661
662        if (tracktime)                          /* sort to minimize thrashing */
663                sortambvals(0);
664                                                /* interpolate ambient value */
665        setcolor(acol, 0.0, 0.0, 0.0);
666        d = sumambient(acol, r, nrm, rdepth,
667                        &atrunk, thescene.cuorg, thescene.cusize);
668        if (d > FTINY) {
669                d = 1.0/d;
670                scalecolor(acol, d);
671                multcolor(aval, acol);
672                return;
673        }
674        rdepth++;                               /* need to cache new value */
675        d = makeambient(acol, r, nrm, rdepth-1);
676        rdepth--;
677        if (d > FTINY) {
678                multcolor(aval, acol);          /* got new value */
679                return;
680        }
681 dumbamb:                                        /* return global value */
682        if ((ambvwt <= 0) | (navsum == 0)) {
683                multcolor(aval, ambval);
684                return;
685        }
686        l = bright(ambval);                     /* average in computations */
687        if (l > FTINY) {
688                d = (log(l)*(double)ambvwt + avsum) /
689                                (double)(ambvwt + navsum);
690                d = exp(d) / l;
691                scalecolor(aval, d);
692                multcolor(aval, ambval);        /* apply color of ambval */
693        } else {
694                d = exp( avsum / (double)navsum );
695                scalecolor(aval, d);            /* neutral color */
696        }
697 }
698
699
700 static double
701 sumambient(     /* get interpolated ambient value */
702        COLOR  acol,
703        RAY  *r,
704        FVECT  rn,
705        int  al,
706        AMBTREE  *at,
707        FVECT  c0,
708        double  s
709 )
710 {
711        double  d, e1, e2, wt, wsum;
712        COLOR  ct;
713        FVECT  ck0;
714        int  i;
715        int  j;
716        AMBVAL   *av;
717
718        wsum = 0.0;
719                                        /* do this node */
720        for (av = at->alist; av != NULL; av = av->next) {
721                double  rn_dot = -2.0;
722                if (tracktime)
723                        av->latick = ambclock;
724                /*
725                 *  Ambient level test.
726                 */
727                if (av->lvl > al ||     /* list sorted, so this works */
728                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
729                        break;
730                /*
731                 *  Ambient radius test.
732                 */
733                VSUB(ck0, av->pos, r->rop);
734                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
735                if (e1 > ambacc*ambacc*1.21)
736                        continue;
737                /*
738                 *  Direction test using closest normal.
739                 */
740                d = DOT(av->dir, r->ron);
741                if (rn != r->ron) {
742                        rn_dot = DOT(av->dir, rn);
743                        if (rn_dot > 1.0-FTINY)
744                                rn_dot = 1.0-FTINY;
745                        if (rn_dot >= d-FTINY) {
746                                d = rn_dot;
747                                rn_dot = -2.0;
748                        }
749                }
750                e2 = (1.0 - d) * r->rweight;
751                if (e2 < 0.0)
752                        e2 = 0.0;
753                else if (e1 + e2 > ambacc*ambacc*1.21)
754                        continue;
755                /*
756                 *  Ray behind test.
757                 */
758                d = 0.0;
759                for (j = 0; j < 3; j++)
760                        d += (r->rop[j] - av->pos[j]) *
761                                        (av->dir[j] + r->ron[j]);
762                if (d*0.5 < -minarad*ambacc-.001)
763                        continue;
764                /*
765                 *  Jittering final test reduces image artifacts.
766                 */
767                e1 = sqrt(e1);
768                e2 = sqrt(e2);
769                wt = e1 + e2;
770                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
771                        continue;
772                /*
773                 *  Recompute directional error using perturbed normal
774                 */
775                if (rn_dot > 0.0) {
776                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
777                        wt = e1 + e2;
778                }
779                if (wt <= 1e-3)
780                        wt = 1e3;
781                else
782                        wt = 1.0 / wt;
783                wsum += wt;
784                extambient(ct, av, r->rop, rn);
785                scalecolor(ct, wt);
786                addcolor(acol, ct);
787        }
788        if (at->kid == NULL)
789                return(wsum);
790                                        /* do children */
791        s *= 0.5;
792        for (i = 0; i < 8; i++) {
793                for (j = 0; j < 3; j++) {
794                        ck0[j] = c0[j];
795                        if (1<<j & i)
796                                ck0[j] += s;
797                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
798                                break;
799                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
800                                break;
801                }
802                if (j == 3)
803                        wsum += sumambient(acol, r, rn, al,
804                                                at->kid+i, ck0, s);
805        }
806        return(wsum);
807 }
808
809
810 static double
811 makeambient(            /* make a new ambient value for storage */
812        COLOR  acol,
813        RAY  *r,
814        FVECT  rn,
815        int  al
816 )
817 {
818        AMBVAL  amb;
819        FVECT   gp, gd;
820        int     i;
821
822        amb.weight = 1.0;                       /* compute weight */
823        for (i = al; i-- > 0; )
824                amb.weight *= AVGREFL;
825        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
826                amb.weight = 1.25*r->rweight;
827        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
828                                                /* compute ambient */
829        amb.rad = doambient(acol, r, amb.weight, gp, gd);
830        if (amb.rad <= FTINY) {
831                setcolor(acol, 0.0, 0.0, 0.0);
832                return(0.0);
833        }
834        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
835                                                /* store value */
836        VCOPY(amb.pos, r->rop);
837        VCOPY(amb.dir, r->ron);
838        amb.lvl = al;
839        copycolor(amb.val, acol);
840        VCOPY(amb.gpos, gp);
841        VCOPY(amb.gdir, gd);
842                                                /* insert into tree */
843        avsave(&amb);                           /* and save to file */
844        if (rn != r->ron)
845                extambient(acol, &amb, r->rop, rn);     /* texture */
846        return(amb.rad);
847 }
848
849
656   static void
851 extambient(             /* extrapolate value at pv, nv */
852        COLOR  cr,
853        AMBVAL   *ap,
854        FVECT  pv,
855        FVECT  nv
856 )
857 {
858        FVECT  v1;
859        int  i;
860        double  d;
861
862        d = 1.0;                        /* zeroeth order */
863                                        /* gradient due to translation */
864        for (i = 0; i < 3; i++)
865                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
866                                        /* gradient due to rotation */
867        VCROSS(v1, ap->dir, nv);
868        d += DOT(ap->gdir, v1);
869        if (d <= 0.0) {
870                setcolor(cr, 0.0, 0.0, 0.0);
871                return;
872        }
873        copycolor(cr, ap->val);
874        scalecolor(cr, d);
875 }
876
877
878 static void
879 avinsert(                               /* insert ambient value in our tree */
880        AMBVAL *av
881 )
882 {
883        AMBTREE  *at;
884        AMBVAL  *ap;
885        AMBVAL  avh;
886        FVECT  ck0;
887        double  s;
888        int  branch;
889        int  i;
890
891        if (av->rad <= FTINY)
892                error(CONSISTENCY, "zero ambient radius in avinsert");
893        at = &atrunk;
894        VCOPY(ck0, thescene.cuorg);
895        s = thescene.cusize;
896        while (s*(OCTSCALE/2) > av->rad*ambacc) {
897                if (at->kid == NULL)
898                        if ((at->kid = newambtree()) == NULL)
899                                error(SYSTEM, "out of memory in avinsert");
900                s *= 0.5;
901                branch = 0;
902                for (i = 0; i < 3; i++)
903                        if (av->pos[i] > ck0[i] + s) {
904                                ck0[i] += s;
905                                branch |= 1 << i;
906                        }
907                at = at->kid + branch;
908        }
909        avh.next = at->alist;           /* order by increasing level */
910        for (ap = &avh; ap->next != NULL; ap = ap->next)
911                if ( ap->next->lvl > av->lvl ||
912                                (ap->next->lvl == av->lvl) &
913                                (ap->next->weight <= av->weight) )
914                        break;
915        av->next = ap->next;
916        ap->next = (AMBVAL*)av;
917        at->alist = avh.next;
918 }
919
920 #endif  /* ! NEWAMB */
921
922 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
923
924 static void
657   initambfile(            /* initialize ambient file */
658          int  cre8
659   )
# Line 1022 | Line 754 | newambtree(void)                               /* allocate 8 ambient tree structs
754          }
755          atp = atfreelist;
756          atfreelist = atp->kid;
757 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
757 >        memset(atp, 0, 8*sizeof(AMBTREE));
758          return(atp);
759   }
760  
# Line 1048 | Line 780 | unloadatree(                   /* unload an ambient value tree */
780                                          /* transfer values at this node */
781          for (av = at->alist; av != NULL; av = at->alist) {
782                  at->alist = av->next;
783 +                av->next = NULL;
784                  (*f)(av);
785          }
786          if (at->kid == NULL)
# Line 1125 | Line 858 | avlmemi(                               /* find list position from address */
858   {
859          AMBVAL  **avlpp;
860  
861 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
862 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
861 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
862 >                        nambvals, sizeof(AMBVAL *), aposcmp);
863          if (avlpp == NULL)
864                  error(CONSISTENCY, "address not found in avlmemi");
865          return(avlpp - avlist2);
# Line 1169 | Line 902 | sortambvals(                   /* resort ambient values */
902          }
903          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
904                  if (avlist2 != NULL)
905 <                        free((void *)avlist2);
905 >                        free(avlist2);
906                  if (always) {           /* rebuild without sorting */
907                          oldatrunk = atrunk;
908                          atrunk.alist = NULL;
909                          atrunk.kid = NULL;
910 <                        unloadatree(&oldatrunk, &avinsert);
910 >                        unloadatree(&oldatrunk, avinsert);
911                  }
912          } else {                        /* sort memory by last access time */
913                  /*
# Line 1191 | Line 924 | sortambvals(                   /* resort ambient values */
924                  eputs(errmsg);
925   #endif
926                  i_avlist = 0;
927 <                unloadatree(&atrunk, &av2list); /* empty current tree */
927 >                unloadatree(&atrunk, av2list);  /* empty current tree */
928   #ifdef DEBUG
929                  if (i_avlist < nambvals)
930                          error(CONSISTENCY, "missing ambient values in sortambvals");
931   #endif
932 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
933 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
932 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
933 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
934                  for (i = 0; i < nambvals; i++) {
935                          if (avlist1[i].p == NULL)
936                                  continue;
# Line 1213 | Line 946 | sortambvals(                   /* resort ambient values */
946                          avinsert(avlist2[j]);
947                          avlist1[j].p = NULL;
948                  }
949 <                free((void *)avlist1);
950 <                free((void *)avlist2);
949 >                free(avlist1);
950 >                free(avlist2);
951                                                  /* compute new sort interval */
952                  sortintvl = ambclock - lastsort;
953                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1242 | Line 975 | aflock(                        /* lock/unlock ambient file */
975  
976          if (typ == fls.l_type)          /* already called? */
977                  return;
978 +
979          fls.l_type = typ;
980 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
981 <                error(SYSTEM, "cannot (un)lock ambient file");
980 >        do
981 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
982 >                        return;
983 >        while (errno == EINTR);
984 >        
985 >        error(SYSTEM, "cannot (un)lock ambient file");
986   }
987  
988  
# Line 1263 | Line 1001 | ambsync(void)                  /* synchronize ambient file */
1001          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1002                  goto seekerr;
1003          if ((n = flen - lastpos) > 0) {         /* file has grown */
1004 <                if (ambinp == NULL) {           /* use duplicate filedes */
1005 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1004 >                if (ambinp == NULL) {           /* get new file pointer */
1005 >                        ambinp = fopen(ambfile, "rb");
1006                          if (ambinp == NULL)
1007 <                                error(SYSTEM, "fdopen failed in ambsync");
1007 >                                error(SYSTEM, "fopen failed in ambsync");
1008                  }
1009                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1010                          goto seekerr;
# Line 1281 | Line 1019 | ambsync(void)                  /* synchronize ambient file */
1019                          avstore(&avs);
1020                          n -= AMBVALSIZ;
1021                  }
1022 <                lastpos = flen - n;
1023 <                /*** seek always as safety measure
1024 <                if (n) ***/                     /* alignment */
1287 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1288 <                                goto seekerr;
1022 >                lastpos = flen - n;             /* check alignment */
1023 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1024 >                        goto seekerr;
1025          }
1026          n = fflush(ambfp);                      /* calls write() at last */
1027 <        if (n != EOF)
1292 <                lastpos += (long)nunflshed*AMBVALSIZ;
1293 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1294 <                goto seekerr;
1295 <                
1027 >        lastpos += (long)nunflshed*AMBVALSIZ;
1028          aflock(F_UNLCK);                        /* release file */
1029          nunflshed = 0;
1030          return(n);
1031   seekerr:
1032          error(SYSTEM, "seek failed in ambsync");
1033 <        return -1; /* pro forma return */
1033 >        return(EOF);    /* pro forma return */
1034   }
1035  
1036   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines