nsprpub/lib/libc/src/strdup.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/lib/libc/src/strdup.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "plstr.h"
    1.10 +#include "prmem.h"
    1.11 +#include <string.h>
    1.12 +
    1.13 +PR_IMPLEMENT(char *)
    1.14 +PL_strdup(const char *s)
    1.15 +{
    1.16 +    char *rv;
    1.17 +    size_t n;
    1.18 +
    1.19 +    if( (const char *)0 == s )
    1.20 +        s = "";
    1.21 +
    1.22 +    n = strlen(s) + 1;
    1.23 +
    1.24 +    rv = (char *)malloc(n);
    1.25 +    if( (char *)0 == rv ) return rv;
    1.26 +
    1.27 +    (void)memcpy(rv, s, n);
    1.28 +
    1.29 +    return rv;
    1.30 +}
    1.31 +
    1.32 +PR_IMPLEMENT(void)
    1.33 +PL_strfree(char *s)
    1.34 +{
    1.35 +    free(s);
    1.36 +}
    1.37 +
    1.38 +PR_IMPLEMENT(char *)
    1.39 +PL_strndup(const char *s, PRUint32 max)
    1.40 +{
    1.41 +    char *rv;
    1.42 +    size_t l;
    1.43 +
    1.44 +    if( (const char *)0 == s )
    1.45 +        s = "";
    1.46 +
    1.47 +    l = PL_strnlen(s, max);
    1.48 +
    1.49 +    rv = (char *)malloc(l+1);
    1.50 +    if( (char *)0 == rv ) return rv;
    1.51 +
    1.52 +    (void)memcpy(rv, s, l);
    1.53 +    rv[l] = '\0';
    1.54 +
    1.55 +    return rv;
    1.56 +}

mercurial