9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "ray.h" |
12 |
> |
#include <string.h> |
13 |
|
|
14 |
+ |
#include "platform.h" |
15 |
+ |
#include "ray.h" |
16 |
|
#include "otypes.h" |
17 |
< |
|
17 |
> |
#include "resolu.h" |
18 |
|
#include "ambient.h" |
17 |
– |
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
|
#ifndef OCTSCALE |
24 |
|
|
25 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
26 |
|
|
27 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
27 |
> |
#ifndef MAXASET |
28 |
> |
#define MAXASET 2047 /* maximum number of elements in ambient set */ |
29 |
> |
#endif |
30 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
31 |
|
|
32 |
|
double maxarad; /* maximum ambient radius */ |
38 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
39 |
|
|
40 |
|
#ifndef SORT_THRESH |
41 |
< |
#ifdef BIGMEM |
39 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
40 |
< |
#else |
41 |
> |
#ifdef SMLMEM |
42 |
|
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
43 |
+ |
#else |
44 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
45 |
|
#endif |
46 |
|
#endif |
47 |
|
#ifndef SORT_INTVL |
78 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
|
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
< |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
82 |
< |
static int avlmemi(); |
83 |
< |
static AMBVAL *avstore(); |
81 |
> |
static void initambfile(int creat); |
82 |
> |
static void avsave(AMBVAL *av); |
83 |
> |
static AMBVAL *avstore(AMBVAL *aval); |
84 |
> |
static AMBTREE *newambtree(void); |
85 |
> |
static void freeambtree(AMBTREE *atp); |
86 |
> |
|
87 |
> |
typedef void unloadtf_t(void *); |
88 |
> |
static unloadtf_t avinsert; |
89 |
> |
static unloadtf_t av2list; |
90 |
> |
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
91 |
> |
|
92 |
> |
static int aposcmp(const void *avp1, const void *avp2); |
93 |
> |
static int avlmemi(AMBVAL *avaddr); |
94 |
> |
static void sortambvals(int always); |
95 |
> |
|
96 |
|
#ifdef F_SETLKW |
97 |
< |
static void aflock(); |
97 |
> |
static void aflock(int typ); |
98 |
|
#endif |
99 |
|
|
100 |
|
|
101 |
< |
void |
102 |
< |
setambres(ar) /* set ambient resolution */ |
103 |
< |
int ar; |
101 |
> |
extern void |
102 |
> |
setambres( /* set ambient resolution */ |
103 |
> |
int ar |
104 |
> |
) |
105 |
|
{ |
106 |
|
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
107 |
|
/* set min & max radii */ |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
< |
void |
125 |
< |
setambacc(newa) /* set ambient accuracy */ |
126 |
< |
double newa; |
124 |
> |
extern void |
125 |
> |
setambacc( /* set ambient accuracy */ |
126 |
> |
double newa |
127 |
> |
) |
128 |
|
{ |
129 |
|
double ambdiff; |
130 |
|
|
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
< |
void |
140 |
< |
setambient() /* initialize calculation */ |
139 |
> |
extern void |
140 |
> |
setambient(void) /* initialize calculation */ |
141 |
|
{ |
142 |
|
int readonly = 0; |
143 |
|
long pos, flen; |
175 |
|
} |
176 |
|
/* align file pointer */ |
177 |
|
pos += (long)nambvals*AMBVALSIZ; |
178 |
< |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
178 |
> |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
179 |
|
if (flen != pos) { |
180 |
|
sprintf(errmsg, |
181 |
|
"ignoring last %ld values in ambient file (corrupted)", |
182 |
|
(flen - pos)/AMBVALSIZ); |
183 |
|
error(WARNING, errmsg); |
184 |
|
fseek(ambfp, pos, 0); |
185 |
+ |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
|
ftruncate(fileno(ambfp), (off_t)pos); |
187 |
+ |
#endif |
188 |
|
} |
189 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
|
initambfile(1); /* else create new file */ |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
< |
void |
201 |
< |
ambdone() /* close ambient file and free memory */ |
200 |
> |
extern void |
201 |
> |
ambdone(void) /* close ambient file and free memory */ |
202 |
|
{ |
203 |
|
if (ambfp != NULL) { /* close ambient file */ |
204 |
|
ambsync(); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
void |
227 |
< |
ambnotify(obj) /* record new modifier */ |
228 |
< |
OBJECT obj; |
226 |
> |
extern void |
227 |
> |
ambnotify( /* record new modifier */ |
228 |
> |
OBJECT obj |
229 |
> |
) |
230 |
|
{ |
231 |
|
static int hitlimit = 0; |
232 |
|
register OBJREC *o; |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
< |
void |
257 |
< |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
258 |
< |
COLOR acol; |
259 |
< |
register RAY *r; |
260 |
< |
FVECT nrm; |
256 |
> |
extern void |
257 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
258 |
> |
COLOR aval, |
259 |
> |
register RAY *r, |
260 |
> |
FVECT nrm |
261 |
> |
) |
262 |
|
{ |
263 |
|
static int rdepth = 0; /* ambient recursion */ |
264 |
+ |
COLOR acol; |
265 |
|
double d, l; |
266 |
|
|
267 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
276 |
|
|
277 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
278 |
|
rdepth++; |
279 |
< |
d = doambient(acol, r, r->rweight, NULL, NULL); |
279 |
> |
d = doambient(acol, r, aval, intens(aval)*r->rweight, |
280 |
> |
NULL, NULL); |
281 |
|
rdepth--; |
282 |
|
if (d <= FTINY) |
283 |
|
goto dumbamb; |
284 |
+ |
multcolor(aval, acol); |
285 |
|
return; |
286 |
|
} |
287 |
|
|
289 |
|
sortambvals(0); |
290 |
|
/* get ambient value */ |
291 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
292 |
< |
d = sumambient(acol, r, nrm, rdepth, |
292 |
> |
d = sumambient(acol, r, intens(aval)*r->rweight, nrm, rdepth, |
293 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
294 |
|
if (d > FTINY) { |
295 |
|
scalecolor(acol, 1.0/d); |
296 |
+ |
multcolor(aval, acol); |
297 |
|
return; |
298 |
|
} |
299 |
|
rdepth++; /* need to cache new value */ |
300 |
< |
d = makeambient(acol, r, nrm, rdepth-1); |
300 |
> |
d = makeambient(acol, r, aval, nrm, rdepth-1); |
301 |
|
rdepth--; |
302 |
< |
if (d > FTINY) |
302 |
> |
if (d > FTINY) { |
303 |
> |
multcolor(aval, acol); /* got new value */ |
304 |
|
return; |
305 |
+ |
} |
306 |
|
dumbamb: /* return global value */ |
307 |
< |
copycolor(acol, ambval); |
308 |
< |
if (ambvwt <= 0 | navsum == 0) |
307 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
308 |
> |
multcolor(aval, ambval); |
309 |
|
return; |
310 |
+ |
} |
311 |
|
l = bright(ambval); /* average in computations */ |
312 |
|
if (l > FTINY) { |
313 |
|
d = (log(l)*(double)ambvwt + avsum) / |
314 |
|
(double)(ambvwt + navsum); |
315 |
|
d = exp(d) / l; |
316 |
< |
scalecolor(acol, d); /* apply color of ambval */ |
316 |
> |
scalecolor(aval, d); |
317 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
318 |
|
} else { |
319 |
|
d = exp( avsum / (double)navsum ); |
320 |
< |
setcolor(acol, d, d, d); /* neutral color */ |
320 |
> |
scalecolor(aval, d); /* neutral color */ |
321 |
|
} |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
< |
double |
326 |
< |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
327 |
< |
COLOR acol; |
328 |
< |
register RAY *r; |
329 |
< |
FVECT rn; |
330 |
< |
int al; |
331 |
< |
AMBTREE *at; |
332 |
< |
FVECT c0; |
333 |
< |
double s; |
325 |
> |
extern double |
326 |
> |
sumambient( /* get interpolated ambient value */ |
327 |
> |
COLOR acol, |
328 |
> |
register RAY *r, |
329 |
> |
double rw, |
330 |
> |
FVECT rn, |
331 |
> |
int al, |
332 |
> |
AMBTREE *at, |
333 |
> |
FVECT c0, |
334 |
> |
double s |
335 |
> |
) |
336 |
|
{ |
337 |
|
double d, e1, e2, wt, wsum; |
338 |
|
COLOR ct; |
352 |
|
*/ |
353 |
|
if (av->lvl > al) /* list sorted, so this works */ |
354 |
|
break; |
355 |
< |
if (av->weight < r->rweight-FTINY) |
355 |
> |
if (av->weight < 0.9*rw) |
356 |
|
continue; |
357 |
|
/* |
358 |
|
* Ambient radius test. |
431 |
|
break; |
432 |
|
} |
433 |
|
if (j == 3) |
434 |
< |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
434 |
> |
wsum += sumambient(acol, r, rw, rn, al, |
435 |
> |
at->kid+i, ck0, s); |
436 |
|
} |
437 |
|
return(wsum); |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
< |
double |
442 |
< |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
443 |
< |
COLOR acol; |
444 |
< |
register RAY *r; |
445 |
< |
FVECT rn; |
446 |
< |
int al; |
441 |
> |
extern double |
442 |
> |
makeambient( /* make a new ambient value */ |
443 |
> |
COLOR acol, |
444 |
> |
RAY *r, |
445 |
> |
COLOR ac, |
446 |
> |
FVECT rn, |
447 |
> |
int al |
448 |
> |
) |
449 |
|
{ |
450 |
|
AMBVAL amb; |
451 |
+ |
double coef; |
452 |
|
FVECT gp, gd; |
453 |
|
/* compute weight */ |
454 |
|
amb.weight = pow(AVGREFL, (double)al); |
455 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
456 |
< |
amb.weight = r->rweight; |
455 |
> |
coef = intens(ac)*r->rweight; |
456 |
> |
if (coef < 0.1*amb.weight) /* heuristic */ |
457 |
> |
amb.weight = coef; |
458 |
|
/* compute ambient */ |
459 |
< |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
459 |
> |
amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); |
460 |
|
if (amb.rad <= FTINY) |
461 |
|
return(0.0); |
462 |
|
/* store it */ |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
< |
void |
478 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
479 |
< |
COLOR cr; |
480 |
< |
register AMBVAL *ap; |
481 |
< |
FVECT pv, nv; |
477 |
> |
extern void |
478 |
> |
extambient( /* extrapolate value at pv, nv */ |
479 |
> |
COLOR cr, |
480 |
> |
register AMBVAL *ap, |
481 |
> |
FVECT pv, |
482 |
> |
FVECT nv |
483 |
> |
) |
484 |
|
{ |
485 |
|
FVECT v1; |
486 |
|
register int i; |
503 |
|
|
504 |
|
|
505 |
|
static void |
506 |
< |
initambfile(creat) /* initialize ambient file */ |
507 |
< |
int creat; |
506 |
> |
initambfile( /* initialize ambient file */ |
507 |
> |
int creat |
508 |
> |
) |
509 |
|
{ |
510 |
|
extern char *progname, *octname; |
511 |
|
static char *mybuf = NULL; |
513 |
|
#ifdef F_SETLKW |
514 |
|
aflock(creat ? F_WRLCK : F_RDLCK); |
515 |
|
#endif |
516 |
< |
#ifdef MSDOS |
478 |
< |
setmode(fileno(ambfp), O_BINARY); |
479 |
< |
#endif |
516 |
> |
SET_FILE_BINARY(ambfp); |
517 |
|
if (mybuf == NULL) |
518 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
519 |
|
setbuf(ambfp, mybuf); |
540 |
|
|
541 |
|
|
542 |
|
static void |
543 |
< |
avsave(av) /* insert and save an ambient value */ |
544 |
< |
AMBVAL *av; |
543 |
> |
avsave( /* insert and save an ambient value */ |
544 |
> |
AMBVAL *av |
545 |
> |
) |
546 |
|
{ |
547 |
|
avinsert(avstore(av)); |
548 |
|
if (ambfp == NULL) |
559 |
|
|
560 |
|
|
561 |
|
static AMBVAL * |
562 |
< |
avstore(aval) /* allocate memory and store aval */ |
563 |
< |
register AMBVAL *aval; |
562 |
> |
avstore( /* allocate memory and store aval */ |
563 |
> |
register AMBVAL *aval |
564 |
> |
) |
565 |
|
{ |
566 |
|
register AMBVAL *av; |
567 |
|
double d; |
568 |
|
|
569 |
|
if ((av = newambval()) == NULL) |
570 |
|
error(SYSTEM, "out of memory in avstore"); |
571 |
< |
copystruct(av, aval); |
571 |
> |
*av = *aval; |
572 |
|
av->latick = ambclock; |
573 |
|
av->next = NULL; |
574 |
|
nambvals++; |
587 |
|
|
588 |
|
|
589 |
|
static AMBTREE * |
590 |
< |
newambtree() /* allocate 8 ambient tree structs */ |
590 |
> |
newambtree(void) /* allocate 8 ambient tree structs */ |
591 |
|
{ |
592 |
|
register AMBTREE *atp, *upperlim; |
593 |
|
|
603 |
|
} |
604 |
|
atp = atfreelist; |
605 |
|
atfreelist = atp->kid; |
606 |
< |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
606 |
> |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
607 |
|
return(atp); |
608 |
|
} |
609 |
|
|
610 |
|
|
611 |
|
static void |
612 |
< |
freeambtree(atp) /* free 8 ambient tree structs */ |
613 |
< |
AMBTREE *atp; |
612 |
> |
freeambtree( /* free 8 ambient tree structs */ |
613 |
> |
AMBTREE *atp |
614 |
> |
) |
615 |
|
{ |
616 |
|
atp->kid = atfreelist; |
617 |
|
atfreelist = atp; |
619 |
|
|
620 |
|
|
621 |
|
static void |
622 |
< |
avinsert(av) /* insert ambient value in our tree */ |
623 |
< |
register AMBVAL *av; |
622 |
> |
avinsert( /* insert ambient value in our tree */ |
623 |
> |
void *av |
624 |
> |
) |
625 |
|
{ |
626 |
|
register AMBTREE *at; |
627 |
|
register AMBVAL *ap; |
631 |
|
int branch; |
632 |
|
register int i; |
633 |
|
|
634 |
< |
if (av->rad <= FTINY) |
634 |
> |
if (((AMBVAL*)av)->rad <= FTINY) |
635 |
|
error(CONSISTENCY, "zero ambient radius in avinsert"); |
636 |
|
at = &atrunk; |
637 |
|
VCOPY(ck0, thescene.cuorg); |
638 |
|
s = thescene.cusize; |
639 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
639 |
> |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
640 |
|
if (at->kid == NULL) |
641 |
|
if ((at->kid = newambtree()) == NULL) |
642 |
|
error(SYSTEM, "out of memory in avinsert"); |
643 |
|
s *= 0.5; |
644 |
|
branch = 0; |
645 |
|
for (i = 0; i < 3; i++) |
646 |
< |
if (av->pos[i] > ck0[i] + s) { |
646 |
> |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
647 |
|
ck0[i] += s; |
648 |
|
branch |= 1 << i; |
649 |
|
} |
651 |
|
} |
652 |
|
avh.next = at->alist; /* order by increasing level */ |
653 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
654 |
< |
if (ap->next->lvl >= av->lvl) |
654 |
> |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
655 |
|
break; |
656 |
< |
av->next = ap->next; |
657 |
< |
ap->next = av; |
656 |
> |
((AMBVAL*)av)->next = ap->next; |
657 |
> |
ap->next = (AMBVAL*)av; |
658 |
|
at->alist = avh.next; |
659 |
|
} |
660 |
|
|
661 |
|
|
662 |
|
static void |
663 |
< |
unloadatree(at, f) /* unload an ambient value tree */ |
664 |
< |
register AMBTREE *at; |
665 |
< |
void (*f)(); |
663 |
> |
unloadatree( /* unload an ambient value tree */ |
664 |
> |
register AMBTREE *at, |
665 |
> |
unloadtf_t *f |
666 |
> |
) |
667 |
|
{ |
668 |
|
register AMBVAL *av; |
669 |
|
register int i; |
688 |
|
static AMBVAL **avlist2; /* memory positions for sorting */ |
689 |
|
static int i_avlist; /* index for lists */ |
690 |
|
|
691 |
+ |
static int alatcmp(const void *av1, const void *av2); |
692 |
|
|
693 |
< |
static int |
694 |
< |
av2list(av) |
695 |
< |
register AMBVAL *av; |
693 |
> |
static void |
694 |
> |
av2list( |
695 |
> |
void *av |
696 |
> |
) |
697 |
|
{ |
698 |
|
#ifdef DEBUG |
699 |
|
if (i_avlist >= nambvals) |
700 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
701 |
|
#endif |
702 |
< |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
703 |
< |
avlist1[i_avlist++].t = av->latick; |
702 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
703 |
> |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
704 |
|
} |
705 |
|
|
706 |
|
|
707 |
|
static int |
708 |
< |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
709 |
< |
struct avl *av1, *av2; |
708 |
> |
alatcmp( /* compare ambient values for MRA */ |
709 |
> |
const void *av1, |
710 |
> |
const void *av2 |
711 |
> |
) |
712 |
|
{ |
713 |
< |
register long lc = av2->t - av1->t; |
713 |
> |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
714 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
715 |
|
} |
716 |
|
|
722 |
|
* assumes pointers differ by exact struct size increments. |
723 |
|
*/ |
724 |
|
static int |
725 |
< |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
726 |
< |
const void *avp1, *avp2; |
725 |
> |
aposcmp( /* compare ambient value positions */ |
726 |
> |
const void *avp1, |
727 |
> |
const void *avp2 |
728 |
> |
) |
729 |
|
{ |
730 |
|
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
731 |
|
if (diff < 0) |
735 |
|
|
736 |
|
#if 1 |
737 |
|
static int |
738 |
< |
avlmemi(avaddr) /* find list position from address */ |
739 |
< |
AMBVAL *avaddr; |
738 |
> |
avlmemi( /* find list position from address */ |
739 |
> |
AMBVAL *avaddr |
740 |
> |
) |
741 |
|
{ |
742 |
|
register AMBVAL **avlpp; |
743 |
|
|
754 |
|
|
755 |
|
|
756 |
|
static void |
757 |
< |
sortambvals(always) /* resort ambient values */ |
758 |
< |
int always; |
757 |
> |
sortambvals( /* resort ambient values */ |
758 |
> |
int always |
759 |
> |
) |
760 |
|
{ |
761 |
|
AMBTREE oldatrunk; |
762 |
|
AMBVAL tav, *tap, *pnext; |
791 |
|
if (avlist2 != NULL) |
792 |
|
free((void *)avlist2); |
793 |
|
if (always) { /* rebuild without sorting */ |
794 |
< |
copystruct(&oldatrunk, &atrunk); |
794 |
> |
oldatrunk = atrunk; |
795 |
|
atrunk.alist = NULL; |
796 |
|
atrunk.kid = NULL; |
797 |
|
unloadatree(&oldatrunk, avinsert); |
822 |
|
if (avlist1[i].p == NULL) |
823 |
|
continue; |
824 |
|
tap = avlist2[i]; |
825 |
< |
copystruct(&tav, tap); |
825 |
> |
tav = *tap; |
826 |
|
for (j = i; (pnext = avlist1[j].p) != tap; |
827 |
|
j = avlmemi(pnext)) { |
828 |
< |
copystruct(avlist2[j], pnext); |
828 |
> |
*(avlist2[j]) = *pnext; |
829 |
|
avinsert(avlist2[j]); |
830 |
|
avlist1[j].p = NULL; |
831 |
|
} |
832 |
< |
copystruct(avlist2[j], &tav); |
832 |
> |
*(avlist2[j]) = tav; |
833 |
|
avinsert(avlist2[j]); |
834 |
|
avlist1[j].p = NULL; |
835 |
|
} |
854 |
|
#ifdef F_SETLKW |
855 |
|
|
856 |
|
static void |
857 |
< |
aflock(typ) /* lock/unlock ambient file */ |
858 |
< |
int typ; |
857 |
> |
aflock( /* lock/unlock ambient file */ |
858 |
> |
int typ |
859 |
> |
) |
860 |
|
{ |
861 |
|
static struct flock fls; /* static so initialized to zeroes */ |
862 |
|
|
866 |
|
} |
867 |
|
|
868 |
|
|
869 |
< |
int |
870 |
< |
ambsync() /* synchronize ambient file */ |
869 |
> |
extern int |
870 |
> |
ambsync(void) /* synchronize ambient file */ |
871 |
|
{ |
872 |
|
long flen; |
873 |
|
AMBVAL avs; |
880 |
|
/* gain exclusive access */ |
881 |
|
aflock(F_WRLCK); |
882 |
|
/* see if file has grown */ |
883 |
< |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
883 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
884 |
|
goto seekerr; |
885 |
< |
if (n = flen - lastpos) { /* file has grown */ |
885 |
> |
if ( (n = flen - lastpos) ) { /* file has grown */ |
886 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
887 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
888 |
|
if (ambinp == NULL) |
903 |
|
} |
904 |
|
/*** seek always as safety measure |
905 |
|
if (n) ***/ /* alignment */ |
906 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
906 |
> |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
907 |
|
goto seekerr; |
908 |
|
} |
909 |
|
#ifdef DEBUG |
916 |
|
#endif |
917 |
|
syncend: |
918 |
|
n = fflush(ambfp); /* calls write() at last */ |
919 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
919 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
920 |
|
goto seekerr; |
921 |
|
aflock(F_UNLCK); /* release file */ |
922 |
|
nunflshed = 0; |
923 |
|
return(n); |
924 |
|
seekerr: |
925 |
|
error(SYSTEM, "seek failed in ambsync"); |
926 |
+ |
return -1; /* pro forma return */ |
927 |
|
} |
928 |
|
|
929 |
|
#else |
930 |
|
|
931 |
< |
int |
932 |
< |
ambsync() /* flush ambient file */ |
931 |
> |
extern int |
932 |
> |
ambsync(void) /* flush ambient file */ |
933 |
|
{ |
934 |
|
if (nunflshed == 0) |
935 |
|
return(0); |