1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Jeremie Le Hen <[email protected]>
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 #define LIBSTDBUF32 "/usr/lib32/libstdbuf.so"
38
39 extern char *__progname;
40
41 static void
usage(int s)42 usage(int s)
43 {
44
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);
47 exit(s);
48 }
49
50 int
main(int argc,char * argv[])51 main(int argc, char *argv[])
52 {
53 char *ibuf, *obuf, *ebuf;
54 char *preload0, *preload1;
55 int i;
56
57 ibuf = obuf = ebuf = NULL;
58 while ((i = getopt(argc, argv, "e:i:o:")) != -1) {
59 switch (i) {
60 case 'e':
61 ebuf = optarg;
62 break;
63 case 'i':
64 ibuf = optarg;
65 break;
66 case 'o':
67 obuf = optarg;
68 break;
69 case '?':
70 default:
71 usage(1);
72 break;
73 }
74 }
75 argc -= optind;
76 argv += optind;
77 if (argc == 0)
78 exit(0);
79
80 if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1)
81 warn("Failed to set environment variable: %s=%s",
82 "_STDBUF_I", ibuf);
83 if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1)
84 warn("Failed to set environment variable: %s=%s",
85 "_STDBUF_O", obuf);
86 if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1)
87 warn("Failed to set environment variable: %s=%s",
88 "_STDBUF_E", ebuf);
89
90 preload0 = getenv("LD_PRELOAD");
91 if (preload0 == NULL)
92 i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF);
93 else
94 i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0,
95 LIBSTDBUF);
96
97 if (i < 0 || putenv(preload1) == -1)
98 warn("Failed to set environment variable: LD_PRELOAD");
99
100 preload0 = getenv("LD_32_PRELOAD");
101 if (preload0 == NULL)
102 i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32);
103 else
104 i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0,
105 LIBSTDBUF32);
106
107 if (i < 0 || putenv(preload1) == -1)
108 warn("Failed to set environment variable: LD_32_PRELOAD");
109
110 execvp(argv[0], argv);
111 err(2, "%s", argv[0]);
112 }
113