xref: /csrg-svn/lib/libc/string/strncpy.c (revision 42168)
1*42168Sbostic /*-
2*42168Sbostic  * Copyright (c) 1990 The Regents of the University of California.
335098Sbostic  * All rights reserved.
435098Sbostic  *
5*42168Sbostic  * This code is derived from software contributed to Berkeley by
6*42168Sbostic  * Chris Torek.
7*42168Sbostic  *
8*42168Sbostic  * %sccs.include.redist.c%
91989Swnj  */
101989Swnj 
1135098Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*42168Sbostic static char sccsid[] = "@(#)strncpy.c	5.5 (Berkeley) 05/17/90";
1335098Sbostic #endif /* LIBC_SCCS and not lint */
1435098Sbostic 
15*42168Sbostic #include <string.h>
16*42168Sbostic #include <sys/stdc.h>
17*42168Sbostic 
18*42168Sbostic /*
19*42168Sbostic  * Copy src to dst, truncating or null-padding to always copy n bytes.
20*42168Sbostic  * Return dst.
21*42168Sbostic  */
221989Swnj char *
23*42168Sbostic strncpy(dst, src, n)
24*42168Sbostic 	char *dst;
25*42168Sbostic 	const char *src;
26*42168Sbostic 	register size_t n;
271989Swnj {
28*42168Sbostic 	if (n != 0) {
29*42168Sbostic 		register char *d = dst;
30*42168Sbostic 		register const char *s = src;
311989Swnj 
32*42168Sbostic 		do {
33*42168Sbostic 			if ((*d++ = *s++) == 0) {
34*42168Sbostic 				/* NUL pad the remaining n-1 bytes */
35*42168Sbostic 				while (--n != 0)
36*42168Sbostic 					*d++ = 0;
37*42168Sbostic 				break;
38*42168Sbostic 			}
39*42168Sbostic 		} while (--n != 0);
40*42168Sbostic 	}
41*42168Sbostic 	return (dst);
421989Swnj }
43