printenv.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. //usage:#define printenv_trivial_usage
  11. //usage: "[VARIABLE]..."
  12. //usage:#define printenv_full_usage "\n\n"
  13. //usage: "Print environment VARIABLEs.\n"
  14. //usage: "If no VARIABLE specified, print all."
  15. #include "libbb.h"
  16. /* This is a NOFORK applet. Be very careful! */
  17. int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int printenv_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. int exit_code = EXIT_SUCCESS;
  21. /* no variables specified, show whole env */
  22. if (!argv[1]) {
  23. char **e = environ;
  24. /* environ can be NULL! (for example, after clearenv())
  25. * Check for that:
  26. */
  27. if (e)
  28. while (*e)
  29. puts(*e++);
  30. } else {
  31. /* search for specified variables and print them out if found */
  32. char *arg, *env;
  33. while ((arg = *++argv) != NULL) {
  34. env = getenv(arg);
  35. if (env)
  36. puts(env);
  37. else
  38. exit_code = EXIT_FAILURE;
  39. }
  40. }
  41. fflush_stdout_and_exit(exit_code);
  42. }