diff --git a/code/os/win/win_env.c b/code/os/win/win_env.c index 6154ea7..f72b20e 100644 --- a/code/os/win/win_env.c +++ b/code/os/win/win_env.c @@ -3,7 +3,7 @@ #include "win.h" -char* getenv(const char *name) { +char *getenv(const char *name) { // The string pointed to shall not be modified by the program, but may be // overwritten by a subsequent call to the getenv function static size_t env_string_cap; diff --git a/code/os/win/win_io.c b/code/os/win/win_io.c index 49d0834..c887e7c 100644 --- a/code/os/win/win_io.c +++ b/code/os/win/win_io.c @@ -3,6 +3,8 @@ #include #include +#include +#include // It's just mapped directly to HANDLE struct FILE { @@ -57,14 +59,14 @@ FILE *_os_fopen(char const *restrict name, _OS_ModeFlags flags) { } HANDLE fileHandle = CreateFileA( - name, - desaddr, - share, - NULL, - disp, - FILE_ATTRIBUTE_NORMAL, - NULL - ); + name, + desaddr, + share, + NULL, + disp, + FILE_ATTRIBUTE_NORMAL, + NULL + ); FILE *file = (FILE *)fileHandle; if(fileHandle == INVALID_HANDLE_VALUE) { file = NULL; @@ -83,3 +85,45 @@ void _os_file_write(void* ctx, size_t n, const char str[]) { DWORD written = 0; WriteFile((HANDLE) ctx, str, n, &written, NULL); } + +int system(const char* string) { + int wchars_required = MultiByteToWideChar(65001 /* UTF8 */, 0, string, -1, NULL, 0); + wchar_t* cmd_line = malloc(sizeof(L"cmd.exe ") + (wchars_required * sizeof(wchar_t))); + if (cmd_line == NULL) { + goto error; + } + + memcpy(cmd_line, L"cmd.exe ", sizeof(L"cmd.exe ")); + MultiByteToWideChar(65001 /* UTF8 */, 0, string, -1, cmd_line + sizeof("cmd.exe ") - 1, wchars_required); + + STARTUPINFOW si = { + .cb = sizeof(STARTUPINFOW), + .dwFlags = STARTF_USESTDHANDLES, + .hStdInput = GetStdHandle(STD_INPUT_HANDLE), + .hStdError = GetStdHandle(STD_ERROR_HANDLE), + .hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE) + }; + PROCESS_INFORMATION pi = {}; + + if (!CreateProcessW(NULL, cmd_line, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) { + goto error; + } + + // Wait until child process exits. + WaitForSingleObject(pi.hProcess, INFINITE); + + DWORD exit_code; + if (!GetExitCodeProcess(pi.hProcess, &exit_code)) { + goto error; + } + + // Close process and thread handles. + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + free(cmd_line); + return exit_code; + + error: + free(cmd_line); + return -1; +} diff --git a/inc/math.h b/inc/math.h index 244aee2..3326e0c 100644 --- a/inc/math.h +++ b/inc/math.h @@ -5,7 +5,7 @@ typedef float float_t; typedef double double_t; #ifndef _HUGE_ENUF - #define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow +#define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow #endif #define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF)) @@ -27,10 +27,9 @@ typedef double double_t; #define FP_NORMAL 2 #define FP_SUBNORMAL 3 #define FP_ZERO 4 -#define fpclassify(x) ( \ - _is_float(x) ? _fpclassify_f(x) : - _is_double(x) ? _fpclassify_d(x) : - FP_NAN) + +#define fpclassify(x) 0 +#define isfinite(x) 0 double acos(double x); float acosf(float x); diff --git a/inc/stdlib.h b/inc/stdlib.h index ae9c2fa..672cfbd 100644 --- a/inc/stdlib.h +++ b/inc/stdlib.h @@ -70,7 +70,7 @@ _Noreturn void exit(int status); _Noreturn void _Exit(int status); char *getenv(const char *name); // _Noreturn void quick_exit(int status); -// int system(const char *string); +int system(const char *string); const void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); // void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); // int abs(int j); diff --git a/test/test4.c b/test/test4.c index 29c4f09..da1cb51 100644 --- a/test/test4.c +++ b/test/test4.c @@ -22,6 +22,8 @@ int data_cmp(void const *lhs, void const *rhs) int main(void) { + system("clang -v"); + struct data key = { .nr = 3 }; struct data const *res = bsearch(&key, dat, sizeof dat / sizeof dat[0], sizeof dat[0], data_cmp); if (res) {