Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / printenv.c
1 /* printenv -- print all or part of environment
2    Copyright (C) 1989-1997, 1999-2003 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 "error.h"
37 #include "exitfail.h"
38 #include "long-options.h"
39
40 /* The official name of this program (e.g., no `g' prefix).  */
41 #define PROGRAM_NAME "printenv"
42
43 #define AUTHORS "David MacKenzie", "Richard Mlynarik"
44
45 /* The name this program was run with. */
46 char *program_name;
47
48 static struct option const long_options[] =
49 {
50   {0, 0, 0, 0}
51 };
52
53 extern char **environ;
54
55 void
56 usage (int status)
57 {
58   if (status != 0)
59     fprintf (stderr, _("Try `%s --help' for more information.\n"),
60              program_name);
61   else
62     {
63       printf (_("\
64 Usage: %s [VARIABLE]...\n\
65   or:  %s OPTION\n\
66 If no environment VARIABLE specified, print them all.\n\
67 \n\
68 "),
69               program_name, program_name);
70       fputs (HELP_OPTION_DESCRIPTION, stdout);
71       fputs (VERSION_OPTION_DESCRIPTION, stdout);
72       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
73     }
74   exit (status);
75 }
76
77 int
78 main (int argc, char **argv)
79 {
80   char **env;
81   char *ep, *ap;
82   int i;
83   int matches = 0;
84   int c;
85   int exit_status;
86
87   initialize_main (&argc, &argv);
88   program_name = argv[0];
89   setlocale (LC_ALL, "");
90   bindtextdomain (PACKAGE, LOCALEDIR);
91   textdomain (PACKAGE);
92
93   exit_failure = 2;
94   atexit (close_stdout);
95
96   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
97                       usage, AUTHORS, NULL);
98
99   while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
100     {
101       switch (c)
102         {
103         case 0:
104           break;
105
106         default:
107           usage (EXIT_FAILURE);
108         }
109     }
110
111   if (optind >= argc)
112     {
113       for (env = environ; *env != NULL; ++env)
114         puts (*env);
115       exit_status = 0;
116     }
117   else
118     {
119       for (i = optind; i < argc; ++i)
120         {
121           for (env = environ; *env; ++env)
122             {
123               ep = *env;
124               ap = argv[i];
125               while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
126                 {
127                   if (*ep == '=' && *ap == '\0')
128                     {
129                       puts (ep + 1);
130                       ++matches;
131                       break;
132                     }
133                 }
134             }
135         }
136       exit_status = (matches != argc - optind);
137     }
138
139   exit (exit_status);
140 }