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.93 by greg, Fri Nov 21 00:53:52 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 618 | Line 653 | avinsert(                              /* insert ambient value in our tree */
653   }
654  
655  
621 #else /* ! NEWAMB */
622
623 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
624                                AMBTREE *at, FVECT c0, double s);
625 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
626 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
627
628
629 void
630 multambient(            /* compute ambient component & multiply by coef. */
631        COLOR  aval,
632        RAY  *r,
633        FVECT  nrm
634 )
635 {
636        static int  rdepth = 0;                 /* ambient recursion */
637        COLOR   acol;
638        double  d, l;
639
640        if (ambdiv <= 0)                        /* no ambient calculation */
641                goto dumbamb;
642                                                /* check number of bounces */
643        if (rdepth >= ambounce)
644                goto dumbamb;
645                                                /* check ambient list */
646        if (ambincl != -1 && r->ro != NULL &&
647                        ambincl != inset(ambset, r->ro->omod))
648                goto dumbamb;
649
650        if (ambacc <= FTINY) {                  /* no ambient storage */
651                copycolor(acol, aval);
652                rdepth++;
653                d = doambient(acol, r, r->rweight, NULL, NULL);
654                rdepth--;
655                if (d <= FTINY)
656                        goto dumbamb;
657                copycolor(aval, acol);
658                return;
659        }
660
661        if (tracktime)                          /* sort to minimize thrashing */
662                sortambvals(0);
663                                                /* interpolate ambient value */
664        setcolor(acol, 0.0, 0.0, 0.0);
665        d = sumambient(acol, r, nrm, rdepth,
666                        &atrunk, thescene.cuorg, thescene.cusize);
667        if (d > FTINY) {
668                d = 1.0/d;
669                scalecolor(acol, d);
670                multcolor(aval, acol);
671                return;
672        }
673        rdepth++;                               /* need to cache new value */
674        d = makeambient(acol, r, nrm, rdepth-1);
675        rdepth--;
676        if (d > FTINY) {
677                multcolor(aval, acol);          /* got new value */
678                return;
679        }
680 dumbamb:                                        /* return global value */
681        if ((ambvwt <= 0) | (navsum == 0)) {
682                multcolor(aval, ambval);
683                return;
684        }
685        l = bright(ambval);                     /* average in computations */
686        if (l > FTINY) {
687                d = (log(l)*(double)ambvwt + avsum) /
688                                (double)(ambvwt + navsum);
689                d = exp(d) / l;
690                scalecolor(aval, d);
691                multcolor(aval, ambval);        /* apply color of ambval */
692        } else {
693                d = exp( avsum / (double)navsum );
694                scalecolor(aval, d);            /* neutral color */
695        }
696 }
697
698
699 static double
700 sumambient(     /* get interpolated ambient value */
701        COLOR  acol,
702        RAY  *r,
703        FVECT  rn,
704        int  al,
705        AMBTREE  *at,
706        FVECT  c0,
707        double  s
708 )
709 {
710        double  d, e1, e2, wt, wsum;
711        COLOR  ct;
712        FVECT  ck0;
713        int  i;
714        int  j;
715        AMBVAL   *av;
716
717        wsum = 0.0;
718                                        /* do this node */
719        for (av = at->alist; av != NULL; av = av->next) {
720                double  rn_dot = -2.0;
721                if (tracktime)
722                        av->latick = ambclock;
723                /*
724                 *  Ambient level test.
725                 */
726                if (av->lvl > al ||     /* list sorted, so this works */
727                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
728                        break;
729                /*
730                 *  Ambient radius test.
731                 */
732                VSUB(ck0, av->pos, r->rop);
733                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
734                if (e1 > ambacc*ambacc*1.21)
735                        continue;
736                /*
737                 *  Direction test using closest normal.
738                 */
739                d = DOT(av->dir, r->ron);
740                if (rn != r->ron) {
741                        rn_dot = DOT(av->dir, rn);
742                        if (rn_dot > 1.0-FTINY)
743                                rn_dot = 1.0-FTINY;
744                        if (rn_dot >= d-FTINY) {
745                                d = rn_dot;
746                                rn_dot = -2.0;
747                        }
748                }
749                e2 = (1.0 - d) * r->rweight;
750                if (e2 < 0.0)
751                        e2 = 0.0;
752                else if (e1 + e2 > ambacc*ambacc*1.21)
753                        continue;
754                /*
755                 *  Ray behind test.
756                 */
757                d = 0.0;
758                for (j = 0; j < 3; j++)
759                        d += (r->rop[j] - av->pos[j]) *
760                                        (av->dir[j] + r->ron[j]);
761                if (d*0.5 < -minarad*ambacc-.001)
762                        continue;
763                /*
764                 *  Jittering final test reduces image artifacts.
765                 */
766                e1 = sqrt(e1);
767                e2 = sqrt(e2);
768                wt = e1 + e2;
769                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
770                        continue;
771                /*
772                 *  Recompute directional error using perturbed normal
773                 */
774                if (rn_dot > 0.0) {
775                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
776                        wt = e1 + e2;
777                }
778                if (wt <= 1e-3)
779                        wt = 1e3;
780                else
781                        wt = 1.0 / wt;
782                wsum += wt;
783                extambient(ct, av, r->rop, rn);
784                scalecolor(ct, wt);
785                addcolor(acol, ct);
786        }
787        if (at->kid == NULL)
788                return(wsum);
789                                        /* do children */
790        s *= 0.5;
791        for (i = 0; i < 8; i++) {
792                for (j = 0; j < 3; j++) {
793                        ck0[j] = c0[j];
794                        if (1<<j & i)
795                                ck0[j] += s;
796                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
797                                break;
798                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
799                                break;
800                }
801                if (j == 3)
802                        wsum += sumambient(acol, r, rn, al,
803                                                at->kid+i, ck0, s);
804        }
805        return(wsum);
806 }
807
808
809 static double
810 makeambient(            /* make a new ambient value for storage */
811        COLOR  acol,
812        RAY  *r,
813        FVECT  rn,
814        int  al
815 )
816 {
817        AMBVAL  amb;
818        FVECT   gp, gd;
819        int     i;
820
821        amb.weight = 1.0;                       /* compute weight */
822        for (i = al; i-- > 0; )
823                amb.weight *= AVGREFL;
824        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
825                amb.weight = 1.25*r->rweight;
826        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
827                                                /* compute ambient */
828        amb.rad = doambient(acol, r, amb.weight, gp, gd);
829        if (amb.rad <= FTINY) {
830                setcolor(acol, 0.0, 0.0, 0.0);
831                return(0.0);
832        }
833        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
834                                                /* store value */
835        VCOPY(amb.pos, r->rop);
836        VCOPY(amb.dir, r->ron);
837        amb.lvl = al;
838        copycolor(amb.val, acol);
839        VCOPY(amb.gpos, gp);
840        VCOPY(amb.gdir, gd);
841                                                /* insert into tree */
842        avsave(&amb);                           /* and save to file */
843        if (rn != r->ron)
844                extambient(acol, &amb, r->rop, rn);     /* texture */
845        return(amb.rad);
846 }
847
848
656   static void
850 extambient(             /* extrapolate value at pv, nv */
851        COLOR  cr,
852        AMBVAL   *ap,
853        FVECT  pv,
854        FVECT  nv
855 )
856 {
857        FVECT  v1;
858        int  i;
859        double  d;
860
861        d = 1.0;                        /* zeroeth order */
862                                        /* gradient due to translation */
863        for (i = 0; i < 3; i++)
864                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
865                                        /* gradient due to rotation */
866        VCROSS(v1, ap->dir, nv);
867        d += DOT(ap->gdir, v1);
868        if (d <= 0.0) {
869                setcolor(cr, 0.0, 0.0, 0.0);
870                return;
871        }
872        copycolor(cr, ap->val);
873        scalecolor(cr, d);
874 }
875
876
877 static void
878 avinsert(                               /* insert ambient value in our tree */
879        AMBVAL *av
880 )
881 {
882        AMBTREE  *at;
883        AMBVAL  *ap;
884        AMBVAL  avh;
885        FVECT  ck0;
886        double  s;
887        int  branch;
888        int  i;
889
890        if (av->rad <= FTINY)
891                error(CONSISTENCY, "zero ambient radius in avinsert");
892        at = &atrunk;
893        VCOPY(ck0, thescene.cuorg);
894        s = thescene.cusize;
895        while (s*(OCTSCALE/2) > av->rad*ambacc) {
896                if (at->kid == NULL)
897                        if ((at->kid = newambtree()) == NULL)
898                                error(SYSTEM, "out of memory in avinsert");
899                s *= 0.5;
900                branch = 0;
901                for (i = 0; i < 3; i++)
902                        if (av->pos[i] > ck0[i] + s) {
903                                ck0[i] += s;
904                                branch |= 1 << i;
905                        }
906                at = at->kid + branch;
907        }
908        avh.next = at->alist;           /* order by increasing level */
909        for (ap = &avh; ap->next != NULL; ap = ap->next)
910                if ( ap->next->lvl > av->lvl ||
911                                (ap->next->lvl == av->lvl) &
912                                (ap->next->weight <= av->weight) )
913                        break;
914        av->next = ap->next;
915        ap->next = (AMBVAL*)av;
916        at->alist = avh.next;
917 }
918
919 #endif  /* ! NEWAMB */
920
921 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
922
923 static void
657   initambfile(            /* initialize ambient file */
658          int  cre8
659   )
# Line 1021 | 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 1047 | 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 1124 | 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 1168 | 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 1190 | 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 1212 | 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 1241 | 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 1262 | 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 1280 | 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 */
1286 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1287 <                                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)
1291 <                lastpos += (long)nunflshed*AMBVALSIZ;
1292 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1293 <                goto seekerr;
1294 <                
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