Better handling of collision with AIX stpncpy() function.
[gnulib.git] / lib / stpncpy.c
1 /* Copyright (C) 1993, 1995-1997, 2002-2003 Free Software Foundation, Inc.
2
3    NOTE: The canonical source of this file is maintained with the GNU C Library.
4    Bugs can be reported to bug-glibc@gnu.org.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19    USA.  */
20
21 /* This is almost copied from strncpy.c, written by Torbjorn Granlund.  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Specification.  */
28 #include "stpncpy.h"
29
30 #if !HAVE_STPNCPY
31
32 #ifndef _LIBC
33 /* We cannot generally use the name 'stpncpy' since AIX 4 defines an unusable
34    variant of the function but we cannot use it.  */
35 # undef stpncpy
36 # define stpncpy gnu_stpncpy
37 #endif
38
39 #ifndef weak_alias
40 # define __stpncpy stpncpy
41 #endif
42
43 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
44    last non-NUL byte written into DST.  */
45 char *
46 __stpncpy (char *dest, const char *src, size_t n)
47 {
48   char c;
49   char *s = dest;
50
51   if (n >= 4)
52     {
53       size_t n4 = n >> 2;
54
55       for (;;)
56         {
57           c = *src++;
58           *dest++ = c;
59           if (c == '\0')
60             break;
61           c = *src++;
62           *dest++ = c;
63           if (c == '\0')
64             break;
65           c = *src++;
66           *dest++ = c;
67           if (c == '\0')
68             break;
69           c = *src++;
70           *dest++ = c;
71           if (c == '\0')
72             break;
73           if (--n4 == 0)
74             goto last_chars;
75         }
76       n -= dest - s;
77       goto zero_fill;
78     }
79
80  last_chars:
81   n &= 3;
82   if (n == 0)
83     return dest;
84
85   for (;;)
86     {
87       c = *src++;
88       --n;
89       *dest++ = c;
90       if (c == '\0')
91         break;
92       if (n == 0)
93         return dest;
94     }
95
96  zero_fill:
97   while (n-- > 0)
98     dest[n] = '\0';
99
100   return dest - 1;
101 }
102 #ifdef weak_alias
103 weak_alias (__stpncpy, stpncpy)
104 #endif
105
106 #endif