Merge "Add stack unwinding directives to memcpy."
diff --git a/libc/Android.mk b/libc/Android.mk
index 7219211..884d759 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -111,35 +111,8 @@
 	string/strspn.c \
 	string/strstr.c \
 	string/strtok.c \
-	wchar/wcpcpy.c \
-	wchar/wcpncpy.c \
-	wchar/wcscasecmp.c \
-	wchar/wcscat.c \
-	wchar/wcschr.c \
-	wchar/wcscmp.c \
-	wchar/wcscpy.c \
-	wchar/wcscspn.c \
-	wchar/wcsdup.c \
-	wchar/wcslcat.c \
-	wchar/wcslcpy.c \
-	wchar/wcslen.c \
-	wchar/wcsncasecmp.c \
-	wchar/wcsncat.c \
-	wchar/wcsncmp.c \
-	wchar/wcsncpy.c \
-	wchar/wcsnlen.c \
-	wchar/wcspbrk.c \
-	wchar/wcsrchr.c \
-	wchar/wcsspn.c \
-	wchar/wcsstr.c \
-	wchar/wcstok.c \
 	wchar/wcswidth.c \
 	wchar/wcsxfrm.c \
-	wchar/wmemchr.c \
-	wchar/wmemcmp.c \
-	wchar/wmemcpy.c \
-	wchar/wmemmove.c \
-	wchar/wmemset.c \
 	tzcode/asctime.c \
 	tzcode/difftime.c \
 	tzcode/localtime.c \
@@ -195,7 +168,6 @@
 	bionic/ptsname_r.c \
 	bionic/pututline.c \
 	bionic/pwrite.c \
-	bionic/realpath.c \
 	bionic/reboot.c \
 	bionic/recv.c \
 	bionic/sched_cpualloc.c \
@@ -317,6 +289,36 @@
     bionic/__vsprintf_chk.cpp \
     bionic/wchar.cpp \
 
+libc_upstream_freebsd_src_files := \
+    upstream-freebsd/lib/libc/stdlib/realpath.c \
+    upstream-freebsd/lib/libc/string/wcpcpy.c \
+    upstream-freebsd/lib/libc/string/wcpncpy.c \
+    upstream-freebsd/lib/libc/string/wcscasecmp.c \
+    upstream-freebsd/lib/libc/string/wcscat.c \
+    upstream-freebsd/lib/libc/string/wcschr.c \
+    upstream-freebsd/lib/libc/string/wcscmp.c \
+    upstream-freebsd/lib/libc/string/wcscpy.c \
+    upstream-freebsd/lib/libc/string/wcscspn.c \
+    upstream-freebsd/lib/libc/string/wcsdup.c \
+    upstream-freebsd/lib/libc/string/wcslcat.c \
+    upstream-freebsd/lib/libc/string/wcslcpy.c \
+    upstream-freebsd/lib/libc/string/wcslen.c \
+    upstream-freebsd/lib/libc/string/wcsncasecmp.c \
+    upstream-freebsd/lib/libc/string/wcsncat.c \
+    upstream-freebsd/lib/libc/string/wcsncmp.c \
+    upstream-freebsd/lib/libc/string/wcsncpy.c \
+    upstream-freebsd/lib/libc/string/wcsnlen.c \
+    upstream-freebsd/lib/libc/string/wcspbrk.c \
+    upstream-freebsd/lib/libc/string/wcsrchr.c \
+    upstream-freebsd/lib/libc/string/wcsspn.c \
+    upstream-freebsd/lib/libc/string/wcsstr.c \
+    upstream-freebsd/lib/libc/string/wcstok.c \
+    upstream-freebsd/lib/libc/string/wmemchr.c \
+    upstream-freebsd/lib/libc/string/wmemcmp.c \
+    upstream-freebsd/lib/libc/string/wmemcpy.c \
+    upstream-freebsd/lib/libc/string/wmemmove.c \
+    upstream-freebsd/lib/libc/string/wmemset.c \
+
 libc_upstream_netbsd_src_files := \
     upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \
     upstream-netbsd/common/lib/libc/inet/inet_addr.c \
@@ -745,6 +747,29 @@
 
 
 # ========================================================
+# libc_freebsd.a - upstream FreeBSD C library code
+# ========================================================
+#
+# These files are built with the freebsd-compat.h header file
+# automatically included.
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_upstream_freebsd_src_files)
+LOCAL_CFLAGS := \
+    $(libc_common_cflags) \
+    -I$(LOCAL_PATH)/upstream-freebsd \
+    -I$(LOCAL_PATH)/upstream-freebsd/libc/include \
+    -include upstream-freebsd/freebsd-compat.h
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_MODULE := libc_freebsd
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
 # libc_netbsd.a - upstream NetBSD C library code
 # ========================================================
 #
