xref: /original-bsd/lib/libc/string/strncpy.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)strncpy.c	8.1 (Berkeley) 06/04/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/cdefs.h>
16 #include <string.h>
17 
18 /*
19  * Copy src to dst, truncating or null-padding to always copy n bytes.
20  * Return dst.
21  */
22 char *
23 strncpy(dst, src, n)
24 	char *dst;
25 	const char *src;
26 	register size_t n;
27 {
28 	if (n != 0) {
29 		register char *d = dst;
30 		register const char *s = src;
31 
32 		do {
33 			if ((*d++ = *s++) == 0) {
34 				/* NUL pad the remaining n-1 bytes */
35 				while (--n != 0)
36 					*d++ = 0;
37 				break;
38 			}
39 		} while (--n != 0);
40 	}
41 	return (dst);
42 }
43