diff options
Diffstat (limited to 'src/printenv.c')
-rw-r--r-- | src/printenv.c | 116 |
1 files changed, 68 insertions, 48 deletions
diff --git a/src/printenv.c b/src/printenv.c index e06b704..e351d3a 100644 --- a/src/printenv.c +++ b/src/printenv.c @@ -1,10 +1,10 @@ /* printenv -- print all or part of environment - Copyright (C) 1989-1997, 1999-2005 Free Software Foundation, Inc. + Copyright (C) 1989-2016 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -12,8 +12,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* Usage: printenv [variable...] @@ -34,41 +33,46 @@ #include <getopt.h> #include "system.h" -#include "error.h" -#include "long-options.h" /* Exit status for syntax errors, etc. */ enum { PRINTENV_FAILURE = 2 }; -/* The official name of this program (e.g., no `g' prefix). */ +/* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "printenv" -#define AUTHORS "David MacKenzie", "Richard Mlynarik" +#define AUTHORS \ + proper_name ("David MacKenzie"), \ + proper_name ("Richard Mlynarik") -/* The name this program was run with. */ -char *program_name; - -extern char **environ; +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) { if (status != EXIT_SUCCESS) - fprintf (stderr, _("Try `%s --help' for more information.\n"), - program_name); + emit_try_help (); else { printf (_("\ -Usage: %s [VARIABLE]...\n\ - or: %s OPTION\n\ -If no environment VARIABLE specified, print them all.\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 NUL, not newline\n\ +"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME); - printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); + emit_ancillary_info (PROGRAM_NAME); } exit (status); } @@ -80,9 +84,11 @@ main (int argc, char **argv) char *ep, *ap; int i; bool ok; + int optc; + bool opt_nul_terminate_output = false; initialize_main (&argc, &argv); - program_name = argv[0]; + set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); @@ -90,15 +96,24 @@ main (int argc, char **argv) initialize_exit_failure (PRINTENV_FAILURE); atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, 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 @@ -106,29 +121,34 @@ main (int argc, char **argv) int matches = 0; for (i = optind; i < argc; ++i) - { - bool matched = false; - - for (env = environ; *env; ++env) - { - ep = *env; - ap = argv[i]; - while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++) - { - if (*ep == '=' && *ap == '\0') - { - puts (ep + 1); - matched = true; - break; - } - } - } - - matches += matched; - } + { + bool matched = false; + + /* 'printenv a=b' is silent, even if 'a=b=c' is in environ. */ + if (strchr (argv[i], '=')) + continue; + + for (env = environ; *env; ++env) + { + ep = *env; + ap = argv[i]; + while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++) + { + if (*ep == '=' && *ap == '\0') + { + printf ("%s%c", ep + 1, + opt_nul_terminate_output ? '\0' : '\n'); + matched = true; + break; + } + } + } + + matches += matched; + } ok = (matches == argc - optind); } - exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); + return ok ? EXIT_SUCCESS : EXIT_FAILURE; } |