Esempio n. 1
0
int
__sflush(FILE *fp)
{
	unsigned char *p;
	int n, t;

	t = fp->pub._flags;
	if ((t & __SWR) == 0)
		return (0);

	if ((p = fp->_bf._base) == NULL)
		return (0);

	n = fp->pub._p - p;	/* write this much */

	/*
	 * Set these immediately to avoid problems with longjmp and to allow
	 * exchange buffering (via setvbuf) in user write function.
	 */
	fp->pub._p = p;
	fp->pub._w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;

	for (; n > 0; n -= t, p += t) {
		t = _swrite(fp, (char *)p, n);
		if (t <= 0) {
			fp->pub._flags |= __SERR;
			return (EOF);
		}
	}
	return (0);
}
Esempio n. 2
0
/*
 * Write some memory regions.  Return zero on success, EOF on error.
 *
 * This routine is large and unsightly, but most of the ugliness due
 * to the three different kinds of output buffering is handled here.
 */
int
__sfvwrite(FILE *fp, struct __suio *uio)
{
	size_t len;
	char *p;
	struct __siov *iov;
	int w, s;
	char *nl;
	int nlknown, nldist;

	if ((len = uio->uio_resid) == 0)
		return (0);
	/* make sure we can write */
	if (prepwrite(fp) != 0)
		return (EOF);

#define	MIN(a, b)	((a) < (b) ? (a) : (b))
#define	COPY(n)		memcpy((void *)fp->pub._p, (void *)p, (size_t)(n))

	iov = uio->uio_iov;
	p = iov->iov_base;
	len = iov->iov_len;
	iov++;
#define	GETIOV(extra_work)		\
	while (len == 0) {		\
		extra_work;		\
		p = iov->iov_base;	\
		len = iov->iov_len;	\
		iov++;			\
	}
	if (fp->pub._flags & __SNBF) {
		/*
		 * Unbuffered: write up to BUFSIZ bytes at a time.
		 */
		do {
			GETIOV(;);
			w = _swrite(fp, p, MIN(len, BUFSIZ));
			if (w <= 0)
				goto err;
			p += w;
			len -= w;
		} while ((uio->uio_resid -= w) != 0);
	} else if ((fp->pub._flags & __SLBF) == 0) {