*** empty log message ***
[coreutils.git] / src / printenv.c
blob1ecfaf18994da02480570aeb9ddf7fa2548dfb67
1 /* printenv -- print all or part of environment
2 Copyright (C) 1989-1997, 1999 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
28 David MacKenzie and Richard Mlynarik */
30 #include <config.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
35 #include "system.h"
36 #include "closeout.h"
37 #include "error.h"
38 #include "long-options.h"
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "printenv"
43 #define AUTHORS "David MacKenzie and Richard Mlynarik"
45 /* The name this program was run with. */
46 char *program_name;
48 static struct option const long_options[] =
50 {0, 0, 0, 0}
53 extern char **environ;
55 void
56 usage (int status)
58 if (status != 0)
59 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 program_name);
61 else
63 printf (_("Usage: %s [OPTION]... [VARIABLE]...\n"), program_name);
64 printf (_("\
65 If no environment VARIABLE specified, print them all.\n\
66 \n\
67 --help display this help and exit\n\
68 --version output version information and exit\n"));
69 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
71 exit (status);
74 int
75 main (int argc, char **argv)
77 char **env;
78 char *ep, *ap;
79 int i;
80 int matches = 0;
81 int c;
82 int exit_status;
84 program_name = argv[0];
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
87 textdomain (PACKAGE);
89 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
90 AUTHORS, usage);
92 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
94 switch (c)
96 case 0:
97 break;
99 default:
100 usage (1);
104 if (optind == argc)
106 for (env = environ; *env != NULL; ++env)
107 puts (*env);
108 exit_status = 0;
110 else
112 for (i = optind; i < argc; ++i)
114 for (env = environ; *env; ++env)
116 ep = *env;
117 ap = argv[i];
118 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
120 if (*ep == '=' && *ap == '\0')
122 puts (ep + 1);
123 ++matches;
124 break;
129 exit_status = (matches != argc - optind);
132 close_stdout_status (2);
134 exit (exit_status);