source: trunk/coreutils/src/printenv.c@ 2662

Last change on this file since 2662 was 2554, checked in by bird, 20 years ago

coretuils-5.94

File size: 3.3 KB
Line 
1/* printenv -- print all or part of environment
2 Copyright (C) 1989-1997, 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 2 if some other error occurred
28
29 David MacKenzie and Richard Mlynarik */
30
31#include <config.h>
32#include <stdio.h>
33#include <sys/types.h>
34#include <getopt.h>
35
36#include "system.h"
37#include "error.h"
38#include "long-options.h"
39
40/* Exit status for syntax errors, etc. */
41enum { PRINTENV_FAILURE = 2 };
42
43/* The official name of this program (e.g., no `g' prefix). */
44#define PROGRAM_NAME "printenv"
45
46#define AUTHORS "David MacKenzie", "Richard Mlynarik"
47
48/* The name this program was run with. */
49char *program_name;
50
51extern char **environ;
52
53void
54usage (int status)
55{
56 if (status != EXIT_SUCCESS)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 program_name);
59 else
60 {
61 printf (_("\
62Usage: %s [VARIABLE]...\n\
63 or: %s OPTION\n\
64If no environment VARIABLE specified, print them all.\n\
65\n\
66"),
67 program_name, program_name);
68 fputs (HELP_OPTION_DESCRIPTION, stdout);
69 fputs (VERSION_OPTION_DESCRIPTION, stdout);
70 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
71 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
72 }
73 exit (status);
74}
75
76int
77main (int argc, char **argv)
78{
79 char **env;
80 char *ep, *ap;
81 int i;
82 bool ok;
83
84 initialize_main (&argc, &argv);
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
89
90 initialize_exit_failure (PRINTENV_FAILURE);
91 atexit (close_stdout);
92
93 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
94 usage, AUTHORS, (char const *) NULL);
95 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
96 usage (PRINTENV_FAILURE);
97
98 if (optind >= argc)
99 {
100 for (env = environ; *env != NULL; ++env)
101 puts (*env);
102 ok = true;
103 }
104 else
105 {
106 int matches = 0;
107
108 for (i = optind; i < argc; ++i)
109 {
110 bool matched = false;
111
112 for (env = environ; *env; ++env)
113 {
114 ep = *env;
115 ap = argv[i];
116 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
117 {
118 if (*ep == '=' && *ap == '\0')
119 {
120 puts (ep + 1);
121 matched = true;
122 break;
123 }
124 }
125 }
126
127 matches += matched;
128 }
129
130 ok = (matches == argc - optind);
131 }
132
133 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
134}
Note: See TracBrowser for help on using the repository browser.