Debian Codegraph @

nsprpub/lib/libc/src/strdup.c

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