Upgrade more functions to the current upstream NetBSD copy.

Change-Id: Ie0b3f8b3fccef28609eb210434413ebd51d6ef45
diff --git a/libc/Android.mk b/libc/Android.mk
index 79f2c9d..bbe1887 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -104,7 +104,6 @@
 	stdlib/toupper_.c \
 	string/index.c \
 	string/strcasecmp.c \
-	string/strcasestr.c \
 	string/strcat.c \
 	string/strchr.c \
 	string/strcspn.c \
@@ -148,11 +147,6 @@
 	wchar/wmemcpy.c \
 	wchar/wmemmove.c \
 	wchar/wmemset.c \
-	inet/inet_addr.c \
-	inet/inet_aton.c \
-	inet/inet_ntoa.c \
-	inet/inet_ntop.c \
-	inet/inet_pton.c \
 	tzcode/asctime.c \
 	tzcode/difftime.c \
 	tzcode/localtime.c \
@@ -236,7 +230,6 @@
 	bionic/setresuid.c \
 	bionic/setreuid.c \
 	bionic/setuid.c \
-	bionic/sha1.c \
 	bionic/sigblock.c \
 	bionic/siginterrupt.c \
 	bionic/siglist.c \
@@ -256,13 +249,11 @@
 	bionic/system_properties.c \
 	bionic/tcgetpgrp.c \
 	bionic/tcsetpgrp.c \
-	bionic/tdestroy.c \
 	bionic/thread_atexit.c \
 	bionic/time64.c \
 	bionic/umount.c \
 	bionic/unlockpt.c \
 	bionic/usleep.c \
-	bionic/utime.c \
 	bionic/utmp.c \
 	bionic/wait.c \
 	bionic/wcscoll.c \
@@ -315,6 +306,7 @@
 	bionic/__strncpy_chk.cpp \
 	bionic/strsignal.cpp \
 	bionic/stubs.cpp \
+	bionic/tdestroy.cpp \
 	bionic/tmpfile.cpp \
 	bionic/__umask_chk.cpp \
 	bionic/__vsnprintf_chk.cpp \
@@ -322,30 +314,37 @@
 	bionic/wchar.cpp \
 
 libc_upstream_netbsd_src_files := \
-	upstream-netbsd/libc/compat-43/creat.c \
-	upstream-netbsd/libc/gen/ftw.c \
-	upstream-netbsd/libc/gen/nftw.c \
-	upstream-netbsd/libc/gen/nice.c \
-	upstream-netbsd/libc/gen/psignal.c \
-	upstream-netbsd/libc/regex/regcomp.c \
-	upstream-netbsd/libc/regex/regerror.c \
-	upstream-netbsd/libc/regex/regexec.c \
-	upstream-netbsd/libc/regex/regfree.c \
-	upstream-netbsd/libc/stdio/getdelim.c \
-	upstream-netbsd/libc/stdio/getline.c \
-	upstream-netbsd/libc/stdlib/drand48.c \
-	upstream-netbsd/libc/stdlib/erand48.c \
-	upstream-netbsd/libc/stdlib/jrand48.c \
-	upstream-netbsd/libc/stdlib/lrand48.c \
-	upstream-netbsd/libc/stdlib/mrand48.c \
-	upstream-netbsd/libc/stdlib/nrand48.c \
-	upstream-netbsd/libc/stdlib/_rand48.c \
-	upstream-netbsd/libc/stdlib/seed48.c \
-	upstream-netbsd/libc/stdlib/srand48.c \
-	upstream-netbsd/libc/stdlib/tdelete.c \
-	upstream-netbsd/libc/stdlib/tfind.c \
-	upstream-netbsd/libc/stdlib/tsearch.c \
-	upstream-netbsd/libc/string/strxfrm.c \
+    upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
+    upstream-netbsd/common/lib/libc/inet/inet_addr.c \
+    upstream-netbsd/libc/compat-43/creat.c \
+    upstream-netbsd/libc/gen/ftw.c \
+    upstream-netbsd/libc/gen/nftw.c \
+    upstream-netbsd/libc/gen/nice.c \
+    upstream-netbsd/libc/gen/psignal.c \
+    upstream-netbsd/libc/gen/utime.c \
+    upstream-netbsd/libc/inet/inet_ntoa.c \
+    upstream-netbsd/libc/inet/inet_ntop.c \
+    upstream-netbsd/libc/inet/inet_pton.c \
+    upstream-netbsd/libc/regex/regcomp.c \
+    upstream-netbsd/libc/regex/regerror.c \
+    upstream-netbsd/libc/regex/regexec.c \
+    upstream-netbsd/libc/regex/regfree.c \
+    upstream-netbsd/libc/stdio/getdelim.c \
+    upstream-netbsd/libc/stdio/getline.c \
+    upstream-netbsd/libc/stdlib/drand48.c \
+    upstream-netbsd/libc/stdlib/erand48.c \
+    upstream-netbsd/libc/stdlib/jrand48.c \
+    upstream-netbsd/libc/stdlib/lrand48.c \
+    upstream-netbsd/libc/stdlib/mrand48.c \
+    upstream-netbsd/libc/stdlib/nrand48.c \
+    upstream-netbsd/libc/stdlib/_rand48.c \
+    upstream-netbsd/libc/stdlib/seed48.c \
+    upstream-netbsd/libc/stdlib/srand48.c \
+    upstream-netbsd/libc/stdlib/tdelete.c \
+    upstream-netbsd/libc/stdlib/tfind.c \
+    upstream-netbsd/libc/stdlib/tsearch.c \
+    upstream-netbsd/libc/string/strcasestr.c \
+    upstream-netbsd/libc/string/strxfrm.c \
 
 # The following files are common, but must be compiled
 # with different C flags when building a static C library.
