Module Name:    src
Committed By:   kamil
Date:           Sat Feb  8 10:30:22 UTC 2020

Modified Files:
        src/usr.bin/env: env.1 env.c

Log Message:
Implement env(1) -u

The option '-u name' causes removal of the name environment variable if
it is in the environment.  This is similar to the unset command in sh(1).
The value for name must not include the '=' character.

Add HISTORY section in the man page.

The -u argument is implemented by Linux, FreeBSD and Darwin.
The lack of -u in NetBSD is a cause for a frequent fallout in the LLVM
toolchain test-suite.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/usr.bin/env/env.1
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/env/env.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/env/env.1
diff -u src/usr.bin/env/env.1:1.12 src/usr.bin/env/env.1:1.13
--- src/usr.bin/env/env.1:1.12	Fri Jun  8 18:20:42 2007
+++ src/usr.bin/env/env.1	Sat Feb  8 10:30:22 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: env.1,v 1.12 2007/06/08 18:20:42 wiz Exp $
+.\"	$NetBSD: env.1,v 1.13 2020/02/08 10:30:22 kamil Exp $
 .\"
 .\" Copyright (c) 1980, 1990 The Regents of the University of California.
 .\" All rights reserved.
@@ -30,9 +30,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	from: @(#)printenv.1	6.7 (Berkeley) 7/28/91
-.\"	$NetBSD: env.1,v 1.12 2007/06/08 18:20:42 wiz Exp $
+.\"	$NetBSD: env.1,v 1.13 2020/02/08 10:30:22 kamil Exp $
 .\"
-.Dd June 8, 2007
+.Dd February 8, 2020
 .Dt ENV 1
 .Os
 .Sh NAME
@@ -41,6 +41,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl i
+.Op Fl u Ar name
 .Op Ar name=value ...
 .Oo
 .Ar utility
@@ -66,6 +67,21 @@ causes
 to completely ignore the environment
 it inherits.
 .Pp
+The option
+.Sq Fl u Ar name
+causes removal of the
+.Ar name
+environment variable if it is in the environment.
+This is similar to the
+.Ic unset
+command in
+.Xr sh 1 .
+The value for
+.Ar name
+must not include the
+.Ql =
+character.
+.Pp
 If no
 .Ar utility
 is specified,
@@ -106,6 +122,10 @@ could not be found.
 The historic
 .Fl
 option has been deprecated but is still supported in this implementation.
+.Pp
+The
+.Fl u
+option is a non-standard extension.
 .Sh SEE ALSO
 .Xr execvp 3 ,
 .Xr environ 7
@@ -114,6 +134,16 @@ The
 .Nm
 utility conforms to
 .St -p1003.2-92 .
+.Sh HISTORY
+The
+.Nm
+command appeared in
+.Bx 4.4 .
+.Pp
+The
+.Fl u
+option first appeared in
+.Nx 10 .
 .Sh BUGS
 .Nm
 doesn't handle commands with equal

Index: src/usr.bin/env/env.c
diff -u src/usr.bin/env/env.c:1.20 src/usr.bin/env/env.c:1.21
--- src/usr.bin/env/env.c:1.20	Tue Nov 16 02:53:49 2010
+++ src/usr.bin/env/env.c	Sat Feb  8 10:30:22 2020
@@ -35,7 +35,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 
 #ifndef lint
 /*static char sccsid[] = "@(#)env.c	8.3 (Berkeley) 4/2/94";*/
-__RCSID("$NetBSD: env.c,v 1.20 2010/11/16 02:53:49 christos Exp $");
+__RCSID("$NetBSD: env.c,v 1.21 2020/02/08 10:30:22 kamil Exp $");
 #endif /* not lint */
 
 #include <err.h>
@@ -60,13 +60,17 @@ main(int argc, char **argv)
 	setprogname(*argv);
 	(void)setlocale(LC_ALL, "");
 
-	while ((ch = getopt(argc, argv, "-i")) != -1)
+	while ((ch = getopt(argc, argv, "-iu:")) != -1)
 		switch((char)ch) {
 		case '-':			/* obsolete */
 		case 'i':
 			environ = cleanenv;
 			cleanenv[0] = NULL;
 			break;
+		case 'u':
+			if (unsetenv(optarg) == -1)
+				errx(EXIT_FAILURE, "unsetenv %s", optarg);
+			break;
 		case '?':
 		default:
 			usage();
@@ -93,7 +97,8 @@ main(int argc, char **argv)
 static void
 usage(void)
 {
-	(void)fprintf(stderr, "Usage: %s [-i] [name=value ...] [command]\n",
+	(void)fprintf(stderr,
+	    "Usage: %s [-i] [-u name] [name=value ...] [command]\n",
 	    getprogname());
 	exit(1);
 }

Reply via email to