2e2de11508f86b25b8b1cd7bbcb57bbd6f274080
[platform/upstream/linaro-glibc.git] / sysdeps / generic / strncat.c
1 /* Copyright (C) 1991, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <string.h>
20
21 #ifdef _LIBC
22 # include <memcopy.h>
23 #else
24 typedef char reg_char;
25 #endif
26
27 #undef strncat
28
29 char *
30 strncat (s1, s2, n)
31      char *s1;
32      const char *s2;
33      size_t n;
34 {
35   reg_char c;
36   char *s = s1;
37
38   /* Find the end of S1.  */
39   do
40     c = *s1++;
41   while (c != '\0');
42
43   /* Make S1 point before next character, so we can increment
44      it while memory is read (wins on pipelined cpus).  */
45   s1 -= 2;
46
47   if (n >= 4)
48     {
49       size_t n4 = n >> 2;
50       do
51         {
52           c = *s2++;
53           *++s1 = c;
54           if (c == '\0')
55             return s;
56           c = *s2++;
57           *++s1 = c;
58           if (c == '\0')
59             return s;
60           c = *s2++;
61           *++s1 = c;
62           if (c == '\0')
63             return s;
64           c = *s2++;
65           *++s1 = c;
66           if (c == '\0')
67             return s;
68         } while (--n4 > 0);
69       n &= 3;
70     }
71
72   while (n > 0)
73     {
74       c = *s2++;
75       *++s1 = c;
76       if (c == '\0')
77         return s;
78       n--;
79     }
80
81   if (c != '\0')
82     *++s1 = '\0';
83
84   return s;
85 }