From 4c003115e16331d7389a8a810caa17d526881703 Mon Sep 17 00:00:00 2001 From: Mosder Date: Tue, 20 Oct 2026 16:18:04 +0200 Subject: feat: improved compatibility for specific platforms --- compat/compat.c | 1 + include/moslib/string.h | 4 ++-- src/ds/hashmap.c | 10 +++++----- src/tester.c | 7 +++---- tests/safe_tests.c | 12 ++++++------ 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/compat/compat.c b/compat/compat.c index 4d8526c..f5377ce 100644 --- a/compat/compat.c +++ b/compat/compat.c @@ -13,6 +13,7 @@ char *mos_strdup(const char *s) { #endif // NO_STRDUP #ifdef NO_MEMMEM +#include void *mos_memmem(const void *h, size_t h_size, const void *n, size_t n_size) { if (n_size == 0) return (void *)h; diff --git a/include/moslib/string.h b/include/moslib/string.h index 65a38c5..5d50478 100644 --- a/include/moslib/string.h +++ b/include/moslib/string.h @@ -25,7 +25,7 @@ // // It will share the same pointer as the original, so freeing the original will free this one too. // -// If you want to use a string literal to create a slice at compile time, use: +// You can also define it via the macro which extends to a compound literal: // // StringSlice ss = SS_LIT("literal"); // @@ -213,7 +213,7 @@ extern MosStringSlice mos_ss_from_str(const char *str); // a copy of a string slice extern MosStringSlice mos_ss_copy(MosStringSlice ss); -// Create a string slice from a string literal at compile time +// Create a string slice as a compound literal // // Arguments: // lit diff --git a/src/ds/hashmap.c b/src/ds/hashmap.c index a08ce70..db09eac 100644 --- a/src/ds/hashmap.c +++ b/src/ds/hashmap.c @@ -27,29 +27,29 @@ static void *hdr2hm(Header *hdr) { } static int filled(const void *hm, size_t i) { - return hm2hdr(hm)->filled_dead_map[i / 8] & (1 << (i % 8 + 8)); + return hm2hdr(hm)->filled_dead_map[i / 8] & (1u << (i % 8 + 8)); } static void fill(void *hm, size_t i) { Header *hdr = hm2hdr(hm); hdr->n_filled++; - hdr->filled_dead_map[i / 8] |= (1 << (i % 8 + 8)); + hdr->filled_dead_map[i / 8] |= (1u << (i % 8 + 8)); } static int dead(const void *hm, size_t i) { - return hm2hdr(hm)->filled_dead_map[i / 8] & (1 << i % 8); + return hm2hdr(hm)->filled_dead_map[i / 8] & (1u << i % 8); } static void kill(void *hm, size_t i) { Header *hdr = hm2hdr(hm); hdr->n_dead++; - hdr->filled_dead_map[i / 8] |= (1 << i % 8); + hdr->filled_dead_map[i / 8] |= (1u << i % 8); } static void unkill(void *hm, size_t i) { Header *hdr = hm2hdr(hm); hdr->n_dead--; - hdr->filled_dead_map[i / 8] &= ~(1 << i % 8); + hdr->filled_dead_map[i / 8] &= ~(1u << i % 8); } // FNV-1a diff --git a/src/tester.c b/src/tester.c index f6df372..7723e97 100644 --- a/src/tester.c +++ b/src/tester.c @@ -7,9 +7,9 @@ #include #include #include -#include #ifndef _WIN32 +#include #include #define mos_close close #define mos_isatty isatty @@ -48,9 +48,8 @@ static Outputs suppressed = { }; #define print_color(color, ...) \ - (mos_isatty(STDOUT_FILENO) \ - ? (printf("\033[%dm", color), printf(__VA_ARGS__), printf("\033[0m")) \ - : printf(__VA_ARGS__)) + (mos_isatty(1) ? (printf("\033[%dm", color), printf(__VA_ARGS__), printf("\033[0m")) \ + : printf(__VA_ARGS__)) #define nl printf("\n") static void print_center(const char *text, int color, char padding_char) { diff --git a/tests/safe_tests.c b/tests/safe_tests.c index fd45603..8f94bd3 100644 --- a/tests/safe_tests.c +++ b/tests/safe_tests.c @@ -86,7 +86,7 @@ TEST_HELPER(dup_exit) { } TEST(test_safe_dup) { - int fd = safe_dup(STDOUT_FILENO); + int fd = safe_dup(1); test_assert(fd != -1, "Failed to dup stdout"); mos_close(fd); @@ -98,17 +98,17 @@ TEST(test_safe_dup) { } TEST_HELPER(dup2_exit) { - safe_dup2(-1, STDOUT_FILENO); + safe_dup2(-1, 1); } TEST(test_safe_dup2) { - int res = safe_dup2(STDOUT_FILENO, STDOUT_FILENO); + int res = safe_dup2(1, 1); test_assert(res != -1, "Failed to dup stdout 2 itself"); - int fd = safe_dup(STDOUT_FILENO); - res = safe_dup2(STDERR_FILENO, STDOUT_FILENO); + int fd = safe_dup(1); + res = safe_dup2(2, 1); test_assert(res != -1, "Failed to dup stderr 2 stdout"); - res = safe_dup2(fd, STDOUT_FILENO); + res = safe_dup2(fd, 1); test_assert(res != -1, "Failed to restore stdout"); mos_close(fd); -- cgit v1.2.3