xref: /freebsd/lib/libc/string/wcsncpy.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
19829d36aSTakuya SHIOZAKI /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44bf546e1STim J. Robbins  * Copyright (c) 1990, 1993
54bf546e1STim J. Robbins  *	The Regents of the University of California.  All rights reserved.
64bf546e1STim J. Robbins  *
74bf546e1STim J. Robbins  * This code is derived from software contributed to Berkeley by
84bf546e1STim J. Robbins  * Chris Torek.
99829d36aSTakuya SHIOZAKI  *
109829d36aSTakuya SHIOZAKI  * Redistribution and use in source and binary forms, with or without
119829d36aSTakuya SHIOZAKI  * modification, are permitted provided that the following conditions
129829d36aSTakuya SHIOZAKI  * are met:
139829d36aSTakuya SHIOZAKI  * 1. Redistributions of source code must retain the above copyright
149829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer.
159829d36aSTakuya SHIOZAKI  * 2. Redistributions in binary form must reproduce the above copyright
169829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer in the
179829d36aSTakuya SHIOZAKI  *    documentation and/or other materials provided with the distribution.
183fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
194bf546e1STim J. Robbins  *    may be used to endorse or promote products derived from this software
204bf546e1STim J. Robbins  *    without specific prior written permission.
219829d36aSTakuya SHIOZAKI  *
224bf546e1STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239829d36aSTakuya SHIOZAKI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249829d36aSTakuya SHIOZAKI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254bf546e1STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269829d36aSTakuya SHIOZAKI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279829d36aSTakuya SHIOZAKI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289829d36aSTakuya SHIOZAKI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299829d36aSTakuya SHIOZAKI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309829d36aSTakuya SHIOZAKI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319829d36aSTakuya SHIOZAKI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329829d36aSTakuya SHIOZAKI  * SUCH DAMAGE.
339829d36aSTakuya SHIOZAKI  */
349829d36aSTakuya SHIOZAKI 
351707c9c3STim J. Robbins #if 0
361707c9c3STim J. Robbins #if defined(LIBC_SCCS) && !defined(lint)
371707c9c3STim J. Robbins static char sccsid[] = "@(#)strncpy.c	8.1 (Berkeley) 6/4/93";
381707c9c3STim J. Robbins #endif /* LIBC_SCCS and not lint */
391707c9c3STim J. Robbins #endif
409829d36aSTakuya SHIOZAKI #include <sys/cdefs.h>
41de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
429829d36aSTakuya SHIOZAKI 
439829d36aSTakuya SHIOZAKI #include <wchar.h>
449829d36aSTakuya SHIOZAKI 
454bf546e1STim J. Robbins /*
464bf546e1STim J. Robbins  * Copy src to dst, truncating or null-padding to always copy n bytes.
474bf546e1STim J. Robbins  * Return dst.
484bf546e1STim J. Robbins  */
499829d36aSTakuya SHIOZAKI wchar_t *
504bf546e1STim J. Robbins wcsncpy(wchar_t * __restrict dst, const wchar_t * __restrict src, size_t n)
519829d36aSTakuya SHIOZAKI {
524bf546e1STim J. Robbins 	if (n != 0) {
534bf546e1STim J. Robbins 		wchar_t *d = dst;
544bf546e1STim J. Robbins 		const wchar_t *s = src;
559829d36aSTakuya SHIOZAKI 
564bf546e1STim J. Robbins 		do {
574bf546e1STim J. Robbins 			if ((*d++ = *s++) == L'\0') {
584bf546e1STim J. Robbins 				/* NUL pad the remaining n-1 bytes */
594bf546e1STim J. Robbins 				while (--n != 0)
604bf546e1STim J. Robbins 					*d++ = L'\0';
614bf546e1STim J. Robbins 				break;
629829d36aSTakuya SHIOZAKI 			}
634bf546e1STim J. Robbins 		} while (--n != 0);
644bf546e1STim J. Robbins 	}
654bf546e1STim J. Robbins 	return (dst);
664bf546e1STim J. Robbins }
67