Last active
August 29, 2015 13:59
-
-
Save w-vi/10547943 to your computer and use it in GitHub Desktop.
If strdup and strndup is not present
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
char * | |
strdup (const char *s) | |
{ | |
size_t len = strlen (s) + 1; | |
void *new = malloc (len); | |
if(NULL == new) return NULL; | |
return (char *) memcpy (new, s, len); | |
} | |
char * | |
strndup (const char *s, size_t n) | |
{ | |
size_t len = strnlen (s, n); | |
char *new = (char *) malloc (len + 1); | |
if(NULL == new) return NULL; | |
new[len] = '\0'; | |
return (char *) memcpy (new, s, len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment