Skip to content

Instantly share code, notes, and snippets.

@w-vi
Last active August 29, 2015 13:59
Show Gist options
  • Save w-vi/10547943 to your computer and use it in GitHub Desktop.
Save w-vi/10547943 to your computer and use it in GitHub Desktop.
If strdup and strndup is not present
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