annotate src/strcpy.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children abe6d1db359e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 /* In SunOS 4.1.1 the strcpy function references memory past the last byte of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 the string! This will core dump if the memory following the last byte is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 not mapped.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 Here are correct versions by hbs@lucid.com.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 # include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 # ifndef REGISTER /* Strictly enforced in 20.3 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 # define REGISTER
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 # endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 #define MAGIC 0x7efefeff
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #define HIGH_BIT_P(c) ((c) & hi_bit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 /* CONST IS LOSING, but const is part of the interface of strcpy */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 char *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 strcpy (char *to, const char *from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 char *return_value = to;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 if (to == from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 return to;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 else if (ALIGNED (to) && ALIGNED (from))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 unsigned long *to1 = (unsigned long *) to;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 const unsigned long *from1 = (const unsigned long *) from;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 unsigned long c;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 unsigned long magic = MAGIC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 unsigned long not_magic = ~magic;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 /* unsigned long hi_bit = 0x80000000; */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 while ((c = *from1) != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 if (HAS_ZERO(c))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 to = (char *) to1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 from = (CONST char *) from1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 goto slow_loop;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 *to1 = c;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 to1++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 from1++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 to = (char *) to1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 *to = (char) 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 return return_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 char c;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 slow_loop:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 while ((c = *from) != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 *to = c;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 to++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 from++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 *to = (char) 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 return return_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 }