#include <folly/portability/Stdlib.h>
#ifdef _WIN32
#include <cstring>
#include <errno.h>
#include <folly/portability/Fcntl.h>
#include <folly/portability/SysStat.h>
#include <folly/portability/Windows.h>
extern "C" {
char* mktemp(char* tn) {
return _mktemp(tn);
}
char* mkdtemp(char* tn) {
char* ptr = nullptr;
auto len = strlen(tn);
int ret = 0;
do {
strcpy(tn + len - 6, "XXXXXX");
ptr = mktemp(tn);
if (ptr == nullptr || *ptr == '\0') {
return nullptr;
}
ret = mkdir(ptr, 0700);
if (ret != 0 && errno != EEXIST) {
return nullptr;
}
} while (ret != 0);
return tn;
}
int mkstemp(char* tn) {
char* ptr = nullptr;
auto len = strlen(tn);
int ret = 0;
do {
strcpy(tn + len - 6, "XXXXXX");
ptr = mktemp(tn);
if (ptr == nullptr || *ptr == '\0') {
return -1;
}
ret = open(ptr, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
if (ret == -1 && errno != EEXIST) {
return -1;
}
} while (ret == -1);
return ret;
}
char* realpath(const char* path, char* resolved_path) {
return _fullpath(resolved_path, path, _MAX_PATH);
}
int setenv(const char* name, const char* value, int overwrite) {
if (overwrite == 0 && getenv(name) != nullptr) {
return 0;
}
if (*value != '\0') {
auto e = _putenv_s(name, value);
if (e != 0) {
errno = e;
return -1;
}
return 0;
}
if (_putenv_s(name, " ") != 0) {
errno = EINVAL;
return -1;
}
*getenv(name) = '\0';
*(getenv(name) + 1) = '=';
if (_wenviron != nullptr) {
wchar_t buf[_MAX_ENV + 1];
size_t len;
if (mbstowcs_s(&len, buf, _MAX_ENV + 1, name, _MAX_ENV) != 0) {
errno = EINVAL;
return -1;
}
*_wgetenv(buf) = u'\0';
*(_wgetenv(buf) + 1) = u'=';
}
if (!SetEnvironmentVariableA(name, value)) {
errno = EINVAL;
return -1;
}
return 0;
}
int unsetenv(const char* name) {
if (_putenv_s(name, "") != 0) {
return -1;
}
return 0;
}
}
#endif
#if !__linux__ && !FOLLY_MOBILE && !defined(__wasm32__)
#include <string>
#include <vector>
extern "C" int clearenv() { … }
#endif