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/. */ 5 6 #include "plstr.h" 7 #include "prmem.h" 8 #include <string.h> 9 10 PR_IMPLEMENT(char *) PL_strdup(const char * s)11PL_strdup(const char *s) 12 { 13 char *rv; 14 size_t n; 15 16 if( (const char *)0 == s ) { 17 s = ""; 18 } 19 20 n = strlen(s) + 1; 21 22 rv = (char *)malloc(n); 23 if( (char *)0 == rv ) { 24 return rv; 25 } 26 27 (void)memcpy(rv, s, n); 28 29 return rv; 30 } 31 32 PR_IMPLEMENT(void) PL_strfree(char * s)33PL_strfree(char *s) 34 { 35 free(s); 36 } 37 38 PR_IMPLEMENT(char *) PL_strndup(const char * s,PRUint32 max)39PL_strndup(const char *s, PRUint32 max) 40 { 41 char *rv; 42 size_t l; 43 44 if( (const char *)0 == s ) { 45 s = ""; 46 } 47 48 l = PL_strnlen(s, max); 49 50 rv = (char *)malloc(l+1); 51 if( (char *)0 == rv ) { 52 return rv; 53 } 54 55 (void)memcpy(rv, s, l); 56 rv[l] = '\0'; 57 58 return rv; 59 } 60