annotate lib/stpncpy.c @ 4221:06a92cabf1fc

New module stpncpy.
author Bruno Haible <bruno@clisp.org>
date Wed, 29 Jan 2003 16:47:24 +0000
parents
children ce37d22a271f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4221
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Copyright (C) 1993, 1995-1997, 2002-2003 Free Software Foundation, Inc.
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #ifdef _LIBC
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # include <string.h>
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #else
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # include <sys/types.h>
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #endif
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 #ifndef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 # define __stpncpy stpncpy
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #endif
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 /* Copy no more than N characters of SRC to DEST, returning the address of
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 the terminating '\0' in DEST, if any, or else DEST + N. */
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 char *
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 __stpncpy (char *dest, const char *src, size_t n)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 char c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 char *s = dest;
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 if (n >= 4)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 size_t n4 = n >> 2;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 for (;;)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (--n4 == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 goto last_chars;
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 n -= dest - s;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 goto zero_fill;
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
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 last_chars:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 n &= 3;
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 for (;;)
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 c = *src++;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 --n;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 *dest++ = c;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 if (c == '\0')
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 break;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 if (n == 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 return dest;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 }
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 zero_fill:
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 while (n-- > 0)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 dest[n] = '\0';
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 return dest - 1;
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 }
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 #ifdef weak_alias
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 weak_alias (__stpncpy, stpncpy)
06a92cabf1fc New module stpncpy.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 #endif