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.98 by greg, Sun Aug 23 00:17:12 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 216 | 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 257 | Line 264 | ambnotify(                     /* record new modifier */
264                  }
265   }
266  
260 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
267  
262 #ifndef OLDAMB
263
264 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
265
266 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
267 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
268                                AMBTREE *at, FVECT c0, double s);
269 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
270 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
271                                FVECT uvw[3]);
272
268   void
269   multambient(            /* compute ambient component & multiply by coef. */
270          COLOR  aval,
# Line 277 | 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 288 | 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 305 | 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 359 | 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 404 | 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 573 | 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 592 | 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 643 | Line 660 | avinsert(                              /* insert ambient value in our tree */
660   }
661  
662  
646 #else /* ! NEWAMB */
647
648 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
649                                AMBTREE *at, FVECT c0, double s);
650 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
651 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
652
653
654 void
655 multambient(            /* compute ambient component & multiply by coef. */
656        COLOR  aval,
657        RAY  *r,
658        FVECT  nrm
659 )
660 {
661        static int  rdepth = 0;                 /* ambient recursion */
662        COLOR   acol, caustic;
663        double  d, l;
664
665        /* PMAP: Factor in ambient from global photon map (if enabled) and return
666         * as all ambient components accounted for */
667        if (ambPmap(aval, r, rdepth))
668                return;
669
670        /* PMAP: Otherwise factor in ambient from caustic photon map
671         * (ambPmapCaustic() returns zero if caustic photons disabled) and
672         * continue with RADIANCE ambient calculation */
673        copycolor(caustic, aval);
674        ambPmapCaustic(caustic, r, rdepth);
675        
676        if (ambdiv <= 0)                        /* no ambient calculation */
677                goto dumbamb;
678                                                /* check number of bounces */
679        if (rdepth >= ambounce)
680                goto dumbamb;
681                                                /* check ambient list */
682        if (ambincl != -1 && r->ro != NULL &&
683                        ambincl != inset(ambset, r->ro->omod))
684                goto dumbamb;
685
686        if (ambacc <= FTINY) {                  /* no ambient storage */
687                copycolor(acol, aval);
688                rdepth++;
689                d = doambient(acol, r, r->rweight, NULL, NULL);
690                rdepth--;
691                if (d <= FTINY)
692                        goto dumbamb;
693                copycolor(aval, acol);          
694        
695           /* PMAP: add in caustic */
696                addcolor(aval, caustic);        
697                return;
698        }
699
700        if (tracktime)                          /* sort to minimize thrashing */
701                sortambvals(0);
702                                                /* interpolate ambient value */
703        setcolor(acol, 0.0, 0.0, 0.0);
704        d = sumambient(acol, r, nrm, rdepth,
705                        &atrunk, thescene.cuorg, thescene.cusize);
706                        
707        if (d > FTINY) {
708                d = 1.0/d;
709                scalecolor(acol, d);
710                multcolor(aval, acol);
711                
712                /* PMAP: add in caustic */
713                addcolor(aval, caustic);        
714                return;
715        }
716        
717        rdepth++;                               /* need to cache new value */
718        d = makeambient(acol, r, nrm, rdepth-1);
719        rdepth--;
720        
721        if (d > FTINY) {
722                multcolor(aval, acol);          /* got new value */
723
724                /* PMAP: add in caustic */
725                addcolor(aval, caustic);                        
726                return;
727        }
728        
729 dumbamb:                                        /* return global value */
730        if ((ambvwt <= 0) | (navsum == 0)) {
731                multcolor(aval, ambval);
732
733                /* PMAP: add in caustic */
734                addcolor(aval, caustic);        
735                return;
736        }
737        
738        l = bright(ambval);                     /* average in computations */
739        if (l > FTINY) {
740                d = (log(l)*(double)ambvwt + avsum) /
741                                (double)(ambvwt + navsum);
742                d = exp(d) / l;
743                scalecolor(aval, d);
744                multcolor(aval, ambval);        /* apply color of ambval */
745        } else {
746                d = exp( avsum / (double)navsum );
747                scalecolor(aval, d);            /* neutral color */
748        }
749 }
750
751
752 static double
753 sumambient(     /* get interpolated ambient value */
754        COLOR  acol,
755        RAY  *r,
756        FVECT  rn,
757        int  al,
758        AMBTREE  *at,
759        FVECT  c0,
760        double  s
761 )
762 {
763        double  d, e1, e2, wt, wsum;
764        COLOR  ct;
765        FVECT  ck0;
766        int  i;
767        int  j;
768        AMBVAL   *av;
769
770        wsum = 0.0;
771                                        /* do this node */
772        for (av = at->alist; av != NULL; av = av->next) {
773                double  rn_dot = -2.0;
774                if (tracktime)
775                        av->latick = ambclock;
776                /*
777                 *  Ambient level test.
778                 */
779                if (av->lvl > al ||     /* list sorted, so this works */
780                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
781                        break;
782                /*
783                 *  Ambient radius test.
784                 */
785                VSUB(ck0, av->pos, r->rop);
786                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
787                if (e1 > ambacc*ambacc*1.21)
788                        continue;
789                /*
790                 *  Direction test using closest normal.
791                 */
792                d = DOT(av->dir, r->ron);
793                if (rn != r->ron) {
794                        rn_dot = DOT(av->dir, rn);
795                        if (rn_dot > 1.0-FTINY)
796                                rn_dot = 1.0-FTINY;
797                        if (rn_dot >= d-FTINY) {
798                                d = rn_dot;
799                                rn_dot = -2.0;
800                        }
801                }
802                e2 = (1.0 - d) * r->rweight;
803                if (e2 < 0.0)
804                        e2 = 0.0;
805                else if (e1 + e2 > ambacc*ambacc*1.21)
806                        continue;
807                /*
808                 *  Ray behind test.
809                 */
810                d = 0.0;
811                for (j = 0; j < 3; j++)
812                        d += (r->rop[j] - av->pos[j]) *
813                                        (av->dir[j] + r->ron[j]);
814                if (d*0.5 < -minarad*ambacc-.001)
815                        continue;
816                /*
817                 *  Jittering final test reduces image artifacts.
818                 */
819                e1 = sqrt(e1);
820                e2 = sqrt(e2);
821                wt = e1 + e2;
822                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
823                        continue;
824                /*
825                 *  Recompute directional error using perturbed normal
826                 */
827                if (rn_dot > 0.0) {
828                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
829                        wt = e1 + e2;
830                }
831                if (wt <= 1e-3)
832                        wt = 1e3;
833                else
834                        wt = 1.0 / wt;
835                wsum += wt;
836                extambient(ct, av, r->rop, rn);
837                scalecolor(ct, wt);
838                addcolor(acol, ct);
839        }
840        if (at->kid == NULL)
841                return(wsum);
842                                        /* do children */
843        s *= 0.5;
844        for (i = 0; i < 8; i++) {
845                for (j = 0; j < 3; j++) {
846                        ck0[j] = c0[j];
847                        if (1<<j & i)
848                                ck0[j] += s;
849                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
850                                break;
851                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
852                                break;
853                }
854                if (j == 3)
855                        wsum += sumambient(acol, r, rn, al,
856                                                at->kid+i, ck0, s);
857        }
858        return(wsum);
859 }
860
861
862 static double
863 makeambient(            /* make a new ambient value for storage */
864        COLOR  acol,
865        RAY  *r,
866        FVECT  rn,
867        int  al
868 )
869 {
870        AMBVAL  amb;
871        FVECT   gp, gd;
872        int     i;
873
874        amb.weight = 1.0;                       /* compute weight */
875        for (i = al; i-- > 0; )
876                amb.weight *= AVGREFL;
877        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
878                amb.weight = 1.25*r->rweight;
879        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
880                                                /* compute ambient */
881        amb.rad = doambient(acol, r, amb.weight, gp, gd);
882        if (amb.rad <= FTINY) {
883                setcolor(acol, 0.0, 0.0, 0.0);
884                return(0.0);
885        }
886        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
887                                                /* store value */
888        VCOPY(amb.pos, r->rop);
889        VCOPY(amb.dir, r->ron);
890        amb.lvl = al;
891        copycolor(amb.val, acol);
892        VCOPY(amb.gpos, gp);
893        VCOPY(amb.gdir, gd);
894                                                /* insert into tree */
895        avsave(&amb);                           /* and save to file */
896        if (rn != r->ron)
897                extambient(acol, &amb, r->rop, rn);     /* texture */
898        return(amb.rad);
899 }
900
901
663   static void
903 extambient(             /* extrapolate value at pv, nv */
904        COLOR  cr,
905        AMBVAL   *ap,
906        FVECT  pv,
907        FVECT  nv
908 )
909 {
910        FVECT  v1;
911        int  i;
912        double  d;
913
914        d = 1.0;                        /* zeroeth order */
915                                        /* gradient due to translation */
916        for (i = 0; i < 3; i++)
917                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
918                                        /* gradient due to rotation */
919        VCROSS(v1, ap->dir, nv);
920        d += DOT(ap->gdir, v1);
921        if (d <= 0.0) {
922                setcolor(cr, 0.0, 0.0, 0.0);
923                return;
924        }
925        copycolor(cr, ap->val);
926        scalecolor(cr, d);
927 }
928
929
930 static void
931 avinsert(                               /* insert ambient value in our tree */
932        AMBVAL *av
933 )
934 {
935        AMBTREE  *at;
936        AMBVAL  *ap;
937        AMBVAL  avh;
938        FVECT  ck0;
939        double  s;
940        int  branch;
941        int  i;
942
943        if (av->rad <= FTINY)
944                error(CONSISTENCY, "zero ambient radius in avinsert");
945        at = &atrunk;
946        VCOPY(ck0, thescene.cuorg);
947        s = thescene.cusize;
948        while (s*(OCTSCALE/2) > av->rad*ambacc) {
949                if (at->kid == NULL)
950                        if ((at->kid = newambtree()) == NULL)
951                                error(SYSTEM, "out of memory in avinsert");
952                s *= 0.5;
953                branch = 0;
954                for (i = 0; i < 3; i++)
955                        if (av->pos[i] > ck0[i] + s) {
956                                ck0[i] += s;
957                                branch |= 1 << i;
958                        }
959                at = at->kid + branch;
960        }
961        avh.next = at->alist;           /* order by increasing level */
962        for (ap = &avh; ap->next != NULL; ap = ap->next)
963                if ( ap->next->lvl > av->lvl ||
964                                (ap->next->lvl == av->lvl) &
965                                (ap->next->weight <= av->weight) )
966                        break;
967        av->next = ap->next;
968        ap->next = (AMBVAL*)av;
969        at->alist = avh.next;
970 }
971
972 #endif  /* ! NEWAMB */
973
974 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
975
976 static void
664   initambfile(            /* initialize ambient file */
665          int  cre8
666   )
# Line 1074 | 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 1100 | 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 1177 | 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 1221 | 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 1243 | 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 1265 | 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 1294 | 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 1315 | 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 1333 | 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 */
1339 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1340 <                                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)
1344 <                lastpos += (long)nunflshed*AMBVALSIZ;
1345 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1346 <                goto seekerr;
1347 <                
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