| 1 | /* $OpenBSD: fflush.c,v 1.7 2009/10/22 01:23:16 guenther 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 <errno.h> |
| 13 | #include <stdio.h> |
| 14 | #include "local.h" |
| 15 | |
| 16 | /* Flush a single file, or (if fp is NULL) all files. */ |
| 17 | int ust_safe_fflush(LTTNG_UST_LFILE *fp) |
| 18 | { |
| 19 | |
| 20 | if (fp == NULL) |
| 21 | return 0; |
| 22 | // return (_fwalk(__sflush)); |
| 23 | if ((fp->_flags & (__SWR | __SRW)) == 0) { |
| 24 | errno = EBADF; |
| 25 | return (EOF); |
| 26 | } |
| 27 | return (__sflush(fp)); |
| 28 | } |
| 29 | |
| 30 | int |
| 31 | __sflush(LTTNG_UST_LFILE *fp) |
| 32 | { |
| 33 | unsigned char *p; |
| 34 | int n, t; |
| 35 | |
| 36 | t = fp->_flags; |
| 37 | if ((t & __SWR) == 0) |
| 38 | return (0); |
| 39 | |
| 40 | if ((p = fp->_bf._base) == NULL) |
| 41 | return (0); |
| 42 | |
| 43 | n = fp->_p - p; /* write this much */ |
| 44 | |
| 45 | /* |
| 46 | * Set these immediately to avoid problems with longjmp and to allow |
| 47 | * exchange buffering (via setvbuf) in user write function. |
| 48 | */ |
| 49 | fp->_p = p; |
| 50 | fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; |
| 51 | |
| 52 | for (; n > 0; n -= t, p += t) { |
| 53 | t = (*fp->_write)(fp->_cookie, (char *)p, n); |
| 54 | if (t <= 0) { |
| 55 | fp->_flags |= __SERR; |
| 56 | return (EOF); |
| 57 | } |
| 58 | } |
| 59 | return (0); |
| 60 | } |