Better linebreaking of copyright message.
[gnulib.git] / lib / stpncpy.c
1 /* Copyright (C) 1993, 1995-1997, 2002-2003, 2005 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 Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* This is almost copied from strncpy.c, written by Torbjorn Granlund.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 /* Specification.  */
27 #include "stpncpy.h"
28
29 #ifndef weak_alias
30 # define __stpncpy stpncpy
31 #endif
32
33 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
34    last non-NUL byte written into DST.  */
35 char *
36 __stpncpy (char *dest, const char *src, size_t n)
37 {
38   char c;
39   char *s = dest;
40
41   if (n >= 4)
42     {
43       size_t n4 = n >> 2;
44
45       for (;;)
46         {
47           c = *src++;
48           *dest++ = c;
49           if (c == '\0')
50             break;
51           c = *src++;
52           *dest++ = c;
53           if (c == '\0')
54             break;
55           c = *src++;
56           *dest++ = c;
57           if (c == '\0')
58             break;
59           c = *src++;
60           *dest++ = c;
61           if (c == '\0')
62             break;
63           if (--n4 == 0)
64             goto last_chars;
65         }
66       n -= dest - s;
67       goto zero_fill;
68     }
69
70  last_chars:
71   n &= 3;
72   if (n == 0)
73     return dest;
74
75   for (;;)
76     {
77       c = *src++;
78       --n;
79       *dest++ = c;
80       if (c == '\0')
81         break;
82       if (n == 0)
83         return dest;
84     }
85
86  zero_fill:
87   while (n-- > 0)
88     dest[n] = '\0';
89
90   return dest - 1;
91 }
92 #ifdef weak_alias
93 weak_alias (__stpncpy, stpncpy)
94 #endif