xref: /freebsd/usr.bin/stdbuf/stdbuf.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #include <err.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 #define	LIBSTDBUF	"/usr/lib/libstdbuf.so"
35 #define	LIBSTDBUF32	"/usr/lib32/libstdbuf.so"
36 
37 static int
38 appendenv(const char *key, const char *value)
39 {
40 	char *curval, *newpair;
41 	int ret;
42 
43 	curval = getenv(key);
44 	if (curval == NULL)
45 		ret = asprintf(&newpair, "%s=%s", key, value);
46 	else
47 		ret = asprintf(&newpair, "%s=%s:%s", key, curval, value);
48 	if (ret > 0)
49 		ret = putenv(newpair);
50 	if (ret < 0)
51 		warn("Failed to set environment variable: %s", key);
52 	return (ret);
53 }
54 
55 static void
56 usage(void)
57 {
58 
59 	fprintf(stderr,
60 	    "usage: stdbuf [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] "
61 	    "<cmd> [args ...]\n");
62 	exit(1);
63 }
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	char *ibuf, *obuf, *ebuf;
69 	int i;
70 
71 	ibuf = obuf = ebuf = NULL;
72 	while ((i = getopt(argc, argv, "e:i:o:")) != -1) {
73 		switch (i) {
74 		case 'e':
75 			ebuf = optarg;
76 			break;
77 		case 'i':
78 			ibuf = optarg;
79 			break;
80 		case 'o':
81 			obuf = optarg;
82 			break;
83 		default:
84 			usage();
85 			break;
86 		}
87 	}
88 	argc -= optind;
89 	argv += optind;
90 	if (argc == 0)
91 		exit(0);
92 
93 	if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1)
94 		warn("Failed to set environment variable: %s=%s",
95 		    "_STDBUF_I", ibuf);
96 	if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1)
97 		warn("Failed to set environment variable: %s=%s",
98 		    "_STDBUF_O", obuf);
99 	if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1)
100 		warn("Failed to set environment variable: %s=%s",
101 		    "_STDBUF_E", ebuf);
102 
103 	appendenv("LD_PRELOAD", LIBSTDBUF);
104 	appendenv("LD_32_PRELOAD", LIBSTDBUF32);
105 
106 	execvp(argv[0], argv);
107 	err(2, "%s", argv[0]);
108 }
109