printenv.c 767 B

123456789101112131415161718192021222324252627282930313233
  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 ATTRIBUTE_UNUSED, char **argv)
  13. {
  14. /* no variables specified, show whole env */
  15. if (!argv[1]) {
  16. int e = 0;
  17. while (environ[e])
  18. puts(environ[e++]);
  19. } else {
  20. /* search for specified variables and print them out if found */
  21. char *arg, *env;
  22. while ((arg = *++argv) != NULL) {
  23. env = getenv(arg);
  24. if (env)
  25. puts(env);
  26. }
  27. }
  28. fflush_stdout_and_exit(0);
  29. }