b45e4c7b9f19138d718b009c2f8307abe5769627
[platform/upstream/bash.git] / support / printenv.c
1 /* printenv -- minimal clone of BSD printenv(1).
2
3    usage: printenv [varname]
4
5    Chet Ramey
6    chet@po.cwru.edu
7 */
8
9 /* Copyright (C) 1997-2002 Free Software Foundation, Inc.
10
11    This file is part of GNU Bash, the Bourne Again SHell.
12
13    Bash is free software: you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation, either version 3 of the License, or
16    (at your option) any later version.
17
18    Bash is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #if defined (HAVE_CONFIG_H)
28 #  include  <config.h>
29 #endif
30
31 #include "bashansi.h"
32
33 extern char **environ;
34
35 int
36 main (argc, argv) 
37      int argc;
38      char **argv;
39 {
40   register char **envp, *eval;
41   int len;
42
43   argv++;
44   argc--;
45
46   /* printenv */
47   if (argc == 0)
48     {
49       for (envp = environ; *envp; envp++)
50         puts (*envp);
51       exit (0);
52     }
53
54   /* printenv varname */
55   len = strlen (*argv);
56   for (envp = environ; *envp; envp++)
57     {
58       if (**argv == **envp && strncmp (*envp, *argv, len) == 0)
59         {
60           eval = *envp + len;
61           /* If the environment variable doesn't have an `=', ignore it. */
62           if (*eval == '=')
63             {
64               puts (eval + 1);
65               exit (0);
66             }
67         }
68     }
69   exit (1);
70 }
71