bl/strfuncs.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 10:30:16 2012 +0000 (2012-01-27)
changeset 5 f600b0d1fc5d
parent 0 gclib/strfuncs.c@c2f4c0285180
permissions -rw-r--r--
Rename to bookloupe
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <string.h>
ali@5
     3
#include <bl/mem.h>
ali@5
     4
#include <bl/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
}