EnvironmentUtilities.cpp [plain text]
#include "config.h"
#include "EnvironmentUtilities.h"
#include <wtf/text/CString.h>
namespace WebKit {
namespace EnvironmentUtilities {
void stripValuesEndingWithString(const char* environmentVariable, const char* searchValue)
{
ASSERT(environmentVariable);
ASSERT(searchValue);
char* environmentValue = getenv(environmentVariable);
if (!environmentValue || environmentValue[0] == '\0')
return;
const size_t environmentValueLength = strlen(environmentValue);
const size_t environmentValueBufferLength = environmentValueLength + 1;
size_t searchLength = strlen(searchValue);
if (!searchLength)
return;
Vector<char> searchValueWithColonVector;
searchValueWithColonVector.grow(searchLength + 2);
char* searchValueWithColon = searchValueWithColonVector.data();
size_t searchLengthWithColon = searchLength + 1;
memcpy(searchValueWithColon, searchValue, searchLength);
searchValueWithColon[searchLength] = ':';
searchValueWithColon[searchLengthWithColon] = '\0';
char* componentStart = environmentValue;
char* match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
bool foundAnyMatches = match != NULL;
while (match != NULL) {
char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
while (nextColon && nextColon < match) {
componentStart = nextColon;
nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
}
RELEASE_ASSERT(componentStart >= environmentValue);
size_t environmentValueOffset = static_cast<size_t>(componentStart - environmentValue);
RELEASE_ASSERT(environmentValueOffset < environmentValueBufferLength);
if (componentStart[0] == ':') {
strlcpy(componentStart, match + searchLength, environmentValueBufferLength - environmentValueOffset);
} else {
strlcpy(componentStart, match + searchLengthWithColon, environmentValueBufferLength - environmentValueOffset);
}
match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
}
match = strnstr(componentStart, searchValue, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
while (match != NULL) {
if (match[searchLength] == '\0')
break;
match = strnstr(match + 1, searchValue, environmentValueLength - static_cast<size_t>(match + 1 - environmentValue));
}
if (match) {
char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
while (nextColon && nextColon < match) {
componentStart = nextColon;
nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
}
componentStart[0] = '\0';
foundAnyMatches = true;
}
if (!foundAnyMatches)
return;
if (environmentValue[0] == '\0') {
unsetenv(environmentVariable);
return;
}
setenv(environmentVariable, environmentValue, 1);
}
}
}