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