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: stable/10/usr.bin/stdbuf/stdbuf.c 321883 2017-08-01 16:48:33Z asomers $ 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" 35235142Sjlh#define LIBSTDBUF32 "/usr/lib32/libstdbuf.so" 36234772Sjlh 37234772Sjlhextern char *__progname; 38234772Sjlh 39234772Sjlhstatic void 40234772Sjlhusage(int s) 41234772Sjlh{ 42245419Sjlh 43321883Sasomers fprintf(stderr, "Usage: %s [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] " 44234772Sjlh "<cmd> [args ...]\n", __progname); 45234772Sjlh exit(s); 46234772Sjlh} 47234772Sjlh 48234772Sjlhint 49234772Sjlhmain(int argc, char *argv[]) 50234772Sjlh{ 51234772Sjlh char *ibuf, *obuf, *ebuf; 52234772Sjlh char *preload0, *preload1; 53234772Sjlh int i; 54234772Sjlh 55234772Sjlh ibuf = obuf = ebuf = NULL; 56234779Sjlh while ((i = getopt(argc, argv, "e:i:o:")) != -1) { 57234772Sjlh switch (i) { 58234772Sjlh case 'e': 59234772Sjlh ebuf = optarg; 60234772Sjlh break; 61234772Sjlh case 'i': 62234772Sjlh ibuf = optarg; 63234772Sjlh break; 64234772Sjlh case 'o': 65234772Sjlh obuf = optarg; 66234772Sjlh break; 67234772Sjlh case '?': 68234772Sjlh default: 69234772Sjlh usage(1); 70234772Sjlh break; 71234772Sjlh } 72234772Sjlh } 73234772Sjlh argc -= optind; 74234772Sjlh argv += optind; 75245418Sjlh if (argc == 0) 76245418Sjlh exit(0); 77234772Sjlh 78234772Sjlh if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1) 79234772Sjlh warn("Failed to set environment variable: %s=%s", 80234772Sjlh "_STDBUF_I", ibuf); 81234772Sjlh if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1) 82234772Sjlh warn("Failed to set environment variable: %s=%s", 83234772Sjlh "_STDBUF_O", obuf); 84234772Sjlh if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1) 85234772Sjlh warn("Failed to set environment variable: %s=%s", 86234772Sjlh "_STDBUF_E", ebuf); 87234772Sjlh 88234772Sjlh preload0 = getenv("LD_PRELOAD"); 89234772Sjlh if (preload0 == NULL) 90234772Sjlh i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF); 91234772Sjlh else 92234772Sjlh i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0, 93234772Sjlh LIBSTDBUF); 94234772Sjlh 95234772Sjlh if (i < 0 || putenv(preload1) == -1) 96235142Sjlh warn("Failed to set environment variable: LD_PRELOAD"); 97245419Sjlh 98235142Sjlh preload0 = getenv("LD_32_PRELOAD"); 99235142Sjlh if (preload0 == NULL) 100235142Sjlh i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32); 101235142Sjlh else 102235142Sjlh i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0, 103235142Sjlh LIBSTDBUF32); 104234772Sjlh 105235142Sjlh if (i < 0 || putenv(preload1) == -1) 106235142Sjlh warn("Failed to set environment variable: LD_32_PRELOAD"); 107235142Sjlh 108234772Sjlh execvp(argv[0], argv); 109234772Sjlh err(2, "%s", argv[0]); 110234772Sjlh} 111