author | ali <ali@juiblex.co.uk> |
Wed Jan 25 19:33:43 2012 +0000 (2012-01-25) | |
changeset 2 | cbd63f7ba40f |
permissions | -rw-r--r-- |
1 #include <stdlib.h>
2 #include <string.h>
3 #include <gclib/mem.h>
4 #include <gclib/strfuncs.h>
6 /*
7 * Like strndup, but only returns NULL if str is NULL.
8 * Note that this routine copies n bytes rather than n characters.
9 */
10 char *str_ndup(const char *str,size_t n)
11 {
12 char *dup;
13 if (!str)
14 return NULL;
15 dup=mem_alloc0(n+1,1);
16 strncpy(dup,str,n);
17 return dup;
18 }
20 /*
21 * Like strdup, but only returns NULL if str is NULL.
22 */
23 char *str_dup(const char *str)
24 {
25 return str_ndup(str,strlen(str));
26 }