Move realpath.c to upstream-freebsd.

This is actually a slightly newer upstream version than the one I
originally pulled. Hopefully now it's in upstream-freebsd it will
be easier to track upstream, though I still need to sit down and
write the necessary scripts at some point.

Bug: 5110679
Change-Id: I87e563f0f95aa8e68b45578e2a8f448bbf827a33
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);
+}