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.97 by greg, Fri Aug 21 18:21:05 2015 UTC vs.
Revision 2.111 by greg, Thu Nov 18 19:38:21 2021 UTC

# Line 12 | 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"
# Line 76 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #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 94 | 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 132 | 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 184 | Line 193 | setambient(void)                               /* initialize calculation */
193                                          (flen - lastpos)/AMBVALSIZ);
194                          error(WARNING, errmsg);
195                          fseek(ambfp, lastpos, SEEK_SET);
187 #ifndef _WIN32 /* XXX we need a replacement for that one */
196                          ftruncate(fileno(ambfp), (off_t)lastpos);
189 #endif
197                  }
198          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
199                  initambfile(1);                 /* else create new file */
# Line 196 | Line 203 | setambient(void)                               /* initialize calculation */
203                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
204                  error(SYSTEM, errmsg);
205          }
199 #if 0
200        flockfile(ambfp);                       /* application-level lock */
201 #endif
206   #ifdef  F_SETLKW
207          aflock(F_UNLCK);                        /* release file */
208   #endif
# Line 219 | 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 260 | Line 264 | ambnotify(                     /* record new modifier */
264                  }
265   }
266  
263 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
267  
265 #ifndef OLDAMB
266
267 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
268
269 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
270 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
271                                AMBTREE *at, FVECT c0, double s);
272 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
273 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
274                                FVECT uvw[3]);
275
268   void
269   multambient(            /* compute ambient component & multiply by coef. */
270          COLOR  aval,
# Line 280 | Line 272 | multambient(           /* compute ambient component & multiply
272          FVECT  nrm
273   )
274   {
275 +        static double  logAvgAbsorp = 1;
276          static int  rdepth = 0;                 /* ambient recursion */
277          COLOR   acol, caustic;
278 <        int     ok;
278 >        int     i, ok;
279          double  d, l;
280  
281          /* PMAP: Factor in ambient from photon map, if enabled and ray is
# Line 291 | Line 284 | multambient(           /* compute ambient component & multiply
284          if (ambPmap(aval, r, rdepth))
285                  return;
286  
287 +        if (logAvgAbsorp > 0)                   /* exclude in -aw to avoid growth */
288 +                logAvgAbsorp = log(1.-AVGREFL);
289 +
290          /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
291           * map, if enabled and ray is primary, else caustic is zero.  Continue
292           * with RADIANCE ambient calculation */
# Line 308 | Line 304 | multambient(           /* compute ambient component & multiply
304                  goto dumbamb;
305  
306          if (ambacc <= FTINY) {                  /* no ambient storage */
307 +                FVECT   uvd[2];
308 +                float   dgrad[2], *dgp = NULL;
309 +
310 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
311 +                        dgp = dgrad;            /* compute rotational grad. */
312                  copycolor(acol, aval);
313                  rdepth++;
314                  ok = doambient(acol, r, r->rweight,
315 <                                NULL, NULL, NULL, NULL, NULL);
315 >                                uvd, NULL, NULL, dgp, NULL);
316                  rdepth--;
317                  if (!ok)
318                          goto dumbamb;
319 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
320 +                        FVECT   v1;
321 +                        VCROSS(v1, r->ron, nrm);
322 +                        d = 1.0;
323 +                        for (i = 3; i--; )
324 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
325 +                        if (d >= 0.05)
326 +                                scalecolor(acol, d);
327 +                }
328                  copycolor(aval, acol);
329  
330                  /* PMAP: add in caustic */
# Line 362 | Line 372 | dumbamb:                                       /* return global value */
372          
373          l = bright(ambval);                     /* average in computations */  
374          if (l > FTINY) {
375 <                d = (log(l)*(double)ambvwt + avsum) /
375 >                d = (log(l)*(double)ambvwt + avsum + logAvgAbsorp*navsum) /
376                                  (double)(ambvwt + navsum);
377                  d = exp(d) / l;
378                  scalecolor(aval, d);
379                  multcolor(aval, ambval);        /* apply color of ambval */
380          } else {
381 <                d = exp( avsum / (double)navsum );
381 >                d = exp( avsum/(double)navsum + logAvgAbsorp );
382                  scalecolor(aval, d);            /* neutral color */
383          }
384   }
# Line 407 | Line 417 | plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
417          VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
418          rtst.rmax = normalize(rtst.rdir);       /* short ray test */
419          while (localhit(&rtst, &thescene)) {    /* check for occluder */
420 <                if (rtst.ro->omod != OVOID &&
420 >                OBJREC  *m = findmaterial(rtst.ro);
421 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
422                                  (rtst.clipset == NULL ||
423                                          !inset(rtst.clipset, rtst.ro->omod)))
424                          return(1);              /* plug light leak */
# Line 576 | Line 587 | extambient(            /* extrapolate value at pv, nv */
587   )
588   {
589          const double    min_d = 0.05;
590 +        const double    max_d = 20.;
591          static FVECT    my_uvw[3];
592          FVECT           v1;
593          int             i;
# Line 595 | Line 607 | extambient(            /* extrapolate value at pv, nv */
607          for (i = 3; i--; )
608                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
609          
610 <        if (d < min_d)                  /* should not use if we can avoid it */
610 >        if (d < min_d)                  /* clamp min/max scaling */
611                  d = min_d;
612 +        else if (d > max_d)
613 +                d = max_d;
614          copycolor(cr, ap->val);
615          scalecolor(cr, d);
616          return(d > min_d);
# Line 646 | Line 660 | avinsert(                              /* insert ambient value in our tree */
660   }
661  
662  
649 #else /* ! NEWAMB */
650
651 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
652                                AMBTREE *at, FVECT c0, double s);
653 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
654 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
655
656
657 void
658 multambient(            /* compute ambient component & multiply by coef. */
659        COLOR  aval,
660        RAY  *r,
661        FVECT  nrm
662 )
663 {
664        static int  rdepth = 0;                 /* ambient recursion */
665        COLOR   acol, caustic;
666        double  d, l;
667
668        /* PMAP: Factor in ambient from global photon map (if enabled) and return
669         * as all ambient components accounted for */
670        if (ambPmap(aval, r, rdepth))
671                return;
672
673        /* PMAP: Otherwise factor in ambient from caustic photon map
674         * (ambPmapCaustic() returns zero if caustic photons disabled) and
675         * continue with RADIANCE ambient calculation */
676        copycolor(caustic, aval);
677        ambPmapCaustic(caustic, r, rdepth);
678        
679        if (ambdiv <= 0)                        /* no ambient calculation */
680                goto dumbamb;
681                                                /* check number of bounces */
682        if (rdepth >= ambounce)
683                goto dumbamb;
684                                                /* check ambient list */
685        if (ambincl != -1 && r->ro != NULL &&
686                        ambincl != inset(ambset, r->ro->omod))
687                goto dumbamb;
688
689        if (ambacc <= FTINY) {                  /* no ambient storage */
690                copycolor(acol, aval);
691                rdepth++;
692                d = doambient(acol, r, r->rweight, NULL, NULL);
693                rdepth--;
694                if (d <= FTINY)
695                        goto dumbamb;
696                copycolor(aval, acol);          
697        
698           /* PMAP: add in caustic */
699                addcolor(aval, caustic);        
700                return;
701        }
702
703        if (tracktime)                          /* sort to minimize thrashing */
704                sortambvals(0);
705                                                /* interpolate ambient value */
706        setcolor(acol, 0.0, 0.0, 0.0);
707        d = sumambient(acol, r, nrm, rdepth,
708                        &atrunk, thescene.cuorg, thescene.cusize);
709                        
710        if (d > FTINY) {
711                d = 1.0/d;
712                scalecolor(acol, d);
713                multcolor(aval, acol);
714                
715                /* PMAP: add in caustic */
716                addcolor(aval, caustic);        
717                return;
718        }
719        
720        rdepth++;                               /* need to cache new value */
721        d = makeambient(acol, r, nrm, rdepth-1);
722        rdepth--;
723        
724        if (d > FTINY) {
725                multcolor(aval, acol);          /* got new value */
726
727                /* PMAP: add in caustic */
728                addcolor(aval, caustic);                        
729                return;
730        }
731        
732 dumbamb:                                        /* return global value */
733        if ((ambvwt <= 0) | (navsum == 0)) {
734                multcolor(aval, ambval);
735
736                /* PMAP: add in caustic */
737                addcolor(aval, caustic);        
738                return;
739        }
740        
741        l = bright(ambval);                     /* average in computations */
742        if (l > FTINY) {
743                d = (log(l)*(double)ambvwt + avsum) /
744                                (double)(ambvwt + navsum);
745                d = exp(d) / l;
746                scalecolor(aval, d);
747                multcolor(aval, ambval);        /* apply color of ambval */
748        } else {
749                d = exp( avsum / (double)navsum );
750                scalecolor(aval, d);            /* neutral color */
751        }
752 }
753
754
755 static double
756 sumambient(     /* get interpolated ambient value */
757        COLOR  acol,
758        RAY  *r,
759        FVECT  rn,
760        int  al,
761        AMBTREE  *at,
762        FVECT  c0,
763        double  s
764 )
765 {
766        double  d, e1, e2, wt, wsum;
767        COLOR  ct;
768        FVECT  ck0;
769        int  i;
770        int  j;
771        AMBVAL   *av;
772
773        wsum = 0.0;
774                                        /* do this node */
775        for (av = at->alist; av != NULL; av = av->next) {
776                double  rn_dot = -2.0;
777                if (tracktime)
778                        av->latick = ambclock;
779                /*
780                 *  Ambient level test.
781                 */
782                if (av->lvl > al ||     /* list sorted, so this works */
783                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
784                        break;
785                /*
786                 *  Ambient radius test.
787                 */
788                VSUB(ck0, av->pos, r->rop);
789                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
790                if (e1 > ambacc*ambacc*1.21)
791                        continue;
792                /*
793                 *  Direction test using closest normal.
794                 */
795                d = DOT(av->dir, r->ron);
796                if (rn != r->ron) {
797                        rn_dot = DOT(av->dir, rn);
798                        if (rn_dot > 1.0-FTINY)
799                                rn_dot = 1.0-FTINY;
800                        if (rn_dot >= d-FTINY) {
801                                d = rn_dot;
802                                rn_dot = -2.0;
803                        }
804                }
805                e2 = (1.0 - d) * r->rweight;
806                if (e2 < 0.0)
807                        e2 = 0.0;
808                else if (e1 + e2 > ambacc*ambacc*1.21)
809                        continue;
810                /*
811                 *  Ray behind test.
812                 */
813                d = 0.0;
814                for (j = 0; j < 3; j++)
815                        d += (r->rop[j] - av->pos[j]) *
816                                        (av->dir[j] + r->ron[j]);
817                if (d*0.5 < -minarad*ambacc-.001)
818                        continue;
819                /*
820                 *  Jittering final test reduces image artifacts.
821                 */
822                e1 = sqrt(e1);
823                e2 = sqrt(e2);
824                wt = e1 + e2;
825                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
826                        continue;
827                /*
828                 *  Recompute directional error using perturbed normal
829                 */
830                if (rn_dot > 0.0) {
831                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
832                        wt = e1 + e2;
833                }
834                if (wt <= 1e-3)
835                        wt = 1e3;
836                else
837                        wt = 1.0 / wt;
838                wsum += wt;
839                extambient(ct, av, r->rop, rn);
840                scalecolor(ct, wt);
841                addcolor(acol, ct);
842        }
843        if (at->kid == NULL)
844                return(wsum);
845                                        /* do children */
846        s *= 0.5;
847        for (i = 0; i < 8; i++) {
848                for (j = 0; j < 3; j++) {
849                        ck0[j] = c0[j];
850                        if (1<<j & i)
851                                ck0[j] += s;
852                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
853                                break;
854                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
855                                break;
856                }
857                if (j == 3)
858                        wsum += sumambient(acol, r, rn, al,
859                                                at->kid+i, ck0, s);
860        }
861        return(wsum);
862 }
863
864
865 static double
866 makeambient(            /* make a new ambient value for storage */
867        COLOR  acol,
868        RAY  *r,
869        FVECT  rn,
870        int  al
871 )
872 {
873        AMBVAL  amb;
874        FVECT   gp, gd;
875        int     i;
876
877        amb.weight = 1.0;                       /* compute weight */
878        for (i = al; i-- > 0; )
879                amb.weight *= AVGREFL;
880        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
881                amb.weight = 1.25*r->rweight;
882        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
883                                                /* compute ambient */
884        amb.rad = doambient(acol, r, amb.weight, gp, gd);
885        if (amb.rad <= FTINY) {
886                setcolor(acol, 0.0, 0.0, 0.0);
887                return(0.0);
888        }
889        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
890                                                /* store value */
891        VCOPY(amb.pos, r->rop);
892        VCOPY(amb.dir, r->ron);
893        amb.lvl = al;
894        copycolor(amb.val, acol);
895        VCOPY(amb.gpos, gp);
896        VCOPY(amb.gdir, gd);
897                                                /* insert into tree */
898        avsave(&amb);                           /* and save to file */
899        if (rn != r->ron)
900                extambient(acol, &amb, r->rop, rn);     /* texture */
901        return(amb.rad);
902 }
903
904
663   static void
906 extambient(             /* extrapolate value at pv, nv */
907        COLOR  cr,
908        AMBVAL   *ap,
909        FVECT  pv,
910        FVECT  nv
911 )
912 {
913        FVECT  v1;
914        int  i;
915        double  d;
916
917        d = 1.0;                        /* zeroeth order */
918                                        /* gradient due to translation */
919        for (i = 0; i < 3; i++)
920                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
921                                        /* gradient due to rotation */
922        VCROSS(v1, ap->dir, nv);
923        d += DOT(ap->gdir, v1);
924        if (d <= 0.0) {
925                setcolor(cr, 0.0, 0.0, 0.0);
926                return;
927        }
928        copycolor(cr, ap->val);
929        scalecolor(cr, d);
930 }
931
932
933 static void
934 avinsert(                               /* insert ambient value in our tree */
935        AMBVAL *av
936 )
937 {
938        AMBTREE  *at;
939        AMBVAL  *ap;
940        AMBVAL  avh;
941        FVECT  ck0;
942        double  s;
943        int  branch;
944        int  i;
945
946        if (av->rad <= FTINY)
947                error(CONSISTENCY, "zero ambient radius in avinsert");
948        at = &atrunk;
949        VCOPY(ck0, thescene.cuorg);
950        s = thescene.cusize;
951        while (s*(OCTSCALE/2) > av->rad*ambacc) {
952                if (at->kid == NULL)
953                        if ((at->kid = newambtree()) == NULL)
954                                error(SYSTEM, "out of memory in avinsert");
955                s *= 0.5;
956                branch = 0;
957                for (i = 0; i < 3; i++)
958                        if (av->pos[i] > ck0[i] + s) {
959                                ck0[i] += s;
960                                branch |= 1 << i;
961                        }
962                at = at->kid + branch;
963        }
964        avh.next = at->alist;           /* order by increasing level */
965        for (ap = &avh; ap->next != NULL; ap = ap->next)
966                if ( ap->next->lvl > av->lvl ||
967                                (ap->next->lvl == av->lvl) &
968                                (ap->next->weight <= av->weight) )
969                        break;
970        av->next = ap->next;
971        ap->next = (AMBVAL*)av;
972        at->alist = avh.next;
973 }
974
975 #endif  /* ! NEWAMB */
976
977 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
978
979 static void
664   initambfile(            /* initialize ambient file */
665          int  cre8
666   )
# Line 1077 | Line 761 | newambtree(void)                               /* allocate 8 ambient tree structs
761          }
762          atp = atfreelist;
763          atfreelist = atp->kid;
764 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
764 >        memset(atp, 0, 8*sizeof(AMBTREE));
765          return(atp);
766   }
767  
# Line 1103 | Line 787 | unloadatree(                   /* unload an ambient value tree */
787                                          /* transfer values at this node */
788          for (av = at->alist; av != NULL; av = at->alist) {
789                  at->alist = av->next;
790 +                av->next = NULL;
791                  (*f)(av);
792          }
793          if (at->kid == NULL)
# Line 1180 | Line 865 | avlmemi(                               /* find list position from address */
865   {
866          AMBVAL  **avlpp;
867  
868 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
869 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
868 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
869 >                        nambvals, sizeof(AMBVAL *), aposcmp);
870          if (avlpp == NULL)
871                  error(CONSISTENCY, "address not found in avlmemi");
872          return(avlpp - avlist2);
# Line 1224 | Line 909 | sortambvals(                   /* resort ambient values */
909          }
910          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
911                  if (avlist2 != NULL)
912 <                        free((void *)avlist2);
912 >                        free(avlist2);
913                  if (always) {           /* rebuild without sorting */
914                          oldatrunk = atrunk;
915                          atrunk.alist = NULL;
916                          atrunk.kid = NULL;
917 <                        unloadatree(&oldatrunk, &avinsert);
917 >                        unloadatree(&oldatrunk, avinsert);
918                  }
919          } else {                        /* sort memory by last access time */
920                  /*
# Line 1246 | Line 931 | sortambvals(                   /* resort ambient values */
931                  eputs(errmsg);
932   #endif
933                  i_avlist = 0;
934 <                unloadatree(&atrunk, &av2list); /* empty current tree */
934 >                unloadatree(&atrunk, av2list);  /* empty current tree */
935   #ifdef DEBUG
936                  if (i_avlist < nambvals)
937                          error(CONSISTENCY, "missing ambient values in sortambvals");
938   #endif
939 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
940 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
939 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
940 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
941                  for (i = 0; i < nambvals; i++) {
942                          if (avlist1[i].p == NULL)
943                                  continue;
# Line 1268 | Line 953 | sortambvals(                   /* resort ambient values */
953                          avinsert(avlist2[j]);
954                          avlist1[j].p = NULL;
955                  }
956 <                free((void *)avlist1);
957 <                free((void *)avlist2);
956 >                free(avlist1);
957 >                free(avlist2);
958                                                  /* compute new sort interval */
959                  sortintvl = ambclock - lastsort;
960                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1297 | Line 982 | aflock(                        /* lock/unlock ambient file */
982  
983          if (typ == fls.l_type)          /* already called? */
984                  return;
985 +
986          fls.l_type = typ;
987 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
988 <                error(SYSTEM, "cannot (un)lock ambient file");
987 >        do
988 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
989 >                        return;
990 >        while (errno == EINTR);
991 >        
992 >        error(SYSTEM, "cannot (un)lock ambient file");
993   }
994  
995  
# Line 1318 | Line 1008 | ambsync(void)                  /* synchronize ambient file */
1008          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1009                  goto seekerr;
1010          if ((n = flen - lastpos) > 0) {         /* file has grown */
1011 <                if (ambinp == NULL) {           /* use duplicate filedes */
1012 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1011 >                if (ambinp == NULL) {           /* get new file pointer */
1012 >                        ambinp = fopen(ambfile, "rb");
1013                          if (ambinp == NULL)
1014 <                                error(SYSTEM, "fdopen failed in ambsync");
1014 >                                error(SYSTEM, "fopen failed in ambsync");
1015                  }
1016                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1017                          goto seekerr;
# Line 1336 | Line 1026 | ambsync(void)                  /* synchronize ambient file */
1026                          avstore(&avs);
1027                          n -= AMBVALSIZ;
1028                  }
1029 <                lastpos = flen - n;
1030 <                /*** seek always as safety measure
1031 <                if (n) ***/                     /* alignment */
1342 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1343 <                                goto seekerr;
1029 >                lastpos = flen - n;             /* check alignment */
1030 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1031 >                        goto seekerr;
1032          }
1033          n = fflush(ambfp);                      /* calls write() at last */
1034 <        if (n != EOF)
1347 <                lastpos += (long)nunflshed*AMBVALSIZ;
1348 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1349 <                goto seekerr;
1350 <                
1034 >        lastpos += (long)nunflshed*AMBVALSIZ;
1035          aflock(F_UNLCK);                        /* release file */
1036          nunflshed = 0;
1037          return(n);
1038   seekerr:
1039          error(SYSTEM, "seek failed in ambsync");
1040 <        return -1; /* pro forma return */
1040 >        return(EOF);    /* pro forma return */
1041   }
1042  
1043   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines