3
0

printenv.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* printenv -- minimal clone of BSD printenv(1).
  2. usage: printenv [varname]
  3. Chet Ramey
  4. chet@po.cwru.edu
  5. */
  6. /* Copyright (C) 1997-2002 Free Software Foundation, Inc.
  7. This file is part of GNU Bash, the Bourne Again SHell.
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 2, or (at your option) any later
  11. version.
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. for more details.
  16. You should have received a copy of the GNU General Public License along
  17. with Bash; see the file COPYING. If not, write to the Free Software
  18. Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. extern char **environ;
  22. int
  23. main (argc, argv)
  24. int argc;
  25. char **argv;
  26. {
  27. register char **envp, *eval;
  28. int len;
  29. argv++;
  30. argc--;
  31. /* printenv */
  32. if (argc == 0)
  33. {
  34. for (envp = environ; *envp; envp++)
  35. puts (*envp);
  36. exit(EXIT_SUCCESS);
  37. }
  38. /* printenv varname */
  39. len = strlen (*argv);
  40. for (envp = environ; *envp; envp++)
  41. {
  42. if (**argv == **envp && strncmp (*envp, *argv, len) == 0)
  43. {
  44. eval = *envp + len;
  45. /* If the environment variable doesn't have an `=', ignore it. */
  46. if (*eval == '=')
  47. {
  48. puts (eval + 1);
  49. exit(EXIT_SUCCESS);
  50. }
  51. }
  52. }
  53. exit(EXIT_FAILURE);
  54. }