Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * printenv -- minimal builtin clone of BSD printenv(1). |
| 3 | * |
| 4 | * usage: printenv [varname] |
| 5 | * |
| 6 | */ |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | /* |
| 9 | Copyright (C) 1999-2009 Free Software Foundation, Inc. |
| 10 | |
| 11 | This file is part of GNU Bash. |
| 12 | Bash 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. |
| 16 | |
| 17 | Bash 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. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 26 | #include <config.h> |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #include "builtins.h" |
| 30 | #include "shell.h" |
| 31 | #include "bashgetopt.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 32 | #include "common.h" |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 33 | |
| 34 | extern char **export_env; |
| 35 | |
| 36 | int |
| 37 | printenv_builtin (list) |
| 38 | WORD_LIST *list; |
| 39 | { |
| 40 | register char **envp; |
| 41 | int opt; |
| 42 | SHELL_VAR *var; |
| 43 | |
| 44 | reset_internal_getopt (); |
| 45 | while ((opt = internal_getopt (list, "")) != -1) |
| 46 | { |
| 47 | switch (opt) |
| 48 | { |
| 49 | default: |
| 50 | builtin_usage (); |
| 51 | return (EX_USAGE); |
| 52 | } |
| 53 | } |
| 54 | list = loptend; |
| 55 | |
| 56 | /* printenv */ |
| 57 | if (list == 0) |
| 58 | { |
| 59 | maybe_make_export_env (); /* this allows minimal code */ |
| 60 | for (envp = export_env; *envp; envp++) |
| 61 | printf ("%s\n", *envp); |
| 62 | return (EXECUTION_SUCCESS); |
| 63 | } |
| 64 | |
| 65 | /* printenv varname */ |
| 66 | var = find_variable (list->word->word); |
| 67 | if (var == 0 || (exported_p (var) == 0)) |
| 68 | return (EXECUTION_FAILURE); |
| 69 | |
| 70 | if (function_p (var)) |
| 71 | print_var_function (var); |
| 72 | else |
| 73 | print_var_value (var, 0); |
| 74 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 75 | printf("\n"); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 76 | return (EXECUTION_SUCCESS); |
| 77 | } |
| 78 | |
| 79 | char *printenv_doc[] = { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 80 | "Display environment.", |
| 81 | "", |
| 82 | "Print names and values of environment variables", |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 83 | (char *)NULL |
| 84 | }; |
| 85 | |
| 86 | struct builtin printenv_struct = { |
| 87 | "printenv", |
| 88 | printenv_builtin, |
| 89 | BUILTIN_ENABLED, |
| 90 | printenv_doc, |
| 91 | "printenv [varname]", |
| 92 | 0 |
| 93 | }; |