diff --git a/libc/bionic/tdestroy.c b/libc/bionic/tdestroy.cpp
similarity index 100%
rename from libc/bionic/tdestroy.c
rename to libc/bionic/tdestroy.cpp
diff --git a/libc/include/arpa/inet.h b/libc/include/arpa/inet.h
index e78e7c5..b008812 100644
--- a/libc/include/arpa/inet.h
+++ b/libc/include/arpa/inet.h
@@ -42,7 +42,7 @@
 extern char*         inet_ntoa(struct in_addr);
 
 extern int           inet_pton(int, const char *, void *);
-extern const char*   inet_ntop(int, const void *, char *, size_t);
+extern const char*   inet_ntop(int, const void *, char *, socklen_t);
 
 extern unsigned int  inet_nsap_addr(const char *, unsigned char *, int);
 extern char*         inet_nsap_ntoa(int, const unsigned char *, char *);
diff --git a/libc/include/sha1.h b/libc/include/sha1.h
index f7ada46..7f6cf5d 100644
--- a/libc/include/sha1.h
+++ b/libc/include/sha1.h
@@ -1,31 +1,23 @@
-/*	$NetBSD: sha1.h,v 1.13 2005/12/26 18:41:36 perry Exp $	*/
-
 /*
- * SHA-1 in C
- * By Steve Reid <steve@edmweb.com>
- * 100% Public Domain
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
-#ifndef _SYS_SHA1_H_
-#define	_SYS_SHA1_H_
+#ifndef _SHA1_H_
+#define _SHA1_H_
 
-#include <sys/cdefs.h>
-#include <sys/types.h>
+#warning "include <sys/sha1.h> instead for better portability"
+#include <sys/sha1.h>
 
-#define SHA1_DIGEST_LENGTH		20
-#define SHA1_DIGEST_STRING_LENGTH	41
-
-typedef struct {
-	uint32_t state[5];
-	uint32_t count[2];
-	u_char buffer[64];
-} SHA1_CTX;
-
-__BEGIN_DECLS
-void	SHA1Transform(uint32_t[5], const u_char[64]);
-void	SHA1Init(SHA1_CTX *);
-void	SHA1Update(SHA1_CTX *, const u_char *, u_int);
-void	SHA1Final(u_char[SHA1_DIGEST_LENGTH], SHA1_CTX *);
-__END_DECLS
-
-#endif /* _SYS_SHA1_H_ */
+#endif
diff --git a/libc/include/sys/sha1.h b/libc/include/sys/sha1.h
new file mode 100644
index 0000000..f7ada46
--- /dev/null
+++ b/libc/include/sys/sha1.h
@@ -0,0 +1,31 @@
+/*	$NetBSD: sha1.h,v 1.13 2005/12/26 18:41:36 perry Exp $	*/
+
+/*
+ * SHA-1 in C
+ * By Steve Reid <steve@edmweb.com>
+ * 100% Public Domain
+ */
+
+#ifndef _SYS_SHA1_H_
+#define	_SYS_SHA1_H_
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#define SHA1_DIGEST_LENGTH		20
+#define SHA1_DIGEST_STRING_LENGTH	41
+
+typedef struct {
+	uint32_t state[5];
+	uint32_t count[2];
+	u_char buffer[64];
+} SHA1_CTX;
+
+__BEGIN_DECLS
+void	SHA1Transform(uint32_t[5], const u_char[64]);
+void	SHA1Init(SHA1_CTX *);
+void	SHA1Update(SHA1_CTX *, const u_char *, u_int);
+void	SHA1Final(u_char[SHA1_DIGEST_LENGTH], SHA1_CTX *);
+__END_DECLS
+
+#endif /* _SYS_SHA1_H_ */
diff --git a/libc/inet/inet_addr.c b/libc/inet/inet_addr.c
deleted file mode 100644
index 8e480a4..0000000
--- a/libc/inet/inet_addr.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*	$OpenBSD: inet_addr.c,v 1.9 2005/08/06 20:30:03 espie Exp $	*/
-
-/*
- * ++Copyright++ 1983, 1990, 1993
- * -
- * Copyright (c) 1983, 1990, 1993
- *    The Regents of the University of California.  All rights reserved.
- * 
- * 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.
- * -
- * Portions Copyright (c) 1993 by Digital Equipment Corporation.
- * 
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies, and that
- * the name of Digital Equipment Corporation not be used in advertising or
- * publicity pertaining to distribution of the document or software without
- * specific, written prior permission.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
- * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
- * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
- * -
- * --Copyright--
- */
-
-#include <sys/types.h>
-#include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <ctype.h>
-
-/*
- * Ascii internet address interpretation routine.
- * The value returned is in network order.
- */
-in_addr_t
-inet_addr(const char *cp)
-{
-	struct in_addr val;
-
-	if (inet_aton(cp, &val))
-		return (val.s_addr);
-	return (INADDR_NONE);
-}
diff --git a/libc/inet/inet_aton.c b/libc/inet/inet_aton.c
deleted file mode 100644
index c456d07..0000000
--- a/libc/inet/inet_aton.c
+++ /dev/null
@@ -1,157 +0,0 @@
-/*	$OpenBSD: inet_addr.c,v 1.9 2005/08/06 20:30:03 espie Exp $	*/
-
-/*
- * ++Copyright++ 1983, 1990, 1993
- * -
- * Copyright (c) 1983, 1990, 1993
- *    The Regents of the University of California.  All rights reserved.
- * 
- * 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.
- * -
- * Portions Copyright (c) 1993 by Digital Equipment Corporation.
- * 
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies, and that
- * the name of Digital Equipment Corporation not be used in advertising or
- * publicity pertaining to distribution of the document or software without
- * specific, written prior permission.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
- * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
- * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
- * -
- * --Copyright--
- */
-
-#include <sys/types.h>
-#include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <ctype.h>
-
-/* 
- * Check whether "cp" is a valid ascii representation
- * of an Internet address and convert to a binary address.
- * Returns 1 if the address is valid, 0 if not.
- * This replaces inet_addr, the return value from which
- * cannot distinguish between failure and a local broadcast address.
- */
-int
-inet_aton(const char *cp, struct in_addr *addr)
-{
-	in_addr_t val;
-	int base, n;
-	char c;
-	u_int parts[4];
-	u_int *pp = parts;
-
-	c = *cp;
-	for (;;) {
-		/*
-		 * Collect number up to ``.''.
-		 * Values are specified as for C:
-		 * 0x=hex, 0=octal, isdigit=decimal.
-		 */
-		if (!isdigit(c))
-			return (0);
-		val = 0; base = 10;
-		if (c == '0') {
-			c = *++cp;
-			if (c == 'x' || c == 'X')
-				base = 16, c = *++cp;
-			else
-				base = 8;
-		}
-		for (;;) {
-			if (isascii(c) && isdigit(c)) {
-				val = (val * base) + (c - '0');
-				c = *++cp;
-			} else if (base == 16 && isascii(c) && isxdigit(c)) {
-				val = (val << 4) |
-					(c + 10 - (islower(c) ? 'a' : 'A'));
-				c = *++cp;
-			} else
-				break;
-		}
-		if (c == '.') {
-			/*
-			 * Internet format:
-			 *	a.b.c.d
-			 *	a.b.c	(with c treated as 16 bits)
-			 *	a.b	(with b treated as 24 bits)
-			 */
-			if (pp >= parts + 3)
-				return (0);
-			*pp++ = val;
-			c = *++cp;
-		} else
-			break;
-	}
-	/*
-	 * Check for trailing characters.
-	 */
-	if (c != '\0' && (!isascii(c) || !isspace(c)))
-		return (0);
-	/*
-	 * Concoct the address according to
-	 * the number of parts specified.
-	 */
-	n = pp - parts + 1;
-	switch (n) {
-
-	case 0:
-		return (0);		/* initial nondigit */
-
-	case 1:				/* a -- 32 bits */
-		break;
-
-	case 2:				/* a.b -- 8.24 bits */
-		if ((val > 0xffffff) || (parts[0] > 0xff))
-			return (0);
-		val |= parts[0] << 24;
-		break;
-
-	case 3:				/* a.b.c -- 8.8.16 bits */
-		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
-			return (0);
-		val |= (parts[0] << 24) | (parts[1] << 16);
-		break;
-
-	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
-		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
-			return (0);
-		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
-		break;
-	}
-	if (addr)
-		addr->s_addr = htonl(val);
-	return (1);
-}
diff --git a/libc/bionic/sha1.c b/libc/upstream-netbsd/common/lib/libc/hash/sha1/sha1.c
similarity index 77%
rename from libc/bionic/sha1.c
rename to libc/upstream-netbsd/common/lib/libc/hash/sha1/sha1.c
index efa95a5..cbd60fc 100644
--- a/libc/bionic/sha1.c
+++ b/libc/upstream-netbsd/common/lib/libc/hash/sha1/sha1.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: sha1.c,v 1.1 2005/12/20 20:29:40 christos Exp $	*/
+/*	$NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $	*/
 /*	$OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $	*/
 
 /*
@@ -18,11 +18,28 @@
 #define SHA1HANDSOFF		/* Copies data before messing with it. */
 
 #include <sys/cdefs.h>
