2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Jeremie Le Hen <jlh@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 /* This should not happen. */
53 change_buf(FILE *s, const char *bufmode)
60 if (bufmode[0] == '0' && bufmode[1] == '\0')
62 else if (bufmode[0] == 'L' && bufmode[1] == '\0')
64 else if (bufmode[0] == 'B' && bufmode[1] == '\0') {
69 * This library being preloaded, depending on libutil
70 * would lead to excessive namespace pollution.
71 * Thus we do not use expand_number().
74 bufsize = strtol(bufmode, &unit, 0);
75 if (errno == EINVAL || errno == ERANGE || unit == bufmode)
76 warn("Wrong buffer mode '%s' for %s", bufmode,
80 bufsize *= 1024 * 1024 * 1024;
83 bufsize *= 1024 * 1024;
91 warnx("Unknown suffix '%c' for %s", *unit,
97 if (setvbuf(s, NULL, mode, bufsize) != 0)
98 warn("Cannot set buffer mode '%s' for %s", bufmode,
102 __attribute__ ((constructor)) static void
105 char *i_mode, *o_mode, *e_mode;
107 i_mode = getenv("_STDBUF_I");
108 o_mode = getenv("_STDBUF_O");
109 e_mode = getenv("_STDBUF_E");
112 change_buf(stderr, e_mode);
114 change_buf(stdin, i_mode);
116 change_buf(stdout, o_mode);