From 46379d7001b77bed73c2a61b4c7a3de04de33c19 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Wed, 25 Mar 2026 03:11:33 +0700 Subject: [PATCH] Make test files buildable for glibc --- tests/dirent/fdopendir.c | 3 +++ tests/dirent/posix_getdents.c | 7 +++++++ tests/err.c | 10 ++++++++++ tests/error.c | 8 ++++++++ tests/fcntl/open.c | 2 -- tests/features.c | 7 +++++++ tests/includes.c | 4 +++- tests/limits.c | 4 ++++ tests/pthread/cleanup.c | 7 +++++++ tests/sigaltstack.c | 3 ++- tests/signals/sigpause-error.c | 2 -- tests/signals/sigpause-invalid.c | 2 -- tests/signals/sigpause-revert.c | 2 -- tests/signals/sigprocmask-11.c | 2 -- tests/signals/sigrelse-1.c | 2 -- tests/signals/sigset-1.c | 2 -- tests/signals/sigset-10.c | 2 -- tests/signals/sigset-2.c | 2 -- tests/signals/sigset-3.c | 2 -- tests/signals/sigset-4.c | 2 -- tests/signals/sigset-5.c | 2 -- tests/signals/sigset-9.c | 2 -- tests/stdio/renameat.c | 2 -- tests/stdlib/ptsname.c | 1 - tests/string/mem.c | 2 +- tests/string/strlen.c | 3 +++ tests/sys_epoll/epoll.c | 2 +- tests/sys_mman/fmap.c | 1 + tests/sys_stat/chmod.c | 2 -- tests/sys_stat/fstatat.c | 2 -- tests/time/strptime.c | 2 -- tests/unistd/swab.c | 2 ++ tests/wchar/wcstod.c | 4 ++++ tests/wchar/wscanf.c | 4 ++++ 34 files changed, 67 insertions(+), 39 deletions(-) diff --git a/tests/dirent/fdopendir.c b/tests/dirent/fdopendir.c index cb9857935d..268dda2283 100644 --- a/tests/dirent/fdopendir.c +++ b/tests/dirent/fdopendir.c @@ -117,6 +117,8 @@ int main(void) { continue; } +// fdclosedir is a BSD extension +#ifndef __GLIBC__ // fdclosedir returns ownership of the original fd. int returned_fd = fdclosedir(iter); // Internally, both closedir and fdclosedir consume the boxed DIR. @@ -133,6 +135,7 @@ int main(void) { fputs("fdclosedir shouldn't have closed the fd\n", stderr); goto closedirfd; } +#endif status = EXIT_SUCCESS; closediriter: diff --git a/tests/dirent/posix_getdents.c b/tests/dirent/posix_getdents.c index 19f9245633..ee62be96ff 100644 --- a/tests/dirent/posix_getdents.c +++ b/tests/dirent/posix_getdents.c @@ -10,6 +10,7 @@ #define BUFFER_SIZE 4096 +#ifndef __GLIBC__ void read_and_print_directory(int fd) { char buffer[BUFFER_SIZE]; long nread; @@ -44,3 +45,9 @@ int main(void) { close(fd); } +#else + +int main(void) { + // glibc doesn't support posix_getdents & O_DIRECTORY +} +#endif diff --git a/tests/err.c b/tests/err.c index 704edcc1b4..eda95d1743 100644 --- a/tests/err.c +++ b/tests/err.c @@ -4,6 +4,9 @@ #include #include +// does not compile with glibc +#ifndef __GLIBC__ + __attribute__((nonnull(2))) static void vwarn_test(int code, const char* fmt, ...) { va_list ap; @@ -45,7 +48,14 @@ int main(void) { // two functions handle everything internally. errc(EXIT_SUCCESS, EUSERS, "Bye. It's crowded."); + // Unreachable puts("err did not exit"); return EXIT_FAILURE; } + +#else +int main(void) { + exit(0); +} +#endif diff --git a/tests/error.c b/tests/error.c index 1dbf13703d..61ba98f7f3 100644 --- a/tests/error.c +++ b/tests/error.c @@ -8,6 +8,8 @@ #include "test_helpers.h" +// does not compile with glibc +#ifndef __GLIBC__ int main(void) { chdir("nonexistent"); int err = errno; @@ -50,3 +52,9 @@ int main(void) { return EXIT_SUCCESS; } + +#else +int main(void) { + exit(0); +} +#endif diff --git a/tests/fcntl/open.c b/tests/fcntl/open.c index 2d4721ae54..ed8a999f3a 100644 --- a/tests/fcntl/open.c +++ b/tests/fcntl/open.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include #include #include diff --git a/tests/features.c b/tests/features.c index 9d4c844dce..d33bda8429 100644 --- a/tests/features.c +++ b/tests/features.c @@ -1,18 +1,25 @@ // These tests are primarily to ensure the macros compile without // causing any funny business. +// TODO: does not compile with glibc #include #include #include #include +#ifndef __GLIBC__ __deprecated +#endif static void legacy(void) {} +#ifndef __GLIBC__ __deprecatedNote("Sometimes deletes user's home (oops); use foobar") +#endif static void legacy_notes(void) {} +#ifndef __GLIBC__ __nodiscard +#endif static uint8_t the_answer(void) { return 42; } diff --git a/tests/includes.c b/tests/includes.c index e04bf78554..014235d740 100644 --- a/tests/includes.c +++ b/tests/includes.c @@ -9,13 +9,15 @@ #include #include +#ifndef __GLIBC__ #include #include +#include +#endif #include #include #include #include -#include #include #include #include diff --git a/tests/limits.c b/tests/limits.c index d323a13867..f59585ff7c 100644 --- a/tests/limits.c +++ b/tests/limits.c @@ -22,8 +22,10 @@ int main() { unsigned int ui_max = UINT_MAX; unsigned long ul_max = ULONG_MAX; unsigned long long ull_max = ULLONG_MAX; +#ifndef __GLIBC__ int long_bit = LONG_BIT; int word_bit = WORD_BIT; +#endif printf("CHAR : [%d, %d]\n", c_min, c_max); printf("SCHAR : [%d, %d]\n", sc_min, sc_max); @@ -37,8 +39,10 @@ int main() { printf("UINT_MAX : %u\n", ui_max); printf("ULONG_MAX : %lu\n", ul_max); printf("ULLONG_MAX : %llu\n\n", ull_max); +#ifndef __GLIBC__ printf("LONG_BIT : %d\n", long_bit); printf("WORD_BIT : %d\n", word_bit); +#endif return 0; } diff --git a/tests/pthread/cleanup.c b/tests/pthread/cleanup.c index 194965ec98..015b56eda2 100644 --- a/tests/pthread/cleanup.c +++ b/tests/pthread/cleanup.c @@ -6,9 +6,16 @@ #include +// TODO: glibc requires arg to be not const, but not relibc +#ifndef __GLIBC__ const char *msg1 = "first"; const char *msg2 = "second"; const char *msg3 = "third"; +#else +char *msg1 = "first"; +char *msg2 = "second"; +char *msg3 = "third"; +#endif void cleanup1(void *arg) { printf("Running %s cleanup callback\n", (const char *)arg); diff --git a/tests/sigaltstack.c b/tests/sigaltstack.c index 22f575c991..c0dbe7a188 100644 --- a/tests/sigaltstack.c +++ b/tests/sigaltstack.c @@ -40,7 +40,8 @@ int main(void) { int status; stack_size = 1024 * 1024; // TODO? - assert(stack_size >= MINSIGSTKSZ * 100); + // in glibc MINSIGSTKSZ is a sysconf(MINSIGSTKSZ) macro + assert(stack_size >= (size_t)MINSIGSTKSZ * 100); stack_base = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); ERROR_IF(mmap, stack_base, == MAP_FAILED); diff --git a/tests/signals/sigpause-error.c b/tests/signals/sigpause-error.c index 47d9e38388..b7299bf092 100644 --- a/tests/signals/sigpause-error.c +++ b/tests/signals/sigpause-error.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 700 - #include #include #include diff --git a/tests/signals/sigpause-invalid.c b/tests/signals/sigpause-invalid.c index 79e80add58..e036bfee5f 100644 --- a/tests/signals/sigpause-invalid.c +++ b/tests/signals/sigpause-invalid.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 700 - #include "../test_helpers.h" #include #include diff --git a/tests/signals/sigpause-revert.c b/tests/signals/sigpause-revert.c index 7e5fe8a3ab..3cf46964fd 100644 --- a/tests/signals/sigpause-revert.c +++ b/tests/signals/sigpause-revert.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 700 - #include #include #include diff --git a/tests/signals/sigprocmask-11.c b/tests/signals/sigprocmask-11.c index 07f1c4a367..8c54851f65 100644 --- a/tests/signals/sigprocmask-11.c +++ b/tests/signals/sigprocmask-11.c @@ -1,6 +1,4 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigrelse-1.c b/tests/signals/sigrelse-1.c index 49b53f82a6..c1ecbce0ba 100644 --- a/tests/signals/sigrelse-1.c +++ b/tests/signals/sigrelse-1.c @@ -9,8 +9,6 @@ #include "signals_list.h" #include "../test_helpers.h" -#define _XOPEN_SOURCE 700 - volatile sig_atomic_t handler_called = 0; void sig_handler(int signo) diff --git a/tests/signals/sigset-1.c b/tests/signals/sigset-1.c index 17f261d482..fe2f04df1c 100644 --- a/tests/signals/sigset-1.c +++ b/tests/signals/sigset-1.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-10.c b/tests/signals/sigset-10.c index 04986b739e..68f8d37808 100644 --- a/tests/signals/sigset-10.c +++ b/tests/signals/sigset-10.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-2.c b/tests/signals/sigset-2.c index 35aacbcb13..17ba64392b 100644 --- a/tests/signals/sigset-2.c +++ b/tests/signals/sigset-2.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-3.c b/tests/signals/sigset-3.c index 0f226cc51b..16abedc22f 100644 --- a/tests/signals/sigset-3.c +++ b/tests/signals/sigset-3.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-4.c b/tests/signals/sigset-4.c index 858b49332b..a25c7de6f6 100644 --- a/tests/signals/sigset-4.c +++ b/tests/signals/sigset-4.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-5.c b/tests/signals/sigset-5.c index dc34f2848b..1bcaeff80f 100644 --- a/tests/signals/sigset-5.c +++ b/tests/signals/sigset-5.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/signals/sigset-9.c b/tests/signals/sigset-9.c index 8f40fe84b5..637c24505b 100644 --- a/tests/signals/sigset-9.c +++ b/tests/signals/sigset-9.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 600 - #include #include #include diff --git a/tests/stdio/renameat.c b/tests/stdio/renameat.c index 2287039553..9fba5fc762 100644 --- a/tests/stdio/renameat.c +++ b/tests/stdio/renameat.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include #include #include diff --git a/tests/stdlib/ptsname.c b/tests/stdlib/ptsname.c index d5a9591f34..82d2687d84 100644 --- a/tests/stdlib/ptsname.c +++ b/tests/stdlib/ptsname.c @@ -16,7 +16,6 @@ License along with the GNU C Library; if not, see . */ -#define _GNU_SOURCE #include #include #include diff --git a/tests/string/mem.c b/tests/string/mem.c index 7529a3bb1b..ed27237237 100644 --- a/tests/string/mem.c +++ b/tests/string/mem.c @@ -21,7 +21,7 @@ int main(void) { puts("Correct memrchr"); char arr2[51]; memset(arr2, 0, 51); // Compiler builtin, should work - memccpy((void *)arr2, (void *)arr, 1, 100); + memccpy((void *)arr2, (void *)arr, 1, 51); if (arr[50] != 1) { puts("Incorrect memccpy"); exit(EXIT_FAILURE); diff --git a/tests/string/strlen.c b/tests/string/strlen.c index f7fd46f5b5..d5125c52dc 100644 --- a/tests/string/strlen.c +++ b/tests/string/strlen.c @@ -48,6 +48,8 @@ int main(void) { exit(EXIT_FAILURE); } +// no strnlen_s on glibc +#ifndef __GLIBC__ dest1_len = strnlen_s(dest1, 6); printf("%d\n", dest1_len); if(dest1_len != 6) { @@ -68,6 +70,7 @@ int main(void) { puts("strnlen_s(NULL, 100) failed"); exit(EXIT_FAILURE); } +#endif return 0; } diff --git a/tests/sys_epoll/epoll.c b/tests/sys_epoll/epoll.c index 197b6ac028..29220ba18c 100644 --- a/tests/sys_epoll/epoll.c +++ b/tests/sys_epoll/epoll.c @@ -33,7 +33,7 @@ int reader(int fd) { return 1; } - int nfds_n1 = epoll_wait(epollfd, events, -1, -1); + int nfds_n1 = epoll_wait(epollfd, events, 8, -1); if (nfds_n1 != -1 || errno != EINVAL) { perror("epoll_wait"); return 1; diff --git a/tests/sys_mman/fmap.c b/tests/sys_mman/fmap.c index 499a6014b3..002f5cc5c4 100644 --- a/tests/sys_mman/fmap.c +++ b/tests/sys_mman/fmap.c @@ -26,6 +26,7 @@ #include #include #include +#include #include int diff --git a/tests/sys_stat/chmod.c b/tests/sys_stat/chmod.c index 5424669422..30be2a6f19 100644 --- a/tests/sys_stat/chmod.c +++ b/tests/sys_stat/chmod.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE // Linux to run locally - #include #include #include diff --git a/tests/sys_stat/fstatat.c b/tests/sys_stat/fstatat.c index 296af3eebf..a3a2b075c0 100644 --- a/tests/sys_stat/fstatat.c +++ b/tests/sys_stat/fstatat.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE // Linux for CI - #include #include #include diff --git a/tests/time/strptime.c b/tests/time/strptime.c index 9d1b3211c5..af96064513 100644 --- a/tests/time/strptime.c +++ b/tests/time/strptime.c @@ -1,5 +1,3 @@ -#define _XOPEN_SOURCE 700 - #include #include #include diff --git a/tests/unistd/swab.c b/tests/unistd/swab.c index c883b0cb35..dd4603913a 100644 --- a/tests/unistd/swab.c +++ b/tests/unistd/swab.c @@ -7,6 +7,7 @@ int main(void) { const char flipped_source[] = {1, 0, 3, 2, 5, 4}; const char first_two_source_bytes_flipped[] = {1, 0}; +#ifndef __GLIBC__ swab(source, destination, /* nbytes */ -3); for (size_t i = 0; i < sizeof(destination); ++i) { if (destination[i] != 0) { @@ -14,6 +15,7 @@ int main(void) { return 1; } } +#endif swab(source, destination, /* nbytes */ 0); for (size_t i = 0; i < sizeof(destination); ++i) { diff --git a/tests/wchar/wcstod.c b/tests/wchar/wcstod.c index c51d51823c..dd71e42191 100644 --- a/tests/wchar/wcstod.c +++ b/tests/wchar/wcstod.c @@ -1,5 +1,9 @@ #include #include +// TODO: remove glibc? +#ifdef __GLIBC__ +#include +#endif void attempt(wchar_t *s) { wchar_t *end; diff --git a/tests/wchar/wscanf.c b/tests/wchar/wscanf.c index c74c4ded86..c06d4eab8c 100644 --- a/tests/wchar/wscanf.c +++ b/tests/wchar/wscanf.c @@ -1,6 +1,10 @@ /* swscanf example */ #include #include +// TODO: remove glibc? +#ifdef __GLIBC__ +#include +#endif #include "test_helpers.h"