Include closeout.h. (main): Use close_stdout_status.
[platform/upstream/coreutils.git] / src / printenv.c
1 /* printenv -- print all or part of environment
2    Copyright (C) 89,90,91,92,93,94,95,96,1997, 1999 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Usage: printenv [variable...]
19
20    If no arguments are given, print the entire environment.
21    If one or more variable names are given, print the value of
22    each one that is set, and nothing for ones that are not set.
23
24    Exit status:
25    0 if all variables specified were found
26    1 if not
27
28    David MacKenzie and Richard Mlynarik */
29
30 #include <config.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
34
35 #include "system.h"
36 #include "closeout.h"
37 #include "error.h"
38
39 /* The name this program was run with. */
40 char *program_name;
41
42 /* If nonzero, display usage information and exit.  */
43 static int show_help;
44
45 /* If nonzero, print the version on standard output and exit.  */
46 static int show_version;
47
48 static struct option const long_options[] =
49 {
50   {"help", no_argument, &show_help, 1},
51   {"version", no_argument, &show_version, 1},
52   {0, 0, 0, 0}
53 };
54
55 extern char **environ;
56
57 static void
58 usage (int status)
59 {
60   if (status != 0)
61     fprintf (stderr, _("Try `%s --help' for more information.\n"),
62              program_name);
63   else
64     {
65       printf (_("Usage: %s [OPTION]... [VARIABLE]...\n"), program_name);
66       printf (_("\
67 If no environment VARIABLE specified, print them all.\n\
68 \n\
69   --help      display this help and exit\n\
70   --version   output version information and exit\n"));
71       puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
72     }
73   exit (status);
74 }
75
76 int
77 main (int argc, char **argv)
78 {
79   char **env;
80   char *ep, *ap;
81   int i;
82   int matches = 0;
83   int c;
84   int exit_status;
85
86   program_name = argv[0];
87   setlocale (LC_ALL, "");
88   bindtextdomain (PACKAGE, LOCALEDIR);
89   textdomain (PACKAGE);
90
91   while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
92     {
93       switch (c)
94         {
95         case 0:
96           break;
97
98         default:
99           usage (1);
100         }
101     }
102
103   if (show_version)
104     {
105       printf ("printenv (%s) %s\n", GNU_PACKAGE, VERSION);
106       exit (0);
107     }
108
109   if (show_help)
110     usage (0);
111
112   if (optind == argc)
113     {
114       for (env = environ; *env != NULL; ++env)
115         puts (*env);
116       exit_status = 0;
117     }
118   else
119     {
120       for (i = optind; i < argc; ++i)
121         {
122           for (env = environ; *env; ++env)
123             {
124               ep = *env;
125               ap = argv[i];
126               while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
127                 {
128                   if (*ep == '=' && *ap == '\0')
129                     {
130                       puts (ep + 1);
131                       ++matches;
132                       break;
133                     }
134                 }
135             }
136         }
137       exit_status = (matches != argc - optind);
138     }
139
140   close_stdout_status (2);
141
142   exit (exit_status);
143 }