nsprpub/lib/libc/src/strdup.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "plstr.h"
     7 #include "prmem.h"
     8 #include <string.h>
    10 PR_IMPLEMENT(char *)
    11 PL_strdup(const char *s)
    12 {
    13     char *rv;
    14     size_t n;
    16     if( (const char *)0 == s )
    17         s = "";
    19     n = strlen(s) + 1;
    21     rv = (char *)malloc(n);
    22     if( (char *)0 == rv ) return rv;
    24     (void)memcpy(rv, s, n);
    26     return rv;
    27 }
    29 PR_IMPLEMENT(void)
    30 PL_strfree(char *s)
    31 {
    32     free(s);
    33 }
    35 PR_IMPLEMENT(char *)
    36 PL_strndup(const char *s, PRUint32 max)
    37 {
    38     char *rv;
    39     size_t l;
    41     if( (const char *)0 == s )
    42         s = "";
    44     l = PL_strnlen(s, max);
    46     rv = (char *)malloc(l+1);
    47     if( (char *)0 == rv ) return rv;
    49     (void)memcpy(rv, s, l);
    50     rv[l] = '\0';
    52     return rv;
    53 }

mercurial