xref: /csrg-svn/lib/libc/stdio/ftell.c (revision 22135)
1*22135Smckusick #ifndef lint
2*22135Smckusick static char sccsid[] = "@(#)ftell.c	5.1 (Berkeley) 06/05/85";
3*22135Smckusick #endif not lint
4*22135Smckusick 
52011Swnj /*
62011Swnj  * Return file offset.
72011Swnj  * Coordinates with buffering.
82011Swnj  */
92011Swnj 
102011Swnj #include	<stdio.h>
112011Swnj long	lseek();
122011Swnj 
132011Swnj 
142011Swnj long ftell(iop)
1517951Sserge register FILE *iop;
162011Swnj {
1717951Sserge 	register long tres;
182011Swnj 	register adjust;
192011Swnj 
202011Swnj 	if (iop->_cnt < 0)
212011Swnj 		iop->_cnt = 0;
222011Swnj 	if (iop->_flag&_IOREAD)
232011Swnj 		adjust = - iop->_cnt;
243163Stoy 	else if (iop->_flag&(_IOWRT|_IORW)) {
252011Swnj 		adjust = 0;
263163Stoy 		if (iop->_flag&_IOWRT && iop->_base && (iop->_flag&_IONBF)==0)
272011Swnj 			adjust = iop->_ptr - iop->_base;
282011Swnj 	} else
292011Swnj 		return(-1);
302011Swnj 	tres = lseek(fileno(iop), 0L, 1);
312011Swnj 	if (tres<0)
322011Swnj 		return(tres);
332011Swnj 	tres += adjust;
342011Swnj 	return(tres);
352011Swnj }
36