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