xref: /csrg-svn/lib/libc/stdio/ftell.c (revision 3163)
1*3163Stoy /* @(#)ftell.c	4.2 (Berkeley) 03/09/81 */
22011Swnj /*
32011Swnj  * Return file offset.
42011Swnj  * Coordinates with buffering.
52011Swnj  */
62011Swnj 
72011Swnj #include	<stdio.h>
82011Swnj long	lseek();
92011Swnj 
102011Swnj 
112011Swnj long ftell(iop)
122011Swnj FILE *iop;
132011Swnj {
142011Swnj 	long tres;
152011Swnj 	register adjust;
162011Swnj 
172011Swnj 	if (iop->_cnt < 0)
182011Swnj 		iop->_cnt = 0;
192011Swnj 	if (iop->_flag&_IOREAD)
202011Swnj 		adjust = - iop->_cnt;
21*3163Stoy 	else if (iop->_flag&(_IOWRT|_IORW)) {
222011Swnj 		adjust = 0;
23*3163Stoy 		if (iop->_flag&_IOWRT && iop->_base && (iop->_flag&_IONBF)==0)
242011Swnj 			adjust = iop->_ptr - iop->_base;
252011Swnj 	} else
262011Swnj 		return(-1);
272011Swnj 	tres = lseek(fileno(iop), 0L, 1);
282011Swnj 	if (tres<0)
292011Swnj 		return(tres);
302011Swnj 	tres += adjust;
312011Swnj 	return(tres);
322011Swnj }
33