-#include <sys/types.h>
+
+#if defined(_KERNEL) || defined(_STANDALONE)
+__KERNEL_RCSID(0, "$NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $");
+
+#include <lib/libkern/libkern.h>
+
+#else
+
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#include "namespace.h"
 #include <assert.h>
-#include <sha1.h>
 #include <string.h>
 
+#endif
+
+#include <sys/types.h>
+#include <sys/sha1.h>
+
+
 #if HAVE_NBTOOL_CONFIG_H
 #include "nbtool_config.h"
 #endif
@@ -53,9 +70,19 @@
 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
 
+
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#if defined(__weak_alias)
+__weak_alias(SHA1Transform,_SHA1Transform)
+__weak_alias(SHA1Init,_SHA1Init)
+__weak_alias(SHA1Update,_SHA1Update)
+__weak_alias(SHA1Final,_SHA1Final)
+#endif
+#endif
+
 typedef union {
-    u_char c[64];
-    u_int l[16];
+    uint8_t c[64];
+    uint32_t l[16];
 } CHAR64LONG16;
 
 /* old sparc64 gcc could not compile this */
@@ -65,10 +92,10 @@
 #endif
 
 #ifdef SPARC64_GCC_WORKAROUND
-void do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
-void do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
-void do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
-void do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
+void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
+void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
+void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
+void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
 
 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
