Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / strncat.c
blob7351913f95b8a4e93f5b301fdb2b43a15d600c8f
1 /*
2 FUNCTION
3 <<strncat>>---concatenate strings
5 INDEX
6 strncat
8 SYNOPSIS
9 #include <string.h>
10 char *strncat(char *restrict <[dst]>, const char *restrict <[src]>,
11 size_t <[length]>);
13 DESCRIPTION
14 <<strncat>> appends not more than <[length]> characters from
15 the string pointed to by <[src]> (including the terminating
16 null character) to the end of the string pointed to by
17 <[dst]>. The initial character of <[src]> overwrites the null
18 character at the end of <[dst]>. A terminating null character
19 is always appended to the result
21 WARNINGS
22 Note that a null is always appended, so that if the copy is
23 limited by the <[length]> argument, the number of characters
24 appended to <[dst]> is <<n + 1>>.
26 RETURNS
27 This function returns the initial value of <[dst]>
29 PORTABILITY
30 <<strncat>> is ANSI C.
32 <<strncat>> requires no supporting OS subroutines.
34 QUICKREF
35 strncat ansi pure
38 #include <string.h>
39 #include <limits.h>
41 /* Nonzero if X is aligned on a "long" boundary. */
42 #define ALIGNED(X) \
43 (((long)X & (sizeof (long) - 1)) == 0)
45 #if LONG_MAX == 2147483647L
46 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
47 #else
48 #if LONG_MAX == 9223372036854775807L
49 /* Nonzero if X (a long int) contains a NULL byte. */
50 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
51 #else
52 #error long int is not a 32bit or 64bit type.
53 #endif
54 #endif
56 #ifndef DETECTNULL
57 #error long int is not a 32bit or 64bit byte
58 #endif
60 char *
61 strncat (char *__restrict s1,
62 const char *__restrict s2,
63 size_t n)
65 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
66 char *s = s1;
68 while (*s1)
69 s1++;
70 while (n-- != 0 && (*s1++ = *s2++))
72 if (n == 0)
73 *s1 = '\0';
76 return s;
77 #else
78 char *s = s1;
80 /* Skip over the data in s1 as quickly as possible. */
81 if (ALIGNED (s1))
83 unsigned long *aligned_s1 = (unsigned long *)s1;
84 while (!DETECTNULL (*aligned_s1))
85 aligned_s1++;
87 s1 = (char *)aligned_s1;
90 while (*s1)
91 s1++;
93 /* s1 now points to the its trailing null character, now copy
94 up to N bytes from S2 into S1 stopping if a NULL is encountered
95 in S2.
97 It is not safe to use strncpy here since it copies EXACTLY N
98 characters, NULL padding if necessary. */
99 while (n-- != 0 && (*s1++ = *s2++))
101 if (n == 0)
102 *s1 = '\0';
105 return s;
106 #endif /* not PREFER_SIZE_OVER_SPEED */