annotate lib/stpncpy.c @ 9309:bbbbbf4cd1c5

Change copyright notice from GPLv2+ to GPLv3+.
author Bruno Haible <bruno@clisp.org>
date Sun, 07 Oct 2007 19:14:58 +0200
parents a1d177cd9523
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7944
a1d177cd9523 * doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents: 7304
diff changeset
1 /* Copyright (C) 1993, 1995-1997, 2002-2003, 2005-2007 Free Software Foundation, Inc.
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 Bugs can be reported to bug-glibc@gnu.org.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
6 This program is free software: you can redistribute it and/or modify it
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
8 Free Software Foundation; either version 3 of the License, or any
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 later version.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 GNU General Public License for more details.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* This is almost copied from strncpy.c, written by Torbjorn Granlund. */
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7025
diff changeset
21 #include <config.h>
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
7944
a1d177cd9523 * doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents: 7304
diff changeset
24 #include <string.h>
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #ifndef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 # define __stpncpy stpncpy
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #endif
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
4746
7f0802de0710 Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
30 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
7f0802de0710 Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
31 last non-NUL byte written into DST. */
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 char *
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 __stpncpy (char *dest, const char *src, size_t n)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 char c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 char *s = dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 if (n >= 4)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 size_t n4 = n >> 2;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 if (--n4 == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 goto last_chars;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 n -= dest - s;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 goto zero_fill;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 last_chars:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 n &= 3;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 --n;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 zero_fill:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 while (n-- > 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 dest[n] = '\0';
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 return dest - 1;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 #ifdef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 weak_alias (__stpncpy, stpncpy)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 #endif