xref: /onnv-gate/usr/src/lib/libast/common/sfio/sftell.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #include	"sfhdr.h"
234887Schin 
244887Schin /*	Tell the current location in a given stream
254887Schin **
264887Schin **	Written by Kiem-Phong Vo.
274887Schin */
284887Schin 
294887Schin #if __STD_C
sftell(Sfio_t * f)308462SApril.Chin@Sun.COM Sfoff_t sftell(Sfio_t* f)
314887Schin #else
324887Schin Sfoff_t sftell(f)
338462SApril.Chin@Sun.COM Sfio_t	*f;
344887Schin #endif
354887Schin {
364887Schin 	reg int	mode;
374887Schin 	Sfoff_t	p;
388462SApril.Chin@Sun.COM 	SFMTXDECL(f);
394887Schin 
408462SApril.Chin@Sun.COM 	SFMTXENTER(f, (Sfoff_t)(-1));
414887Schin 
424887Schin 	/* set the stream to the right mode */
434887Schin 	if((mode = f->mode&SF_RDWR) != (int)f->mode && _sfmode(f,mode,0) < 0)
444887Schin 		SFMTXRETURN(f, (Sfoff_t)(-1));
454887Schin 
464887Schin 	/* throw away ungetc data */
474887Schin 	if(f->disc == _Sfudisc)
484887Schin 		(void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
494887Schin 
504887Schin 	if(f->flags&SF_STRING)
514887Schin 		SFMTXRETURN(f, (Sfoff_t)(f->next-f->data));
524887Schin 
534887Schin 	/* let sfseek() handle the hard case */
544887Schin 	if(f->extent >= 0 && (f->flags&(SF_SHARE|SF_APPENDWR)) )
554887Schin 		p = sfseek(f,(Sfoff_t)0,SEEK_CUR);
564887Schin 	else	p = f->here + ((f->mode&SF_WRITE) ? f->next-f->data : f->next-f->endb);
574887Schin 
584887Schin 	SFMTXRETURN(f,p);
594887Schin }
60