blob: 5e195deaa1cb0d855082cd125dc6cad6f57354d7 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger6d1d2e52005-04-21 23:23:13 +00002/*
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 Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger6d1d2e52005-04-21 23:23:13 +00009 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010010//config:config PRINTENV
Denys Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "printenv (1.3 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: printenv is used to print all or part of environment.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015
16//applet:IF_PRINTENV(APPLET_NOFORK(printenv, printenv, BB_DIR_BIN, BB_SUID_DROP, printenv))
17
18//kbuild:lib-$(CONFIG_PRINTENV) += printenv.o
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000019
Pere Orga34425382011-03-31 14:43:25 +020020//usage:#define printenv_trivial_usage
21//usage: "[VARIABLE]..."
22//usage:#define printenv_full_usage "\n\n"
23//usage: "Print environment VARIABLEs.\n"
24//usage: "If no VARIABLE specified, print all."
25
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000027
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070028/* This is a NOFORK applet. Be very careful! */
29
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000031int printenv_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000032{
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000033 int exit_code = EXIT_SUCCESS;
34
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000035 /* no variables specified, show whole env */
Denis Vlasenko1d426652008-03-17 09:09:09 +000036 if (!argv[1]) {
Denys Vlasenko86cf0362011-03-08 12:44:02 +010037 char **e = environ;
38
39 /* environ can be NULL! (for example, after clearenv())
40 * Check for that:
41 */
42 if (e)
43 while (*e)
44 puts(*e++);
Denis Vlasenkoe0134752007-08-06 02:55:41 +000045 } else {
46 /* search for specified variables and print them out if found */
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000047 char *arg, *env;
48
Denis Vlasenkoe0134752007-08-06 02:55:41 +000049 while ((arg = *++argv) != NULL) {
50 env = getenv(arg);
51 if (env)
52 puts(env);
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000053 else
54 exit_code = EXIT_FAILURE;
Denis Vlasenkoe0134752007-08-06 02:55:41 +000055 }
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000056 }
57
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000058 fflush_stdout_and_exit(exit_code);
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000059}