xref: /csrg-svn/lib/libc/string/strncpy.c (revision 61193)
142168Sbostic /*-
2*61193Sbostic  * Copyright (c) 1990, 1993
3*61193Sbostic  *	The Regents of the University of California.  All rights reserved.
435098Sbostic  *
542168Sbostic  * This code is derived from software contributed to Berkeley by
642168Sbostic  * Chris Torek.
742168Sbostic  *
842168Sbostic  * %sccs.include.redist.c%
91989Swnj  */
101989Swnj 
1135098Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61193Sbostic static char sccsid[] = "@(#)strncpy.c	8.1 (Berkeley) 06/04/93";
1335098Sbostic #endif /* LIBC_SCCS and not lint */
1435098Sbostic 
1546144Sbostic #include <sys/cdefs.h>
1642168Sbostic #include <string.h>
1742168Sbostic 
1842168Sbostic /*
1942168Sbostic  * Copy src to dst, truncating or null-padding to always copy n bytes.
2042168Sbostic  * Return dst.
2142168Sbostic  */
221989Swnj char *
strncpy(dst,src,n)2342168Sbostic strncpy(dst, src, n)
2442168Sbostic 	char *dst;
2542168Sbostic 	const char *src;
2642168Sbostic 	register size_t n;
271989Swnj {
2842168Sbostic 	if (n != 0) {
2942168Sbostic 		register char *d = dst;
3042168Sbostic 		register const char *s = src;
311989Swnj 
3242168Sbostic 		do {
3342168Sbostic 			if ((*d++ = *s++) == 0) {
3442168Sbostic 				/* NUL pad the remaining n-1 bytes */
3542168Sbostic 				while (--n != 0)
3642168Sbostic 					*d++ = 0;
3742168Sbostic 				break;
3842168Sbostic 			}
3942168Sbostic 		} while (--n != 0);
4042168Sbostic 	}
4142168Sbostic 	return (dst);
421989Swnj }
43