.
[coreutils.git] / src / printenv.c
blob2545d46ff0a41ddb674edbf2b3d1ab1bb21862ae
1 /* printenv -- print all or part of environment
2 Copyright (C) 1989-1997, 1999-2004 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Usage: printenv [variable...]
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.
24 Exit status:
25 0 if all variables specified were found
26 1 if not
27 2 if some other error occurred
29 David MacKenzie and Richard Mlynarik */
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <getopt.h>
36 #include "system.h"
37 #include "error.h"
38 #include "long-options.h"
40 /* Exit status for syntax errors, etc. */
41 enum { PRINTENV_FAILURE = 2 };
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "printenv"
46 #define AUTHORS "David MacKenzie", "Richard Mlynarik"
48 /* The name this program was run with. */
49 char *program_name;
51 static struct option const long_options[] =
53 {0, 0, 0, 0}
56 extern char **environ;
58 void
59 usage (int status)
61 if (status != EXIT_SUCCESS)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("\
67 Usage: %s [VARIABLE]...\n\
68 or: %s OPTION\n\
69 If no environment VARIABLE specified, print them all.\n\
70 \n\
71 "),
72 program_name, program_name);
73 fputs (HELP_OPTION_DESCRIPTION, stdout);
74 fputs (VERSION_OPTION_DESCRIPTION, stdout);
75 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
77 exit (status);
80 int
81 main (int argc, char **argv)
83 char **env;
84 char *ep, *ap;
85 int i;
86 int matches = 0;
87 int c;
88 int exit_status;
90 initialize_main (&argc, &argv);
91 program_name = argv[0];
92 setlocale (LC_ALL, "");
93 bindtextdomain (PACKAGE, LOCALEDIR);
94 textdomain (PACKAGE);
96 initialize_exit_failure (PRINTENV_FAILURE);
97 atexit (close_stdout);
99 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
100 usage, AUTHORS, (char const *) NULL);
102 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
104 switch (c)
106 case 0:
107 break;
109 default:
110 usage (PRINTENV_FAILURE);
114 if (optind >= argc)
116 for (env = environ; *env != NULL; ++env)
117 puts (*env);
118 exit_status = EXIT_SUCCESS;
120 else
122 for (i = optind; i < argc; ++i)
124 for (env = environ; *env; ++env)
126 ep = *env;
127 ap = argv[i];
128 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
130 if (*ep == '=' && *ap == '\0')
132 puts (ep + 1);
133 ++matches;
134 break;
139 exit_status = (matches != argc - optind);
142 exit (exit_status);