nsprpub/lib/libc/src/strdup.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial