192 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
193 |
|
error(SYSTEM, errmsg); |
194 |
|
} |
195 |
< |
nunflshed++; /* lie */ |
196 |
< |
ambsync(); |
195 |
> |
ambsync(); /* load previous values */ |
196 |
|
} |
197 |
|
|
198 |
|
|
876 |
|
AMBVAL avs; |
877 |
|
register int n; |
878 |
|
|
879 |
< |
if (nunflshed == 0) |
879 |
> |
if (ambfp == NULL) /* no ambient file? */ |
880 |
|
return(0); |
881 |
|
if (lastpos < 0) /* initializing (locked in initambfile) */ |
882 |
|
goto syncend; |
883 |
< |
/* gain exclusive access */ |
884 |
< |
aflock(F_WRLCK); |
883 |
> |
/* gain appropriate access */ |
884 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
885 |
|
/* see if file has grown */ |
886 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
887 |
|
goto seekerr; |
904 |
|
avinsert(avstore(&avs)); |
905 |
|
n -= AMBVALSIZ; |
906 |
|
} |
907 |
+ |
lastpos = flen - n; |
908 |
|
/*** seek always as safety measure |
909 |
|
if (n) ***/ /* alignment */ |
910 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
910 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
911 |
|
goto seekerr; |
912 |
|
} |
913 |
|
#ifdef DEBUG |
920 |
|
#endif |
921 |
|
syncend: |
922 |
|
n = fflush(ambfp); /* calls write() at last */ |
923 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
924 |
< |
goto seekerr; |
923 |
> |
if ((n == EOF) | (lastpos < 0)) { |
924 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
925 |
> |
goto seekerr; |
926 |
> |
} else |
927 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
928 |
|
aflock(F_UNLCK); /* release file */ |
929 |
|
nunflshed = 0; |
930 |
|
return(n); |
938 |
|
extern int |
939 |
|
ambsync(void) /* flush ambient file */ |
940 |
|
{ |
941 |
< |
if (nunflshed == 0) |
941 |
> |
if (ambfp == NULL) |
942 |
|
return(0); |
943 |
|
nunflshed = 0; |
944 |
|
return(fflush(ambfp)); |