1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Jeremie Le Hen <jlh@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #define LIBSTDBUF "/usr/lib/libstdbuf.so"
37
38 extern char *__progname;
39
40 static void
usage(int s)41 usage(int s)
42 {
43
44 fprintf(stderr, "Usage: %s [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] "
45 "<cmd> [args ...]\n", __progname);
46 exit(s);
47 }
48
49 int
main(int argc,char * argv[])50 main(int argc, char *argv[])
51 {
52 char *ibuf, *obuf, *ebuf;
53 char *preload0, *preload1;
54 int i;
55
56 ibuf = obuf = ebuf = NULL;
57 while ((i = getopt(argc, argv, "e:i:o:")) != -1) {
58 switch (i) {
59 case 'e':
60 ebuf = optarg;
61 break;
62 case 'i':
63 ibuf = optarg;
64 break;
65 case 'o':
66 obuf = optarg;
67 break;
68 case '?':
69 default:
70 usage(1);
71 break;
72 }
73 }
74 argc -= optind;
75 argv += optind;
76 if (argc == 0)
77 exit(0);
78
79 if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1)
80 warn("Failed to set environment variable: %s=%s",
81 "_STDBUF_I", ibuf);
82 if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1)
83 warn("Failed to set environment variable: %s=%s",
84 "_STDBUF_O", obuf);
85 if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1)
86 warn("Failed to set environment variable: %s=%s",
87 "_STDBUF_E", ebuf);
88
89 preload0 = getenv("LD_PRELOAD");
90 if (preload0 == NULL)
91 i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF);
92 else
93 i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0,
94 LIBSTDBUF);
95
96 if (i < 0 || putenv(preload1) == -1)
97 warn("Failed to set environment variable: LD_PRELOAD");
98
99 execvp(argv[0], argv);
100 err(2, "%s", argv[0]);
101 }
102