-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a strndup fallback implementation for Windows.
- Loading branch information
1 parent
cad6d32
commit 0bfe10c
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "dupstring.h" | ||
|
||
#include <string.h> | ||
|
||
#if defined(HAVE_STRNDUP) && HAVE_STRNDUP | ||
|
||
char *dupstring(const char *source, size_t maxlen) { | ||
return strndup(source, maxlen); | ||
} | ||
|
||
#else /* Fallback implementation */ | ||
#include <stdlib.h> | ||
|
||
char *dupstring(const char *source, size_t maxlen) { | ||
size_t dest_size = strnlen(source, maxlen); | ||
char *dest = malloc(dest_size + 1); | ||
if (dest == NULL) { | ||
return NULL; | ||
} | ||
memcmp(dest, source, dest_size); | ||
dest[dest_size] = '\0'; | ||
return dest; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef DUPSTRING_H | ||
#define DUPSTRING_H | ||
|
||
#include <stddef.h> | ||
|
||
/* Wrapper around strndup */ | ||
char *dupstring(const char *source, size_t maxlen); | ||
|
||
#endif /* DUPSTRING_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters