xref: /freebsd/lib/libc/string/wcsdup.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
1d62e8d4cSTim J. Robbins /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
4d62e8d4cSTim J. Robbins  * Copyright (c) 2005 Tim J. Robbins.
5d62e8d4cSTim J. Robbins  * All rights reserved.
6d62e8d4cSTim J. Robbins  *
7d62e8d4cSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
8d62e8d4cSTim J. Robbins  * modification, are permitted provided that the following conditions
9d62e8d4cSTim J. Robbins  * are met:
10d62e8d4cSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
11d62e8d4cSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
12d62e8d4cSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
13d62e8d4cSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
14d62e8d4cSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
15d62e8d4cSTim J. Robbins  *
16d62e8d4cSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17d62e8d4cSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d62e8d4cSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d62e8d4cSTim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20d62e8d4cSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d62e8d4cSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d62e8d4cSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d62e8d4cSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d62e8d4cSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d62e8d4cSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d62e8d4cSTim J. Robbins  * SUCH DAMAGE.
27d62e8d4cSTim J. Robbins  */
28d62e8d4cSTim J. Robbins 
29d62e8d4cSTim J. Robbins #include <sys/cdefs.h>
30d62e8d4cSTim J. Robbins __FBSDID("$FreeBSD$");
31d62e8d4cSTim J. Robbins 
32d62e8d4cSTim J. Robbins #include <stdlib.h>
33d62e8d4cSTim J. Robbins #include <wchar.h>
34d62e8d4cSTim J. Robbins 
35d62e8d4cSTim J. Robbins wchar_t *
36d62e8d4cSTim J. Robbins wcsdup(const wchar_t *s)
37d62e8d4cSTim J. Robbins {
38d62e8d4cSTim J. Robbins 	wchar_t *copy;
39d62e8d4cSTim J. Robbins 	size_t len;
40d62e8d4cSTim J. Robbins 
41d62e8d4cSTim J. Robbins 	len = wcslen(s) + 1;
42d62e8d4cSTim J. Robbins 	if ((copy = malloc(len * sizeof(wchar_t))) == NULL)
43d62e8d4cSTim J. Robbins 		return (NULL);
44d62e8d4cSTim J. Robbins 	return (wmemcpy(copy, s, len));
45d62e8d4cSTim J. Robbins }
46