From nobody Thu Jun 29 15:31:47 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QsMr36NDqz4kStj; Thu, 29 Jun 2023 15:31:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QsMr34tkwz44QM; Thu, 29 Jun 2023 15:31:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688052707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mDB5nUpntFvLiwAOqEYR09unh5prKsn6dFwXqoTKjNM=; b=D9OuBwpvKsxSA2YlyL+pSlBg+khFI9kxADxy8xSvIiqYLZpX4FshPY4iOyA9w+ueOcjvvO 4I25MK8cM3KQcDchQOoZ24B8bSXW8lPeaUaoAaGDIVvOiRllNxslt9Jio4MSeYBqcHx+H9 H0KD9Figz1tfV8Al6YHfPRRDQkeBLhPAgqq9CiY1ZrZtGW6WlEoZVFONBUhZmG1gSzazoi wHpW4W7Is4D7QkiMlHptn80rFm/L+xM+RvoSshg73veGA4BxTy8pmSKdl/CG7dmSMsXbsl YGPjSaElDGoboSFTz/bIPRj/263zlw5MyvZnLq+HyahQQFw23bD73wB1KFuvtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688052707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mDB5nUpntFvLiwAOqEYR09unh5prKsn6dFwXqoTKjNM=; b=MxIdXNG4L0qnCkZ7K2nSfQd0kWG2dA2f7LJw+OYcSCtvxd6XdlZAqhE4tBcYxeOgmDPSZw 803gFDXGipVZpl3wzs97ZW6YUBl3IJvIY26FTQyw7KAbvtf4jLX+LYDgneJjag+6jw9CmU c9Ki9xbybVQa5hu7HDgBfSFPKBaeBSoFN9sE3iwYmJFXPIk9vb4tLNeEmtHoNJFIH0X5hm RxDIM2SbJuF3JMuezP+yLBBZ0Lxd4wXk0i36gS1G3XmUaez5Epg/pBPPVHcB8h0r/ZNDef T6HSS4dFnJWegxLUrlCC4ZT7Nuns94bwPVeDEx2RD0+8QP45LMO5a28rYNpj4w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688052707; a=rsa-sha256; cv=none; b=U7237ba6oyGcyx0KO9308ykbiXgmgcuHNvdON3Gr7o8fACnhSqTO0G8p8VdOwfgGB9I7rH Y9MsJmR8YuXsq/28RUdJBOhcWGxPdqRStkojHtGyfM0JTipg91Kr/KF2kdMNiv0+iLapU5 W0TPewfiN4x6wj+zOROA4gkNcu2ijGcT2xzFp3JEIM0sSs8dqb4GH4eHw+HpZLesVlyVpO PeDXpeGEVOSR1nzzjPwAdSA/G1NPqXB1F/2AzV8GRA0lECnukmGxvVtSkqgdzzSYb9trQ1 /KJdcED7iHyGHiSlCKpfQ7kHozXMWlD0Ek+ZMGwSLE3PyYEici2lMgDwA51y4w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QsMr33h7QzMPd; Thu, 29 Jun 2023 15:31:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 35TFVl9D096373; Thu, 29 Jun 2023 15:31:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 35TFVlgY096372; Thu, 29 Jun 2023 15:31:47 GMT (envelope-from git) Date: Thu, 29 Jun 2023 15:31:47 GMT Message-Id: <202306291531.35TFVlgY096372@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: 78baa632092a - main - stdbuf: Code cleanup. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 78baa632092a87707481a47f47bfd45de9a072ef Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=78baa632092a87707481a47f47bfd45de9a072ef commit 78baa632092a87707481a47f47bfd45de9a072ef Author: Dag-Erling Smørgrav AuthorDate: 2023-06-29 15:30:05 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-06-29 15:30:05 +0000 stdbuf: Code cleanup. * Factor out path-setting code. * Normalize usage(). * Remove unnecessary switch case. Sponsored by: Klara, Inc. Reviewed by: kevans, imp Differential Revision: https://reviews.freebsd.org/D40799 --- usr.bin/stdbuf/stdbuf.c | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/usr.bin/stdbuf/stdbuf.c b/usr.bin/stdbuf/stdbuf.c index ecbcd50a64fb..d80555fd7a5e 100644 --- a/usr.bin/stdbuf/stdbuf.c +++ b/usr.bin/stdbuf/stdbuf.c @@ -36,22 +36,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; @@ -66,9 +82,8 @@ main(int argc, char *argv[]) case 'o': obuf = optarg; break; - case '?': default: - usage(1); + usage(); break; } } @@ -87,25 +102,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]);