Subject: [PATCH] printenv: -n option added -- show names of variables.

From: Van de Bugger <[email-masked]>
Date: Fri, 23 Nov 2012 02:42:21 +0400

>From 50aefa315b50b151a2192168fa46e9a311c06147 Mon Sep 17 00:00:00 2001
From: Van de Bugger <[email-masked]>
Date: Fri, 23 Nov 2012 02:38:48 +0400
Subject: [PATCH] printenv: -n option added -- show names of variables.

* src/printenv.c: -n option added -- show names of variables.
---
 src/printenv.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/printenv.c b/src/printenv.c
index 5c01f8d..b4583bd 100644
--- a/src/printenv.c
+++ b/src/printenv.c
@@ -49,6 +49,7 @@ static struct option const longopts[] =
   {"null", no_argument, NULL, '0'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
+  {"names", no_argument, NULL, 'n'},
   {NULL, 0, NULL, 0}
 };
 
@@ -69,6 +70,9 @@ If no VARIABLE is specified, print name and value
pairs for them all.\n\
       fputs (_("\
   -0, --null     end each output line with 0 byte rather than newline\n
\
 "), stdout);
+      fputs (_("\
+  -n, --names    print also names of the specified environment
VARIABLE(s)\n\
+"), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
@@ -81,11 +85,12 @@ int
 main (int argc, char **argv)
 {
   char **env;
-  char *ep, *ap;
+  char *ep, *ap, *bp;
   int i;
   bool ok;
   int optc;
   bool opt_nul_terminate_output = false;
+  bool opt_show_names = false;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -96,13 +101,16 @@ main (int argc, char **argv)
   initialize_exit_failure (PRINTENV_FAILURE);
   atexit (close_stdout);
 
-  while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) !=
-1)
+  while ((optc = getopt_long (argc, argv, "+iu:0n", longopts, NULL)) !=
-1)
     {
       switch (optc)
         {
         case '0':
           opt_nul_terminate_output = true;
           break;
+        case 'n':
+          opt_show_names = true;
+          break;
         case_GETOPT_HELP_CHAR;
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
         default:
@@ -130,13 +138,14 @@ main (int argc, char **argv)
 
           for (env = environ; *env; ++env)
             {
-              ep = *env;
+              bp = ep = *env;
               ap = argv[i];
               while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
                 {
                   if (*ep == '=' && *ap == '\0')
                     {
-                      printf ("%s%c", ep + 1,
+                      printf ("%s%c",
+                              opt_show_names ? bp : ep + 1,
                               opt_nul_terminate_output ? '\0' : '\n');
                       matched = true;
                       break;
-- 
1.7.11.7