Blame snprintf/wsetup.c

Packit c04fcb
/*	$OpenBSD: wsetup.c,v 1.7 2005/08/08 08:05:36 espie 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 <assert.h>
Packit c04fcb
#include "local.h"
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Various output routines call wsetup to be sure it is safe to write,
Packit c04fcb
 * because either _flags does not include __SWR, or _buf is NULL.
Packit c04fcb
 * _wsetup returns 0 if OK to write, nonzero otherwise.
Packit c04fcb
 */
Packit c04fcb
int
Packit c04fcb
__swsetup(LTTNG_UST_LFILE *fp)
Packit c04fcb
{
Packit c04fcb
	/* make sure stdio is set up */
Packit c04fcb
//	if (!__sdidinit)
Packit c04fcb
//		__sinit();
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * If we are not writing, we had better be reading and writing.
Packit c04fcb
	 */
Packit c04fcb
	if ((fp->_flags & __SWR) == 0) {
Packit c04fcb
		if ((fp->_flags & __SRW) == 0)
Packit c04fcb
			return (EOF);
Packit c04fcb
		if (fp->_flags & __SRD) {
Packit c04fcb
			/* clobber any ungetc data */
Packit c04fcb
			if (HASUB(fp))
Packit c04fcb
				FREEUB(fp);
Packit c04fcb
			fp->_flags &= ~(__SRD|__SEOF);
Packit c04fcb
			fp->_r = 0;
Packit c04fcb
			fp->_p = fp->_bf._base;
Packit c04fcb
		}
Packit c04fcb
		fp->_flags |= __SWR;
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	/*
Packit c04fcb
	 * Make a buffer if necessary, then set _w.
Packit c04fcb
	 */
Packit c04fcb
	if (fp->_bf._base == NULL) {
Packit c04fcb
//		if ((fp->_flags & (__SSTR | __SALC)) == __SSTR)
Packit c04fcb
//			return (EOF);
Packit c04fcb
//		__smakebuf(fp);
Packit c04fcb
		assert(0);
Packit c04fcb
	}
Packit c04fcb
	if (fp->_flags & __SLBF) {
Packit c04fcb
		/*
Packit c04fcb
		 * It is line buffered, so make _lbfsize be -_bufsize
Packit c04fcb
		 * for the putc() macro.  We will change _lbfsize back
Packit c04fcb
		 * to 0 whenever we turn off __SWR.
Packit c04fcb
		 */
Packit c04fcb
		fp->_w = 0;
Packit c04fcb
		fp->_lbfsize = -fp->_bf._size;
Packit c04fcb
	} else
Packit c04fcb
		fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size;
Packit c04fcb
	return (0);
Packit c04fcb
}