stdbuf.c revision 321883
1221828Sgrehan/*- 2221828Sgrehan * Copyright (c) 2012 Jeremie Le Hen <jlh@FreeBSD.org> 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17221828Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan * 26221828Sgrehan * $FreeBSD: stable/10/usr.bin/stdbuf/stdbuf.c 321883 2017-08-01 16:48:33Z asomers $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#include <err.h> 30221828Sgrehan#include <stdio.h> 31221828Sgrehan#include <stdlib.h> 32240941Sneel#include <unistd.h> 33267427Sjhb 34222610Sjhb#define LIBSTDBUF "/usr/lib/libstdbuf.so" 35240941Sneel#define LIBSTDBUF32 "/usr/lib32/libstdbuf.so" 36276349Sneel 37221828Sgrehanextern char *__progname; 38249324Sneel 39221828Sgrehanstatic void 40222610Sjhbusage(int s) 41267427Sjhb{ 42221828Sgrehan 43221828Sgrehan fprintf(stderr, "Usage: %s [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] " 44240941Sneel "<cmd> [args ...]\n", __progname); 45240941Sneel exit(s); 46267427Sjhb} 47221828Sgrehan 48221828Sgrehanint 49276349Sneelmain(int argc, char *argv[]) 50276349Sneel{ 51276349Sneel char *ibuf, *obuf, *ebuf; 52222610Sjhb char *preload0, *preload1; 53222610Sjhb int i; 54252335Sgrehan 55222610Sjhb ibuf = obuf = ebuf = NULL; 56252335Sgrehan while ((i = getopt(argc, argv, "e:i:o:")) != -1) { 57252335Sgrehan switch (i) { 58276349Sneel case 'e': 59276349Sneel ebuf = optarg; 60276349Sneel break; 61276349Sneel case 'i': 62276349Sneel ibuf = optarg; 63276349Sneel break; 64276349Sneel case 'o': 65276349Sneel obuf = optarg; 66276349Sneel break; 67276349Sneel case '?': 68276349Sneel default: 69276349Sneel usage(1); 70276349Sneel break; 71276349Sneel } 72276349Sneel } 73276349Sneel argc -= optind; 74276349Sneel argv += optind; 75276349Sneel if (argc == 0) 76276349Sneel exit(0); 77276349Sneel 78276349Sneel if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1) 79276349Sneel warn("Failed to set environment variable: %s=%s", 80276349Sneel "_STDBUF_I", ibuf); 81276349Sneel if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1) 82276349Sneel warn("Failed to set environment variable: %s=%s", 83276349Sneel "_STDBUF_O", obuf); 84221828Sgrehan if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1) 85240941Sneel warn("Failed to set environment variable: %s=%s", 86240941Sneel "_STDBUF_E", ebuf); 87221828Sgrehan 88267427Sjhb preload0 = getenv("LD_PRELOAD"); 89267427Sjhb if (preload0 == NULL) 90276349Sneel i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF); 91276349Sneel else 92240941Sneel i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0, 93221828Sgrehan LIBSTDBUF); 94222610Sjhb 95222610Sjhb if (i < 0 || putenv(preload1) == -1) 96222610Sjhb warn("Failed to set environment variable: LD_PRELOAD"); 97222610Sjhb 98222610Sjhb preload0 = getenv("LD_32_PRELOAD"); 99222610Sjhb if (preload0 == NULL) 100222610Sjhb i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32); 101222610Sjhb else 102222610Sjhb i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0, 103222610Sjhb LIBSTDBUF32); 104222610Sjhb 105222610Sjhb if (i < 0 || putenv(preload1) == -1) 106222610Sjhb warn("Failed to set environment variable: LD_32_PRELOAD"); 107221828Sgrehan 108246774Sneel execvp(argv[0], argv); 109246774Sneel err(2, "%s", argv[0]); 110222610Sjhb} 111222610Sjhb