3
0

printenv.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 (1.3 kb)"
  12. //config: default y
  13. //config: help
  14. //config: printenv is used to print all or part of environment.
  15. //applet:IF_PRINTENV(APPLET_NOFORK(printenv, printenv, BB_DIR_BIN, BB_SUID_DROP, printenv))
  16. //kbuild:lib-$(CONFIG_PRINTENV) += printenv.o
  17. //usage:#define printenv_trivial_usage
  18. //usage: "[VARIABLE]..."
  19. //usage:#define printenv_full_usage "\n\n"
  20. //usage: "Print environment VARIABLEs.\n"
  21. //usage: "If no VARIABLE specified, print all."
  22. #include "libbb.h"
  23. /* This is a NOFORK applet. Be very careful! */
  24. int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int printenv_main(int argc UNUSED_PARAM, char **argv)
  26. {
  27. int exit_code = EXIT_SUCCESS;
  28. /* no variables specified, show whole env */
  29. if (!argv[1]) {
  30. char **e = environ;
  31. /* environ can be NULL! (for example, after clearenv())
  32. * Check for that:
  33. */
  34. if (e)
  35. while (*e)
  36. puts(*e++);
  37. } else {
  38. /* search for specified variables and print them out if found */
  39. char *arg, *env;
  40. while ((arg = *++argv) != NULL) {
  41. env = getenv(arg);
  42. if (env)
  43. puts(env);
  44. else
  45. exit_code = EXIT_FAILURE;
  46. }
  47. }
  48. fflush_stdout_and_exit(exit_code);
  49. }