git: 78baa632092a - main - stdbuf: Code cleanup.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 29 Jun 2023 15:31:47 UTC
The branch main has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=78baa632092a87707481a47f47bfd45de9a072ef
commit 78baa632092a87707481a47f47bfd45de9a072ef
Author: Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-06-29 15:30:05 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
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|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] "
- "<cmd> [args ...]\n", __progname);
- exit(s);
+ fprintf(stderr,
+ "usage: stdbuf [-e 0|L|B|<sz>] [-i 0|L|B|<sz>] [-o 0|L|B|<sz>] "
+ "<cmd> [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]);