stdbuf.c revision 234772
1234772Sjlh/*-
2234772Sjlh * Copyright (c) 2012 Jeremie Le Hen <jlh@FreeBSD.org>
3234772Sjlh * All rights reserved.
4234772Sjlh *
5234772Sjlh * Redistribution and use in source and binary forms, with or without
6234772Sjlh * modification, are permitted provided that the following conditions
7234772Sjlh * are met:
8234772Sjlh * 1. Redistributions of source code must retain the above copyright
9234772Sjlh *    notice, this list of conditions and the following disclaimer.
10234772Sjlh * 2. Redistributions in binary form must reproduce the above copyright
11234772Sjlh *    notice, this list of conditions and the following disclaimer in the
12234772Sjlh *    documentation and/or other materials provided with the distribution.
13234772Sjlh *
14234772Sjlh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15234772Sjlh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16234772Sjlh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17234772Sjlh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18234772Sjlh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19234772Sjlh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20234772Sjlh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21234772Sjlh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22234772Sjlh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23234772Sjlh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24234772Sjlh * SUCH DAMAGE.
25234772Sjlh *
26234772Sjlh * $FreeBSD: head/usr.bin/stdbuf/stdbuf.c 234772 2012-04-28 20:52:20Z jlh $
27234772Sjlh */
28234772Sjlh
29234772Sjlh#include <err.h>
30234772Sjlh#include <stdio.h>
31234772Sjlh#include <stdlib.h>
32234772Sjlh#include <unistd.h>
33234772Sjlh
34234772Sjlh#define	LIBSTDBUF	"/usr/lib/libstdbuf.so"
35234772Sjlh
36234772Sjlhextern char *__progname;
37234772Sjlh
38234772Sjlhstatic void
39234772Sjlhusage(int s)
40234772Sjlh{
41234772Sjlh
42234772Sjlh	fprintf(stderr, "Usage: %s [-e 0|L|<sz>] [-i 0|L|<sz>] [-o 0|L|<sz>] "
43234772Sjlh	    "<cmd> [args ...]\n", __progname);
44234772Sjlh	exit(s);
45234772Sjlh}
46234772Sjlh
47234772Sjlhint
48234772Sjlhmain(int argc, char *argv[])
49234772Sjlh{
50234772Sjlh	char *ibuf, *obuf, *ebuf;
51234772Sjlh	char *preload0, *preload1;
52234772Sjlh	int i;
53234772Sjlh
54234772Sjlh	ibuf = obuf = ebuf = NULL;
55234772Sjlh	while ((i = getopt(argc, argv, ":e:i:o:")) != -1) {
56234772Sjlh		switch (i) {
57234772Sjlh		case 'e':
58234772Sjlh			ebuf = optarg;
59234772Sjlh			break;
60234772Sjlh		case 'i':
61234772Sjlh			ibuf = optarg;
62234772Sjlh			break;
63234772Sjlh		case 'o':
64234772Sjlh			obuf = optarg;
65234772Sjlh			break;
66234772Sjlh		case ':':
67234772Sjlh			warnx("Missing argument for option -%c", optopt);
68234772Sjlh			usage(1);
69234772Sjlh			break;
70234772Sjlh		case '?':
71234772Sjlh		default:
72234772Sjlh			warnx("Unknown option: %c", optopt);
73234772Sjlh			usage(1);
74234772Sjlh			break;
75234772Sjlh		}
76234772Sjlh	}
77234772Sjlh	argc -= optind;
78234772Sjlh	argv += optind;
79234772Sjlh	if (argc < 2)
80234772Sjlh		usage(0);
81234772Sjlh
82234772Sjlh	if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1)
83234772Sjlh		warn("Failed to set environment variable: %s=%s",
84234772Sjlh		    "_STDBUF_I", ibuf);
85234772Sjlh	if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1)
86234772Sjlh		warn("Failed to set environment variable: %s=%s",
87234772Sjlh		    "_STDBUF_O", obuf);
88234772Sjlh	if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1)
89234772Sjlh		warn("Failed to set environment variable: %s=%s",
90234772Sjlh		    "_STDBUF_E", ebuf);
91234772Sjlh
92234772Sjlh	preload0 = getenv("LD_PRELOAD");
93234772Sjlh	if (preload0 == NULL)
94234772Sjlh		i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF);
95234772Sjlh	else
96234772Sjlh		i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0,
97234772Sjlh		    LIBSTDBUF);
98234772Sjlh
99234772Sjlh	if (i < 0 || putenv(preload1) == -1)
100234772Sjlh		warn("Failed to set environment variable: %s", preload1);
101234772Sjlh
102234772Sjlh	execvp(argv[0], argv);
103234772Sjlh	err(2, "%s", argv[0]);
104234772Sjlh}
105