Back to index

glibc  2.9
strncat.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 
00021 #ifdef _LIBC
00022 # include <memcopy.h>
00023 #else
00024 typedef char reg_char;
00025 #endif
00026 
00027 #undef strncat
00028 
00029 char *
00030 strncat (s1, s2, n)
00031      char *s1;
00032      const char *s2;
00033      size_t n;
00034 {
00035   reg_char c;
00036   char *s = s1;
00037 
00038   /* Find the end of S1.  */
00039   do
00040     c = *s1++;
00041   while (c != '\0');
00042 
00043   /* Make S1 point before next character, so we can increment
00044      it while memory is read (wins on pipelined cpus).  */
00045   s1 -= 2;
00046 
00047   if (n >= 4)
00048     {
00049       size_t n4 = n >> 2;
00050       do
00051        {
00052          c = *s2++;
00053          *++s1 = c;
00054          if (c == '\0')
00055            return s;
00056          c = *s2++;
00057          *++s1 = c;
00058          if (c == '\0')
00059            return s;
00060          c = *s2++;
00061          *++s1 = c;
00062          if (c == '\0')
00063            return s;
00064          c = *s2++;
00065          *++s1 = c;
00066          if (c == '\0')
00067            return s;
00068        } while (--n4 > 0);
00069       n &= 3;
00070     }
00071 
00072   while (n > 0)
00073     {
00074       c = *s2++;
00075       *++s1 = c;
00076       if (c == '\0')
00077        return s;
00078       n--;
00079     }
00080 
00081   if (c != '\0')
00082     *++s1 = '\0';
00083 
00084   return s;
00085 }