xref: /netbsd-src/lib/libc/string/wcsdup.c (revision 3249d3dc15e1f55b0ed4cc6808e5e4243d55e4f8)
1*3249d3dcSchristos /*	$NetBSD: wcsdup.c,v 1.6 2022/03/12 17:31:40 christos Exp $	*/
2a2480880Schristos 
3a2480880Schristos /*
4a2480880Schristos  * Copyright (C) 2006 Aleksey Cheusov
5a2480880Schristos  *
6a2480880Schristos  * This material is provided "as is", with absolutely no warranty expressed
7a2480880Schristos  * or implied. Any use is at your own risk.
8a2480880Schristos  *
9a2480880Schristos  * Permission to use or copy this software for any purpose is hereby granted
10a2480880Schristos  * without fee. Permission to modify the code and to distribute modified
11a2480880Schristos  * code is also granted without any restrictions.
12a2480880Schristos  */
1354097ce7Schristos 
1454097ce7Schristos #include <sys/cdefs.h>
1554097ce7Schristos 
1654097ce7Schristos #if defined(LIBC_SCCS) && !defined(lint)
17*3249d3dcSchristos __RCSID("$NetBSD: wcsdup.c,v 1.6 2022/03/12 17:31:40 christos Exp $");
1854097ce7Schristos #endif /* LIBC_SCCS and not lint */
1954097ce7Schristos 
2054097ce7Schristos #include "namespace.h"
2154097ce7Schristos #include <stdlib.h>
2254097ce7Schristos #include <assert.h>
2399f42adeSnia #include <errno.h>
2454097ce7Schristos #include <wchar.h>
2554097ce7Schristos 
__weak_alias(wcsdup,_wcsdup)2654097ce7Schristos __weak_alias(wcsdup,_wcsdup)
2754097ce7Schristos 
2854097ce7Schristos wchar_t *
2954097ce7Schristos wcsdup(const wchar_t *str)
3054097ce7Schristos {
3154097ce7Schristos 	wchar_t *copy;
3254097ce7Schristos 	size_t len;
3354097ce7Schristos 
3454097ce7Schristos 	_DIAGASSERT(str != NULL);
3554097ce7Schristos 
3654097ce7Schristos 	len = wcslen(str) + 1;
3754097ce7Schristos 
3846bf9c16Snia 	copy = NULL;
39*3249d3dcSchristos 	errno = reallocarr(&copy, len, sizeof(*copy));
40*3249d3dcSchristos 	if (errno)
4154097ce7Schristos 		return NULL;
4254097ce7Schristos 
4354097ce7Schristos 	return wmemcpy(copy, str, len);
4454097ce7Schristos }
45