3 <<strncpy>>---counted copy string
10 char *strncpy(char *restrict <[dst]>, const char *restrict <[src]>,
14 <<strncpy>> copies not more than <[length]> characters from the
15 the string pointed to by <[src]> (including the terminating
16 null character) to the array pointed to by <[dst]>. If the
17 string pointed to by <[src]> is shorter than <[length]>
18 characters, null characters are appended to the destination
19 array until a total of <[length]> characters have been
23 This function returns the initial value of <[dst]>.
26 <<strncpy>> is ANSI C.
28 <<strncpy>> requires no supporting OS subroutines.
40 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
41 #define UNALIGNED(X, Y) \
42 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
44 #if LONG_MAX == 2147483647L
45 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
47 #if LONG_MAX == 9223372036854775807L
48 /* Nonzero if X (a long int) contains a NULL byte. */
49 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
51 #error long int is not a 32bit or 64bit type.
56 #error long int is not a 32bit or 64bit byte
59 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
62 strncpy (char *__restrict dst0
,
63 const char *__restrict src0
,
66 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
75 if ((*dscan
++ = *sscan
++) == '\0')
84 const char *src
= src0
;
86 const long *aligned_src
;
88 /* If SRC and DEST is aligned and count large enough, then copy words. */
89 if (!UNALIGNED (src
, dst
) && !TOO_SMALL (count
))
91 aligned_dst
= (long*)dst
;
92 aligned_src
= (long*)src
;
94 /* SRC and DEST are both "long int" aligned, try to do "long int"
96 while (count
>= sizeof (long int) && !DETECTNULL(*aligned_src
))
98 count
-= sizeof (long int);
99 *aligned_dst
++ = *aligned_src
++;
102 dst
= (char*)aligned_dst
;
103 src
= (char*)aligned_src
;
109 if ((*dst
++ = *src
++) == '\0')
117 #endif /* not PREFER_SIZE_OVER_SPEED */