3
0

printenv.c 867 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "busybox.h"
  14. int printenv_main(int argc, char **argv)
  15. {
  16. extern char **environ;
  17. int e = 0;
  18. /* no variables specified, show whole env */
  19. if (argc == 1)
  20. while (environ[e])
  21. puts(environ[e++]);
  22. /* search for specified variables and print them out if found */
  23. else {
  24. int i;
  25. size_t l;
  26. char *arg, *env;
  27. for (i=1; (arg = argv[i]); ++i)
  28. for (; (env = environ[e]); ++e) {
  29. l = strlen(arg);
  30. if (!strncmp(env, arg, l) && env[l] == '=')
  31. puts(env + l + 1);
  32. }
  33. }
  34. fflush_stdout_and_exit(0);
  35. }