3 <<strncpy>>---counted copy string
10 char *strncpy(char *<[dst]>, const char *<[src]>, size_t <[length]>);
14 char *strncpy(<[dst]>, <[src]>, <[length]>)
20 <<strncpy>> copies not more than <[length]> characters from the
21 the string pointed to by <[src]> (including the terminating
22 null character) to the array pointed to by <[dst]>. If the
23 string pointed to by <[src]> is shorter than <[length]>
24 characters, null characters are appended to the destination
25 array until a total of <[length]> characters have been
29 This function returns the initial value of <[dst]>.
32 <<strncpy>> is ANSI C.
34 <<strncpy>> requires no supporting OS subroutines.
46 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
47 #define UNALIGNED(X, Y) \
48 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
50 #if LONG_MAX == 2147483647L
51 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
53 #if LONG_MAX == 9223372036854775807L
54 /* Nonzero if X (a long int) contains a NULL byte. */
55 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
57 #error long int is not a 32bit or 64bit type.
62 #error long int is not a 32bit or 64bit byte
65 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
68 _DEFUN (strncpy
, (dst0
, src0
),
70 _CONST
char *src0 _AND
73 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
82 if ((*dscan
++ = *sscan
++) == '\0')
91 _CONST
char *src
= src0
;
93 _CONST
long *aligned_src
;
95 /* If SRC and DEST is aligned and count large enough, then copy words. */
96 if (!UNALIGNED (src
, dst
) && !TOO_SMALL (count
))
98 aligned_dst
= (long*)dst
;
99 aligned_src
= (long*)src
;
101 /* SRC and DEST are both "long int" aligned, try to do "long int"
103 while (count
>= sizeof (long int) && !DETECTNULL(*aligned_src
))
105 count
-= sizeof (long int);
106 *aligned_dst
++ = *aligned_src
++;
109 dst
= (char*)aligned_dst
;
110 src
= (char*)aligned_src
;
116 if ((*dst
++ = *src
++) == '\0')
124 #endif /* not PREFER_SIZE_OVER_SPEED */