printenv.c 841 B

12345678910111213141516171819202122232425262728293031323334353637
  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 tarball for details.
  9. */
  10. #include "libbb.h"
  11. int printenv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  12. int printenv_main(int argc UNUSED_PARAM, char **argv)
  13. {
  14. int exit_code = EXIT_SUCCESS;
  15. /* no variables specified, show whole env */
  16. if (!argv[1]) {
  17. int e = 0;
  18. while (environ[e])
  19. puts(environ[e++]);
  20. } else {
  21. /* search for specified variables and print them out if found */
  22. char *arg, *env;
  23. while ((arg = *++argv) != NULL) {
  24. env = getenv(arg);
  25. if (env)
  26. puts(env);
  27. else
  28. exit_code = EXIT_FAILURE;
  29. }
  30. }
  31. fflush_stdout_and_exit(exit_code);
  32. }