printenv.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. extern char **environ;
  23. int
  24. main (argc, argv)
  25. int argc;
  26. char **argv;
  27. {
  28. register char **envp, *eval;
  29. int len;
  30. argv++;
  31. argc--;
  32. /* printenv */
  33. if (argc == 0)
  34. {
  35. for (envp = environ; *envp; envp++)
  36. puts (*envp);
  37. exit(EXIT_SUCCESS);
  38. }
  39. /* printenv varname */
  40. len = strlen (*argv);
  41. for (envp = environ; *envp; envp++)
  42. {
  43. if (**argv == **envp && strncmp (*envp, *argv, len) == 0)
  44. {
  45. eval = *envp + len;
  46. /* If the environment variable doesn't have an '=', ignore it. */
  47. if (*eval == '=')
  48. {
  49. puts (eval + 1);
  50. exit(EXIT_SUCCESS);
  51. }
  52. }
  53. }
  54. exit(EXIT_FAILURE);
  55. }