diff options
author | Eric Blake <ebb9@byu.net> | 2009-10-27 06:36:40 -0600 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2009-10-27 19:55:35 -0600 |
commit | d6de2f198ef0f59f5210af0bf9bcf42687386a04 (patch) | |
tree | a1c5072f0b9aae40c24263d03e42ef92d81edbc9 /src | |
parent | 1a813420896317aecf5598f76c852195796e534b (diff) | |
download | coreutils-d6de2f198ef0f59f5210af0bf9bcf42687386a04.tar.xz |
env, printenv: add -0/--null option
Allows for unambiguous processing when environment values (or even
non-portable names!) contain newline.
* src/env.c (longopts): Add new option.
(usage): Document it.
(main): Implement it.
* src/printenv.c (longopts): New variable.
(usage): Document new option.
(main): Implement it.
* doc/coreutils.texi (Common options): New macro optNull.
(du invocation, env invocation, printenv invocation): Use it.
* NEWS: Mention this.
* tests/misc/env-null: New test.
* tests/Makefile.am (TESTS): Run it.
Diffstat (limited to 'src')
-rw-r--r-- | src/env.c | 27 | ||||
-rw-r--r-- | src/printenv.c | 40 |
2 files changed, 53 insertions, 14 deletions
@@ -29,6 +29,11 @@ Unset variable VARIABLE (remove it from the environment). If VARIABLE was not set, does nothing. + -0 + --null + When no program is specified, output environment information + terminated by NUL bytes rather than newlines. + variable=value (an arg containing a "=" character) Set the environment variable VARIABLE to value VALUE. VALUE may be of zero length ("variable="). Setting a variable to a @@ -96,6 +101,7 @@ static struct option const longopts[] = { {"ignore-environment", no_argument, NULL, 'i'}, + {"null", no_argument, NULL, '0'}, {"unset", required_argument, NULL, 'u'}, {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, @@ -116,8 +122,9 @@ Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"), fputs (_("\ Set each NAME to VALUE in the environment and run COMMAND.\n\ \n\ - -i, --ignore-environment start with an empty environment\n\ - -u, --unset=NAME remove variable from the environment\n\ + -i, --ignore-environment start with an empty environment\n\ + -0, --null end each output line with 0 byte rather than newline\n\ + -u, --unset=NAME remove variable from the environment\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); @@ -135,6 +142,7 @@ main (int argc, char **argv) { int optc; bool ignore_environment = false; + bool opt_nul_terminate_output = false; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -145,7 +153,7 @@ main (int argc, char **argv) initialize_exit_failure (EXIT_CANCELED); atexit (close_stdout); - while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1) + while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1) { switch (optc) { @@ -154,6 +162,9 @@ main (int argc, char **argv) break; case 'u': break; + case '0': + opt_nul_terminate_output = true; + break; case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: @@ -171,7 +182,7 @@ main (int argc, char **argv) } optind = 0; /* Force GNU getopt to re-initialize. */ - while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1) + while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1) if (optc == 'u' && unsetenv (optarg)) error (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg)); @@ -191,10 +202,16 @@ main (int argc, char **argv) { char *const *e = environ; while (*e) - puts (*e++); + printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n'); exit (EXIT_SUCCESS); } + if (opt_nul_terminate_output) + { + error (0, errno, _("cannot specify --null (-0) with command")); + usage (EXIT_CANCELED); + } + execvp (argv[optind], &argv[optind]); { diff --git a/src/printenv.c b/src/printenv.c index dcdcb83f3..6bc03f410 100644 --- a/src/printenv.c +++ b/src/printenv.c @@ -45,6 +45,14 @@ enum { PRINTENV_FAILURE = 2 }; proper_name ("David MacKenzie"), \ proper_name ("Richard Mlynarik") +static struct option const longopts[] = +{ + {"null", no_argument, NULL, '0'}, + {GETOPT_HELP_OPTION_DECL}, + {GETOPT_VERSION_OPTION_DECL}, + {NULL, 0, NULL, 0} +}; + void usage (int status) { @@ -54,13 +62,15 @@ usage (int status) else { printf (_("\ -Usage: %s [VARIABLE]...\n\ - or: %s OPTION\n\ +Usage: %s [OPTION]... [VARIABLE]...\n\ Print the values of the specified environment VARIABLE(s).\n\ If no VARIABLE is specified, print name and value pairs for them all.\n\ \n\ "), - program_name, program_name); + program_name); + fputs (_("\ + -0, --null end each output line with 0 byte rather than newline\n\ +"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME); @@ -76,6 +86,8 @@ main (int argc, char **argv) char *ep, *ap; int i; bool ok; + int optc; + bool opt_nul_terminate_output = false; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -86,15 +98,24 @@ main (int argc, char **argv) initialize_exit_failure (PRINTENV_FAILURE); atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "+", NULL, NULL) != -1) - usage (PRINTENV_FAILURE); + while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1) + { + switch (optc) + { + case '0': + opt_nul_terminate_output = true; + break; + case_GETOPT_HELP_CHAR; + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + default: + usage (PRINTENV_FAILURE); + } + } if (optind >= argc) { for (env = environ; *env != NULL; ++env) - puts (*env); + printf ("%s%c", *env, opt_nul_terminate_output ? '\0' : '\n'); ok = true; } else @@ -113,7 +134,8 @@ main (int argc, char **argv) { if (*ep == '=' && *ap == '\0') { - puts (ep + 1); + printf ("%s%c", ep + 1, + opt_nul_terminate_output ? '\0' : '\n'); matched = true; break; } |