This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add the files from dist/meta to perl's repo
[metaconfig.git] / dist / U / d_safemcpy.U
1 ?RCS: $Id: d_safemcpy.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_safemcpy.U,v $
12 ?RCS: Revision 3.0.1.5  1997/02/28  15:41:12  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.4  1995/07/25  13:58:46  ram
18 ?RCS: patch56: re-arranged compile line to include ldflags before objects
19 ?RCS:
20 ?RCS: Revision 3.0.1.3  1995/03/21  08:47:26  ram
21 ?RCS: patch52: swapped two first arguments of memcpy() calls
22 ?RCS:
23 ?RCS: Revision 3.0.1.2  1994/10/29  16:15:10  ram
24 ?RCS: patch36: added 'ldflags' to the test compile line (ADO)
25 ?RCS:
26 ?RCS: Revision 3.0.1.1  1994/05/06  14:53:12  ram
27 ?RCS: patch23: ensure string is not optimized in read-only memory (ADO)
28 ?RCS:
29 ?RCS: Revision 3.0  1993/08/18  12:06:58  ram
30 ?RCS: Baseline for dist 3.0 netwide release.
31 ?RCS:
32 ?MAKE:d_safemcpy: cat d_memcpy +cc +optimize +ccflags +libs +ldflags rm \
33         d_memmove i_memory i_stdlib i_string i_unistd Oldconfig Setvar
34 ?MAKE:  -pick add $@ %<
35 ?S:d_safemcpy:
36 ?S:     This variable conditionally defines the HAS_SAFE_MEMCPY symbol if
37 ?S:     the memcpy() routine can do overlapping copies.
38 ?S:.
39 ?C:HAS_SAFE_MEMCPY (SAFE_MEMCPY):
40 ?C:     This symbol, if defined, indicates that the memcpy routine is available
41 ?C:     to copy potentially overlapping memory blocks. Otherwise you should
42 ?C:     probably use memmove() or memcpy(). If neither is defined, roll your
43 ?C:     own version.
44 ?C:.
45 ?H:#$d_safemcpy HAS_SAFE_MEMCPY /**/
46 ?H:.
47 ?F:!safemcpy
48 ?LINT: set d_safemcpy
49 : can memcpy handle overlapping blocks?
50 ?X: assume the worst
51 val="$undef"
52 case "$d_memcpy" in
53 "$define")
54         echo " "
55         echo "Checking to see if your memcpy() can do overlapping copies..." >&4
56         $cat >foo.c <<EOCP
57 #$i_memory I_MEMORY
58 #$i_stdlib I_STDLIB
59 #$i_string I_STRING
60 #$i_unistd I_UNISTD
61 EOCP
62         $cat >>foo.c <<'EOCP'
63 #include <stdio.h>
64 #ifdef I_MEMORY
65 #  include <memory.h>
66 #endif
67 #ifdef I_STDLIB
68 #  include <stdlib.h>
69 #endif
70 #ifdef I_STRING
71 #  include <string.h>
72 #else
73 #  include <strings.h>
74 #endif
75 #ifdef I_UNISTD
76 #  include <unistd.h>  /* Needed for NetBSD */
77 #endif
78 int main()
79 {
80 char buf[128], abc[128];
81 char *b;
82 int len;
83 int off;
84 int align;
85
86 /* Copy "abcde..." string to char abc[] so that gcc doesn't
87    try to store the string in read-only memory. */
88 memcpy(abc, "abcdefghijklmnopqrstuvwxyz0123456789", 36);
89
90 for (align = 7; align >= 0; align--) {
91         for (len = 36; len; len--) {
92                 b = buf+align;
93                 memcpy(b, abc, len);
94                 for (off = 1; off <= len; off++) {
95                         memcpy(b+off, b, len);
96                         memcpy(b, b+off, len);
97                         if (memcmp(b, abc, len))
98                                 exit(1);
99                 }
100         }
101 }
102 exit(0);
103 }
104 EOCP
105         if $cc $optimize $ccflags $ldflags \
106                     -o safemcpy foo.c $libs >/dev/null 2>&1; then
107                 if ./safemcpy 2>/dev/null; then
108                         echo "Yes, it can."
109                         val="$define"
110                 else
111                         echo "It can't, sorry."
112                         case "$d_memmove" in
113                         "$define") echo "But that's Ok since you have memmove()." ;;
114                         esac
115                 fi
116         else
117                 echo "(I can't compile the test program, so we'll assume not...)"
118                 case "$d_memmove" in
119                 "$define") echo "But that's Ok since you have memmove()." ;;
120                 esac
121         fi
122         ;;
123 esac
124 $rm -f foo.* safemcpy core
125 set d_safemcpy
126 eval $setvar
127