@@ -796,7 +821,7 @@
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_common
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_netbsd
+LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_freebsd libc_netbsd
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
 include $(BUILD_STATIC_LIBRARY)
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index ef88e04..a4e19f0 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -149,6 +149,11 @@
 extern wint_t		 towctrans(wint_t, wctrans_t);
 extern wctrans_t	 wctrans (const char *);
 
+#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
+wchar_t* wcsdup(const wchar_t*);
+size_t wcsnlen(const wchar_t*, size_t);
+#endif
+
 __END_DECLS
 
 #endif /* _WCHAR_H_ */
diff --git a/libc/kernel/common/linux/msm_ion.h b/libc/kernel/common/linux/msm_ion.h
index c750f12..5e85ea1 100644
--- a/libc/kernel/common/linux/msm_ion.h
+++ b/libc/kernel/common/linux/msm_ion.h
@@ -16,8 +16,90 @@
  ***
  ****************************************************************************
  ****************************************************************************/
-#ifndef _LINUX_MSM_ION_H
-#define _LINUX_MSM_ION_H
+#ifndef __LINUX_MSM_ION_H__
+#define __LINUX_MSM_ION_H__
 #include <linux/ion.h>
-#endif
+enum msm_ion_heap_types {
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ ION_HEAP_TYPE_IOMMU = ION_HEAP_TYPE_CUSTOM + 1,
+ ION_HEAP_TYPE_CP = ION_HEAP_TYPE_CUSTOM + 2,
+};
+enum ion_heap_ids {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ INVALID_HEAP_ID = -1,
+ ION_CP_MM_HEAP_ID = 8,
+ ION_CP_MFC_HEAP_ID = 12,
+ ION_CP_WB_HEAP_ID = 16,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ ION_CAMERA_HEAP_ID = 20,
+ ION_SF_HEAP_ID = 24,
+ ION_IOMMU_HEAP_ID = 25,
+ ION_QSECOM_HEAP_ID = 27,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ ION_AUDIO_HEAP_ID = 28,
+ ION_MM_FIRMWARE_HEAP_ID = 29,
+ ION_SYSTEM_HEAP_ID = 30,
+ ION_HEAP_ID_RESERVED = 31
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+};
+enum ion_fixed_position {
+ NOT_FIXED,
+ FIXED_LOW,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ FIXED_MIDDLE,
+ FIXED_HIGH,
+};
+enum cp_mem_usage {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ VIDEO_BITSTREAM = 0x1,
+ VIDEO_PIXEL = 0x2,
+ VIDEO_NONPIXEL = 0x3,
+ MAX_USAGE = 0x4,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ UNKNOWN = 0x7FFFFFFF,
+};
+#define ION_HEAP_CP_MASK (1 << ION_HEAP_TYPE_CP)
+#define ION_SECURE (1 << ION_HEAP_ID_RESERVED)
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define ION_HEAP(bit) (1 << (bit))
+#define ION_VMALLOC_HEAP_NAME "vmalloc"
+#define ION_AUDIO_HEAP_NAME "audio"
+#define ION_SF_HEAP_NAME "sf"
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define ION_MM_HEAP_NAME "mm"
+#define ION_CAMERA_HEAP_NAME "camera_preview"
+#define ION_IOMMU_HEAP_NAME "iommu"
+#define ION_MFC_HEAP_NAME "mfc"
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define ION_WB_HEAP_NAME "wb"
+#define ION_MM_FIRMWARE_HEAP_NAME "mm_fw"
+#define ION_QSECOM_HEAP_NAME "qsecom"
+#define ION_FMEM_HEAP_NAME "fmem"
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define CACHED 1
+#define UNCACHED 0
+#define ION_CACHE_SHIFT 0
+#define ION_SET_CACHE(__cache) ((__cache) << ION_CACHE_SHIFT)
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define ION_IS_CACHED(__flags) ((__flags) & (1 << ION_CACHE_SHIFT))
+struct ion_flush_data {
+ struct ion_handle *handle;
+ int fd;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ void *vaddr;
+ unsigned int offset;
+ unsigned int length;
+};
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+struct ion_flag_data {
+ struct ion_handle *handle;
+ unsigned long flags;
+};
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MAGIC, 20,   struct ion_flush_data)
+#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MAGIC, 21,   struct ion_flush_data)
+#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MAGIC, 22,   struct ion_flush_data)
+#define ION_IOC_GET_FLAGS _IOWR(ION_IOC_MAGIC, 23,   struct ion_flag_data)
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#endif
+
diff --git a/libc/upstream-freebsd/freebsd-compat.h b/libc/upstream-freebsd/freebsd-compat.h
new file mode 100644
index 0000000..08dec15
--- /dev/null
+++ b/libc/upstream-freebsd/freebsd-compat.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2013 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_FREEBSD_COMPAT_H_included
+#define _BIONIC_FREEBSD_COMPAT_H_included
+
+#endif
diff --git a/libc/bionic/realpath.c b/libc/upstream-freebsd/lib/libc/stdlib/realpath.c
similarity index 88%
rename from libc/bionic/realpath.c
rename to libc/upstream-freebsd/lib/libc/stdlib/realpath.c
index b909831..8fd5457 100644
--- a/libc/bionic/realpath.c
+++ b/libc/upstream-freebsd/lib/libc/stdlib/realpath.c
@@ -30,8 +30,9 @@
 static char sccsid[] = "@(#)realpath.c	8.1 (Berkeley) 2/16/94";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 11:04:30Z kib $");
