1 /*
2 FUNCTION
3 	<<strncat>>---concatenate strings
4 
5 INDEX
6 	strncat
7 
8 ANSI_SYNOPSIS
9 	#include <string.h>
10 	char *strncat(char *restrict <[dst]>, const char *restrict <[src]>,
11                       size_t <[length]>);
12 
13 TRAD_SYNOPSIS
14 	#include <string.h>
15 	char *strncat(<[dst]>, <[src]>, <[length]>)
16 	char *<[dst]>;
17 	char *<[src]>;
18 	size_t <[length]>;
19 
20 DESCRIPTION
21 	<<strncat>> appends not more than <[length]> characters from
22 	the string pointed to by <[src]> (including the	terminating
23 	null character) to the end of the string pointed to by
24 	<[dst]>.  The initial character of <[src]> overwrites the null
25 	character at the end of <[dst]>.  A terminating null character
26 	is always appended to the result
27 
28 WARNINGS
29 	Note that a null is always appended, so that if the copy is
30 	limited by the <[length]> argument, the number of characters
31 	appended to <[dst]> is <<n + 1>>.
32 
33 RETURNS
34 	This function returns the initial value of <[dst]>
35 
36 PORTABILITY
37 <<strncat>> is ANSI C.
38 
39 <<strncat>> requires no supporting OS subroutines.
40 
41 QUICKREF
42 	strncat ansi pure
43 */
44 
45 #include <section_config.h>
46 #include <basic_types.h>
47 
48 #include <string.h>
49 #include <limits.h>
50 
51 /* Nonzero if X is aligned on a "long" boundary.  */
52 #define ALIGNED(X) \
53   (((long)X & (sizeof (long) - 1)) == 0)
54 
55 #if LONG_MAX == 2147483647L
56 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
57 #else
58 #if LONG_MAX == 9223372036854775807L
59 /* Nonzero if X (a long int) contains a NULL byte. */
60 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
61 #else
62 #error long int is not a 32bit or 64bit type.
63 #endif
64 #endif
65 
66 #ifndef DETECTNULL
67 #error long int is not a 32bit or 64bit byte
68 #endif
69 
70 LIBC_ROM_TEXT_SECTION
71 _LONG_CALL_
_strncat(char * __restrict s1,const char * __restrict s2,size_t n)72 char * _strncat(char *__restrict s1 , const char *__restrict s2 , size_t n)
73 {
74 #if defined(PREFER_SIZE_OVER_SPEED)
75   char *s = s1;
76 
77   while (*s1)
78     s1++;
79   while (n-- != 0 && (*s1++ = *s2++))
80     {
81       if (n == 0)
82 	*s1 = '\0';
83     }
84 
85   return s;
86 #else
87   char *s = s1;
88 
89   /* Skip over the data in s1 as quickly as possible.  */
90   if (ALIGNED (s1))
91     {
92       unsigned long *aligned_s1 = (unsigned long *)s1;
93       while (!DETECTNULL (*aligned_s1))
94 	aligned_s1++;
95 
96       s1 = (char *)aligned_s1;
97     }
98 
99   while (*s1)
100     s1++;
101 
102   /* s1 now points to the its trailing null character, now copy
103      up to N bytes from S2 into S1 stopping if a NULL is encountered
104      in S2.
105 
106      It is not safe to use strncpy here since it copies EXACTLY N
107      characters, NULL padding if necessary.  */
108   while (n-- != 0 && (*s1++ = *s2++))
109     {
110       if (n == 0)
111 	*s1 = '\0';
112     }
113 
114   return s;
115 #endif /* not PREFER_SIZE_OVER_SPEED */
116 }
117