1 /* Copyright (c) 2009 Corinna Vinschen <corinna@vinschen.de> */
2 /*
3 FUNCTION
4 	<<wcsdup>>---wide character string duplicate
5 
6 INDEX
7 	wcsdup
8 INDEX
9 	_wcsdup_r
10 
11 SYNOPSIS
12 	#include <wchar.h>
13 	wchar_t *wcsdup(const wchar_t *<[str]>);
14 
15 	#include <wchar.h>
16 	wchar_t *_wcsdup_r(struct _reent *<[ptr]>, const wchar_t *<[str]>);
17 
18 DESCRIPTION
19 	<<wcsdup>> allocates a new wide character string using <<malloc>>,
20 	and copies the content of the argument <[str]> into the newly
21 	allocated string, thus making a copy of <[str]>.
22 
23 RETURNS
24 	<<wcsdup>> returns a pointer to the copy of <[str]> if enough
25 	memory for the copy was available.  Otherwise it returns NULL
26 	and errno is set to ENOMEM.
27 
28 PORTABILITY
29 POSIX-1.2008
30 
31 QUICKREF
32 	wcsdup
33 */
34 
35 #define _DEFAULT_SOURCE
36 #include <stdlib.h>
37 #include <wchar.h>
38 
39 wchar_t *
wcsdup(const wchar_t * str)40 wcsdup (const wchar_t *str)
41 {
42   size_t len = wcslen (str) + 1;
43   wchar_t *copy = malloc (len * sizeof (wchar_t));
44   if (copy)
45     wmemcpy (copy, str, len);
46   return copy;
47 }
48