summaryrefslogtreecommitdiff
path: root/coreutils/printenv.c (plain)
blob: fbd64945d0e4351fa20f2b3ce7664b691ce32665
1/* vi: set sw=4 ts=4: */
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 *
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 */
10//config:config PRINTENV
11//config: bool "printenv"
12//config: default y
13//config: help
14//config: printenv is used to print all or part of environment.
15
16//applet:IF_PRINTENV(APPLET_NOFORK(printenv, printenv, BB_DIR_BIN, BB_SUID_DROP, printenv))
17
18//kbuild:lib-$(CONFIG_PRINTENV) += printenv.o
19
20//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
26#include "libbb.h"
27
28/* This is a NOFORK applet. Be very careful! */
29
30int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int printenv_main(int argc UNUSED_PARAM, char **argv)
32{
33 int exit_code = EXIT_SUCCESS;
34
35 /* no variables specified, show whole env */
36 if (!argv[1]) {
37 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++);
45 } else {
46 /* search for specified variables and print them out if found */
47 char *arg, *env;
48
49 while ((arg = *++argv) != NULL) {
50 env = getenv(arg);
51 if (env)
52 puts(env);
53 else
54 exit_code = EXIT_FAILURE;
55 }
56 }
57
58 fflush_stdout_and_exit(exit_code);
59}
60