Fix Windows build: implement own strndup(3)

This commit is contained in:
Dmitri Tikhonov 2020-10-21 10:08:01 -04:00
parent ef3958b8fb
commit a500a209cf
2 changed files with 27 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
@ -313,3 +314,23 @@ lsquic_sockaddr2str (const struct sockaddr *addr, char *buf, size_t sz)
if (len < (int) sz)
snprintf(buf + len, sz - (size_t) len, ":%hu", port);
}
#ifdef _MSC_VER
char *
lsquic_strndup (const char *s, size_t n)
{
size_t len;
char *copy;
len = strnlen(s, n);
copy = malloc(n + 1);
if (copy)
{
memcpy(copy, s, len);
copy[len] = '\0';
}
return copy;
}
#endif

View File

@ -49,6 +49,12 @@ lsquic_sockaddr2str (const struct sockaddr *addr, char *buf, size_t sz);
#define SA2STR(sa_, buf_) (lsquic_sockaddr2str(sa_, buf_, sizeof(buf_)), buf_)
#ifdef _MSC_VER
char *
lsquic_strndup(const char *s, size_t n);
#define strndup lsquic_strndup
#endif
#ifdef __cplusplus
}
#endif