xref: /csrg-svn/lib/libc/stdio/ftell.c (revision 17951)
1*17951Sserge /* @(#)ftell.c	4.3 (Berkeley) 02/13/85 */
22011Swnj /*
32011Swnj  * Return file offset.
42011Swnj  * Coordinates with buffering.
52011Swnj  */
62011Swnj 
72011Swnj #include	<stdio.h>
82011Swnj long	lseek();
92011Swnj 
102011Swnj 
112011Swnj long ftell(iop)
12*17951Sserge register FILE *iop;
132011Swnj {
14*17951Sserge 	register long tres;
152011Swnj 	register adjust;
162011Swnj 
172011Swnj 	if (iop->_cnt < 0)
182011Swnj 		iop->_cnt = 0;
192011Swnj 	if (iop->_flag&_IOREAD)
202011Swnj 		adjust = - iop->_cnt;
213163Stoy 	else if (iop->_flag&(_IOWRT|_IORW)) {
222011Swnj 		adjust = 0;
233163Stoy 		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