xref: /freebsd/lib/libc/string/wcsncpy.c (revision 4bf546e192301c0e17b27f10f64261307b9e794e)
19829d36aSTakuya SHIOZAKI /*-
24bf546e1STim J. Robbins  * Copyright (c) 1990, 1993
34bf546e1STim J. Robbins  *	The Regents of the University of California.  All rights reserved.
44bf546e1STim J. Robbins  *
54bf546e1STim J. Robbins  * This code is derived from software contributed to Berkeley by
64bf546e1STim J. Robbins  * Chris Torek.
79829d36aSTakuya SHIOZAKI  *
89829d36aSTakuya SHIOZAKI  * Redistribution and use in source and binary forms, with or without
99829d36aSTakuya SHIOZAKI  * modification, are permitted provided that the following conditions
109829d36aSTakuya SHIOZAKI  * are met:
119829d36aSTakuya SHIOZAKI  * 1. Redistributions of source code must retain the above copyright
129829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer.
139829d36aSTakuya SHIOZAKI  * 2. Redistributions in binary form must reproduce the above copyright
149829d36aSTakuya SHIOZAKI  *    notice, this list of conditions and the following disclaimer in the
159829d36aSTakuya SHIOZAKI  *    documentation and/or other materials provided with the distribution.
164bf546e1STim J. Robbins  * 3. All advertising materials mentioning features or use of this software
174bf546e1STim J. Robbins  *    must display the following acknowledgement:
184bf546e1STim J. Robbins  *	This product includes software developed by the University of
194bf546e1STim J. Robbins  *	California, Berkeley and its contributors.
204bf546e1STim J. Robbins  * 4. Neither the name of the University nor the names of its contributors
214bf546e1STim J. Robbins  *    may be used to endorse or promote products derived from this software
224bf546e1STim J. Robbins  *    without specific prior written permission.
239829d36aSTakuya SHIOZAKI  *
244bf546e1STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259829d36aSTakuya SHIOZAKI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269829d36aSTakuya SHIOZAKI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274bf546e1STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289829d36aSTakuya SHIOZAKI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299829d36aSTakuya SHIOZAKI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309829d36aSTakuya SHIOZAKI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319829d36aSTakuya SHIOZAKI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329829d36aSTakuya SHIOZAKI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339829d36aSTakuya SHIOZAKI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349829d36aSTakuya SHIOZAKI  * SUCH DAMAGE.
359829d36aSTakuya SHIOZAKI  */
369829d36aSTakuya SHIOZAKI 
379829d36aSTakuya SHIOZAKI #include <sys/cdefs.h>
38de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
399829d36aSTakuya SHIOZAKI 
409829d36aSTakuya SHIOZAKI #include <wchar.h>
419829d36aSTakuya SHIOZAKI 
424bf546e1STim J. Robbins /*
434bf546e1STim J. Robbins  * Copy src to dst, truncating or null-padding to always copy n bytes.
444bf546e1STim J. Robbins  * Return dst.
454bf546e1STim J. Robbins  */
469829d36aSTakuya SHIOZAKI wchar_t *
474bf546e1STim J. Robbins wcsncpy(wchar_t * __restrict dst, const wchar_t * __restrict src, size_t n)
489829d36aSTakuya SHIOZAKI {
494bf546e1STim J. Robbins 	if (n != 0) {
504bf546e1STim J. Robbins 		wchar_t *d = dst;
514bf546e1STim J. Robbins 		const wchar_t *s = src;
529829d36aSTakuya SHIOZAKI 
534bf546e1STim J. Robbins 		do {
544bf546e1STim J. Robbins 			if ((*d++ = *s++) == L'\0') {
554bf546e1STim J. Robbins 				/* NUL pad the remaining n-1 bytes */
564bf546e1STim J. Robbins 				while (--n != 0)
574bf546e1STim J. Robbins 					*d++ = L'\0';
584bf546e1STim J. Robbins 				break;
599829d36aSTakuya SHIOZAKI 			}
604bf546e1STim J. Robbins 		} while (--n != 0);
614bf546e1STim J. Robbins 	}
624bf546e1STim J. Robbins 	return (dst);
634bf546e1STim J. Robbins }
649829d36aSTakuya SHIOZAKI 
65