xref: /netbsd-src/lib/libc/string/wcsdup.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: wcsdup.c,v 1.5 2021/10/29 11:03:46 nia Exp $	*/
2 
3 /*
4  * Copyright (C) 2006 Aleksey Cheusov
5  *
6  * This material is provided "as is", with absolutely no warranty expressed
7  * or implied. Any use is at your own risk.
8  *
9  * Permission to use or copy this software for any purpose is hereby granted
10  * without fee. Permission to modify the code and to distribute modified
11  * code is also granted without any restrictions.
12  */
13 
14 #include <sys/cdefs.h>
15 
16 #if defined(LIBC_SCCS) && !defined(lint)
17 __RCSID("$NetBSD: wcsdup.c,v 1.5 2021/10/29 11:03:46 nia Exp $");
18 #endif /* LIBC_SCCS and not lint */
19 
20 #include "namespace.h"
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <wchar.h>
25 
26 __weak_alias(wcsdup,_wcsdup)
27 
28 wchar_t *
29 wcsdup(const wchar_t *str)
30 {
31 	wchar_t *copy;
32 	size_t len;
33 
34 	_DIAGASSERT(str != NULL);
35 
36 	len = wcslen(str) + 1;
37 
38 	copy = NULL;
39 	if (reallocarr(&copy, len, sizeof(wchar_t)) != 0) {
40 		errno = ENOMEM;
41 		return NULL;
42 	}
43 
44 	return wmemcpy(copy, str, len);
45 }
46