summaryrefslogtreecommitdiffhomepage
path: root/compat/setenv.cpp
blob: cb98fa3dffb0271ecf35432bb9f13b1347c44be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "setenv.hpp"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringIterable.h>

namespace floormat {

#ifdef _WIN32
int setenv(const char* name, const char* value, int overwrite)
{
    if (!std::strchr(name, '=') || !value || !*value)
    {
        errno = EINVAL;
        return -1;
    }
    if (!overwrite)
        if (const auto* s = std::getenv(name); s && *s)
            return 0;

    return _putenv("="_s.join(StringIterable{name, value}).data());
}

int unsetenv(const char* name)
{
    if (!name || !*name)
    {
        errno = EINVAL;
        return -1;
    }

    return _putenv("="_s.join(StringIterable{name, ""_s}).data());
}
#endif

} // namespace floormat