Index | Thread | Search

From:
Klemens Nanni <kn@openbsd.org>
Subject:
env: -u name
To:
tech@openbsd.org
Date:
Sat, 27 Jul 2024 19:40:12 +0000

Download raw body.

Thread
  • Klemens Nanni:

    env: -u name

     -u name
             Remove name from the environment.

to hide some env var without involving sh -c 'unset A; ...'

  $ env -i A=1 B=2 ./obj/env -u A
  B=2

Trivial addition, I just don't understand why we return 126 on setenv(3) failure.

I'd return 1 in both un/setenv(3) failure cases, but that's not a hill I'd like
to die on, so diff below keeps -u failure's exit value consistent.

FreeBSD and GNU env(1) also have -u, the latter returning 125 on setenv failure.
With -u in base, I can ditch genv(1) from our sysutils/coreutils port.

Feedback? Objection? OK?


Index: env.1
===================================================================
RCS file: /cvs/src/usr.bin/env/env.1,v
diff -u -p -r1.20 env.1
--- env.1	12 Jan 2015 21:42:53 -0000	1.20
+++ env.1	27 Jul 2024 19:32:55 -0000
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm env
 .Op Fl i
+.Op Fl u Ar name
 .Oo
 .Ar name Ns = Ns Ar value ...
 .Oc
@@ -66,6 +67,10 @@ The options are as follows:
 Causes
 .Nm
 to completely ignore the environment it inherits.
+.It Fl u Ar name
+Remove
+.Ar name
+from the environment.
 .El
 .Pp
 If no
Index: env.c
===================================================================
RCS file: /cvs/src/usr.bin/env/env.c,v
diff -u -p -r1.17 env.c
--- env.c	28 Oct 2016 07:22:59 -0000	1.17
+++ env.c	27 Jul 2024 19:33:25 -0000
@@ -49,13 +49,17 @@ main(int argc, char *argv[])
 	if (pledge("stdio exec", NULL) == -1)
 		err(1, "pledge");
 
-	while ((ch = getopt(argc, argv, "i-")) != -1)
+	while ((ch = getopt(argc, argv, "-iu:")) != -1)
 		switch(ch) {
 		case '-':			/* obsolete */
 		case 'i':
 			if ((environ = calloc(1, sizeof(char *))) == NULL)
 				err(126, "calloc");
 			break;
+		case 'u':
+			if (unsetenv(optarg) == -1)
+				err(126, "unsetenv");
+			break;
 		default:
 			usage();
 		}
@@ -91,7 +95,7 @@ usage(void)
 {
 	extern char *__progname;
 
-	(void)fprintf(stderr, "usage: %s [-i] [name=value ...] "
+	(void)fprintf(stderr, "usage: %s [-i] [-u name] [name=value ...] "
 	    "[utility [argument ...]]\n", __progname);
 	exit(1);
 }