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   char *s = s1;
81 
82   while (*s1)
83     s1++;
84 
85   while ((*s1++ = *s2++))
86     ;
87   return s;
88 #else
89   char *s = s1;
90 
91 
92   /* Skip over the data in s1 as quickly as possible.  */
93   if (ALIGNED (s1))
94     {
95       unsigned long *aligned_s1 = (unsigned long *)s1;
96       while (!DETECTNULL (*aligned_s1))
97 	aligned_s1++;
98 
99       s1 = (char *)aligned_s1;
100     }
101 
102   while (*s1)
103     s1++;
104 
105   /* s1 now points to the its trailing null character, we can
106      just use strcpy to do the work for us now.
107 
108      ?!? We might want to just include strcpy here.
109      Also, this will cause many more unaligned string copies because
110      s1 is much less likely to be aligned.  I don't know if its worth
111      tweaking strcpy to handle this better.  */
112   strcpy (s1, s2);
113 
114   return s;
115 #endif /* not PREFER_SIZE_OVER_SPEED */
116 }
117