.
[coreutils.git] / src / printenv.c
blobb334d8e74b125c1a2c36ec20c496430a327e5ba1
1 /* printenv -- print all or part of environment
2 Copyright (C) 89,90,91,92,93,94,95,96,1997 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 "error.h"
38 /* The name this program was run with. */
39 char *program_name;
41 /* If nonzero, display usage information and exit. */
42 static int show_help;
44 /* If nonzero, print the version on standard output and exit. */
45 static int show_version;
47 static struct option const long_options[] =
49 {"help", no_argument, &show_help, 1},
50 {"version", no_argument, &show_version, 1},
51 {0, 0, 0, 0}
54 extern char **environ;
56 static void
57 usage (int status)
59 if (status != 0)
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
61 program_name);
62 else
64 printf (_("Usage: %s [OPTION]... [VARIABLE]...\n"), program_name);
65 printf (_("\
66 If no environment VARIABLE specified, print them all.\n\
67 \n\
68 --help display this help and exit\n\
69 --version output version information and exit\n"));
70 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
72 exit (status);
75 int
76 main (int argc, char **argv)
78 char **env;
79 char *ep, *ap;
80 int i;
81 int matches = 0;
82 int c;
83 int exit_status;
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
90 while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
92 switch (c)
94 case 0:
95 break;
97 default:
98 usage (1);
102 if (show_version)
104 printf ("printenv (%s) %s\n", GNU_PACKAGE, VERSION);
105 exit (0);
108 if (show_help)
109 usage (0);
111 if (optind == argc)
113 for (env = environ; *env != NULL; ++env)
114 puts (*env);
115 exit_status = 0;
117 else
119 for (i = optind; i < argc; ++i)
121 for (env = environ; *env; ++env)
123 ep = *env;
124 ap = argv[i];
125 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
127 if (*ep == '=' && *ap == '\0')
129 puts (ep + 1);
130 ++matches;
131 break;
136 exit_status = (matches != argc - optind);
139 if (ferror (stdout) || fclose (stdout) == EOF)
140 error (2, errno, _("standard output"));
142 exit (exit_status);