ack/lang/cem/libcc.ansi/pure/stdlib/getenv.c

53 lines
1.2 KiB
C
Raw Normal View History

1989-05-16 13:13:53 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-16 13:13:53 +00:00
#include <stdlib.h>
#include <string.h>
1989-05-16 13:13:53 +00:00
extern char* _findenv(const char* name, int* offset);
/*
* getenv(name) --
* Returns ptr to value associated with name, if any, else NULL.
*/
char* getenv(const char* name)
{
2018-06-21 20:33:47 +00:00
int offset;
2018-06-21 20:33:47 +00:00
return (_findenv(name, &offset));
}
/*
* _findenv(name,offset) --
* 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(register const char* name, int* offset)
1989-05-16 13:13:53 +00:00
{
2018-06-21 20:33:47 +00:00
extern char** environ;
register int len;
register char** P;
register const char* C;
if (!environ)
return NULL;
2018-06-21 20:33:47 +00:00
for (C = name, len = 0; *C && *C != '='; ++C, ++len)
;
for (P = environ; *P; ++P)
if (!strncmp(*P, name, len))
if (*(C = *P + len) == '=')
{
*offset = P - environ;
return (char*)(++C);
}
return (NULL);
1989-05-16 13:13:53 +00:00
}