init version.
[bush.git] / examples / loadables / printenv.c
blob86908bd13c98d400053dd26f32f5b9b2371af70f
1 /*
2 * printenv -- minimal builtin clone of BSD printenv(1).
4 * usage: printenv [varname]
6 */
8 /*
9 Copyright (C) 1999-2009 Free Software Foundation, Inc.
11 This file is part of GNU Bush.
12 Bush is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Bush is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Bush. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
27 #include <stdio.h>
29 #include "builtins.h"
30 #include "shell.h"
31 #include "bushgetopt.h"
32 #include "common.h"
34 extern char **export_env;
36 int
37 printenv_builtin (list)
38 WORD_LIST *list;
40 register char **envp;
41 int opt;
42 SHELL_VAR *var;
44 reset_internal_getopt ();
45 while ((opt = internal_getopt (list, "")) != -1)
47 switch (opt)
49 CASE_HELPOPT;
50 default:
51 builtin_usage ();
52 return (EX_USAGE);
55 list = loptend;
57 /* printenv */
58 if (list == 0)
60 maybe_make_export_env (); /* this allows minimal code */
61 for (envp = export_env; *envp; envp++)
62 printf ("%s\n", *envp);
63 return (EXECUTION_SUCCESS);
66 /* printenv varname */
67 var = find_variable (list->word->word);
68 if (var == 0 || (exported_p (var) == 0))
69 return (EXECUTION_FAILURE);
71 if (function_p (var))
72 print_var_function (var);
73 else
74 print_var_value (var, 0);
76 printf("\n");
77 return (EXECUTION_SUCCESS);
80 char *printenv_doc[] = {
81 "Display environment.",
82 "",
83 "Print names and values of environment variables",
84 (char *)NULL
87 struct builtin printenv_struct = {
88 "printenv",
89 printenv_builtin,
90 BUILTIN_ENABLED,
91 printenv_doc,
92 "printenv [varname]",