Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / strcat.c
blob92313c4928b2d6b3096b7eb5da0e00abf12194ef
1 /*
2 FUNCTION
3 <<strcat>>---concatenate strings
5 INDEX
6 strcat
8 SYNOPSIS
9 #include <string.h>
10 char *strcat(char *restrict <[dst]>, const char *restrict <[src]>);
12 DESCRIPTION
13 <<strcat>> appends a copy of the string pointed to by <[src]>
14 (including the terminating null character) to the end of the
15 string pointed to by <[dst]>. The initial character of
16 <[src]> overwrites the null character at the end of <[dst]>.
18 RETURNS
19 This function returns the initial value of <[dst]>
21 PORTABILITY
22 <<strcat>> is ANSI C.
24 <<strcat>> requires no supporting OS subroutines.
26 QUICKREF
27 strcat ansi pure
30 #include <string.h>
31 #include <limits.h>
33 /* Nonzero if X is aligned on a "long" boundary. */
34 #define ALIGNED(X) \
35 (((long)X & (sizeof (long) - 1)) == 0)
37 #if LONG_MAX == 2147483647L
38 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
39 #else
40 #if LONG_MAX == 9223372036854775807L
41 /* Nonzero if X (a long int) contains a NULL byte. */
42 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
43 #else
44 #error long int is not a 32bit or 64bit type.
45 #endif
46 #endif
48 #ifndef DETECTNULL
49 #error long int is not a 32bit or 64bit byte
50 #endif
53 /*SUPPRESS 560*/
54 /*SUPPRESS 530*/
56 char *
57 strcat (char *__restrict s1,
58 const char *__restrict s2)
60 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
61 char *s = s1;
63 while (*s1)
64 s1++;
66 while (*s1++ = *s2++)
68 return s;
69 #else
70 char *s = s1;
73 /* Skip over the data in s1 as quickly as possible. */
74 if (ALIGNED (s1))
76 unsigned long *aligned_s1 = (unsigned long *)s1;
77 while (!DETECTNULL (*aligned_s1))
78 aligned_s1++;
80 s1 = (char *)aligned_s1;
83 while (*s1)
84 s1++;
86 /* s1 now points to the its trailing null character, we can
87 just use strcpy to do the work for us now.
89 ?!? We might want to just include strcpy here.
90 Also, this will cause many more unaligned string copies because
91 s1 is much less likely to be aligned. I don't know if its worth
92 tweaking strcpy to handle this better. */
93 strcpy (s1, s2);
95 return s;
96 #endif /* not PREFER_SIZE_OVER_SPEED */