AuroraRuntime/Source/Process/AuProcessEnvironment.Unix.cpp

87 lines
2.0 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProcessEnvironment.Unix.cpp
Date: 2023-7-10
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuProcessEnvironment.hpp"
#include "AuProcessEnvironment.Unix.hpp"
#include <stdlib.h>
namespace Aurora::Process
{
static AuThreadPrimitives::Mutex gEnvMutex;
void PosixForkResetLocks()
{
AuResetMember(gEnvMutex);
}
AUKN_SYM AuList<AuPair<AuString, AuString>> EnvironmentGetAll()
{
AU_LOCK_GUARD(gEnvMutex);
AuList<AuPair<AuString, AuString>> ret;
auto pIterator = environ;
while (auto pCurrent = *(pIterator++))
{
auto optSplit = EnvironmentSplitString(pCurrent);
if (optSplit)
{
ret.push_back(optSplit.value());
}
}
return ret;
}
AUKN_SYM AuOptional<AuString> EnvironmentGetOne(const AuRONString &key)
{
AU_LOCK_GUARD(gEnvMutex);
if (key.empty())
{
SysPushErrorArg("Missing key");
return {};
}
auto pValue = ::getenv(key.c_str());
return pValue ? AuString(pValue) : AuOptional<AuString> {};
}
AUKN_SYM bool EnvironmentSetOne(const AuRONString &key, const AuRONString &value)
{
AU_LOCK_GUARD(gEnvMutex);
if (key.empty())
{
SysPushErrorArg("Missing key");
return false;
}
#if 0
if (value.empty())
{
SysPushErrorArg("Missing value");
return false;
}
#endif
return ::setenv(key.c_str(), value.c_str(), 1) == 0;
}
AUKN_SYM bool EnvironmentRemoveOne(const AuRONString &key)
{
AU_LOCK_GUARD(gEnvMutex);
if (key.empty())
{
SysPushErrorArg("Missing key");
return false;
}
return ::unsetenv(key.c_str()) == 0;
}
}