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
36 #define LIBSTDBUF "/usr/lib/libstdbuf.so"
37 #define LIBSTDBUF32 "/usr/lib32/libstdbuf.so"
39 extern char *__progname;
45 fprintf(stderr, "Usage: %s [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] "
46 "<cmd> [args ...]\n", __progname);
51 main(int argc, char *argv[])
53 char *ibuf, *obuf, *ebuf;
54 char *preload0, *preload1;
57 ibuf = obuf = ebuf = NULL;
58 while ((i = getopt(argc, argv, "e:i:o:")) != -1) {
80 if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1)
81 warn("Failed to set environment variable: %s=%s",
83 if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1)
84 warn("Failed to set environment variable: %s=%s",
86 if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1)
87 warn("Failed to set environment variable: %s=%s",
90 preload0 = getenv("LD_PRELOAD");
92 i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF);
94 i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0,
97 if (i < 0 || putenv(preload1) == -1)
98 warn("Failed to set environment variable: LD_PRELOAD");
100 preload0 = getenv("LD_32_PRELOAD");
101 if (preload0 == NULL)
102 i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32);
104 i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0,
107 if (i < 0 || putenv(preload1) == -1)
108 warn("Failed to set environment variable: LD_32_PRELOAD");
110 execvp(argv[0], argv);
111 err(2, "%s", argv[0]);