annotate lib/stpncpy.c @ 6163:bba2240c9260

* iconvme.h: Add prototype for iconv_alloc.
author Simon Josefsson <simon@josefsson.org>
date Tue, 30 Aug 2005 07:38:53 +0000
parents a48fb0e98c8c
children b06ebe2b7e19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5584
f43f068c9228 Move some conditionals from the .c file to the .m4 file.
Bruno Haible <bruno@clisp.org>
parents: 4746
diff changeset
1 /* Copyright (C) 1993, 1995-1997, 2002-2003, 2005 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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify it
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 along with this program; if not, write to the Free Software
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5584
diff changeset
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 USA. */
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* This is almost copied from strncpy.c, written by Torbjorn Granlund. */
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 #ifdef HAVE_CONFIG_H
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 # include <config.h>
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #endif
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 /* Specification. */
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "stpncpy.h"
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #ifndef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # define __stpncpy stpncpy
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #endif
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
4746
7f0802de0710 Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents: 4691
diff changeset
34 /* 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
35 last non-NUL byte written into DST. */
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 char *
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 __stpncpy (char *dest, const char *src, size_t n)
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 char c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 char *s = dest;
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 if (n >= 4)
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 size_t n4 = n >> 2;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
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 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (--n4 == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 goto last_chars;
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 n -= dest - s;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 goto zero_fill;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 last_chars:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 n &= 3;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 --n;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 }
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 zero_fill:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 while (n-- > 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 dest[n] = '\0';
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 return dest - 1;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 #ifdef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 weak_alias (__stpncpy, stpncpy)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 #endif