+__FBSDID("$FreeBSD$");
 
+#include "namespace.h"
 #include <sys/param.h>
 #include <sys/stat.h>
 
@@ -39,6 +40,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "un-namespace.h"
 
 /*
  * Find the real name of path, by removing all ".", ".." and symlink
@@ -52,7 +54,7 @@
 	char *p, *q, *s;
 	size_t left_len, resolved_len;
 	unsigned symlinks;
-	int m, serrno, slen;
+	int m, slen;
 	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
 
 	if (path == NULL) {
@@ -63,7 +65,6 @@
 		errno = ENOENT;
 		return (NULL);
 	}
-	serrno = errno;
 	if (resolved == NULL) {
 		resolved = malloc(PATH_MAX);
 		if (resolved == NULL)
@@ -130,8 +131,29 @@
 			resolved[resolved_len++] = '/';
 			resolved[resolved_len] = '\0';
 		}
-		if (next_token[0] == '\0')
+		if (next_token[0] == '\0') {
+			/*
+			 * Handle consequential slashes.  The path
+			 * before slash shall point to a directory.
+			 *
+			 * Only the trailing slashes are not covered
+			 * by other checks in the loop, but we verify
+			 * the prefix for any (rare) "//" or "/\0"
+			 * occurence to not implement lookahead.
+			 */
+			if (lstat(resolved, &sb) != 0) {
+				if (m)
+					free(resolved);
+				return (NULL);
+			}
+			if (!S_ISDIR(sb.st_mode)) {
+				if (m)
+					free(resolved);
+				errno = ENOTDIR;
+				return (NULL);
+			}
 			continue;
+		}
 		else if (strcmp(next_token, ".") == 0)
 			continue;
 		else if (strcmp(next_token, "..") == 0) {
@@ -149,9 +171,7 @@
 		}
 
 		/*
-		 * Append the next path component and lstat() it. If
-		 * lstat() fails we still can return successfully if
-		 * there are no more path components left.
+		 * Append the next path component and lstat() it.
 		 */
 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
 		if (resolved_len >= PATH_MAX) {
@@ -161,10 +181,6 @@
 			return (NULL);
 		}
 		if (lstat(resolved, &sb) != 0) {
-			if (errno == ENOENT && p == NULL) {
-				errno = serrno;
-				return (resolved);
-			}
 			if (m)
 				free(resolved);
 			return (NULL);
@@ -210,7 +226,8 @@
 					symlink[slen] = '/';
 					symlink[slen + 1] = 0;
 				}
