xref: /freebsd/sys/libkern/strncpy.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1df8bae1dSRodney W. Grimes /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * Chris Torek.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
35ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
36ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
37ab0de15bSDavid E. O'Brien 
38452bffb2SJohn Baldwin #include <sys/libkern.h>
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * Copy src to dst, truncating or null-padding to always copy n bytes.
42df8bae1dSRodney W. Grimes  * Return dst.
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes char *
4593eb73aaSJeffrey Hsu strncpy(char * __restrict dst, const char * __restrict src, size_t n)
46df8bae1dSRodney W. Grimes {
47df8bae1dSRodney W. Grimes 	if (n != 0) {
48484820d4SConrad Meyer 		char *d = dst;
49484820d4SConrad Meyer 		const char *s = src;
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes 		do {
52df8bae1dSRodney W. Grimes 			if ((*d++ = *s++) == 0) {
53df8bae1dSRodney W. Grimes 				/* NUL pad the remaining n-1 bytes */
54df8bae1dSRodney W. Grimes 				while (--n != 0)
55df8bae1dSRodney W. Grimes 					*d++ = 0;
56df8bae1dSRodney W. Grimes 				break;
57df8bae1dSRodney W. Grimes 			}
58df8bae1dSRodney W. Grimes 		} while (--n != 0);
59df8bae1dSRodney W. Grimes 	}
60df8bae1dSRodney W. Grimes 	return (dst);
61df8bae1dSRodney W. Grimes }
62