windows fixes
This commit is contained in:
parent
9c876166b2
commit
9d4a8cfa83
7
build.cc
7
build.cc
|
@ -1,5 +1,8 @@
|
||||||
/* A build system for libostd. */
|
/* A build system for libostd. */
|
||||||
|
|
||||||
|
/* for Windows so that we avoid dllimport/dllexport */
|
||||||
|
#define OSTD_BUILD_LIB
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
@ -198,9 +201,9 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
#undef ARG_BOOL
|
#undef ARG_BOOL
|
||||||
|
|
||||||
std::string default_lib = OSTD_SHARED_LIB;
|
std::string default_lib = OSTD_SHARED_LIB.string();
|
||||||
if (build_static) {
|
if (build_static) {
|
||||||
default_lib = OSTD_STATIC_LIB;
|
default_lib = OSTD_STATIC_LIB.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto strip = from_env_or("STRIP", "strip");
|
auto strip = from_env_or("STRIP", "strip");
|
||||||
|
|
|
@ -122,7 +122,7 @@ namespace detail {
|
||||||
#endif
|
#endif
|
||||||
} /* namespace detail */
|
} /* namespace detail */
|
||||||
|
|
||||||
bool stack_traits::is_unbounded() noexcept {
|
OSTD_EXPORT bool stack_traits::is_unbounded() noexcept {
|
||||||
#if defined(OSTD_PLATFORM_WIN32)
|
#if defined(OSTD_PLATFORM_WIN32)
|
||||||
return true;
|
return true;
|
||||||
#elif defined(OSTD_PLATFORM_POSIX)
|
#elif defined(OSTD_PLATFORM_POSIX)
|
||||||
|
@ -130,14 +130,14 @@ bool stack_traits::is_unbounded() noexcept {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t stack_traits::page_size() noexcept {
|
OSTD_EXPORT std::size_t stack_traits::page_size() noexcept {
|
||||||
static std::size_t size = 0;
|
static std::size_t size = 0;
|
||||||
static std::once_flag fl;
|
static std::once_flag fl;
|
||||||
std::call_once(fl, detail::ctx_pagesize, &size);
|
std::call_once(fl, detail::ctx_pagesize, &size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t stack_traits::minimum_size() noexcept {
|
OSTD_EXPORT std::size_t stack_traits::minimum_size() noexcept {
|
||||||
#if defined(OSTD_PLATFORM_WIN32)
|
#if defined(OSTD_PLATFORM_WIN32)
|
||||||
/* no func on windows, sane default of 8 KiB */
|
/* no func on windows, sane default of 8 KiB */
|
||||||
return 8 * 1024;
|
return 8 * 1024;
|
||||||
|
@ -147,7 +147,7 @@ std::size_t stack_traits::minimum_size() noexcept {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t stack_traits::maximum_size() noexcept {
|
OSTD_EXPORT std::size_t stack_traits::maximum_size() noexcept {
|
||||||
#if defined(OSTD_PLATFORM_WIN32)
|
#if defined(OSTD_PLATFORM_WIN32)
|
||||||
/* value is technically undefined when is_unbounded() is
|
/* value is technically undefined when is_unbounded() is
|
||||||
* true, just default to 1 GiB so we actually return something
|
* true, just default to 1 GiB so we actually return something
|
||||||
|
@ -159,7 +159,7 @@ std::size_t stack_traits::maximum_size() noexcept {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t stack_traits::default_size() noexcept {
|
OSTD_EXPORT std::size_t stack_traits::default_size() noexcept {
|
||||||
#if defined(OSTD_PLATFORM_WIN32)
|
#if defined(OSTD_PLATFORM_WIN32)
|
||||||
/* no func on windows either, default to 64 KiB */
|
/* no func on windows either, default to 64 KiB */
|
||||||
return 8 * 8 * 1024;
|
return 8 * 8 * 1024;
|
||||||
|
|
22
src/io.cc
22
src/io.cc
|
@ -20,7 +20,7 @@ static void default_close(FILE *f) {
|
||||||
std::fclose(f);
|
std::fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_stream::open(string_range path, stream_mode mode) {
|
OSTD_EXPORT bool file_stream::open(string_range path, stream_mode mode) {
|
||||||
if (p_f || (path.size() > FILENAME_MAX)) {
|
if (p_f || (path.size() > FILENAME_MAX)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ bool file_stream::open(string_range path, stream_mode mode) {
|
||||||
return is_open();
|
return is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_stream::open(FILE *fptr, close_function f) {
|
OSTD_EXPORT bool file_stream::open(FILE *fptr, close_function f) {
|
||||||
if (p_f) {
|
if (p_f) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ bool file_stream::open(FILE *fptr, close_function f) {
|
||||||
return is_open();
|
return is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_stream::close() {
|
OSTD_EXPORT void file_stream::close() {
|
||||||
if (p_f && p_closef) {
|
if (p_f && p_closef) {
|
||||||
p_closef(p_f);
|
p_closef(p_f);
|
||||||
}
|
}
|
||||||
|
@ -58,11 +58,11 @@ void file_stream::close() {
|
||||||
p_closef = nullptr;
|
p_closef = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_stream::end() const {
|
OSTD_EXPORT bool file_stream::end() const {
|
||||||
return std::feof(p_f) != 0;
|
return std::feof(p_f) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_stream::seek(stream_off_t pos, stream_seek whence) {
|
OSTD_EXPORT void file_stream::seek(stream_off_t pos, stream_seek whence) {
|
||||||
#ifndef OSTD_PLATFORM_WIN32
|
#ifndef OSTD_PLATFORM_WIN32
|
||||||
if (fseeko(p_f, pos, int(whence)))
|
if (fseeko(p_f, pos, int(whence)))
|
||||||
#else
|
#else
|
||||||
|
@ -73,7 +73,7 @@ void file_stream::seek(stream_off_t pos, stream_seek whence) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stream_off_t file_stream::tell() const {
|
OSTD_EXPORT stream_off_t file_stream::tell() const {
|
||||||
#ifndef OSTD_PLATFORM_WIN32
|
#ifndef OSTD_PLATFORM_WIN32
|
||||||
auto ret = ftello(p_f);
|
auto ret = ftello(p_f);
|
||||||
#else
|
#else
|
||||||
|
@ -85,13 +85,13 @@ stream_off_t file_stream::tell() const {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_stream::flush() {
|
OSTD_EXPORT void file_stream::flush() {
|
||||||
if (std::fflush(p_f)) {
|
if (std::fflush(p_f)) {
|
||||||
throw stream_error{EIO, std::generic_category()};
|
throw stream_error{EIO, std::generic_category()};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t file_stream::read_bytes(void *buf, std::size_t count) {
|
OSTD_EXPORT std::size_t file_stream::read_bytes(void *buf, std::size_t count) {
|
||||||
std::size_t readn = std::fread(buf, 1, count, p_f);
|
std::size_t readn = std::fread(buf, 1, count, p_f);
|
||||||
if (readn != count) {
|
if (readn != count) {
|
||||||
if (std::feof(p_f) != 0) {
|
if (std::feof(p_f) != 0) {
|
||||||
|
@ -102,13 +102,13 @@ std::size_t file_stream::read_bytes(void *buf, std::size_t count) {
|
||||||
return readn;
|
return readn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_stream::write_bytes(void const *buf, std::size_t count) {
|
OSTD_EXPORT void file_stream::write_bytes(void const *buf, std::size_t count) {
|
||||||
if (std::fwrite(buf, 1, count, p_f) != count) {
|
if (std::fwrite(buf, 1, count, p_f) != count) {
|
||||||
throw stream_error{EIO, std::generic_category()};
|
throw stream_error{EIO, std::generic_category()};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int file_stream::get_char() {
|
OSTD_EXPORT int file_stream::get_char() {
|
||||||
int ret = std::fgetc(p_f);
|
int ret = std::fgetc(p_f);
|
||||||
if (ret == EOF) {
|
if (ret == EOF) {
|
||||||
throw stream_error{EIO, std::generic_category()};
|
throw stream_error{EIO, std::generic_category()};
|
||||||
|
@ -116,7 +116,7 @@ int file_stream::get_char() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_stream::put_char(int c) {
|
OSTD_EXPORT void file_stream::put_char(int c) {
|
||||||
if (std::fputc(c, p_f) == EOF) {
|
if (std::fputc(c, p_f) == EOF) {
|
||||||
throw stream_error{EIO, std::generic_category()};
|
throw stream_error{EIO, std::generic_category()};
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,14 @@
|
||||||
#include "ostd/process.hh"
|
#include "ostd/process.hh"
|
||||||
#include "ostd/format.hh"
|
#include "ostd/format.hh"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#ifdef OSTD_PLATFORM_WIN32
|
#ifdef OSTD_PLATFORM_WIN32
|
||||||
# include "ostd/filesystem.hh"
|
# include "ostd/filesystem.hh"
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
namespace fs = ostd::filesystem;
|
namespace fs = ostd::filesystem;
|
||||||
#else
|
#else
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <fcntl.h>
|
|
||||||
# include <sys/wait.h>
|
# include <sys/wait.h>
|
||||||
# include <wordexp.h>
|
# include <wordexp.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -365,9 +366,7 @@ static std::wstring resolve_file(wchar_t const *cmd) {
|
||||||
|
|
||||||
auto is_maybe_exec = [](fs::path const &p) {
|
auto is_maybe_exec = [](fs::path const &p) {
|
||||||
auto st = fs::status(p);
|
auto st = fs::status(p);
|
||||||
if (fs::is_regular_file(st) || fs::is_symlink(st)) {
|
return (fs::is_regular_file(st) || fs::is_symlink(st));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fs::path p{cmd};
|
fs::path p{cmd};
|
||||||
|
@ -381,12 +380,8 @@ static std::wstring resolve_file(wchar_t const *cmd) {
|
||||||
}
|
}
|
||||||
/* the directory from which the app loaded */
|
/* the directory from which the app loaded */
|
||||||
if (GetModuleFileNameW(nullptr, buf, sizeof(buf))) {
|
if (GetModuleFileNameW(nullptr, buf, sizeof(buf))) {
|
||||||
#ifdef NTDDI_WIN8
|
fs::path rp{buf};
|
||||||
PathCchRemoveFileSpecW(buf, sizeof(buf));
|
rp.replace_filename(p);
|
||||||
#else
|
|
||||||
PathRemoveFileSpecW(buf);
|
|
||||||
#endif
|
|
||||||
auto rp = fs::path{buf} / p;
|
|
||||||
if (is_maybe_exec(rp)) {
|
if (is_maybe_exec(rp)) {
|
||||||
return rp.native();
|
return rp.native();
|
||||||
}
|
}
|
||||||
|
@ -407,7 +402,7 @@ static std::wstring resolve_file(wchar_t const *cmd) {
|
||||||
}
|
}
|
||||||
/* the windows directory */
|
/* the windows directory */
|
||||||
if (GetWindowsDirectoryW(buf, sizeof(buf))) {
|
if (GetWindowsDirectoryW(buf, sizeof(buf))) {
|
||||||
auto rp = fs::path{path} / p;
|
auto rp = fs::path{buf} / p;
|
||||||
if (is_maybe_exec(rp)) {
|
if (is_maybe_exec(rp)) {
|
||||||
return rp.native();
|
return rp.native();
|
||||||
}
|
}
|
||||||
|
@ -435,13 +430,16 @@ static std::wstring resolve_file(wchar_t const *cmd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto p = wcschr(envp, L';');
|
auto sp = wcschr(envp, L';');
|
||||||
fs::path rp;
|
fs::path rp;
|
||||||
if (!p) {
|
if (!sp) {
|
||||||
rp = fs::path{p} / p;
|
rp = fs::path{envp} / p;
|
||||||
|
} else if (sp == envp) {
|
||||||
|
envp = sp + 1;
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
rp = fs::path{envp, p} / p;
|
rp = fs::path{envp, sp} / p;
|
||||||
envp = p + 1;
|
envp = sp + 1;
|
||||||
}
|
}
|
||||||
if (is_maybe_exec(rp)) {
|
if (is_maybe_exec(rp)) {
|
||||||
return rp.native();
|
return rp.native();
|
||||||
|
@ -514,19 +512,19 @@ OSTD_EXPORT void subprocess::open_impl(
|
||||||
|
|
||||||
pipe pipe_in, pipe_out, pipe_err;
|
pipe pipe_in, pipe_out, pipe_err;
|
||||||
|
|
||||||
pipe_in.open(use_in, false);
|
pipe_in.open(use_in, sa, false);
|
||||||
pipe_out.open(use_out, true);
|
pipe_out.open(use_out, sa, true);
|
||||||
pipe_err.open(use_err, true)
|
pipe_err.open(use_err, sa, true);
|
||||||
|
|
||||||
/* process creation */
|
/* process creation */
|
||||||
|
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
STARTUPINFO si;
|
STARTUPINFOW si;
|
||||||
|
|
||||||
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
|
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
|
||||||
memset(&si, 0, sizeof(STARTUPINFO));
|
memset(&si, 0, sizeof(STARTUPINFOW));
|
||||||
|
|
||||||
si.cb = sizeof(STARTUPINFO);
|
si.cb = sizeof(STARTUPINFOW);
|
||||||
|
|
||||||
if (use_in == process_stream::PIPE) {
|
if (use_in == process_stream::PIPE) {
|
||||||
si.hStdInput = pipe_in.p_r;
|
si.hStdInput = pipe_in.p_r;
|
||||||
|
@ -549,7 +547,7 @@ OSTD_EXPORT void subprocess::open_impl(
|
||||||
if (use_err == process_stream::PIPE) {
|
if (use_err == process_stream::PIPE) {
|
||||||
si.hStdError = pipe_err.p_w;
|
si.hStdError = pipe_err.p_w;
|
||||||
pipe_err.fdopen(err, true);
|
pipe_err.fdopen(err, true);
|
||||||
} else if (use_err = process_stream::STDOUT) {
|
} else if (use_err == process_stream::STDOUT) {
|
||||||
si.hStdError = si.hStdOutput;
|
si.hStdError = si.hStdOutput;
|
||||||
} else {
|
} else {
|
||||||
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
@ -564,7 +562,7 @@ OSTD_EXPORT void subprocess::open_impl(
|
||||||
{
|
{
|
||||||
std::unique_ptr<wchar_t[]> wcmd{new wchar_t[cmd.size() + 1]};
|
std::unique_ptr<wchar_t[]> wcmd{new wchar_t[cmd.size() + 1]};
|
||||||
if (!MultiByteToWideChar(
|
if (!MultiByteToWideChar(
|
||||||
CP_UTF8, 0, cmd.data(), cmd.size(), wcmd.data(), cmd.size() + 1
|
CP_UTF8, 0, cmd.data(), cmd.size(), wcmd.get(), cmd.size() + 1
|
||||||
)) {
|
)) {
|
||||||
throw process_error{"unicode conversion failed"};
|
throw process_error{"unicode conversion failed"};
|
||||||
}
|
}
|
||||||
|
@ -580,7 +578,7 @@ OSTD_EXPORT void subprocess::open_impl(
|
||||||
|
|
||||||
std::unique_ptr<wchar_t[]> cmdline{new wchar_t[astr.size() + 1]};
|
std::unique_ptr<wchar_t[]> cmdline{new wchar_t[astr.size() + 1]};
|
||||||
if (!MultiByteToWideChar(
|
if (!MultiByteToWideChar(
|
||||||
CP_UTF8, 0, astr.data(), astr.size(), cmdline.data(), astr.size() + 1
|
CP_UTF8, 0, astr.data(), astr.size(), cmdline.get(), astr.size() + 1
|
||||||
)) {
|
)) {
|
||||||
throw process_error{"unicode conversion failed"};
|
throw process_error{"unicode conversion failed"};
|
||||||
}
|
}
|
||||||
|
@ -592,7 +590,7 @@ OSTD_EXPORT void subprocess::open_impl(
|
||||||
|
|
||||||
auto success = CreateProcessW(
|
auto success = CreateProcessW(
|
||||||
cmdpath.data(),
|
cmdpath.data(),
|
||||||
cmdline.data(),
|
cmdline.get(),
|
||||||
nullptr, /* process security attributes */
|
nullptr, /* process security attributes */
|
||||||
nullptr, /* primary thread security attributes */
|
nullptr, /* primary thread security attributes */
|
||||||
true, /* inherit handles */
|
true, /* inherit handles */
|
||||||
|
@ -630,7 +628,7 @@ OSTD_EXPORT int subprocess::close() {
|
||||||
throw process_error{"child process wait failed"};
|
throw process_error{"child process wait failed"};
|
||||||
}
|
}
|
||||||
|
|
||||||
int ec;
|
DWORD ec = 0;
|
||||||
if (!GetExitCodeProcess(pd->process, &ec)) {
|
if (!GetExitCodeProcess(pd->process, &ec)) {
|
||||||
CloseHandle(pd->process);
|
CloseHandle(pd->process);
|
||||||
CloseHandle(pd->thread);
|
CloseHandle(pd->thread);
|
||||||
|
@ -642,7 +640,7 @@ OSTD_EXPORT int subprocess::close() {
|
||||||
CloseHandle(pd->thread);
|
CloseHandle(pd->thread);
|
||||||
reset();
|
reset();
|
||||||
|
|
||||||
return ec;
|
return int(ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue