blob: 33be5c09601aea7a38c32349d415ba5fd7428734 [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 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000012
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070013/* This is a NOFORK applet. Be very careful! */
14
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int printenv_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000017{
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000018 int exit_code = EXIT_SUCCESS;
19
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000020 /* no variables specified, show whole env */
Denis Vlasenko1d426652008-03-17 09:09:09 +000021 if (!argv[1]) {
Denis Vlasenkoe0134752007-08-06 02:55:41 +000022 int e = 0;
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000023 while (environ[e])
24 puts(environ[e++]);
Denis Vlasenkoe0134752007-08-06 02:55:41 +000025 } else {
26 /* search for specified variables and print them out if found */
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000027 char *arg, *env;
28
Denis Vlasenkoe0134752007-08-06 02:55:41 +000029 while ((arg = *++argv) != NULL) {
30 env = getenv(arg);
31 if (env)
32 puts(env);
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000033 else
34 exit_code = EXIT_FAILURE;
Denis Vlasenkoe0134752007-08-06 02:55:41 +000035 }
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000036 }
37
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000038 fflush_stdout_and_exit(exit_code);
Mike Frysinger6d1d2e52005-04-21 23:23:13 +000039}