1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<strcat>>---concatenate strings
20
21 INDEX
22 strcat
23
24 SYNOPSIS
25 #include <string.h>
26 char *strcat(char *restrict <[dst]>, const char *restrict <[src]>);
27
28 DESCRIPTION
29 <<strcat>> appends a copy of the string pointed to by <[src]>
30 (including the terminating null character) to the end of the
31 string pointed to by <[dst]>. The initial character of
32 <[src]> overwrites the null character at the end of <[dst]>.
33
34 RETURNS
35 This function returns the initial value of <[dst]>
36
37 PORTABILITY
38 <<strcat>> is ANSI C.
39
40 <<strcat>> requires no supporting OS subroutines.
41
42 QUICKREF
43 strcat ansi pure
44 */
45
46 #include <string.h>
47 #include <limits.h>
48 #include <stdint.h>
49
50 /* Nonzero if X is aligned on a "long" boundary. */
51 #define ALIGNED(X) \
52 (((uintptr_t)X & (sizeof (long) - 1)) == 0)
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
70 /*SUPPRESS 560*/
71 /*SUPPRESS 530*/
72
73 #undef strcat
74
75 char *
strcat(char * __restrict s1,const char * __restrict s2)76 strcat (char *__restrict s1,
77 const char *__restrict s2)
78 {
79 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
80 defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
81 char *s = s1;
82
83 while (*s1)
84 s1++;
85
86 while ((*s1++ = *s2++))
87 ;
88 return s;
89 #else
90 char *s = s1;
91
92
93 /* Skip over the data in s1 as quickly as possible. */
94 if (ALIGNED (s1))
95 {
96 unsigned long *aligned_s1 = (unsigned long *)s1;
97 while (!DETECTNULL (*aligned_s1))
98 aligned_s1++;
99
100 s1 = (char *)aligned_s1;
101 }
102
103 while (*s1)
104 s1++;
105
106 /* s1 now points to the its trailing null character, we can
107 just use strcpy to do the work for us now.
108
109 ?!? We might want to just include strcpy here.
110 Also, this will cause many more unaligned string copies because
111 s1 is much less likely to be aligned. I don't know if its worth
112 tweaking strcpy to handle this better. */
113 strcpy (s1, s2);
114
115 return s;
116 #endif /* not PREFER_SIZE_OVER_SPEED */
117 }
118