1 /*
2 FUNCTION
3 <<strncpy>>---counted copy string
4
5 INDEX
6 strncpy
7
8 ANSI_SYNOPSIS
9 #include <string.h>
10 char *strncpy(char *restrict <[dst]>, const char *restrict <[src]>,
11 size_t <[length]>);
12
13 TRAD_SYNOPSIS
14 #include <string.h>
15 char *strncpy(<[dst]>, <[src]>, <[length]>)
16 char *<[dst]>;
17 char *<[src]>;
18 size_t <[length]>;
19
20 DESCRIPTION
21 <<strncpy>> copies not more than <[length]> characters from the
22 the string pointed to by <[src]> (including the terminating
23 null character) to the array pointed to by <[dst]>. If the
24 string pointed to by <[src]> is shorter than <[length]>
25 characters, null characters are appended to the destination
26 array until a total of <[length]> characters have been
27 written.
28
29 RETURNS
30 This function returns the initial value of <[dst]>.
31
32 PORTABILITY
33 <<strncpy>> is ANSI C.
34
35 <<strncpy>> requires no supporting OS subroutines.
36
37 QUICKREF
38 strncpy ansi pure
39 */
40
41 #include <section_config.h>
42 #include <basic_types.h>
43
44 #include <string.h>
45 #include <limits.h>
46
47 /*SUPPRESS 560*/
48 /*SUPPRESS 530*/
49
50 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
51 #define UNALIGNED(X, Y) \
52 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
53
54 #if LONG_MAX == 2147483647L
55 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
56 #else
57 #if LONG_MAX == 9223372036854775807L
58 /* Nonzero if X (a long int) contains a NULL byte. */
59 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
60 #else
61 #error long int is not a 32bit or 64bit type.
62 #endif
63 #endif
64
65 #ifndef DETECTNULL
66 #error long int is not a 32bit or 64bit byte
67 #endif
68
69 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
70
71 LIBC_ROM_TEXT_SECTION
72 _LONG_CALL_
_strncpy(char * __restrict dst0,const char * __restrict src0,size_t count)73 char * _strncpy(char *__restrict dst0 , const char *__restrict src0 , size_t count)
74 {
75 #if defined(PREFER_SIZE_OVER_SPEED)
76 char *dscan;
77 const char *sscan;
78
79 dscan = dst0;
80 sscan = src0;
81 while (count > 0)
82 {
83 --count;
84 if ((*dscan++ = *sscan++) == '\0')
85 break;
86 }
87 while (count-- > 0)
88 *dscan++ = '\0';
89
90 return dst0;
91 #else
92 char *dst = dst0;
93 const char *src = src0;
94 long *aligned_dst;
95 const long *aligned_src;
96
97 /* If SRC and DEST is aligned and count large enough, then copy words. */
98 if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
99 {
100 aligned_dst = (long*)dst;
101 aligned_src = (long*)src;
102
103 /* SRC and DEST are both "long int" aligned, try to do "long int"
104 sized copies. */
105 while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
106 {
107 count -= sizeof (long int);
108 *aligned_dst++ = *aligned_src++;
109 }
110
111 dst = (char*)aligned_dst;
112 src = (char*)aligned_src;
113 }
114
115 while (count > 0)
116 {
117 --count;
118 if ((*dst++ = *src++) == '\0')
119 break;
120 }
121
122 while (count-- > 0)
123 *dst++ = '\0';
124
125 return dst0;
126 #endif /* not PREFER_SIZE_OVER_SPEED */
127 }
128