"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 2 | /* |
| 3 | * printenv implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org> |
| 6 | * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 11 | //usage:#define printenv_trivial_usage |
| 12 | //usage: "[VARIABLE]..." |
| 13 | //usage:#define printenv_full_usage "\n\n" |
| 14 | //usage: "Print environment VARIABLEs.\n" |
| 15 | //usage: "If no VARIABLE specified, print all." |
| 16 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 18 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 19 | /* This is a NOFORK applet. Be very careful! */ |
| 20 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 21 | int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 22 | int printenv_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 23 | { |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 24 | int exit_code = EXIT_SUCCESS; |
| 25 | |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 26 | /* no variables specified, show whole env */ |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 27 | if (!argv[1]) { |
Denys Vlasenko | 86cf036 | 2011-03-08 12:44:02 +0100 | [diff] [blame] | 28 | char **e = environ; |
| 29 | |
| 30 | /* environ can be NULL! (for example, after clearenv()) |
| 31 | * Check for that: |
| 32 | */ |
| 33 | if (e) |
| 34 | while (*e) |
| 35 | puts(*e++); |
Denis Vlasenko | e013475 | 2007-08-06 02:55:41 +0000 | [diff] [blame] | 36 | } else { |
| 37 | /* search for specified variables and print them out if found */ |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 38 | char *arg, *env; |
| 39 | |
Denis Vlasenko | e013475 | 2007-08-06 02:55:41 +0000 | [diff] [blame] | 40 | while ((arg = *++argv) != NULL) { |
| 41 | env = getenv(arg); |
| 42 | if (env) |
| 43 | puts(env); |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 44 | else |
| 45 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | e013475 | 2007-08-06 02:55:41 +0000 | [diff] [blame] | 46 | } |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 49 | fflush_stdout_and_exit(exit_code); |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 50 | } |