Patch-ID: bash32-032
[bash.git] / examples / loadables / printenv.c
blob16f398fc82a7ab8b3286476a304448ff913ff810
1 /*
2 * printenv -- minimal builtin clone of BSD printenv(1).
4 * usage: printenv [varname]
6 */
8 #include <config.h>
9 #include <stdio.h>
11 #include "builtins.h"
12 #include "shell.h"
13 #include "bashgetopt.h"
15 extern char **export_env;
17 int
18 printenv_builtin (list)
19 WORD_LIST *list;
21 register char **envp;
22 int opt;
23 SHELL_VAR *var;
25 reset_internal_getopt ();
26 while ((opt = internal_getopt (list, "")) != -1)
28 switch (opt)
30 default:
31 builtin_usage ();
32 return (EX_USAGE);
35 list = loptend;
37 /* printenv */
38 if (list == 0)
40 maybe_make_export_env (); /* this allows minimal code */
41 for (envp = export_env; *envp; envp++)
42 printf ("%s\n", *envp);
43 return (EXECUTION_SUCCESS);
46 /* printenv varname */
47 var = find_variable (list->word->word);
48 if (var == 0 || (exported_p (var) == 0))
49 return (EXECUTION_FAILURE);
51 if (function_p (var))
52 print_var_function (var);
53 else
54 print_var_value (var, 0);
56 return (EXECUTION_SUCCESS);
59 char *printenv_doc[] = {
60 "print values of environment variables",
61 (char *)NULL
64 struct builtin printenv_struct = {
65 "printenv",
66 printenv_builtin,
67 BUILTIN_ENABLED,
68 printenv_doc,
69 "printenv [varname]",