Clean up <stdlib.h> slightly.

Interestingly, this mostly involves cleaning up our implementation of
various <string.h> functions.

Change-Id: Ifaef49b5cb997134f7bc0cc31bdac844bdb9e089
diff --git a/libc/Android.mk b/libc/Android.mk
index a2a3108..691017a 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -271,7 +271,6 @@
     upstream-freebsd/lib/libc/string/wcsspn.c \
     upstream-freebsd/lib/libc/string/wcstok.c \
     upstream-freebsd/lib/libc/string/wmemchr.c \
-    upstream-freebsd/lib/libc/string/wmemcpy.c \
     upstream-freebsd/lib/libc/string/wmemset.c \
 
 libc_upstream_netbsd_src_files := \
@@ -506,6 +505,7 @@
     upstream-openbsd/lib/libc/string/strspn.c \
     upstream-openbsd/lib/libc/string/strstr.c \
     upstream-openbsd/lib/libc/string/strtok.c \
+    upstream-openbsd/lib/libc/string/wmemcpy.c \
     upstream-openbsd/lib/libc/string/wcslcpy.c \
     upstream-openbsd/lib/libc/string/wcsstr.c \
     upstream-openbsd/lib/libc/string/wcswidth.c \
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 5f0b1b3..f712c4c 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -40,7 +40,6 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_arm += \
-    upstream-openbsd/lib/libc/string/bcopy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strlcat.c \
     upstream-openbsd/lib/libc/string/strlcpy.c \
diff --git a/libc/arch-arm/cortex-a15/cortex-a15.mk b/libc/arch-arm/cortex-a15/cortex-a15.mk
index ec423c5..cc502cf 100644
--- a/libc/arch-arm/cortex-a15/cortex-a15.mk
+++ b/libc/arch-arm/cortex-a15/cortex-a15.mk
@@ -9,4 +9,6 @@
     arch-arm/cortex-a15/bionic/strcpy.S \
     arch-arm/cortex-a15/bionic/__strcpy_chk.S \
     arch-arm/cortex-a15/bionic/strlen.S \
-    bionic/memmove.c \
+
+libc_openbsd_src_files_arm += \
+    upstream-openbsd/lib/libc/string/memmove.c \
diff --git a/libc/arch-arm/cortex-a9/cortex-a9.mk b/libc/arch-arm/cortex-a9/cortex-a9.mk
index e38c40a..7570567 100644
--- a/libc/arch-arm/cortex-a9/cortex-a9.mk
+++ b/libc/arch-arm/cortex-a9/cortex-a9.mk
@@ -9,4 +9,6 @@
     arch-arm/cortex-a9/bionic/strcpy.S \
     arch-arm/cortex-a9/bionic/__strcpy_chk.S \
     arch-arm/cortex-a9/bionic/strlen.S \
-    bionic/memmove.c \
+
+libc_openbsd_src_files_arm += \
+    upstream-openbsd/lib/libc/string/memmove.c \
diff --git a/libc/arch-arm/generic/generic.mk b/libc/arch-arm/generic/generic.mk
index 96ed949..e49d6d2 100644
--- a/libc/arch-arm/generic/generic.mk
+++ b/libc/arch-arm/generic/generic.mk
@@ -5,8 +5,10 @@
     arch-arm/generic/bionic/strcmp.S \
     arch-arm/generic/bionic/strcpy.S \
     arch-arm/generic/bionic/strlen.c \
-    bionic/memmove.c \
     bionic/__strcat_chk.cpp \
     bionic/__strcpy_chk.cpp \
+
+libc_openbsd_src_files_arm += \
+    upstream-openbsd/lib/libc/string/memmove.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
diff --git a/libc/arch-arm/krait/krait.mk b/libc/arch-arm/krait/krait.mk
index 450f472..8bd5e8b 100644
--- a/libc/arch-arm/krait/krait.mk
+++ b/libc/arch-arm/krait/krait.mk
@@ -12,4 +12,6 @@
     arch-arm/cortex-a15/bionic/strcat.S \
     arch-arm/cortex-a15/bionic/strcpy.S \
     arch-arm/cortex-a15/bionic/strlen.S \
-    bionic/memmove.c \
+
+libc_openbsd_src_files_arm += \
+    upstream-openbsd/lib/libc/string/memmove.c \
diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk
index d558baa..034633e 100644
--- a/libc/arch-mips/mips.mk
+++ b/libc/arch-mips/mips.mk
@@ -29,7 +29,6 @@
     bionic/__strcat_chk.cpp \
     bionic/memchr.c \
     bionic/memcmp.c \
-    bionic/memmove.c \
     bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
@@ -46,7 +45,7 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_mips += \
-    upstream-openbsd/lib/libc/string/bcopy.c \
+    upstream-openbsd/lib/libc/string/memmove.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
@@ -79,10 +78,10 @@
 
 else
 libc_bionic_src_files_mips += \
-    bionic/memcpy.cpp \
     bionic/memset.c \
 
 libc_openbsd_src_files_mips += \
