본문 바로가기
C 언어

getenv

by SpeeDr00t 2016. 7. 9.
반응형

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));
}
반응형

'C 언어' 카테고리의 다른 글

atoll  (0) 2016.07.09
calloc  (0) 2016.07.09
exit  (0) 2016.07.09
heapsort  (0) 2016.07.09
malloc  (0) 2016.07.09