16f398fc82a7ab8b3286476a304448ff913ff810
[platform/upstream/bash.git] / examples / loadables / printenv.c
1 /*
2  * printenv -- minimal builtin clone of BSD printenv(1).
3  *
4  * usage: printenv [varname]
5  *
6  */
7
8 #include <config.h>
9 #include <stdio.h>
10
11 #include "builtins.h"
12 #include "shell.h"
13 #include "bashgetopt.h"
14
15 extern char **export_env;
16
17 int
18 printenv_builtin (list) 
19      WORD_LIST *list;
20 {
21   register char **envp;
22   int opt;
23   SHELL_VAR *var;
24
25   reset_internal_getopt ();
26   while ((opt = internal_getopt (list, "")) != -1)
27     {
28       switch (opt)
29         {
30         default:
31           builtin_usage ();
32           return (EX_USAGE);
33         }
34     }
35   list = loptend;
36
37   /* printenv */
38   if (list == 0)
39     {
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);
44     }
45
46   /* printenv varname */
47   var = find_variable (list->word->word);
48   if (var == 0 || (exported_p (var) == 0))
49     return (EXECUTION_FAILURE);
50
51   if (function_p (var))
52     print_var_function (var);
53   else
54     print_var_value (var, 0);
55
56   return (EXECUTION_SUCCESS);
57 }
58
59 char *printenv_doc[] = {
60         "print values of environment variables",
61         (char *)NULL
62 };
63
64 struct builtin printenv_struct = {
65         "printenv",
66         printenv_builtin,
67         BUILTIN_ENABLED,
68         printenv_doc,
69         "printenv [varname]",
70         0
71 };