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