annotate lib/stpncpy.c @ 17255:d81be792518a

update from texinfo
author Karl Berry <karl@freefriends.org>
date Tue, 01 Jan 2013 15:51:49 -0800
parents e542fd46ad6f
children 815194217914
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
1 /* Copyright (C) 1993, 1995-1997, 2002-2003, 2005-2007, 2009-2013 Free Software
12518
b5e42ef33b49 update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents: 12421
diff changeset
2 * Foundation, Inc.
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 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
5 Bugs can be reported to bug-glibc@gnu.org.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7944
diff changeset
7 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
8 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
9 Free Software Foundation; either version 3 of the License, or any
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 later version.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 GNU General Public License for more details.
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 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
18 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
19
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* This is almost copied from strncpy.c, written by Torbjorn Granlund. */
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7025
diff changeset
22 #include <config.h>
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
7944
a1d177cd9523 * doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents: 7304
diff changeset
25 #include <string.h>
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #ifndef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # define __stpncpy stpncpy
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #endif
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
4746
7f0802de0710 Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
31 /* 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
32 last non-NUL byte written into DST. */
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 char *
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 __stpncpy (char *dest, const char *src, size_t n)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 char c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 char *s = dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 if (n >= 4)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 size_t n4 = n >> 2;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 for (;;)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
44 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
45 c = *src++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
46 *dest++ = c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
47 if (c == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
48 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
49 c = *src++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
50 *dest++ = c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
51 if (c == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
52 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
53 c = *src++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
54 *dest++ = c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
55 if (c == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
56 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
57 c = *src++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
58 *dest++ = c;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
59 if (c == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
60 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
61 if (--n4 == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
62 goto last_chars;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
63 }
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 n -= dest - s;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 goto zero_fill;
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 last_chars:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 n &= 3;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 --n;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 if (c == '\0')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
79 break;
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 if (n == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
81 return dest;
4221
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 zero_fill:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 while (n-- > 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 dest[n] = '\0';
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 return dest - 1;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 #ifdef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 weak_alias (__stpncpy, stpncpy)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #endif