Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/env Implement env(1) -0



details:   https://anonhg.NetBSD.org/src/rev/078220507240
branches:  trunk
changeset: 969122:078220507240
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Feb 08 11:02:07 2020 +0000

description:
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.

diffstat:

 usr.bin/env/env.1 |  32 ++++++++++++++++++++++++--------
 usr.bin/env/env.c |  22 +++++++++++++++-------
 2 files changed, 39 insertions(+), 15 deletions(-)

diffs (153 lines):

diff -r 6df7f96a16bf -r 078220507240 usr.bin/env/env.1
--- a/usr.bin/env/env.1 Sat Feb 08 10:36:02 2020 +0000
+++ b/usr.bin/env/env.1 Sat Feb 08 11:02:07 2020 +0000
@@ -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 @@
 .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 @@
 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 @@
 .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 @@
 .Pp
 The
 .Fl u
-option first appeared in
+and
+.Fl 0
+optionss first appeared in
 .Nx 10 .
 .Sh BUGS
 .Nm
diff -r 6df7f96a16bf -r 078220507240 usr.bin/env/env.c
--- a/usr.bin/env/env.c Sat Feb 08 10:36:02 2020 +0000
+++ b/usr.bin/env/env.c Sat Feb 08 11:02:07 2020 +0000
@@ -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 @@
 
 #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 @@
 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 @@
 
        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 @@
        }
 
        for (ep = environ; *ep; ep++)
-               (void)printf("%s\n", *ep);
+               (void)printf("%s%c", *ep, term);
 
        exit(0);
 }
@@ -99,7 +107,7 @@
 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);
 }



Home | Main Index | Thread Index | Old Index