반응형
char * getenv(const char *name)
#include <stdlib.h> #include <string.h> char *__findenv(const char *name, int *offset); /* * __findenv -- * Returns pointer to value associated with name, if any, else NULL. * Sets offset to be the offset of the name/value combination in the * environmental array, for use by setenv(3) and unsetenv(3). * Explicitly removes '=' in argument name. * * This routine *should* be a static; don't use it. */ char * __findenv(const char *name, int *offset) { extern char **environ; int len, i; const char *np; char **p, *cp; if (name == NULL || environ == NULL) return (NULL); for (np = name; *np && *np != '='; ++np) ; len = np - name; for (p = environ; (cp = *p) != NULL; ++p) { for (np = name, i = len; i && *cp; i--) if (*cp++ != *np++) break; if (i == 0 && *cp++ == '=') { *offset = p - environ; return (cp); } } return (NULL); } /* * getenv -- * Returns ptr to value associated with name, if any, else NULL. */ char * getenv(const char *name) { int offset; return (__findenv(name, &offset)); }
반응형