source: trunk/libs/newlib/src/newlib/libc/string/strncat.c @ 543

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

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

File size: 2.4 KB
Line 
1/*
2FUNCTION
3        <<strncat>>---concatenate strings
4
5INDEX
6        strncat
7
8SYNOPSIS
9        #include <string.h>
10        char *strncat(char *restrict <[dst]>, const char *restrict <[src]>,
11                      size_t <[length]>);
12
13DESCRIPTION
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
21WARNINGS
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
26RETURNS
27        This function returns the initial value of <[dst]>
28
29PORTABILITY
30<<strncat>> is ANSI C.
31
32<<strncat>> requires no supporting OS subroutines.
33
34QUICKREF
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
60char *
61strncat (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}
Note: See TracBrowser for help on using the repository browser.