-				left_len = strlcat(symlink, left, sizeof(left));
+				left_len = strlcat(symlink, left,
+				    sizeof(symlink));
 				if (left_len >= sizeof(left)) {
 					if (m)
 						free(resolved);
diff --git a/libc/wchar/wcpcpy.c b/libc/upstream-freebsd/lib/libc/string/wcpcpy.c
similarity index 100%
rename from libc/wchar/wcpcpy.c
rename to libc/upstream-freebsd/lib/libc/string/wcpcpy.c
diff --git a/libc/wchar/wcpncpy.c b/libc/upstream-freebsd/lib/libc/string/wcpncpy.c
similarity index 100%
rename from libc/wchar/wcpncpy.c
rename to libc/upstream-freebsd/lib/libc/string/wcpncpy.c
diff --git a/libc/wchar/wcscasecmp.c b/libc/upstream-freebsd/lib/libc/string/wcscasecmp.c
similarity index 100%
rename from libc/wchar/wcscasecmp.c
rename to libc/upstream-freebsd/lib/libc/string/wcscasecmp.c
diff --git a/libc/wchar/wcscat.c b/libc/upstream-freebsd/lib/libc/string/wcscat.c
similarity index 100%
rename from libc/wchar/wcscat.c
rename to libc/upstream-freebsd/lib/libc/string/wcscat.c
diff --git a/libc/wchar/wcschr.c b/libc/upstream-freebsd/lib/libc/string/wcschr.c
similarity index 100%
rename from libc/wchar/wcschr.c
rename to libc/upstream-freebsd/lib/libc/string/wcschr.c
diff --git a/libc/wchar/wcscmp.c b/libc/upstream-freebsd/lib/libc/string/wcscmp.c
similarity index 100%
rename from libc/wchar/wcscmp.c
rename to libc/upstream-freebsd/lib/libc/string/wcscmp.c
diff --git a/libc/wchar/wcscpy.c b/libc/upstream-freebsd/lib/libc/string/wcscpy.c
similarity index 100%
rename from libc/wchar/wcscpy.c
rename to libc/upstream-freebsd/lib/libc/string/wcscpy.c
diff --git a/libc/wchar/wcscspn.c b/libc/upstream-freebsd/lib/libc/string/wcscspn.c
similarity index 100%
rename from libc/wchar/wcscspn.c
rename to libc/upstream-freebsd/lib/libc/string/wcscspn.c
diff --git a/libc/wchar/wcsdup.c b/libc/upstream-freebsd/lib/libc/string/wcsdup.c
similarity index 100%
rename from libc/wchar/wcsdup.c
rename to libc/upstream-freebsd/lib/libc/string/wcsdup.c
diff --git a/libc/wchar/wcslcat.c b/libc/upstream-freebsd/lib/libc/string/wcslcat.c
similarity index 100%
rename from libc/wchar/wcslcat.c
rename to libc/upstream-freebsd/lib/libc/string/wcslcat.c
diff --git a/libc/wchar/wcslcpy.c b/libc/upstream-freebsd/lib/libc/string/wcslcpy.c
similarity index 100%
rename from libc/wchar/wcslcpy.c
rename to libc/upstream-freebsd/lib/libc/string/wcslcpy.c
diff --git a/libc/wchar/wcslen.c b/libc/upstream-freebsd/lib/libc/string/wcslen.c
similarity index 100%
rename from libc/wchar/wcslen.c
rename to libc/upstream-freebsd/lib/libc/string/wcslen.c
diff --git a/libc/wchar/wcsncasecmp.c b/libc/upstream-freebsd/lib/libc/string/wcsncasecmp.c
similarity index 100%
rename from libc/wchar/wcsncasecmp.c
rename to libc/upstream-freebsd/lib/libc/string/wcsncasecmp.c
diff --git a/libc/wchar/wcsncat.c b/libc/upstream-freebsd/lib/libc/string/wcsncat.c
similarity index 100%
rename from libc/wchar/wcsncat.c
rename to libc/upstream-freebsd/lib/libc/string/wcsncat.c
diff --git a/libc/wchar/wcsncmp.c b/libc/upstream-freebsd/lib/libc/string/wcsncmp.c
similarity index 100%
rename from libc/wchar/wcsncmp.c
rename to libc/upstream-freebsd/lib/libc/string/wcsncmp.c
diff --git a/libc/wchar/wcsncpy.c b/libc/upstream-freebsd/lib/libc/string/wcsncpy.c
similarity index 100%
rename from libc/wchar/wcsncpy.c
rename to libc/upstream-freebsd/lib/libc/string/wcsncpy.c
diff --git a/libc/wchar/wcsnlen.c b/libc/upstream-freebsd/lib/libc/string/wcsnlen.c
similarity index 100%
rename from libc/wchar/wcsnlen.c
rename to libc/upstream-freebsd/lib/libc/string/wcsnlen.c
diff --git a/libc/wchar/wcspbrk.c b/libc/upstream-freebsd/lib/libc/string/wcspbrk.c
similarity index 100%
rename from libc/wchar/wcspbrk.c
rename to libc/upstream-freebsd/lib/libc/string/wcspbrk.c
diff --git a/libc/wchar/wcsrchr.c b/libc/upstream-freebsd/lib/libc/string/wcsrchr.c
similarity index 100%
rename from libc/wchar/wcsrchr.c
rename to libc/upstream-freebsd/lib/libc/string/wcsrchr.c
diff --git a/libc/wchar/wcsspn.c b/libc/upstream-freebsd/lib/libc/string/wcsspn.c
similarity index 100%
rename from libc/wchar/wcsspn.c
rename to libc/upstream-freebsd/lib/libc/string/wcsspn.c
diff --git a/libc/wchar/wcsstr.c b/libc/upstream-freebsd/lib/libc/string/wcsstr.c
similarity index 100%
rename from libc/wchar/wcsstr.c
rename to libc/upstream-freebsd/lib/libc/string/wcsstr.c
diff --git a/libc/wchar/wcstok.c b/libc/upstream-freebsd/lib/libc/string/wcstok.c
similarity index 100%
rename from libc/wchar/wcstok.c
rename to libc/upstream-freebsd/lib/libc/string/wcstok.c
diff --git a/libc/wchar/wmemchr.c b/libc/upstream-freebsd/lib/libc/string/wmemchr.c
similarity index 100%
rename from libc/wchar/wmemchr.c
rename to libc/upstream-freebsd/lib/libc/string/wmemchr.c
diff --git a/libc/wchar/wmemcmp.c b/libc/upstream-freebsd/lib/libc/string/wmemcmp.c
similarity index 100%
rename from libc/wchar/wmemcmp.c
rename to libc/upstream-freebsd/lib/libc/string/wmemcmp.c
diff --git a/libc/wchar/wmemcpy.c b/libc/upstream-freebsd/lib/libc/string/wmemcpy.c
similarity index 100%
rename from libc/wchar/wmemcpy.c
rename to libc/upstream-freebsd/lib/libc/string/wmemcpy.c
diff --git a/libc/wchar/wmemmove.c b/libc/upstream-freebsd/lib/libc/string/wmemmove.c
similarity index 100%
rename from libc/wchar/wmemmove.c
rename to libc/upstream-freebsd/lib/libc/string/wmemmove.c
diff --git a/libc/wchar/wmemset.c b/libc/upstream-freebsd/lib/libc/string/wmemset.c
similarity index 100%
rename from libc/wchar/wmemset.c
rename to libc/upstream-freebsd/lib/libc/string/wmemset.c
diff --git a/libc/upstream-freebsd/namespace.h b/libc/upstream-freebsd/namespace.h
new file mode 100644
index 0000000..a3f850e
--- /dev/null
+++ b/libc/upstream-freebsd/namespace.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2013 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_FREEBSD_NAMESPACE_H_included
+#define _BIONIC_FREEBSD_NAMESPACE_H_included
+
+#endif
diff --git a/libc/upstream-freebsd/un-namespace.h b/libc/upstream-freebsd/un-namespace.h
new file mode 100644
index 0000000..a3f850e
--- /dev/null
+++ b/libc/upstream-freebsd/un-namespace.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2013 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_FREEBSD_NAMESPACE_H_included
+#define _BIONIC_FREEBSD_NAMESPACE_H_included
+
+#endif
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 8735100..fed39f8 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -17,6 +17,8 @@
 #include <gtest/gtest.h>
 
 #include <errno.h>
+#include <libgen.h>
+#include <limits.h>
 #include <stdint.h>
 #include <stdlib.h>
 
@@ -70,3 +72,40 @@
   // Can't align to a non-power of 2.
   ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128));
 }
+
+TEST(stdlib, realpath__NULL_filename) {
+  errno = 0;
+  char* p = realpath(NULL, NULL);
+  ASSERT_TRUE(p == NULL);
+  ASSERT_EQ(EINVAL, errno);
+}
+
+TEST(stdlib, realpath__empty_filename) {
+  errno = 0;
+  char* p = realpath("", NULL);
+  ASSERT_TRUE(p == NULL);
+  ASSERT_EQ(ENOENT, errno);
+}
+
+TEST(stdlib, realpath__ENOENT) {
+  errno = 0;
+  char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL);
+  ASSERT_TRUE(p == NULL);
+  ASSERT_EQ(ENOENT, errno);
+}
+
+TEST(stdlib, realpath) {
+  // Get the name of this executable.
+  char executable_path[PATH_MAX];
+  int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
+  ASSERT_NE(rc, -1);
+  executable_path[rc] = '\0';
+
+  char buf[PATH_MAX + 1];
+  char* p = realpath("/proc/self/exe", buf);
+  ASSERT_STREQ(executable_path, p);
+
+  p = realpath("/proc/self/exe", NULL);
+  ASSERT_STREQ(executable_path, p);
+  free(p);
+}