blob: 2430f3a1aeca1d33617a29078d943270e9fdea00 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger6d1d2e52005-04-21 23:23:13 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000012
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000014int printenv_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000015{
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000016 int exit_code = EXIT_SUCCESS;
17
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000018 /* no variables specified, show whole env */
Denis Vlasenko1d426652008-03-17 09:09:09 +000019 if (!argv[1]) {
Denis Vlasenkoe0134752007-08-06 02:55:41 +000020 int e = 0;
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000021 while (environ[e])
22 puts(environ[e++]);
Denis Vlasenkoe0134752007-08-06 02:55:41 +000023 } else {
24 /* search for specified variables and print them out if found */
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000025 char *arg, *env;
26
Denis Vlasenkoe0134752007-08-06 02:55:41 +000027 while ((arg = *++argv) != NULL) {
28 env = getenv(arg);
29 if (env)
30 puts(env);
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000031 else
32 exit_code = EXIT_FAILURE;
Denis Vlasenkoe0134752007-08-06 02:55:41 +000033 }
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000034 }
35
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000036 fflush_stdout_and_exit(exit_code);
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000037}