getenv.c 732 B

12345678910111213141516171819202122232425262728293031323334
  1. /* getenv.c for uClibc
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. libc_hidden_proto(getenv)
  10. libc_hidden_proto(memcmp)
  11. libc_hidden_proto(strlen)
  12. libc_hidden_proto(__environ)
  13. /* IEEE Std 1003.1-2001 says getenv need not be thread safe, so
  14. * don't bother locking access to __environ */
  15. char *getenv(const char *var)
  16. {
  17. int len;
  18. char **ep;
  19. if (!(ep=__environ))
  20. return NULL;
  21. len = strlen(var);
  22. while(*ep) {
  23. if (memcmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
  24. return *ep + len + 1;
  25. }
  26. ep++;
  27. }
  28. return NULL;
  29. }
  30. libc_hidden_def(getenv)