+    upstream-openbsd/lib/libc/string/memcpy.c \
     upstream-openbsd/lib/libc/string/strlen.c \
 
 endif
diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk
index 6fa7d33..26733cb 100644
--- a/libc/arch-mips64/mips64.mk
+++ b/libc/arch-mips64/mips64.mk
@@ -11,8 +11,6 @@
     bionic/__strcat_chk.cpp \
     bionic/memchr.c \
     bionic/memcmp.c \
-    bionic/memcpy.cpp \
-    bionic/memmove.c \
     bionic/memrchr.c \
     bionic/memset.c \
     bionic/strchr.cpp \
@@ -30,6 +28,8 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_mips64 += \
+    upstream-openbsd/lib/libc/string/memcpy.c \
+    upstream-openbsd/lib/libc/string/memmove.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
diff --git a/libc/bionic/error.cpp b/libc/bionic/error.cpp
index 9f4d0ee..e8df0e0 100644
--- a/libc/bionic/error.cpp
+++ b/libc/bionic/error.cpp
@@ -31,6 +31,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 unsigned int error_message_count = 0;
 void (*error_print_progname)(void) = NULL;
diff --git a/libc/bionic/memcpy.cpp b/libc/bionic/memcpy.cpp
deleted file mode 100644
index d527e85..0000000
--- a/libc/bionic/memcpy.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#undef _FORTIFY_SOURCE
-#include <string.h>
-#include <strings.h>
-
-// Our unoptimized memcpy just calls the best bcopy available.
-// (It's this way round rather than the opposite because we're based on BSD source.)
-void* memcpy(void* dst, const void* src, size_t n) {
-  bcopy(src, dst, n);
-  return dst;
-}
diff --git a/libc/bionic/memmove.c b/libc/bionic/memmove.c
deleted file mode 100644
index 39c766e..0000000
--- a/libc/bionic/memmove.c
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#define MEMMOVE
-#include "upstream-openbsd/lib/libc/string/bcopy.c"
diff --git a/libc/bionic/ndk_cruft.cpp b/libc/bionic/ndk_cruft.cpp
index 18a4a14..341ba01 100644
--- a/libc/bionic/ndk_cruft.cpp
+++ b/libc/bionic/ndk_cruft.cpp
@@ -235,6 +235,12 @@
   return signal(signum, handler);
 }
 
