source: trunk/libs/newlib/src/newlib/libc/string/strcat.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.1 KB
Line 
1/*
2FUNCTION
3        <<strcat>>---concatenate strings
4
5INDEX
6        strcat
7
8SYNOPSIS
9        #include <string.h>
10        char *strcat(char *restrict <[dst]>, const char *restrict <[src]>);
11
12DESCRIPTION
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]>.
17
18RETURNS
19        This function returns the initial value of <[dst]>
20
21PORTABILITY
22<<strcat>> is ANSI C.
23
24<<strcat>> requires no supporting OS subroutines.
25
26QUICKREF
27        strcat ansi pure
28*/
29
30#include <string.h>
31#include <limits.h>
32
33/* Nonzero if X is aligned on a "long" boundary.  */
34#define ALIGNED(X) \
35  (((long)X & (sizeof (long) - 1)) == 0)
36
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
47
48#ifndef DETECTNULL
49#error long int is not a 32bit or 64bit byte
50#endif
51
52
53/*SUPPRESS 560*/
54/*SUPPRESS 530*/
55
56char *
57strcat (char *__restrict s1,
58        const char *__restrict s2)
59{
60#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
61  char *s = s1;
62
63  while (*s1)
64    s1++;
65
66  while (*s1++ = *s2++)
67    ;
68  return s;
69#else
70  char *s = s1;
71
72
73  /* Skip over the data in s1 as quickly as possible.  */
74  if (ALIGNED (s1))
75    {
76      unsigned long *aligned_s1 = (unsigned long *)s1;
77      while (!DETECTNULL (*aligned_s1))
78        aligned_s1++;
79
80      s1 = (char *)aligned_s1;
81    }
82
83  while (*s1)
84    s1++;
85
86  /* s1 now points to the its trailing null character, we can
87     just use strcpy to do the work for us now.
88
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);
94       
95  return s;
96#endif /* not PREFER_SIZE_OVER_SPEED */
97}
Note: See TracBrowser for help on using the repository browser.