From eb234fbe197a252feb088630cc7ba999bff060d5 Mon Sep 17 00:00:00 2001 From: kn Date: Sun, 28 Jul 2024 10:08:44 +0000 Subject: [PATCH] Support "-u name" to remove variable from environment OK aisha millert Feedback jmc --- usr.bin/env/env.1 | 12 ++++++++++-- usr.bin/env/env.c | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/usr.bin/env/env.1 b/usr.bin/env/env.1 index 050a8c6c73f..db35cb73a04 100644 --- a/usr.bin/env/env.1 +++ b/usr.bin/env/env.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: env.1,v 1.20 2015/01/12 21:42:53 deraadt Exp $ +.\" $OpenBSD: env.1,v 1.21 2024/07/28 10:08:44 kn Exp $ .\" Copyright (c) 1980, 1990 The Regents of the University of California. .\" All rights reserved. .\" @@ -30,7 +30,7 @@ .\" .\" from: @(#)printenv.1 6.7 (Berkeley) 7/28/91 .\" -.Dd $Mdocdate: January 12 2015 $ +.Dd $Mdocdate: July 28 2024 $ .Dt ENV 1 .Os .Sh NAME @@ -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 @@ -121,6 +126,9 @@ The utility is compliant with the .St -p1003.1-2008 specification. +The flag +.Op Fl u +is an extension to that specification. .Pp The historic .Fl diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c index 08aaa75c695..c1629759baa 100644 --- a/usr.bin/env/env.c +++ b/usr.bin/env/env.c @@ -1,4 +1,4 @@ -/* $OpenBSD: env.c,v 1.17 2016/10/28 07:22:59 schwarze Exp $ */ +/* $OpenBSD: env.c,v 1.18 2024/07/28 10:08:44 kn Exp $ */ /* * Copyright (c) 1988, 1993, 1994 @@ -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); } -- 2.20.1