#ifndef lint
static  char sccsid[] = "@(#)filbuf.c 1.1 86/09/24 SMI"; /* from S5R2 2.1 */
#endif

static char rcsid[]=
  "$Header: /tmp_mnt/r/jove_staff3/mogul/code/NNstat/3.3beta-b/RCS/filbuf.c,v 1.5 1993/10/12 02:13:53 mogul Exp $";

/*LINTLIBRARY*/
#include <stdio.h>


#ifdef  ULTRIX
extern _getstdiobuf();
#else
extern _findbuf();
#endif  ULTRIX
extern int read();
extern int fflush();

int
_filbuf(iop)
register FILE *iop;
{

    if ( !(iop->_flag & _IOREAD) )
        if (iop->_flag & _IORW)
            iop->_flag |= _IOREAD;
        else
            return(EOF);

    if (iop->_flag&(
#if defined(_IOSTRG)
	_IOSTRG|
#endif
	_IOEOF))
        return(EOF);

    if (iop->_base == NULL)  /* get buffer if we don't have one */
#ifdef  ULTRIX
        _getstdiobuf(iop);
#else
        _findbuf(iop);
#endif  ULTRIX

    if (iop == stdin) {
        if (stdout->_flag&_IOLBF)
            (void) fflush(stdout);
        if (stderr->_flag&_IOLBF)
            (void) fflush(stderr);
    }

#if defined(__osf__)
    if ( (iop->_bufsiz == 0) && !(iop->_flag & _IONBF) ) {
	iop->_bufsiz = BUFSIZ;	/* XXX weird OSF stdio */
    }
#endif

    iop->_ptr = iop->_base;
/********************************************************
      Intercept read call: call STATread() instead...
 ********************************************************/
    iop->_cnt = STATread(fileno(iop), (char *)iop->_base,
        (unsigned)((iop->_flag & _IONBF) ? 1 :
#if defined(sgi)
# ifdef __SVR3
	/* this was good for IRIX 4, not for IRIX 5 */
	_bufsiz(iop)
# else
	/* would this work for IRIX 5? */
	(_bufendtab[iop->_file] - iop->_base)
# endif /* __SVR3 */
#else
	iop->_bufsiz
#endif /* sgi */
	 ));
    if (--iop->_cnt >= 0)       /* success */
        return (*iop->_ptr++);
    if (iop->_cnt != -1)        /* error */
        iop->_flag |= _IOERR;
    else {              /* end-of-file */
        iop->_flag |= _IOEOF;
        if (iop->_flag & _IORW)
            iop->_flag &= ~_IOREAD;
    }
    iop->_cnt = 0;
    return (EOF);
}
