| 1 |
#include <string.h> |
| 2 |
#include <stdlib.h> |
| 3 |
|
| 4 |
/* |
| 5 |
* strdup and strndup are useful, but technically not part of the |
| 6 |
* C standard, and thus may be missing from some environments. |
| 7 |
* If libc provides these functions then it will use them, |
| 8 |
* otherwise it will fallback on these implementations. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifdef _MSC_VER |
| 12 |
#define EXPORT __declspec(dllexport) |
| 13 |
#else |
| 14 |
#define EXPORT |
| 15 |
#endif |
| 16 |
|
| 17 |
EXPORT |
| 18 |
char * |
| 19 |
ffi_platypus_memory__strdup(const char *olds) |
| 20 |
{ |
| 21 |
char *news; |
| 22 |
size_t size; |
| 23 |
|
| 24 |
size = strlen(olds)+1; |
| 25 |
news = malloc(size); |
| 26 |
if(news != NULL) |
| 27 |
{ |
| 28 |
memcpy(news, olds, size); |
| 29 |
} |
| 30 |
|
| 31 |
return news; |
| 32 |
} |
| 33 |
|
| 34 |
EXPORT |
| 35 |
char * |
| 36 |
ffi_platypus_memory__strndup(const char *olds, size_t max) |
| 37 |
{ |
| 38 |
char *news; |
| 39 |
size_t size; |
| 40 |
|
| 41 |
#ifdef FFI_PL_PROBE_STRNLEN |
| 42 |
size = strnlen(olds, max); |
| 43 |
#else |
| 44 |
for(size=0; size <max && olds[size] != '\0'; size++) |
| 45 |
; |
| 46 |
#endif |
| 47 |
news = malloc(size+1); |
| 48 |
if(news != NULL) |
| 49 |
{ |
| 50 |
news[size] = '\0'; |
| 51 |
memcpy(news, olds, size); |
| 52 |
} |
| 53 |
return news; |
| 54 |
} |