summaryrefslogtreecommitdiff
path: root/mcon/U/d_safemcpy.U
diff options
context:
space:
mode:
authorrmanfredi <rmanfredi@190e5f8e-a817-0410-acf6-e9863daed9af>2006-08-24 12:32:52 +0000
committerrmanfredi <rmanfredi@190e5f8e-a817-0410-acf6-e9863daed9af>2006-08-24 12:32:52 +0000
commit8bfc5756fb68e0b13d7e7c0073ad5b9a4790d1b6 (patch)
treedee05e98bc53766d609ef2a3a07a5672627d812c /mcon/U/d_safemcpy.U
Moving project to sourceforge.
git-svn-id: https://dist.svn.sourceforge.net/svnroot/dist/trunk/dist@1 190e5f8e-a817-0410-acf6-e9863daed9af
Diffstat (limited to 'mcon/U/d_safemcpy.U')
-rw-r--r--mcon/U/d_safemcpy.U127
1 files changed, 127 insertions, 0 deletions
diff --git a/mcon/U/d_safemcpy.U b/mcon/U/d_safemcpy.U
new file mode 100644
index 0000000..d59475b
--- /dev/null
+++ b/mcon/U/d_safemcpy.U
@@ -0,0 +1,127 @@
+?RCS: $Id$
+?RCS:
+?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
+?RCS:
+?RCS: You may redistribute only under the terms of the Artistic Licence,
+?RCS: as specified in the README file that comes with the distribution.
+?RCS: You may reuse parts of this distribution only within the terms of
+?RCS: that same Artistic Licence; a copy of which may be found at the root
+?RCS: of the source tree for dist 4.0.
+?RCS:
+?RCS: $Log: d_safemcpy.U,v $
+?RCS: Revision 3.0.1.5 1997/02/28 15:41:12 ram
+?RCS: patch61: improved overlapping copy check
+?RCS: patch61: comfort them if they have memmove
+?RCS: patch61: added ?F: metalint hint
+?RCS:
+?RCS: Revision 3.0.1.4 1995/07/25 13:58:46 ram
+?RCS: patch56: re-arranged compile line to include ldflags before objects
+?RCS:
+?RCS: Revision 3.0.1.3 1995/03/21 08:47:26 ram
+?RCS: patch52: swapped two first arguments of memcpy() calls
+?RCS:
+?RCS: Revision 3.0.1.2 1994/10/29 16:15:10 ram
+?RCS: patch36: added 'ldflags' to the test compile line (ADO)
+?RCS:
+?RCS: Revision 3.0.1.1 1994/05/06 14:53:12 ram
+?RCS: patch23: ensure string is not optimized in read-only memory (ADO)
+?RCS:
+?RCS: Revision 3.0 1993/08/18 12:06:58 ram
+?RCS: Baseline for dist 3.0 netwide release.
+?RCS:
+?MAKE:d_safemcpy: cat d_memcpy +cc +optimize +ccflags +libs +ldflags rm \
+ d_memmove i_memory i_stdlib i_string i_unistd Oldconfig Setvar
+?MAKE: -pick add $@ %<
+?S:d_safemcpy:
+?S: This variable conditionally defines the HAS_SAFE_MEMCPY symbol if
+?S: the memcpy() routine can do overlapping copies.
+?S:.
+?C:HAS_SAFE_MEMCPY (SAFE_MEMCPY):
+?C: This symbol, if defined, indicates that the memcpy routine is available
+?C: to copy potentially overlapping memory blocks. Otherwise you should
+?C: probably use memmove() or memcpy(). If neither is defined, roll your
+?C: own version.
+?C:.
+?H:#$d_safemcpy HAS_SAFE_MEMCPY /**/
+?H:.
+?F:!safemcpy
+?LINT: set d_safemcpy
+: can memcpy handle overlapping blocks?
+?X: assume the worst
+val="$undef"
+case "$d_memcpy" in
+"$define")
+ echo " "
+ echo "Checking to see if your memcpy() can do overlapping copies..." >&4
+ $cat >foo.c <<EOCP
+#$i_memory I_MEMORY
+#$i_stdlib I_STDLIB
+#$i_string I_STRING
+#$i_unistd I_UNISTD
+EOCP
+ $cat >>foo.c <<'EOCP'
+#include <stdio.h>
+#ifdef I_MEMORY
+# include <memory.h>
+#endif
+#ifdef I_STDLIB
+# include <stdlib.h>
+#endif
+#ifdef I_STRING
+# include <string.h>
+#else
+# include <strings.h>
+#endif
+#ifdef I_UNISTD
+# include <unistd.h> /* Needed for NetBSD */
+#endif
+int main()
+{
+char buf[128], abc[128];
+char *b;
+int len;
+int off;
+int align;
+
+/* Copy "abcde..." string to char abc[] so that gcc doesn't
+ try to store the string in read-only memory. */
+memcpy(abc, "abcdefghijklmnopqrstuvwxyz0123456789", 36);
+
+for (align = 7; align >= 0; align--) {
+ for (len = 36; len; len--) {
+ b = buf+align;
+ memcpy(b, abc, len);
+ for (off = 1; off <= len; off++) {
+ memcpy(b+off, b, len);
+ memcpy(b, b+off, len);
+ if (memcmp(b, abc, len))
+ exit(1);
+ }
+ }
+}
+exit(0);
+}
+EOCP
+ if $cc $optimize $ccflags $ldflags \
+ -o safemcpy foo.c $libs >/dev/null 2>&1; then
+ if ./safemcpy 2>/dev/null; then
+ echo "Yes, it can."
+ val="$define"
+ else
+ echo "It can't, sorry."
+ case "$d_memmove" in
+ "$define") echo "But that's Ok since you have memmove()." ;;
+ esac
+ fi
+ else
+ echo "(I can't compile the test program, so we'll assume not...)"
+ case "$d_memmove" in
+ "$define") echo "But that's Ok since you have memmove()." ;;
+ esac
+ fi
+ ;;
+esac
+$rm -f foo.* safemcpy core
+set d_safemcpy
+eval $setvar
+