This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Plan 9: No Configure.
[metaconfig.git] / U / compline / d_safemcpy.U
1 ?RCS: $Id: d_safemcpy.U,v 3.0.1.5 1997/02/28 15:41:12 ram Exp $
2 ?RCS:
3 ?RCS: Copyright (c) 1991-1993, 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 3.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: Compile cat d_memcpy 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:     For overlapping copies, memmove() should be used, if available.
39 ?S:.
40 ?C:HAS_SAFE_MEMCPY (SAFE_MEMCPY):
41 ?C:     This symbol, if defined, indicates that the memcpy routine is available
42 ?C:     to copy potentially overlapping memory blocks.  If you need to
43 ?C:     copy overlapping memory blocks, you should check HAS_MEMMOVE and
44 ?C:     use memmove() instead, if available.
45 ?C:.
46 ?H:#$d_safemcpy HAS_SAFE_MEMCPY /**/
47 ?H:.
48 ?F:!try
49 ?LINT: set d_safemcpy
50 : can memcpy handle overlapping blocks?
51 echo " "
52 ?X: assume the worst
53 val="$undef"
54 case "$d_memmove" in
55 "$define") echo "I'll use memmove() instead of memcpy() for overlapping copies." ;;
56 *)      case "$d_memcpy" in
57         "$define")
58                 echo "Checking to see if memcpy() can do overlapping copies..." >&4
59                 $cat >try.c <<EOCP
60 #$i_memory I_MEMORY
61 #$i_stdlib I_STDLIB
62 #$i_string I_STRING
63 #$i_unistd I_UNISTD
64 EOCP
65         $cat >>try.c <<'EOCP'
66 #include <stdio.h>
67 #ifdef I_MEMORY
68 #  include <memory.h>
69 #endif
70 #ifdef I_STDLIB
71 #  include <stdlib.h>
72 #endif
73 #ifdef I_STRING
74 #  include <string.h>
75 #else
76 #  include <strings.h>
77 #endif
78 #ifdef I_UNISTD
79 #  include <unistd.h>  /* Needed for NetBSD */
80 #endif
81 int main()
82 {
83 char buf[128], abc[128];
84 char *b;
85 int len;
86 int off;
87 int align;
88
89 /* Copy "abcde..." string to char abc[] so that gcc doesn't
90    try to store the string in read-only memory. */
91 memcpy(abc, "abcdefghijklmnopqrstuvwxyz0123456789", 36);
92
93 for (align = 7; align >= 0; align--) {
94         for (len = 36; len; len--) {
95                 b = buf+align;
96                 memcpy(b, abc, len);
97                 for (off = 1; off <= len; off++) {
98                         memcpy(b+off, b, len);
99                         memcpy(b, b+off, len);
100                         if (memcmp(b, abc, len))
101                                 exit(1);
102                 }
103         }
104 }
105 exit(0);
106 }
107 EOCP
108                 set try
109                 if eval $compile_ok; then
110                         if ./try 2>/dev/null; then
111                                 echo "Yes, it can."
112                                 val="$define"
113                         else
114                                 echo "It can't, sorry."
115                         fi
116                 else
117                         echo "(I can't compile the test program, so we'll assume not...)"
118                 fi
119                 ;;
120         esac
121         $rm -f try.* try core
122         ;;
123 esac
124 set d_safemcpy
125 eval $setvar
126