Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / strcpy.c
1 /*
2 This file is part of SXEmacs
3
4 SXEmacs is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 SXEmacs is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 /* Synched up with: Not in FSF. */
19
20 /* In SunOS 4.1.1 the strcpy function references memory past the last byte of
21    the string!  This will core dump if the memory following the last byte is
22    not mapped.
23
24    Here are correct versions by hbs@lucid.com.
25 */
26
27 # include <config.h>
28 # ifndef REGISTER               /* Strictly enforced in 20.3 */
29 # define REGISTER
30 # endif
31
32 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
33
34 #define MAGIC    0x7efefeff
35 #define HIGH_BIT_P(c) ((c) & hi_bit)
36 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
37
38 char *strcpy(char *to, const char *from)
39 {
40         char *return_value = to;
41         if (to == from)
42                 return to;
43         else if (ALIGNED(to) && ALIGNED(from)) {
44                 unsigned long *to1 = (unsigned long *)to;
45                 const unsigned long *from1 = (const unsigned long *)from;
46                 unsigned long c;
47                 unsigned long magic = MAGIC;
48                 unsigned long not_magic = ~magic;
49 /*      unsigned long hi_bit = 0x80000000; */
50
51                 while ((c = *from1) != 0) {
52                         if (HAS_ZERO(c)) {
53                                 to = (char *)to1;
54                                 from = (const char *)from1;
55                                 goto slow_loop;
56                         } else {
57                                 *to1 = c;
58                                 to1++;
59                                 from1++;
60                         }
61                 }
62
63                 to = (char *)to1;
64                 *to = (char)0;
65                 return return_value;
66         } else {
67                 char c;
68
69               slow_loop:
70
71                 while ((c = *from) != 0) {
72                         *to = c;
73                         to++;
74                         from++;
75                 }
76                 *to = (char)0;
77         }
78         return return_value;
79 }