|
Packit |
c04fcb |
/* $OpenBSD: fvwrite.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
|
|
Packit |
c04fcb |
/*-
|
|
Packit |
c04fcb |
* Copyright (c) 1990, 1993
|
|
Packit |
c04fcb |
* The Regents of the University of California. All rights reserved.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* This code is derived from software contributed to Berkeley by
|
|
Packit |
c04fcb |
* Chris Torek.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
c04fcb |
* modification, are permitted provided that the following conditions
|
|
Packit |
c04fcb |
* are met:
|
|
Packit |
c04fcb |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
c04fcb |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
c04fcb |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
c04fcb |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
c04fcb |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
c04fcb |
* 3. Neither the name of the University nor the names of its contributors
|
|
Packit |
c04fcb |
* may be used to endorse or promote products derived from this software
|
|
Packit |
c04fcb |
* without specific prior written permission.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
c04fcb |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
c04fcb |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
c04fcb |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
Packit |
c04fcb |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
c04fcb |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
c04fcb |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
c04fcb |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
c04fcb |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
c04fcb |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
c04fcb |
* SUCH DAMAGE.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
#include <stdio.h>
|
|
Packit |
c04fcb |
#include <stdlib.h>
|
|
Packit |
c04fcb |
#include <string.h>
|
|
Packit |
c04fcb |
#include <errno.h>
|
|
Packit |
c04fcb |
#include "local.h"
|
|
Packit |
c04fcb |
#include "fvwrite.h"
|
|
Packit |
c04fcb |
#include "various.h"
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Write some memory regions. Return zero on success, EOF on error.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* This routine is large and unsightly, but most of the ugliness due
|
|
Packit |
c04fcb |
* to the three different kinds of output buffering is handled here.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
int
|
|
Packit |
c04fcb |
__sfvwrite(LTTNG_UST_LFILE *fp, struct __lttng_ust_suio *uio)
|
|
Packit |
c04fcb |
{
|
|
Packit |
c04fcb |
size_t len;
|
|
Packit |
c04fcb |
char *p;
|
|
Packit |
c04fcb |
struct __lttng_ust_siov *iov;
|
|
Packit |
c04fcb |
int w, s;
|
|
Packit |
c04fcb |
char *nl;
|
|
Packit |
c04fcb |
int nlknown, nldist;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
if ((len = uio->uio_resid) == 0)
|
|
Packit |
c04fcb |
return (0);
|
|
Packit |
c04fcb |
/* make sure we can write */
|
|
Packit |
c04fcb |
if (cantwrite(fp)) {
|
|
Packit |
c04fcb |
errno = EBADF;
|
|
Packit |
c04fcb |
return (EOF);
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
Packit |
c04fcb |
#define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
iov = uio->uio_iov;
|
|
Packit |
c04fcb |
p = iov->iov_base;
|
|
Packit |
c04fcb |
len = iov->iov_len;
|
|
Packit |
c04fcb |
iov++;
|
|
Packit |
c04fcb |
#define GETIOV(extra_work) \
|
|
Packit |
c04fcb |
while (len == 0) { \
|
|
Packit |
c04fcb |
extra_work; \
|
|
Packit |
c04fcb |
p = iov->iov_base; \
|
|
Packit |
c04fcb |
len = iov->iov_len; \
|
|
Packit |
c04fcb |
iov++; \
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
if (fp->_flags & __SNBF) {
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Unbuffered: write up to BUFSIZ bytes at a time.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
do {
|
|
Packit |
c04fcb |
GETIOV(;);
|
|
Packit |
c04fcb |
w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
|
|
Packit |
c04fcb |
if (w <= 0)
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
p += w;
|
|
Packit |
c04fcb |
len -= w;
|
|
Packit |
c04fcb |
} while ((uio->uio_resid -= w) != 0);
|
|
Packit |
c04fcb |
} else if ((fp->_flags & __SLBF) == 0) {
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Fully buffered: fill partially full buffer, if any,
|
|
Packit |
c04fcb |
* and then flush. If there is no partial buffer, write
|
|
Packit |
c04fcb |
* one _bf._size byte chunk directly (without copying).
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* String output is a special case: write as many bytes
|
|
Packit |
c04fcb |
* as fit, but pretend we wrote everything. This makes
|
|
Packit |
c04fcb |
* snprintf() return the number of bytes needed, rather
|
|
Packit |
c04fcb |
* than the number used, and avoids its write function
|
|
Packit |
c04fcb |
* (so that the write function can be invalid).
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
do {
|
|
Packit |
c04fcb |
GETIOV(;);
|
|
Packit |
c04fcb |
if ((fp->_flags & (__SALC | __SSTR)) ==
|
|
Packit |
c04fcb |
(__SALC | __SSTR) && fp->_w < len) {
|
|
Packit |
c04fcb |
size_t blen = fp->_p - fp->_bf._base;
|
|
Packit |
c04fcb |
unsigned char *_base;
|
|
Packit |
c04fcb |
int _size;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Allocate space exponentially. */
|
|
Packit |
c04fcb |
_size = fp->_bf._size;
|
|
Packit |
c04fcb |
do {
|
|
Packit |
c04fcb |
_size = (_size << 1) + 1;
|
|
Packit |
c04fcb |
} while (_size < blen + len);
|
|
Packit |
c04fcb |
_base = realloc(fp->_bf._base, _size + 1);
|
|
Packit |
c04fcb |
if (_base == NULL)
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
fp->_w += _size - fp->_bf._size;
|
|
Packit |
c04fcb |
fp->_bf._base = _base;
|
|
Packit |
c04fcb |
fp->_bf._size = _size;
|
|
Packit |
c04fcb |
fp->_p = _base + blen;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
w = fp->_w;
|
|
Packit |
c04fcb |
if (fp->_flags & __SSTR) {
|
|
Packit |
c04fcb |
if (len < w)
|
|
Packit |
c04fcb |
w = len;
|
|
Packit |
c04fcb |
COPY(w); /* copy MIN(fp->_w,len), */
|
|
Packit |
c04fcb |
fp->_w -= w;
|
|
Packit |
c04fcb |
fp->_p += w;
|
|
Packit |
c04fcb |
w = len; /* but pretend copied all */
|
|
Packit |
c04fcb |
} else if (fp->_p > fp->_bf._base && len > w) {
|
|
Packit |
c04fcb |
/* fill and flush */
|
|
Packit |
c04fcb |
COPY(w);
|
|
Packit |
c04fcb |
/* fp->_w -= w; */ /* unneeded */
|
|
Packit |
c04fcb |
fp->_p += w;
|
|
Packit |
c04fcb |
if (ust_safe_fflush(fp))
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
} else if (len >= (w = fp->_bf._size)) {
|
|
Packit |
c04fcb |
/* write directly */
|
|
Packit |
c04fcb |
w = (*fp->_write)(fp->_cookie, p, w);
|
|
Packit |
c04fcb |
if (w <= 0)
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
} else {
|
|
Packit |
c04fcb |
/* fill and done */
|
|
Packit |
c04fcb |
w = len;
|
|
Packit |
c04fcb |
COPY(w);
|
|
Packit |
c04fcb |
fp->_w -= w;
|
|
Packit |
c04fcb |
fp->_p += w;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
p += w;
|
|
Packit |
c04fcb |
len -= w;
|
|
Packit |
c04fcb |
} while ((uio->uio_resid -= w) != 0);
|
|
Packit |
c04fcb |
} else {
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Line buffered: like fully buffered, but we
|
|
Packit |
c04fcb |
* must check for newlines. Compute the distance
|
|
Packit |
c04fcb |
* to the first newline (including the newline),
|
|
Packit |
c04fcb |
* or `infinity' if there is none, then pretend
|
|
Packit |
c04fcb |
* that the amount to write is MIN(len,nldist).
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
nlknown = 0;
|
|
Packit |
c04fcb |
nldist = 0; /* XXX just to keep gcc happy */
|
|
Packit |
c04fcb |
do {
|
|
Packit |
c04fcb |
GETIOV(nlknown = 0);
|
|
Packit |
c04fcb |
if (!nlknown) {
|
|
Packit |
c04fcb |
nl = memchr((void *)p, '\n', len);
|
|
Packit |
c04fcb |
nldist = nl ? nl + 1 - p : len + 1;
|
|
Packit |
c04fcb |
nlknown = 1;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
s = MIN(len, nldist);
|
|
Packit |
c04fcb |
w = fp->_w + fp->_bf._size;
|
|
Packit |
c04fcb |
if (fp->_p > fp->_bf._base && s > w) {
|
|
Packit |
c04fcb |
COPY(w);
|
|
Packit |
c04fcb |
/* fp->_w -= w; */
|
|
Packit |
c04fcb |
fp->_p += w;
|
|
Packit |
c04fcb |
if (ust_safe_fflush(fp))
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
} else if (s >= (w = fp->_bf._size)) {
|
|
Packit |
c04fcb |
w = (*fp->_write)(fp->_cookie, p, w);
|
|
Packit |
c04fcb |
if (w <= 0)
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
} else {
|
|
Packit |
c04fcb |
w = s;
|
|
Packit |
c04fcb |
COPY(w);
|
|
Packit |
c04fcb |
fp->_w -= w;
|
|
Packit |
c04fcb |
fp->_p += w;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
if ((nldist -= w) == 0) {
|
|
Packit |
c04fcb |
/* copied the newline: flush and forget */
|
|
Packit |
c04fcb |
if (ust_safe_fflush(fp))
|
|
Packit |
c04fcb |
goto err;
|
|
Packit |
c04fcb |
nlknown = 0;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
p += w;
|
|
Packit |
c04fcb |
len -= w;
|
|
Packit |
c04fcb |
} while ((uio->uio_resid -= w) != 0);
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
return (0);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
err:
|
|
Packit |
c04fcb |
fp->_flags |= __SERR;
|
|
Packit |
c04fcb |
return (EOF);
|
|
Packit |
c04fcb |
}
|