diff --git a/lib/libstdbuf/libstdbuf.3 b/lib/libstdbuf/libstdbuf.3 index 6f3256c88e18..4857f7096499 100644 --- a/lib/libstdbuf/libstdbuf.3 +++ b/lib/libstdbuf/libstdbuf.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd April 28, 2012 +.Dd June 29, 2023 .Dt LIBSTDBUF 3 .Os .Sh NAME @@ -80,9 +80,12 @@ output will not stall until the full buffer fills. STDBUF_1=L awk '$2 > 1 || $3 > 1' | cat -n .Ed .Pp -See also the manpage of +See .Xr stdbuf 1 for a simpler way to do this. +.Sh SEE ALSO +.Xr rtld 1 , +.Xr stdbuf 1 .Sh HISTORY The .Nm diff --git a/usr.bin/stdbuf/stdbuf.c b/usr.bin/stdbuf/stdbuf.c index 02716ad60b06..6b09bb6149f5 100644 --- a/usr.bin/stdbuf/stdbuf.c +++ b/usr.bin/stdbuf/stdbuf.c @@ -34,22 +34,38 @@ #define LIBSTDBUF "/usr/lib/libstdbuf.so" #define LIBSTDBUF32 "/usr/lib32/libstdbuf.so" -extern char *__progname; +static int +appendenv(const char *key, const char *value) +{ + char *curval, *newpair; + int ret; + + curval = getenv(key); + if (curval == NULL) + ret = asprintf(&newpair, "%s=%s", key, value); + else + ret = asprintf(&newpair, "%s=%s:%s", key, curval, value); + if (ret > 0) + ret = putenv(newpair); + if (ret < 0) + warn("Failed to set environment variable: %s", key); + return (ret); +} static void -usage(int s) +usage(void) { - fprintf(stderr, "Usage: %s [-e 0|L|B|] [-i 0|L|B|] [-o 0|L|B|] " - " [args ...]\n", __progname); - exit(s); + fprintf(stderr, + "usage: stdbuf [-e 0|L|B|] [-i 0|L|B|] [-o 0|L|B|] " + " [args ...]\n"); + exit(1); } int main(int argc, char *argv[]) { char *ibuf, *obuf, *ebuf; - char *preload0, *preload1; int i; ibuf = obuf = ebuf = NULL; @@ -64,9 +80,8 @@ main(int argc, char *argv[]) case 'o': obuf = optarg; break; - case '?': default: - usage(1); + usage(); break; } } @@ -85,25 +100,8 @@ main(int argc, char *argv[]) warn("Failed to set environment variable: %s=%s", "_STDBUF_E", ebuf); - preload0 = getenv("LD_PRELOAD"); - if (preload0 == NULL) - i = asprintf(&preload1, "LD_PRELOAD=" LIBSTDBUF); - else - i = asprintf(&preload1, "LD_PRELOAD=%s:%s", preload0, - LIBSTDBUF); - - if (i < 0 || putenv(preload1) == -1) - warn("Failed to set environment variable: LD_PRELOAD"); - - preload0 = getenv("LD_32_PRELOAD"); - if (preload0 == NULL) - i = asprintf(&preload1, "LD_32_PRELOAD=" LIBSTDBUF32); - else - i = asprintf(&preload1, "LD_32_PRELOAD=%s:%s", preload0, - LIBSTDBUF32); - - if (i < 0 || putenv(preload1) == -1) - warn("Failed to set environment variable: LD_32_PRELOAD"); + appendenv("LD_PRELOAD", LIBSTDBUF); + appendenv("LD_32_PRELOAD", LIBSTDBUF32); execvp(argv[0], argv); err(2, "%s", argv[0]);