Commit | Line | Data |
---|---|---|
d8875586 MBT |
1 | ?RCS: $Id: d_safebcpy.U 1 2006-08-24 12:32:52Z rmanfredi $ |
2 | ?RCS: | |
3 | ?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi | |
4 | ?RCS: | |
5 | ?RCS: You may redistribute only under the terms of the Artistic Licence, | |
6 | ?RCS: as specified in the README file that comes with the distribution. | |
7 | ?RCS: You may reuse parts of this distribution only within the terms of | |
8 | ?RCS: that same Artistic Licence; a copy of which may be found at the root | |
9 | ?RCS: of the source tree for dist 4.0. | |
10 | ?RCS: | |
11 | ?RCS: $Log: d_safebcpy.U,v $ | |
12 | ?RCS: Revision 3.0.1.4 1997/02/28 15:40:58 ram | |
13 | ?RCS: patch61: improved overlapping copy check | |
14 | ?RCS: patch61: comfort them if they have memmove | |
15 | ?RCS: patch61: added ?F: metalint hint | |
16 | ?RCS: | |
17 | ?RCS: Revision 3.0.1.3 1995/07/25 13:58:40 ram | |
18 | ?RCS: patch56: re-arranged compile line to include ldflags before objects | |
19 | ?RCS: | |
20 | ?RCS: Revision 3.0.1.2 1995/01/11 15:29:23 ram | |
21 | ?RCS: patch45: added 'ldflags' to the test compile line (ADO) | |
22 | ?RCS: | |
23 | ?RCS: Revision 3.0.1.1 1994/05/06 14:49:03 ram | |
24 | ?RCS: patch23: ensure string is not optimized in read-only memory (ADO) | |
25 | ?RCS: | |
26 | ?RCS: Revision 3.0 1993/08/18 12:06:58 ram | |
27 | ?RCS: Baseline for dist 3.0 netwide release. | |
28 | ?RCS: | |
29 | ?MAKE:d_safebcpy: cat d_bcopy +cc +optimize +ccflags +ldflags +libs rm \ | |
30 | d_memmove i_memory i_stdlib i_string i_unistd Oldconfig Setvar | |
31 | ?MAKE: -pick add $@ %< | |
32 | ?S:d_safebcpy: | |
33 | ?S: This variable conditionally defines the HAS_SAFE_BCOPY symbol if | |
34 | ?S: the bcopy() routine can do overlapping copies. | |
35 | ?S:. | |
36 | ?C:HAS_SAFE_BCOPY (SAFE_BCOPY): | |
37 | ?C: This symbol, if defined, indicates that the bcopy routine is available | |
38 | ?C: to copy potentially overlapping memory blocks. Otherwise you should | |
39 | ?C: probably use memmove() or memcpy(). If neither is defined, roll your | |
40 | ?C: own version. | |
41 | ?C:. | |
42 | ?H:#$d_safebcpy HAS_SAFE_BCOPY /**/ | |
43 | ?H:. | |
44 | ?F:!safebcpy | |
45 | ?LINT: set d_safebcpy | |
46 | : can bcopy handle overlapping blocks? | |
47 | ?X: assume the worst | |
48 | val="$undef" | |
49 | case "$d_bcopy" in | |
50 | "$define") | |
51 | echo " " | |
52 | echo "Checking to see if your bcopy() can do overlapping copies..." >&4 | |
53 | $cat >foo.c <<EOCP | |
54 | #$i_memory I_MEMORY | |
55 | #$i_stdlib I_STDLIB | |
56 | #$i_string I_STRING | |
57 | #$i_unistd I_UNISTD | |
58 | EOCP | |
59 | $cat >>foo.c <<'EOCP' | |
60 | #include <stdio.h> | |
61 | #ifdef I_MEMORY | |
62 | # include <memory.h> | |
63 | #endif | |
64 | #ifdef I_STDLIB | |
65 | # include <stdlib.h> | |
66 | #endif | |
67 | #ifdef I_STRING | |
68 | # include <string.h> | |
69 | #else | |
70 | # include <strings.h> | |
71 | #endif | |
72 | #ifdef I_UNISTD | |
73 | # include <unistd.h> /* Needed for NetBSD */ | |
74 | #endif | |
75 | int main() | |
76 | { | |
77 | char buf[128], abc[128]; | |
78 | char *b; | |
79 | int len; | |
80 | int off; | |
81 | int align; | |
82 | ||
83 | ?X: Copy "abcde..." string to char abc[] so that gcc doesn't | |
84 | ?X: try to store the string in read-only memory. | |
85 | bcopy("abcdefghijklmnopqrstuvwxyz0123456789", abc, 36); | |
86 | ||
87 | for (align = 7; align >= 0; align--) { | |
88 | for (len = 36; len; len--) { | |
89 | b = buf+align; | |
90 | bcopy(abc, b, len); | |
91 | for (off = 1; off <= len; off++) { | |
92 | bcopy(b, b+off, len); | |
93 | bcopy(b+off, b, len); | |
94 | if (bcmp(b, abc, len)) | |
95 | exit(1); | |
96 | } | |
97 | } | |
98 | } | |
99 | exit(0); | |
100 | } | |
101 | EOCP | |
102 | if $cc $optimize $ccflags $ldflags \ | |
103 | -o safebcpy foo.c $libs >/dev/null 2>&1; then | |
104 | if ./safebcpy 2>/dev/null; then | |
105 | echo "Yes, it can." | |
106 | val="$define" | |
107 | else | |
108 | echo "It can't, sorry." | |
109 | case "$d_memmove" in | |
110 | "$define") echo "But that's Ok since you have memmove()." ;; | |
111 | esac | |
112 | fi | |
113 | else | |
114 | echo "(I can't compile the test program, so we'll assume not...)" | |
115 | case "$d_memmove" in | |
116 | "$define") echo "But that's Ok since you have memmove()." ;; | |
117 | esac | |
118 | fi | |
119 | ;; | |
120 | esac | |
121 | $rm -f foo.* safebcpy core | |
122 | set d_safebcpy | |
123 | eval $setvar | |
124 |