Module Name: src
Committed By: kim
Date: Mon Oct 28 11:30:37 UTC 2024
Modified Files:
src/usr.bin/env: env.1 env.c
Log Message:
Implement "env -C dir" to chdir
Use err(3) to expose errno(2) if chdir(2) (or unsetenv(3)) fails.
To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/usr.bin/env/env.1
cvs rdiff -u -r1.23 -r1.24 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.15 src/usr.bin/env/env.1:1.16
--- src/usr.bin/env/env.1:1.15 Sat Feb 8 11:10:08 2020
+++ src/usr.bin/env/env.1 Mon Oct 28 11:30:37 2024
@@ -1,4 +1,4 @@
-.\" $NetBSD: env.1,v 1.15 2020/02/08 11:10:08 leot Exp $
+.\" $NetBSD: env.1,v 1.16 2024/10/28 11:30:37 kim 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.15 2020/02/08 11:10:08 leot Exp $
+.\" $NetBSD: env.1,v 1.16 2024/10/28 11:30:37 kim Exp $
.\"
-.Dd February 8, 2020
+.Dd October 28, 2024
.Dt ENV 1
.Os
.Sh NAME
@@ -41,6 +41,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl 0i
+.Op Fl C Ar dir
.Op Fl u Ar name
.Op Ar name=value ...
.Oo
@@ -53,23 +54,28 @@ executes
.Ar utility
after modifying the environment as
specified on the command line.
-The option
+Each
.Ar name=value
-specifies
+option specifies
an environmental variable,
.Ar name ,
with a value of
.Ar value .
-The option
+The
.Sq Fl i
-causes
+option causes
.Nm
to completely ignore the environment
it inherits.
.Pp
-The option
+The
+.Sq Fl C Ar dir
+option causes the working directory to be changed to
+.Ar dir .
+.Pp
+The
.Sq Fl u Ar name
-causes removal of the
+option causes removal of the
.Ar name
environment variable if it is in the environment.
This is similar to the
@@ -92,9 +98,9 @@ Each
pair is separated by a new line unless
.Fl 0
is specified, in which case name/value pairs are separated by NUL.
-Both
+The
.Fl 0
-and
+option and
.Ar utility
must not be specified together.
.Sh EXIT STATUS
@@ -136,11 +142,12 @@ The historic
option has been deprecated but is still supported in this implementation.
.Pp
The
-.Fl u
+.Fl C , u
and
.Fl 0
options are non-standard extensions.
.Sh SEE ALSO
+.Xr chdir 2 ,
.Xr execvp 3 ,
.Xr environ 7
.Sh STANDARDS
@@ -160,6 +167,11 @@ and
.Fl 0
options first appeared in
.Nx 10 .
+.Pp
+The
+.Fl C
+option first appeared in
+.Nx 10.1 .
.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.23 src/usr.bin/env/env.c:1.24
--- src/usr.bin/env/env.c:1.23 Sat Feb 8 11:02:07 2020
+++ src/usr.bin/env/env.c Mon Oct 28 11:30:37 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: env.c,v 1.23 2020/02/08 11:02:07 kamil Exp $ */
+/* $NetBSD: env.c,v 1.24 2024/10/28 11:30:37 kim Exp $ */
/*
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -36,7 +36,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.23 2020/02/08 11:02:07 kamil Exp $");
+__RCSID("$NetBSD: env.c,v 1.24 2024/10/28 11:30:37 kim Exp $");
#endif /* not lint */
#include <err.h>
@@ -62,11 +62,15 @@ main(int argc, char **argv)
(void)setlocale(LC_ALL, "");
term = '\n';
- while ((ch = getopt(argc, argv, "-0iu:")) != -1)
+ while ((ch = getopt(argc, argv, "-0C:iu:")) != -1)
switch((char)ch) {
case '0':
term = '\0';
break;
+ case 'C':
+ if (chdir(optarg) == -1)
+ err(EXIT_FAILURE, "chdir '%s'", optarg);
+ break;
case '-': /* obsolete */
case 'i':
environ = cleanenv;
@@ -74,7 +78,7 @@ main(int argc, char **argv)
break;
case 'u':
if (unsetenv(optarg) == -1)
- errx(EXIT_FAILURE, "unsetenv %s", optarg);
+ err(EXIT_FAILURE, "unsetenv '%s'", optarg);
break;
case '?':
default:
@@ -107,7 +111,7 @@ static void
usage(void)
{
(void)fprintf(stderr,
- "Usage: %s [-0i] [-u name] [name=value ...] [command]\n",
+ "Usage: %s [-0i] [-C dir] [-u name] [name=value ...] [command]\n",
getprogname());
exit(1);
}