diff options
| author | Mosder <git@mosder.eu> | 2026-09-16 23:47:49 +0200 |
|---|---|---|
| committer | Mosder <git@mosder.eu> | 2026-09-16 23:47:49 +0200 |
| commit | 644130802a51d91b90e85a0769f642c3e0203aef (patch) | |
| tree | ad97ac1da8541a440a86c4c99402354bd94b06ca /compat/compat.c | |
| parent | 4af9b2a86ad5f5b7e59ba882aa527ecead75be0e (diff) | |
feat: added a simple compatibbility layer for functions not from C99 standard
Diffstat (limited to 'compat/compat.c')
| -rw-r--r-- | compat/compat.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/compat/compat.c b/compat/compat.c new file mode 100644 index 0000000..1112146 --- /dev/null +++ b/compat/compat.c @@ -0,0 +1,31 @@ +#include "compat.h" + +#ifdef NO_STRDUP +#include "moslib/safe.h" +#include <string.h> +char *strdup(const char *s) { + char *s2 = mos_safe_malloc(strlen(s) + 1); + strcpy(s2, s); + return s2; +} +#endif // NO_STRDUP + +#ifdef NO_MEMMEM +void *memmem(const void *h, size_t h_size, const void *n, size_t n_size) { + if (n_size == 0) + return (void *)h; + if (n_size > h_size) + return NULL; + + const unsigned char *p = h; + const unsigned char *end = p + h_size - n_size + 1; + unsigned char c = *(unsigned char *)n; + while ((p = memchr(p, c, end - p))) { + if (memcmp(p, n, n_size) == 0) + return (void *)p; + p++; + } + + return NULL; +} +#endif // NO_MEMMEM |