+// This was removed from POSIX 2008.
+#undef bcopy
+extern "C" void bcopy(const void* src, void* dst, size_t n) {
+  memcpy(dst, src, n);
+}
+
 // sysv_signal() was never in POSIX.
 extern sighandler_t _signal(int signum, sighandler_t handler, int flags);
 extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) {
diff --git a/libc/bionic/scandir.cpp b/libc/bionic/scandir.cpp
index 28b4ed0..ee62fee 100644
--- a/libc/bionic/scandir.cpp
+++ b/libc/bionic/scandir.cpp
@@ -18,6 +18,7 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "private/bionic_macros.h"
 #include "private/ScopedReaddir.h"
diff --git a/libc/bionic/syslog.cpp b/libc/bionic/syslog.cpp
index d8b8b19..8e3f34f 100644
--- a/libc/bionic/syslog.cpp
+++ b/libc/bionic/syslog.cpp
@@ -16,6 +16,7 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 #include <syslog.h>
 
 #include "private/libc_logging.h"
diff --git a/libc/dns/resolv/res_state.c b/libc/dns/resolv/res_state.c
index 75bff97..7533d19 100644
--- a/libc/dns/resolv/res_state.c
+++ b/libc/dns/resolv/res_state.c
@@ -34,6 +34,7 @@
 #include "resolv_cache.h"
 #include <pthread.h>
 #include <stdlib.h>
+#include <string.h>
 
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #include <sys/_system_properties.h>
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index 0060ea3..4cb288d 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -25,15 +25,16 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#ifndef _STDLIB_H_
-#define _STDLIB_H_
+
+#ifndef _STDLIB_H
+#define _STDLIB_H
 
 #include <sys/cdefs.h>
+#include <xlocale.h>
 
-#include <stddef.h>
 #include <alloca.h>
-#include <strings.h>
-#include <memory.h>
+#include <malloc.h>
+#include <stddef.h>
 
 __BEGIN_DECLS
 
@@ -173,4 +174,4 @@
 
 __END_DECLS
 
-#endif /* _STDLIB_H_ */
+#endif /* _STDLIB_H */
diff --git a/libc/include/string.h b/libc/include/string.h
index f0bed10..4ca77ae 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -36,6 +36,10 @@
 
 __BEGIN_DECLS
 
+#if defined(__USE_BSD)
+#include <strings.h>
+#endif
+
 extern void*  memccpy(void* __restrict, const void* __restrict, int, size_t);
 extern void*  memchr(const void *, int, size_t) __purefunc;
 extern void*  memrchr(const void *, int, size_t) __purefunc;
diff --git a/libc/upstream-openbsd/lib/libc/string/memcpy.c b/libc/upstream-openbsd/lib/libc/string/memcpy.c
new file mode 100644
index 0000000..1b9715e
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/string/memcpy.c
@@ -0,0 +1,99 @@
+/*	$OpenBSD: memcpy.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+/*
+ * sizeof(word) MUST BE A POWER OF TWO
+ * SO THAT wmask BELOW IS ALL ONES
+ */
+typedef	long word;		/* "word" used for optimal copy speed */
+
+#define	wsize	sizeof(word)
+#define	wmask	(wsize - 1)
+
+/*
+ * Copy a block of memory, not handling overlap.
+ */
+void *
+memcpy(void *dst0, const void *src0, size_t length)
+{
+	char *dst = dst0;
+	const char *src = src0;
+	size_t t;
+
+	if (length == 0 || dst == src)		/* nothing to do */
+		goto done;
+
+	if ((dst < src && dst + length > src) ||
+	    (src < dst && src + length > dst)) {
+		struct syslog_data sdata = SYSLOG_DATA_INIT;
+
+		syslog_r(LOG_CRIT, &sdata, "backwards memcpy");
+		abort();
+	}
+
+	/*
+	 * Macros: loop-t-times; and loop-t-times, t>0
+	 */
+#define	TLOOP(s) if (t) TLOOP1(s)
+#define	TLOOP1(s) do { s; } while (--t)
+
+	/*
+	 * Copy forward.
+	 */
+	t = (long)src;	/* only need low bits */
+	if ((t | (long)dst) & wmask) {
+		/*
+		 * Try to align operands.  This cannot be done
+		 * unless the low bits match.
+		 */
+		if ((t ^ (long)dst) & wmask || length < wsize)
+			t = length;
+		else
+			t = wsize - (t & wmask);
+		length -= t;
+		TLOOP1(*dst++ = *src++);
+	}
+	/*
+	 * Copy whole words, then mop up any trailing bytes.
+	 */
+	t = length / wsize;
+	TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
+	t = length & wmask;
+	TLOOP(*dst++ = *src++);
+done:
+	return (dst0);
+}
diff --git a/libc/upstream-openbsd/lib/libc/string/bcopy.c b/libc/upstream-openbsd/lib/libc/string/memmove.c
similarity index 89%
rename from libc/upstream-openbsd/lib/libc/string/bcopy.c
rename to libc/upstream-openbsd/lib/libc/string/memmove.c
index 4308c64..1baad53 100644
--- a/libc/upstream-openbsd/lib/libc/string/bcopy.c
+++ b/libc/upstream-openbsd/lib/libc/string/memmove.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: bcopy.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */
+/*	$OpenBSD: memmove.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
@@ -44,21 +44,9 @@
 
 /*
  * Copy a block of memory, handling overlap.
- * This is the routine that actually implements
- * (the portable versions of) bcopy, memcpy, and memmove.
  */
-#ifdef MEMCOPY
-void *
-memcpy(void *dst0, const void *src0, size_t length)
-#else
-#ifdef MEMMOVE
 void *
 memmove(void *dst0, const void *src0, size_t length)
-#else
-void
-bcopy(const void *src0, void *dst0, size_t length)
-#endif
-#endif
 {
 	char *dst = dst0;
 	const char *src = src0;
@@ -120,9 +108,5 @@
 		TLOOP(*--dst = *--src);
 	}
 done:
-#if defined(MEMCOPY) || defined(MEMMOVE)
 	return (dst0);
-#else
-	return;
-#endif
 }
diff --git a/libc/upstream-freebsd/lib/libc/string/wmemcpy.c b/libc/upstream-openbsd/lib/libc/string/wmemcpy.c
similarity index 84%
rename from libc/upstream-freebsd/lib/libc/string/wmemcpy.c
rename to libc/upstream-openbsd/lib/libc/string/wmemcpy.c
index c10770c..9bbd836 100644
--- a/libc/upstream-freebsd/lib/libc/string/wmemcpy.c
+++ b/libc/upstream-openbsd/lib/libc/string/wmemcpy.c
@@ -1,3 +1,6 @@
+/*	$OpenBSD: wmemcpy.c,v 1.3 2005/08/08 08:05:37 espie Exp $	*/
+/*	$NetBSD: wmemcpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $	*/
+
 /*-
  * Copyright (c)1999 Citrus Project,
  * All rights reserved.
@@ -26,19 +29,12 @@
  *	citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp
  */
 
-#include <sys/cdefs.h>
-#if 0
-#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: wmemcpy.c,v 1.1 2000/12/23 23:14:37 itojun Exp $");
-#endif /* LIBC_SCCS and not lint */
-#endif
-__FBSDID("$FreeBSD$");
-
 #include <string.h>
 #include <wchar.h>
 
 wchar_t *
-wmemcpy(wchar_t * __restrict d, const wchar_t * __restrict s, size_t n)
+wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
 {
+
 	return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t));
 }