[444] | 1 | /* |
---|
| 2 | FUNCTION |
---|
| 3 | <<strncat>>---concatenate strings |
---|
| 4 | |
---|
| 5 | INDEX |
---|
| 6 | strncat |
---|
| 7 | |
---|
| 8 | SYNOPSIS |
---|
| 9 | #include <string.h> |
---|
| 10 | char *strncat(char *restrict <[dst]>, const char *restrict <[src]>, |
---|
| 11 | size_t <[length]>); |
---|
| 12 | |
---|
| 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 |
---|
| 20 | |
---|
| 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>>. |
---|
| 25 | |
---|
| 26 | RETURNS |
---|
| 27 | This function returns the initial value of <[dst]> |
---|
| 28 | |
---|
| 29 | PORTABILITY |
---|
| 30 | <<strncat>> is ANSI C. |
---|
| 31 | |
---|
| 32 | <<strncat>> requires no supporting OS subroutines. |
---|
| 33 | |
---|
| 34 | QUICKREF |
---|
| 35 | strncat ansi pure |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include <string.h> |
---|
| 39 | #include <limits.h> |
---|
| 40 | |
---|
| 41 | /* Nonzero if X is aligned on a "long" boundary. */ |
---|
| 42 | #define ALIGNED(X) \ |
---|
| 43 | (((long)X & (sizeof (long) - 1)) == 0) |
---|
| 44 | |
---|
| 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 |
---|
| 55 | |
---|
| 56 | #ifndef DETECTNULL |
---|
| 57 | #error long int is not a 32bit or 64bit byte |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | char * |
---|
| 61 | strncat (char *__restrict s1, |
---|
| 62 | const char *__restrict s2, |
---|
| 63 | size_t n) |
---|
| 64 | { |
---|
| 65 | #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) |
---|
| 66 | char *s = s1; |
---|
| 67 | |
---|
| 68 | while (*s1) |
---|
| 69 | s1++; |
---|
| 70 | while (n-- != 0 && (*s1++ = *s2++)) |
---|
| 71 | { |
---|
| 72 | if (n == 0) |
---|
| 73 | *s1 = '\0'; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return s; |
---|
| 77 | #else |
---|
| 78 | char *s = s1; |
---|
| 79 | |
---|
| 80 | /* Skip over the data in s1 as quickly as possible. */ |
---|
| 81 | if (ALIGNED (s1)) |
---|
| 82 | { |
---|
| 83 | unsigned long *aligned_s1 = (unsigned long *)s1; |
---|
| 84 | while (!DETECTNULL (*aligned_s1)) |
---|
| 85 | aligned_s1++; |
---|
| 86 | |
---|
| 87 | s1 = (char *)aligned_s1; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | while (*s1) |
---|
| 91 | s1++; |
---|
| 92 | |
---|
| 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. |
---|
| 96 | |
---|
| 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++)) |
---|
| 100 | { |
---|
| 101 | if (n == 0) |
---|
| 102 | *s1 = '\0'; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return s; |
---|
| 106 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
---|
| 107 | } |
---|