/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfhdr.h"
/* Fill the buffer of a stream with data.
** If n < 0, sffilbuf() attempts to fill the buffer if it's empty.
** If n == 0, if the buffer is not empty, just return the first byte;
** otherwise fill the buffer and return the first byte.
** If n > 0, even if the buffer is not empty, try a read to get as
** close to n as possible. n is reset to -1 if stack pops.
**
** Written by Kiem-Phong Vo
*/
#if __STD_C
#else
int _sffilbuf(f,n)
Sfio_t* f; /* fill the read buffer of this stream */
reg int n; /* see above */
#endif
{
SFMTXDECL(f);
SFMTXENTER(f,-1);
/* any peek data must be preserved across stacked streams */
{ /* check mode */
SFMTXRETURN(f,-1);
/* current extent of available data */
{ /* on first iteration, n is amount beyond current buffer;
afterward, n is the exact amount requested */
break;
/* try shifting left to make room for new data */
{ ssize_t s = r;
/* try to maintain block alignment */
if(s+n > f->size)
s = r;
}
}
}
r = n > 0 ? n : f->size;
if(n > 0)
r = n; /* read only as much as requested */
r = f->iosz; /* limit buffer filling */
}
}
/* SFRD takes care of discipline read and stack popping */
break;
}
}
SFMTXRETURN(f,rcrv);
}