Module Name: src
Committed By: kamil
Date: Sat Feb 8 11:02:07 UTC 2020
Modified Files:
src/usr.bin/env: env.1 env.c
Log Message:
Implement env(1) -0
-0 End each output line with NUL, not newline.
FreeBSD and GNU env(1) implement -0 which is used in 3rd party scripts.
This change is based on the FreeBSD code.
To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/usr.bin/env/env.1
cvs rdiff -u -r1.22 -r1.23 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.13 src/usr.bin/env/env.1:1.14
--- src/usr.bin/env/env.1:1.13 Sat Feb 8 10:30:22 2020
+++ src/usr.bin/env/env.1 Sat Feb 8 11:02:07 2020
@@ -1,4 +1,4 @@
-.\" $NetBSD: env.1,v 1.13 2020/02/08 10:30:22 kamil Exp $
+.\" $NetBSD: env.1,v 1.14 2020/02/08 11:02:07 kamil Exp $
.\"
.\" Copyright (c) 1980, 1990 The Regents of the University of California.
.\" All rights reserved.
@@ -30,7 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" from: @(#)printenv.1 6.7 (Berkeley) 7/28/91
-.\" $NetBSD: env.1,v 1.13 2020/02/08 10:30:22 kamil Exp $
+.\" $NetBSD: env.1,v 1.14 2020/02/08 11:02:07 kamil Exp $
.\"
.Dd February 8, 2020
.Dt ENV 1
@@ -40,7 +40,7 @@
.Nd set and print environment
.Sh SYNOPSIS
.Nm
-.Op Fl i
+.Op Fl 0i
.Op Fl u Ar name
.Op Ar name=value ...
.Oo
@@ -86,10 +86,17 @@ If no
.Ar utility
is specified,
.Nm
-prints out the names and values
-of the variables in the environment, with one
+prints out the names and values of the variables in the environment.
+Each
.Ar name=value
-pair per line.
+pair is separated by a new line unless
+.Fl 0
+is specified, in which case name/value pairs are separated by NUL.
+Both
+.Fl 0
+and
+.Ar utility
+must not be specified together.
.Sh EXIT STATUS
.Nm
exits with one of the following values:
@@ -111,6 +118,11 @@ was invoked, but failed in some way;
see its manual page for more information.
In this case the exit code is returned by the utility itself, not
.Nm .
+.It 125
+.Ar utility
+was specified together with the
+.Fl 0
+option.
.It 126
.Ar utility
was found, but could not be invoked.
@@ -125,7 +137,9 @@ option has been deprecated but is still
.Pp
The
.Fl u
-option is a non-standard extension.
+and
+.Fl 0
+options are non-standard extensions.
.Sh SEE ALSO
.Xr execvp 3 ,
.Xr environ 7
@@ -142,7 +156,9 @@ command appeared in
.Pp
The
.Fl u
-option first appeared in
+and
+.Fl 0
+optionss first appeared in
.Nx 10 .
.Sh BUGS
.Nm
Index: src/usr.bin/env/env.c
diff -u src/usr.bin/env/env.c:1.22 src/usr.bin/env/env.c:1.23
--- src/usr.bin/env/env.c:1.22 Sat Feb 8 10:36:02 2020
+++ src/usr.bin/env/env.c Sat Feb 8 11:02:07 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: env.c,v 1.22 2020/02/08 10:36:02 kamil Exp $ */
+/* $NetBSD: env.c,v 1.23 2020/02/08 11:02:07 kamil 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.22 2020/02/08 10:36:02 kamil Exp $");
+__RCSID("$NetBSD: env.c,v 1.23 2020/02/08 11:02:07 kamil Exp $");
#endif /* not lint */
#include <err.h>
@@ -54,15 +54,19 @@ extern char **environ;
int
main(int argc, char **argv)
{
- char **ep;
+ char **ep, term;
char *cleanenv[1];
int ch;
setprogname(*argv);
(void)setlocale(LC_ALL, "");
- while ((ch = getopt(argc, argv, "-iu:")) != -1)
+ term = '\n';
+ while ((ch = getopt(argc, argv, "-0iu:")) != -1)
switch((char)ch) {
+ case '0':
+ term = '\0';
+ break;
case '-': /* obsolete */
case 'i':
environ = cleanenv;
@@ -82,7 +86,11 @@ main(int argc, char **argv)
if (*argv) {
/* return 127 if the command to be run could not be found; 126
- if the command was found but could not be invoked */
+ if the command was found but could not be invoked; 125 if
+ -0 was specified with utility.*/
+
+ if (term == '\0')
+ errx(125, "cannot specify command with -0");
(void)execvp(*argv, argv);
err((errno == ENOENT) ? 127 : 126, "%s", *argv);
@@ -90,7 +98,7 @@ main(int argc, char **argv)
}
for (ep = environ; *ep; ep++)
- (void)printf("%s\n", *ep);
+ (void)printf("%s%c", *ep, term);
exit(0);
}
@@ -99,7 +107,7 @@ static void
usage(void)
{
(void)fprintf(stderr,
- "Usage: %s [-i] [-u name] [name=value ...] [command]\n",
+ "Usage: %s [-0i] [-u name] [name=value ...] [command]\n",
getprogname());
exit(1);
}