3
0

printenv.c 882 B

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