1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gclib/strfuncs.c Fri Jan 27 00:28:11 2012 +0000
1.3 @@ -0,0 +1,26 @@
1.4 +#include <stdlib.h>
1.5 +#include <string.h>
1.6 +#include <gclib/mem.h>
1.7 +#include <gclib/strfuncs.h>
1.8 +
1.9 +/*
1.10 + * Like strndup, but only returns NULL if str is NULL.
1.11 + * Note that this routine copies n bytes rather than n characters.
1.12 + */
1.13 +char *str_ndup(const char *str,size_t n)
1.14 +{
1.15 + char *dup;
1.16 + if (!str)
1.17 + return NULL;
1.18 + dup=mem_alloc0(n+1,1);
1.19 + strncpy(dup,str,n);
1.20 + return dup;
1.21 +}
1.22 +
1.23 +/*
1.24 + * Like strdup, but only returns NULL if str is NULL.
1.25 + */
1.26 +char *str_dup(const char *str)
1.27 +{
1.28 + return str_ndup(str,strlen(str));
1.29 +}