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