diff options
Diffstat (limited to 'src/printenv.c')
-rw-r--r-- | src/printenv.c | 40 |
1 files changed, 31 insertions, 9 deletions
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; } |