1 #include <sys/types.h>
2 #include <string.h>
3 #include "Strn.h"
4 
5 
6 /*
7  * Copy src to dst, truncating or null-padding to always copy n-1 bytes.
8  * Return dst.
9  */
10 char *
11 Strncpy(char *const dst, const char *const src, const size_t n)
12 {
13 	register char *d;
14 	register const char *s;
15 	register size_t i;
16 
17 	d = dst;
18 	*d = 0;
19 	if (n != 0) {
20 		s = src;
21 		/* If they specified a maximum of n characters, use n - 1 chars to
22 		 * hold the copy, and the last character in the array as a NUL.
23 		 * This is the difference between the regular strncpy routine.
24 		 * strncpy doesn't guarantee that your new string will have a
25 		 * NUL terminator, but this routine does.
26 		 */
27 		for (i=1; i<n; i++) {
28 			if ((*d++ = *s++) == 0) {
29 #if (STRN_ZERO_PAD == 1)
30 				/* Pad with zeros. */
31 				for (; i<n; i++)
32 					*d++ = 0;
33 #endif	/* STRN_ZERO_PAD */
34 				return dst;
35 			}
36 		}
37 		/* If we get here, then we have a full string, with n - 1 characters,
38 		 * so now we NUL terminate it and go home.
39 		 */
40 		*d = 0;
41 	}
42 	return (dst);
43 }	/* Strncpy */
44 
45 /* eof Strn.c */
46