| 1 | /* $OpenBSD: wsetup.c,v 1.7 2005/08/08 08:05:36 espie Exp $ */ |
| 2 | /* |
| 3 | * SPDX-License-Identifier: BSD-3-Clause |
| 4 | * |
| 5 | * Copyright (C) 1990, 1993 |
| 6 | * The Regents of the University of California. All rights reserved. |
| 7 | * |
| 8 | * This code is derived from software contributed to Berkeley by |
| 9 | * Chris Torek. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <assert.h> |
| 15 | #include "local.h" |
| 16 | |
| 17 | /* |
| 18 | * Various output routines call wsetup to be sure it is safe to write, |
| 19 | * because either _flags does not include __SWR, or _buf is NULL. |
| 20 | * _wsetup returns 0 if OK to write, nonzero otherwise. |
| 21 | */ |
| 22 | int |
| 23 | __swsetup(LTTNG_UST_LFILE *fp) |
| 24 | { |
| 25 | /* make sure stdio is set up */ |
| 26 | // if (!__sdidinit) |
| 27 | // __sinit(); |
| 28 | |
| 29 | /* |
| 30 | * If we are not writing, we had better be reading and writing. |
| 31 | */ |
| 32 | if ((fp->_flags & __SWR) == 0) { |
| 33 | if ((fp->_flags & __SRW) == 0) |
| 34 | return (EOF); |
| 35 | if (fp->_flags & __SRD) { |
| 36 | /* clobber any ungetc data */ |
| 37 | if (HASUB(fp)) |
| 38 | FREEUB(fp); |
| 39 | fp->_flags &= ~(__SRD|__SEOF); |
| 40 | fp->_r = 0; |
| 41 | fp->_p = fp->_bf._base; |
| 42 | } |
| 43 | fp->_flags |= __SWR; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Make a buffer if necessary, then set _w. |
| 48 | */ |
| 49 | if (fp->_bf._base == NULL) { |
| 50 | // if ((fp->_flags & (__SSTR | __SALC)) == __SSTR) |
| 51 | // return (EOF); |
| 52 | // __smakebuf(fp); |
| 53 | assert(0); |
| 54 | } |
| 55 | if (fp->_flags & __SLBF) { |
| 56 | /* |
| 57 | * It is line buffered, so make _lbfsize be -_bufsize |
| 58 | * for the putc() macro. We will change _lbfsize back |
| 59 | * to 0 whenever we turn off __SWR. |
| 60 | */ |
| 61 | fp->_w = 0; |
| 62 | fp->_lbfsize = -fp->_bf._size; |
| 63 | } else |
| 64 | fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size; |
| 65 | return (0); |
| 66 | } |