stdbuf.c revision 245419
1126274Sdes/*- 2126274Sdes * Copyright (c) 2012 Jeremie Le Hen <jlh@FreeBSD.org> 3126274Sdes * All rights reserved. 4126274Sdes * 5126274Sdes * Redistribution and use in source and binary forms, with or without 6126274Sdes * modification, are permitted provided that the following conditions 7126274Sdes * are met: 8126274Sdes * 1. Redistributions of source code must retain the above copyright 9126274Sdes * notice, this list of conditions and the following disclaimer. 10126274Sdes * 2. Redistributions in binary form must reproduce the above copyright 11126274Sdes * notice, this list of conditions and the following disclaimer in the 12126274Sdes * documentation and/or other materials provided with the distribution. 13126274Sdes * 14126274Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15126274Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16126274Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17126274Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18126274Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19126274Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20126274Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21126274Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22126274Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23126274Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24126274Sdes * SUCH DAMAGE. 25126274Sdes * 26126274Sdes * $FreeBSD: head/usr.bin/stdbuf/stdbuf.c 245419 2013-01-14 11:06:50Z jlh $ 27126274Sdes */ 28126274Sdes 29162852Sdes#include <err.h> 30162852Sdes#include <stdio.h> 31181111Sdes#include <stdlib.h> 32126274Sdes#include <unistd.h> 33162852Sdes 34162852Sdes#define LIBSTDBUF "/usr/lib/libstdbuf.so" 35126274Sdes#define LIBSTDBUF32 "/usr/lib32/libstdbuf.so" 36126274Sdes 37126274Sdesextern char *__progname; 38126274Sdes 39146998Sdesstatic void 40146998Sdesusage(int s) 41146998Sdes{ 42126274Sdes 43126274Sdes fprintf(stderr, "Usage: %s [-e 0|L|<sz>] [-i 0|L|<sz>] [-o 0|L|<sz>] " 44126274Sdes "<cmd> [args ...]\n", __progname); 45126274Sdes exit(s); 46126274Sdes} 47126274Sdes 48126274Sdesint 49126274Sdesmain(int argc, char *argv[]) 50126274Sdes{ 51126274Sdes char *ibuf, *obuf, *ebuf; 52126274Sdes char *preload0, *preload1; 53126274Sdes int i; 54126274Sdes 55126274Sdes ibuf = obuf = ebuf = NULL; 56126274Sdes while ((i = getopt(argc, argv, "e:i:o:")) != -1) { 57126274Sdes switch (i) { 58126274Sdes case 'e': 59126274Sdes ebuf = optarg; 60126274Sdes break; 61126274Sdes case 'i': 62126274Sdes ibuf = optarg; 63126274Sdes break; 64126274Sdes case 'o': 65126274Sdes obuf = optarg; 66126274Sdes break; 67126274Sdes case '?': 68126274Sdes default: 69126274Sdes usage(1); 70126274Sdes break; 71126274Sdes } 72126274Sdes } 73126274Sdes argc -= optind; 74126274Sdes argv += optind; 75126274Sdes if (argc == 0) 76126274Sdes exit(0); 77126274Sdes 78126274Sdes if (ibuf != NULL && setenv("_STDBUF_I", ibuf, 1) == -1) 79126274Sdes warn("Failed to set environment variable: %s=%s", 80126274Sdes "_STDBUF_I", ibuf); 81126274Sdes if (obuf != NULL && setenv("_STDBUF_O", obuf, 1) == -1) 82126274Sdes warn("Failed to set environment variable: %s=%s", 83126274Sdes "_STDBUF_O", obuf); 84126274Sdes if (ebuf != NULL && setenv("_STDBUF_E", ebuf, 1) == -1) 85126274Sdes warn("Failed to set environment variable: %s=%s", 86126274Sdes "_STDBUF_E", ebuf); 87126274Sdes 88126274Sdes preload0 = getenv("LD_PRELOAD"); 89126274Sdes if (preload0 == NULL) 90126274Sdes i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF); 91126274Sdes else 92126274Sdes i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0, 93126274Sdes LIBSTDBUF); 94126274Sdes 95126274Sdes if (i < 0 || putenv(preload1) == -1) 96126274Sdes warn("Failed to set environment variable: LD_PRELOAD"); 97126274Sdes 98126274Sdes preload0 = getenv("LD_32_PRELOAD"); 99126274Sdes if (preload0 == NULL) 100126274Sdes i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32); 101126274Sdes else 102126274Sdes i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0, 103126274Sdes LIBSTDBUF32); 104126274Sdes 105126274Sdes if (i < 0 || putenv(preload1) == -1) 106126274Sdes warn("Failed to set environment variable: LD_32_PRELOAD"); 107126274Sdes 108149749Sdes execvp(argv[0], argv); 109126274Sdes err(2, "%s", argv[0]); 110126274Sdes} 111126274Sdes