Added strndup replacement

This commit is contained in:
akallabeth
2022-07-01 11:26:44 +02:00
committed by akallabeth
parent d0fece49dc
commit 05b339b0b1
2 changed files with 28 additions and 0 deletions

View File

@@ -23,6 +23,7 @@
#include <wchar.h>
#include <string.h>
#include <winpr/config.h>
#include <winpr/winpr.h>
#include <winpr/wtypes.h>
@@ -211,6 +212,10 @@ extern "C"
WINPR_API INT64 GetLine(char** lineptr, size_t* size, FILE* stream);
#if !defined(HAVE_STRNDUP)
WINPR_API char* strndup(const char* s, size_t n);
#endif
#ifdef __cplusplus
}
#endif

View File

@@ -18,6 +18,7 @@
*/
#include <winpr/config.h>
#include <winpr/assert.h>
#include <errno.h>
#include <stdio.h>
@@ -643,3 +644,25 @@ INT64 GetLine(char** lineptr, size_t* size, FILE* stream)
return -1;
#endif
}
#if !defined(HAVE_STRNDUP)
char* strndup(const char* s, size_t n)
{
char* rc;
size_t len;
WINPR_ASSERT(s || (n == 0));
if (n == 0)
return NULL;
len = strnlen(s, n);
if (len == n)
len++;
rc = calloc(len, sizeof(char));
if (!rc)
return NULL;
memcpy(rc, s, n);
return rc;
}
#endif