Windows compatibility (#207)

* incorrect environment detection fixed
* use %zu instead of %ld for size_t
* fix rounding in integer division
* fix compiler error on Windows
* implement missed functions
* make linker available if MinGW environment is used
* make linker available under MSYS2 Clang64 and MSYS2 MinGW64
This commit is contained in:
kvk1920
2021-07-13 13:59:55 +03:00
committed by GitHub
parent 9a6d8d7657
commit 050382b68e
8 changed files with 157 additions and 8 deletions

View File

@@ -59,4 +59,44 @@ char *strcat_arena(const char *a, const char *b)
memcpy(buffer + a_len, b, b_len);
buffer[a_len + b_len] = '\0';
return buffer;
}
}
#if PLATFORM_WINDOWS
int asprintf(char **strp, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int res = vasprintf(strp, fmt, args);
va_end(args);
return res;
}
int vasnprintf(char **strp, const char *fmt, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
int res = vsprintf(NULL, fmt, args);
if (res < 0) goto END;
char *buf = calloc(res + 1, 1);
if (NULL == buf) goto END;
sprintf(buf, fmt, args_copy); // this can't fail, right?
*strp = buf;
END:
va_end(args_copy);
return res;
}
char *strndup(const char *s, size_t n)
{
n = strnlen(s, n);
char *t = calloc(n + 1, 1);
if (NULL != t)
{
memcpy(t, s, n);
t[n] = '\0';
}
return t;
}
#endif