Luna/libc/src/env.cpp

32 lines
844 B
C++
Raw Normal View History

#include <string.h>
extern "C"
{
char** environ = nullptr;
char* getenv(const char* key)
{
char** env = environ;
size_t len = strlen(key);
while (*env)
{
// Retrieve an environment variable from environ.
char* var = *(env++);
// Check for an equals sign, else we just skip this one.
char* delim = strchr(var, '=');
if (!delim) continue;
// Get the length of this variable's key (the part before the equals sign)
size_t key_length = strcspn(var, "=");
if (len != key_length) continue;
// If the keys match, we found it! Return the value (which is just after the equals sign)
if (!memcmp(key, var, key_length)) { return delim + 1; }
}
return nullptr;
}
}