1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 /* Get the size of a stream.
26 ** Written by Kiem-Phong Vo.
29 Sfoff_t
sfsize(Sfio_t
* f
)
40 SFMTXENTER(f
, (Sfoff_t
)(-1));
42 if((mode
= f
->mode
&SF_RDWR
) != (int)f
->mode
&& _sfmode(f
,mode
,0) < 0)
43 SFMTXRETURN(f
, (Sfoff_t
)(-1));
45 if(f
->flags
&SF_STRING
)
47 SFMTXRETURN(f
, f
->extent
);
55 { if(f
->flags
&(SF_SHARE
|SF_APPENDWR
))
56 { for(disc
= f
->disc
; disc
; disc
= disc
->disc
)
59 if(!_sys_stat
|| disc
)
61 if((e
= SFSK(f
,0,SEEK_END
,disc
)) >= 0)
63 if(SFSK(f
,f
->here
,SEEK_SET
,disc
) != f
->here
)
64 f
->here
= SFSK(f
,(Sfoff_t
)0,SEEK_CUR
,disc
);
69 if(sysfstatf(f
->file
,&st
) < 0)
71 else if((f
->extent
= st
.st_size
) < f
->here
)
72 f
->here
= SFSK(f
,(Sfoff_t
)0,SEEK_CUR
,disc
);
77 if((f
->flags
&(SF_SHARE
|SF_PUBLIC
)) == (SF_SHARE
|SF_PUBLIC
))
78 f
->here
= SFSK(f
,(Sfoff_t
)0,SEEK_CUR
,f
->disc
);
81 if(f
->here
!= s
&& (f
->mode
&SF_READ
) )
82 { /* buffered data is known to be invalid */
84 if((f
->bits
&SF_MMAP
) && f
->data
)
85 { SFMUNMAP(f
,f
->data
,f
->endb
-f
->data
);
86 f
->data
= NIL(uchar
*);
89 f
->next
= f
->endb
= f
->endr
= f
->endw
= f
->data
;
94 else if(f
->extent
< f
->here
)
97 if((s
= f
->extent
) >= 0)
98 { if(f
->flags
&SF_APPENDWR
)
99 s
+= (f
->next
- f
->data
);
100 else if(f
->mode
&SF_WRITE
)
101 { s
= f
->here
+ (f
->next
- f
->data
);