@@ -77,7 +104,7 @@
 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
 
 void
-do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
+do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
 {
     nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
     nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
@@ -87,7 +114,7 @@
 }
 
 void
-do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
+do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
 {
     nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
     nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
@@ -97,7 +124,7 @@
 }
 
 void
-do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
+do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
 {
     nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
     nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
@@ -107,7 +134,7 @@
 }
 
 void
-do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
+do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
 {
     nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
     nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
@@ -120,19 +147,17 @@
 /*
  * Hash a single 512-bit block. This is the core of the algorithm.
  */
-void SHA1Transform(state, buffer)
-    u_int32_t state[5];
-    const u_char buffer[64];
+void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
 {
-    u_int32_t a, b, c, d, e;
+    uint32_t a, b, c, d, e;
     CHAR64LONG16 *block;
 
 #ifdef SHA1HANDSOFF
     CHAR64LONG16 workspace;
 #endif
 
-    assert(buffer != 0);
-    assert(state != 0);
+    _DIAGASSERT(buffer != 0);
+    _DIAGASSERT(state != 0);
 
 #ifdef SHA1HANDSOFF
     block = &workspace;
@@ -192,11 +217,10 @@
 /*
  * SHA1Init - Initialize new context
  */
-void SHA1Init(context)
-    SHA1_CTX *context;
+void SHA1Init(SHA1_CTX *context)
 {
 
-    assert(context != 0);
+    _DIAGASSERT(context != 0);
 
     /* SHA1 initialization constants */
     context->state[0] = 0x67452301;
@@ -211,15 +235,12 @@
 /*
  * Run your data through this.
  */
-void SHA1Update(context, data, len)
-    SHA1_CTX *context;
-    const u_char *data;
-    u_int len;
+void SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len)
 {
-    u_int i, j;
+    unsigned int i, j;
 
-    assert(context != 0);
-    assert(data != 0);
+    _DIAGASSERT(context != 0);
+    _DIAGASSERT(data != 0);
 
     j = context->count[0];
     if ((context->count[0] += len << 3) < j)
@@ -241,28 +262,26 @@
 /*
  * Add padding and return the message digest.
  */
-void SHA1Final(digest, context)
-    u_char digest[20];
-    SHA1_CTX* context;
+void SHA1Final(uint8_t digest[20], SHA1_CTX *context)
 {
-    u_int i;
-    u_char finalcount[8];
+    unsigned int i;
+    uint8_t finalcount[8];
 
-    assert(digest != 0);
-    assert(context != 0);
+    _DIAGASSERT(digest != 0);
+    _DIAGASSERT(context != 0);
 
     for (i = 0; i < 8; i++) {
-	finalcount[i] = (u_char)((context->count[(i >= 4 ? 0 : 1)]
+	finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
 	 >> ((3-(i & 3)) * 8) ) & 255);	 /* Endian independent */
     }
-    SHA1Update(context, (const u_char *)"\200", 1);
+    SHA1Update(context, (const uint8_t *)"\200", 1);
     while ((context->count[0] & 504) != 448)
-	SHA1Update(context, (const u_char *)"\0", 1);
+	SHA1Update(context, (const uint8_t *)"\0", 1);
     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
 
     if (digest) {
 	for (i = 0; i < 20; i++)
-	    digest[i] = (u_char)
+	    digest[i] = (uint8_t)
 		((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
     }
 }
diff --git a/libc/upstream-netbsd/common/lib/libc/inet/inet_addr.c b/libc/upstream-netbsd/common/lib/libc/inet/inet_addr.c
new file mode 100644
index 0000000..1360ce9
--- /dev/null
+++ b/libc/upstream-netbsd/common/lib/libc/inet/inet_addr.c
@@ -0,0 +1,224 @@
+/*	$NetBSD: inet_addr.c,v 1.3 2012/03/09 15:41:16 christos Exp $	*/
+
+/*
+ * Copyright (c) 1983, 1990, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ * 	This product includes software developed by the University of
+ * 	California, Berkeley and its contributors.
+ * 4. 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.
+ */
+
+/*
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
+static const char rcsid[] = "Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp";
+#else
+__RCSID("$NetBSD: inet_addr.c,v 1.3 2012/03/09 15:41:16 christos Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include "port_before.h"
+
+#include "namespace.h"
+#include <sys/types.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <ctype.h>
+
+#include "port_after.h"
+
+#ifdef __weak_alias
+__weak_alias(inet_aton,_inet_aton)
+#endif
+#else
+#include <lib/libkern/libkern.h>
+#include <netinet/in.h>
+#endif
+
+/*
+ * Ascii internet address interpretation routine.
+ * The value returned is in network order.
+ */
+uint32_t
+inet_addr(const char *cp) {
+	struct in_addr val;
+
+	if (inet_aton(cp, &val))
+		return (val.s_addr);
+	return (INADDR_NONE);
+}
+
+/* 
+ * Check whether "cp" is a valid ascii representation
+ * of an Internet address and convert to a binary address.
+ * Returns 1 if the address is valid, 0 if not.
+ * This replaces inet_addr, the return value from which
+ * cannot distinguish between failure and a local broadcast address.
+ */
+int
+inet_aton(const char *cp, struct in_addr *addr) {
+	uint32_t val;
+	int base;
+	size_t n;
+	char c;
+	uint8_t parts[4];
+	uint8_t *pp = parts;
+	int digit;
+
+	c = *cp;
+	for (;;) {
+		/*
+		 * Collect number up to ``.''.
+		 * Values are specified as for C:
+		 * 0x=hex, 0=octal, isdigit=decimal.
+		 */
+		if (!isdigit((unsigned char)c))
+			return (0);
+		val = 0; base = 10; digit = 0;
+		if (c == '0') {
+			c = *++cp;
+			if (c == 'x' || c == 'X')
+				base = 16, c = *++cp;
+			else {
+				base = 8;
+				digit = 1 ;
+			}
+		}
+		for (;;) {
+			if (isascii(c) && isdigit((unsigned char)c)) {
+				if (base == 8 && (c == '8' || c == '9'))
+					return (0);
+				val = (val * base) + (c - '0');
+				c = *++cp;
+				digit = 1;
+			} else if (base == 16 && isascii(c) && 
+				   isxdigit((unsigned char)c)) {
+				val = (val << 4) |
+					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
+				c = *++cp;
+				digit = 1;
+			} else
+				break;
+		}
+		if (c == '.') {
+			/*
+			 * Internet format:
+			 *	a.b.c.d
+			 *	a.b.c	(with c treated as 16 bits)
+			 *	a.b	(with b treated as 24 bits)
+			 */
+			if (pp >= parts + 3 || val > 0xffU)
+				return (0);
+			*pp++ = val;
+			c = *++cp;
+		} else
+			break;
+	}
+	/*
+	 * Check for trailing characters.
+	 */
+	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
+		return (0);
+	/*
+	 * Did we get a valid digit?
+	 */
+	if (!digit)
+		return (0);
+	/*
+	 * Concoct the address according to
+	 * the number of parts specified.
+	 */
+	n = pp - parts + 1;
+	switch (n) {
+	case 1:				/* a -- 32 bits */
+		break;
+
+	case 2:				/* a.b -- 8.24 bits */
+		if (val > 0xffffffU)
+			return (0);
+		val |= parts[0] << 24;
+		break;
+
+	case 3:				/* a.b.c -- 8.8.16 bits */
+		if (val > 0xffffU)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16);
+		break;
+
+	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
+		if (val > 0xffU)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+		break;
+	}
+	if (addr != NULL)
+		addr->s_addr = htonl(val);
+	return (1);
+}
diff --git a/libc/bionic/utime.c b/libc/upstream-netbsd/libc/gen/utime.c
similarity index 81%
rename from libc/bionic/utime.c
rename to libc/upstream-netbsd/libc/gen/utime.c
index 7239847..d41aac7 100644
--- a/libc/bionic/utime.c
+++ b/libc/upstream-netbsd/libc/gen/utime.c
@@ -1,3 +1,5 @@
+/*	$NetBSD: utime.c,v 1.14 2012/06/25 22:32:44 abs Exp $	*/
+
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -10,7 +12,7 @@
  * 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.
- * 4. Neither the name of the University nor the names of its contributors
+ * 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.
  *
@@ -27,29 +29,37 @@
  * SUCH DAMAGE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)utime.c	8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-//__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/utime.c,v 1.3 2007/01/09 00:27:56 imp Exp $");
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)utime.c	8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: utime.c,v 1.14 2012/06/25 22:32:44 abs Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
 
+#include "namespace.h"
 #include <sys/time.h>
 
+#include <assert.h>
+#include <errno.h>
+#include <stddef.h>
 #include <utime.h>
 
 int
-utime(path, times)
-	const char *path;
-	const struct utimbuf *times;
+utime(const char *path, const struct utimbuf *times)
 {
 	struct timeval tv[2], *tvp;
 
-	if (times) {
+	_DIAGASSERT(path != NULL);
+
+	if (times == (struct utimbuf *) NULL)
+		tvp = NULL;
+	else {
 		tv[0].tv_sec = times->actime;
 		tv[1].tv_sec = times->modtime;
 		tv[0].tv_usec = tv[1].tv_usec = 0;
 		tvp = tv;
-	} else
-		tvp = NULL;
+	}
 	return (utimes(path, tvp));
 }
diff --git a/libc/inet/inet_ntoa.c b/libc/upstream-netbsd/libc/inet/inet_ntoa.c
similarity index 73%
rename from libc/inet/inet_ntoa.c
rename to libc/upstream-netbsd/libc/inet/inet_ntoa.c
index ff5d93d..6ed023b 100644
--- a/libc/inet/inet_ntoa.c
+++ b/libc/upstream-netbsd/libc/inet/inet_ntoa.c
@@ -1,4 +1,5 @@
-/*	$OpenBSD: inet_ntoa.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
+/*	$NetBSD: inet_ntoa.c,v 1.2 2012/03/13 21:13:38 christos Exp $	*/
+
 /*
  * Copyright (c) 1983, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -28,24 +29,36 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)inet_ntoa.c	8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: inet_ntoa.c,v 1.2 2012/03/13 21:13:38 christos Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include "namespace.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __weak_alias
+__weak_alias(inet_ntoa,_inet_ntoa)
+#endif
+
 /*
  * Convert network-format internet address
  * to base 256 d.d.d.d representation.
  */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <stdio.h>
+/*const*/ char *
+inet_ntoa(struct in_addr in) {
+	static char ret[18];
 
-char *
-inet_ntoa(struct in_addr in)
-{
-	static char b[18];
-	char *p;
-
-	p = (char *)&in;
-#define	UC(b)	(((int)b)&0xff)
-	(void)snprintf(b, sizeof(b),
-	    "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
-	return (b);
+	strlcpy(ret, "[inet_ntoa error]", sizeof(ret));
+	(void) inet_ntop(AF_INET, &in, ret, (socklen_t)sizeof ret);
+	return ret;
 }
diff --git a/libc/inet/inet_ntop.c b/libc/upstream-netbsd/libc/inet/inet_ntop.c
similarity index 60%
rename from libc/inet/inet_ntop.c
rename to libc/upstream-netbsd/libc/inet/inet_ntop.c
index c3448f1..00c55a8 100644
--- a/libc/inet/inet_ntop.c
+++ b/libc/upstream-netbsd/libc/inet/inet_ntop.c
@@ -1,38 +1,61 @@
-/*	$OpenBSD: inet_ntop.c,v 1.7 2005/08/06 20:30:03 espie Exp $	*/
+/*	$NetBSD: inet_ntop.c,v 1.9 2012/03/20 17:08:13 matt Exp $	*/
 
-/* Copyright (c) 1996 by Internet Software Consortium.
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
  *
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp";
+#else
+__RCSID("$NetBSD: inet_ntop.c,v 1.9 2012/03/20 17:08:13 matt Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include "port_before.h"
+
+#include "namespace.h"
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include "arpa_nameser.h"
-#include <string.h>
+#include <arpa/nameser.h>
+
+#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-/*
+#include "port_after.h"
+
+#ifdef __weak_alias
+__weak_alias(inet_ntop,_inet_ntop)
+#endif
+
+/*%
  * WARNING: Don't even consider trying to compile this on a system where
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
-static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
-static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
+static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
+static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
 
 /* char *
  * inet_ntop(af, src, dst, size)
@@ -43,8 +66,12 @@
  *	Paul Vixie, 1996.
  */
 const char *
-inet_ntop(int af, const void *src, char *dst, size_t size)
+inet_ntop(int af, const void *src, char *dst, socklen_t size)
 {
+
+	_DIAGASSERT(src != NULL);
+	_DIAGASSERT(dst != NULL);
+
 	switch (af) {
 	case AF_INET:
 		return (inet_ntop4(src, dst, size));
@@ -69,19 +96,17 @@
  *	Paul Vixie, 1996.
  */
 static const char *
-inet_ntop4(const u_char *src, char *dst, size_t size)
+inet_ntop4(const u_char *src, char *dst, socklen_t size)
 {
-	static const char fmt[] = "%u.%u.%u.%u";
 	char tmp[sizeof "255.255.255.255"];
 	int l;
 
-#if defined(ANDROID_CHANGES)
-	l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
-	if (l <= 0 || (size_t)l >= size || (size_t)l >= sizeof(tmp)) {
-#else
-	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
-	if (l <= 0 || (size_t)l >= size) {
-#endif
+	_DIAGASSERT(src != NULL);
+	_DIAGASSERT(dst != NULL);
+
+	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
+	    src[0], src[1], src[2], src[3]);
+	if (l <= 0 || (socklen_t) l >= size) {
 		errno = ENOSPC;
 		return (NULL);
 	}
@@ -96,7 +121,7 @@
  *	Paul Vixie, 1996.
  */
 static const char *
-inet_ntop6(const u_char *src, char *dst, size_t size)
+inet_ntop6(const u_char *src, char *dst, socklen_t size)
 {
 	/*
 	 * Note that int32_t and int16_t need only be "at least" large enough
@@ -108,23 +133,26 @@
 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
 	char *tp, *ep;
 	struct { int base, len; } best, cur;
-	u_int words[IN6ADDRSZ / INT16SZ];
+	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
 	int i;
 	int advance;
 
+	_DIAGASSERT(src != NULL);
+	_DIAGASSERT(dst != NULL);
+
 	/*
 	 * Preprocess:
 	 *	Copy the input (bytewise) array into a wordwise array.
 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
 	 */
 	memset(words, '\0', sizeof words);
-	for (i = 0; i < IN6ADDRSZ; i++)
+	for (i = 0; i < NS_IN6ADDRSZ; i++)
 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
 	best.base = -1;
-	best.len  = 0;
+	best.len = 0;
 	cur.base = -1;
-	cur.len  = 0;
-	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
+	cur.len = 0;
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
 		if (words[i] == 0) {
 			if (cur.base == -1)
 				cur.base = i, cur.len = 1;
@@ -150,15 +178,12 @@
 	 */
 	tp = tmp;
 	ep = tmp + sizeof(tmp);
-	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
 		/* Are we inside the best run of 0x00's? */
 		if (best.base != -1 && i >= best.base &&
 		    i < (best.base + best.len)) {
-			if (i == best.base) {
-				if (tp + 1 >= ep)
-					return (NULL);
+			if (i == best.base)
 				*tp++ = ':';
-			}
 			continue;
 		}
 		/* Are we following an initial run of 0x00s or any real hex? */
@@ -169,19 +194,22 @@
 		}
 		/* Is this address an encapsulated IPv4? */
 		if (i == 6 && best.base == 0 &&
-		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
-			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
+		    (best.len == 6 ||
+		    (best.len == 7 && words[7] != 0x0001) ||
+		    (best.len == 5 && words[5] == 0xffff))) {
+			if (!inet_ntop4(src+12, tp, (socklen_t)(ep - tp)))
 				return (NULL);
 			tp += strlen(tp);
 			break;
 		}
-		advance = snprintf(tp, ep - tp, "%x", words[i]);
+		advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]);
 		if (advance <= 0 || advance >= ep - tp)
 			return (NULL);
 		tp += advance;
 	}
 	/* Was it a trailing run of 0x00's? */
-	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
+	if (best.base != -1 && (best.base + best.len) == 
+	    (NS_IN6ADDRSZ / NS_INT16SZ)) {
 		if (tp + 1 >= ep)
 			return (NULL);
 		*tp++ = ':';
@@ -200,3 +228,5 @@
 	strlcpy(dst, tmp, size);
 	return (dst);
 }
+
+/*! \file */
diff --git a/libc/inet/inet_pton.c b/libc/upstream-netbsd/libc/inet/inet_pton.c
similarity index 91%
rename from libc/inet/inet_pton.c
rename to libc/upstream-netbsd/libc/inet/inet_pton.c
index 6e74e6a..c1e9a69 100644
--- a/libc/inet/inet_pton.c
+++ b/libc/upstream-netbsd/libc/inet/inet_pton.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: inet_pton.c,v 1.6.10.1 2011/01/10 00:42:17 riz Exp $	*/
+/*	$NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $	*/
 
 /*
  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
@@ -22,36 +22,30 @@
 #if 0
 static const char rcsid[] = "Id: inet_pton.c,v 1.5 2005/07/28 06:51:47 marka Exp";
 #else
-__RCSID("$NetBSD: inet_pton.c,v 1.6.10.1 2011/01/10 00:42:17 riz Exp $");
+__RCSID("$NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
-// BEGIN android-added
-#define _DIAGASSERT(exp) assert(exp)
-#include "../private/arpa_nameser.h"
-// END android-added
+#include "port_before.h"
 
-// android-removed: #include "port_before.h"
-
-// android-removed: #include "namespace.h"
+#include "namespace.h"
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include <stddef.h>
 #include <string.h>
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 
-// android-removed: #include "port_after.h"
+#include "port_after.h"
 
-// BEGIN android-removed
-// #ifdef __weak_alias
-// __weak_alias(inet_pton,_inet_pton)
-// #endif
-// END android-removed
+#ifdef __weak_alias
+__weak_alias(inet_pton,_inet_pton)
+#endif
 
 /*%
  * WARNING: Don't even consider trying to compile this on a system where
@@ -107,10 +101,10 @@
 {
 	u_int32_t val;
 	u_int digit, base;
-	int n;
+	ptrdiff_t n;
 	unsigned char c;
 	u_int parts[4];
-	register u_int *pp = parts;
+	u_int *pp = parts;
 
 	_DIAGASSERT(src != NULL);
 	_DIAGASSERT(dst != NULL);
@@ -255,7 +249,7 @@
 			pch = strchr((xdigits = xdigits_u), ch);
 		if (pch != NULL) {
 			val <<= 4;
-			val |= (pch - xdigits);
+			val |= (int)(pch - xdigits);
 			if (++seen_xdigits > 4)
 				return (0);
 			continue;
@@ -296,7 +290,7 @@
 		 * Since some memmove()'s erroneously fail to handle
 		 * overlapping regions, we'll do the shift by hand.
 		 */
-		const int n = tp - colonp;
+		const ptrdiff_t n = tp - colonp;
 		int i;
 
 		if (tp == endp)
diff --git a/libc/string/strcasestr.c b/libc/upstream-netbsd/libc/string/strcasestr.c
similarity index 83%
rename from libc/string/strcasestr.c
rename to libc/upstream-netbsd/libc/string/strcasestr.c
index aa74c01..f8a9444 100644
--- a/libc/string/strcasestr.c
+++ b/libc/upstream-netbsd/libc/string/strcasestr.c
@@ -1,5 +1,4 @@
-/*	$OpenBSD: strcasestr.c,v 1.3 2006/03/31 05:34:55 deraadt Exp $	*/
-/*	$NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $	*/
+/*	$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -33,6 +32,13 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#include "namespace.h"
+#include <assert.h>
 #include <ctype.h>
 #include <string.h>
 
@@ -45,8 +51,11 @@
 	char c, sc;
 	size_t len;
 
+	_DIAGASSERT(s != NULL);
+	_DIAGASSERT(find != NULL);
+
 	if ((c = *find++) != 0) {
-		c = (char)tolower((unsigned char)c);
+		c = tolower((unsigned char)c);
 		len = strlen(find);
 		do {
 			do {
@@ -56,5 +65,5 @@
 		} while (strncasecmp(s, find, len) != 0);
 		s--;
 	}
-	return ((char *)s);
+	return __UNCONST(s);
 }
diff --git a/libc/upstream-netbsd/port_after.h b/libc/upstream-netbsd/port_after.h
new file mode 100644
index 0000000..3f8b6f8
--- /dev/null
+++ b/libc/upstream-netbsd/port_after.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _BIONIC_NETBSD_PORT_BEFORE_H_included
+#define _BIONIC_NETBSD_PORT_BEFORE_H_included
+
+#endif
diff --git a/libc/upstream-netbsd/port_before.h b/libc/upstream-netbsd/port_before.h
new file mode 100644
index 0000000..87af595
--- /dev/null
+++ b/libc/upstream-netbsd/port_before.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _BIONIC_NETBSD_PORT_BEFORE_H_included
+#define _BIONIC_NETBSD_PORT_BEFORE_H_included
+
+#include "namespace.h"
+#include <sys/cdefs.h>
+#include <arpa_nameser.h>
+
+#endif