From a7486f4bca446a2df393b513af7457833f25e9a0 Mon Sep 17 00:00:00 2001 From: Kartatz <105828205+Kartatz@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:42:05 -0300 Subject: [PATCH] Initial commit --- src/main.c | 22 +++++++++++++++------- src/os.c | 31 +++++++++++++++++++++++++++++++ src/os.h | 4 ++++ src/wio.c | 4 ++-- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 30e8e3d..55ff15f 100644 --- a/src/main.c +++ b/src/main.c @@ -121,7 +121,7 @@ int main(int argc, argv_t* argv[]) { int exit_code = EXIT_SUCCESS; thread_t thread = {0}; - int thread_status = 1; + int status = 1; struct M3U8HTTPClient* client = NULL; struct M3U8HTTPClientError* cerror = NULL; @@ -152,7 +152,15 @@ int main(int argc, argv_t* argv[]) { #endif #if !defined(__HAIKU__) - if (is_administrator()) { + status = is_administrator(); + + #if defined(_WIN32) + if (status && is_wine()) { + status = 0; + } + #endif + + if (status) { err = M3U8ERR_CLI_PRIVILEGED_PROCESS_UNALLOWED; goto end; } @@ -231,17 +239,17 @@ int main(int argc, argv_t* argv[]) { goto end; } - thread_status = !(options.verbose || options.disable_progress); + status = !(options.verbose || options.disable_progress); - if (thread_status) { + if (status) { hide_cursor(); - thread_create(&thread, loading_progress_callback, (void*) &thread_status); + thread_create(&thread, loading_progress_callback, (void*) &status); } err = m3u8stream_load(&stream, options.url, options.base_url); - if (thread_status) { - thread_status = 0; + if (status) { + status = 0; thread_wait(&thread); show_cursor(); } diff --git a/src/os.c b/src/os.c index 28815f1..8fdbe2d 100644 --- a/src/os.c +++ b/src/os.c @@ -27,10 +27,14 @@ #if defined(_UNICODE) static const wchar_t WENV_APPDATA[] = L"APPDATA"; static const wchar_t WENV_PATH[] = L"PATH"; + + static const wchar_t WNTDLL[] = L"ntdll.dll"; #else static const char ENV_USERPROFILE[] = "USERPROFILE"; static const char ENV_APPDATA[] = "APPDATA"; static const char ENV_PATH[] = "PATH"; + + static const char NTDLL[] = "ntdll.dll"; #endif #endif @@ -153,6 +157,33 @@ int execute_shell_command(const char* const command) { } #endif +#if defined(_WIN32) + int is_wine(void) { + /* + Returns whether the caller's process is running under Wine. + + Returns (1) on true, (0) on false, (-1) on error. + */ + + #if defined(_UNICODE) + HMODULE module = GetModuleHandleW(WNTDLL); + #else + HMODULE module = GetModuleHandleA(NTDLL); + #endif + + if (module == NULL) { + return -1; + } + + if (GetProcAddress(module, "wine_get_version") == NULL) { + return 0; + } + + return 1; + + } +#endif + char* get_configuration_directory(void) { /* Returns the config directory of the current user for applications. diff --git a/src/os.h b/src/os.h index 0005487..ced32e5 100644 --- a/src/os.h +++ b/src/os.h @@ -5,6 +5,10 @@ int is_administrator(void); #endif +#if defined(_WIN32) + int is_wine(void); +#endif + int execute_shell_command(const char* const command); char* get_configuration_directory(void); char* get_temporary_directory(void); diff --git a/src/wio.c b/src/wio.c index 57128f1..715ddfd 100644 --- a/src/wio.c +++ b/src/wio.c @@ -24,7 +24,7 @@ int wio_printf(const char* const format, ...) { goto end; } - value = malloc((size_t) wsize + 1); + value = malloc((size_t) (wsize + 1)); if (value == NULL) { wsize = -1; @@ -87,7 +87,7 @@ int wio_fprintf(FILE* const stream, const char* const format, ...) { goto end; } - value = malloc((size_t) wsize + 1); + value = malloc((size_t) (wsize + 1)); if (value == NULL) { wsize = -1;