---- realpath.c.orig 2006-09-16 19:12:28.000000000 -0700
-+++ realpath.c 2006-09-16 20:18:25.000000000 -0700
-@@ -35,13 +35,41 @@
+--- realpath.c.orig 2010-06-24 17:32:55.000000000 -0700
++++ realpath.c 2010-06-25 13:46:50.000000000 -0700
+@@ -35,13 +35,41 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
#include "namespace.h"
#include <sys/param.h>
#include <sys/stat.h>
/*
* char *realpath(const char *path, char resolved[PATH_MAX]);
*
-@@ -52,24 +80,55 @@
+@@ -50,36 +78,89 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
+ * in which case the path which caused trouble is left in (resolved).
+ */
char *
- realpath(const char *path, char resolved[PATH_MAX])
+-realpath(const char *path, char resolved[PATH_MAX])
++realpath(const char *path, char inresolved[PATH_MAX])
{
+ struct attrs attrs;
struct stat sb;
+ static dev_t rootdev;
+ static int rootdev_inited = 0;
+ ino_t inode;
++ char *resolved;
+ if (path == NULL) {
+ errno = EINVAL;
+ return (NULL);
+ }
+#endif /* __DARWIN_UNIX03 */
++ /*
++ * Extension to the standard; if inresolved == NULL, allocate memory
++ */
++ if (!inresolved) {
++ if ((resolved = malloc(PATH_MAX)) == NULL) return (NULL);
++ } else {
++ resolved = inresolved;
++ }
+ if (!rootdev_inited) {
+ rootdev_inited = 1;
+ if (stat("/", &sb) < 0) {
-+ return (NULL);
++error_return:
++ if (!inresolved) {
++ int e = errno;
++ free(resolved);
++ errno = e;
+ }
++ return (NULL);
++ }
+ rootdev = sb.st_dev;
+ }
serrno = errno;
+#endif /* !VARIANT_DARWINEXTSN && __DARWIN_UNIX03 */
+ {
strlcpy(resolved, ".", PATH_MAX);
- return (NULL);
+- return (NULL);
++ goto error_return;
}
-@@ -80,6 +139,13 @@
+ resolved_len = strlen(resolved);
+ left_len = strlcpy(left, path, sizeof(left));
+ }
+ if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
- return (NULL);
+- return (NULL);
++ goto error_return;
}
+ if (resolved_len > 1) {
+ if (stat(resolved, &sb) < 0) {
-+ return (NULL);
++ goto error_return;
+ }
+ lastdev = sb.st_dev;
+ } else
/*
* Iterate over path components in `left'.
-@@ -127,6 +193,13 @@
+@@ -93,7 +174,7 @@ realpath(const char *path, char resolved
+ s = p ? p : left + left_len;
+ if (s - left >= sizeof(next_token)) {
+ errno = ENAMETOOLONG;
+- return (NULL);
++ goto error_return;
+ }
+ memcpy(next_token, left, s - left);
+ next_token[s - left] = '\0';
+@@ -103,7 +184,7 @@ realpath(const char *path, char resolved
+ if (resolved[resolved_len - 1] != '/') {
+ if (resolved_len + 1 >= PATH_MAX) {
+ errno = ENAMETOOLONG;
+- return (NULL);
++ goto error_return;
+ }
+ resolved[resolved_len++] = '/';
+ resolved[resolved_len] = '\0';
+@@ -127,6 +208,13 @@ realpath(const char *path, char resolved
}
/*
* Append the next path component and lstat() it. If
* lstat() fails we still can return successfully if
* there are no more path components left.
-@@ -136,25 +209,87 @@
+@@ -134,27 +222,89 @@ realpath(const char *path, char resolved
+ resolved_len = strlcat(resolved, next_token, PATH_MAX);
+ if (resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
- return (NULL);
+- return (NULL);
++ goto error_return;
}
- if (lstat(resolved, &sb) != 0) {
+ if (getattrlist(resolved, &_rp_alist, &attrs, sizeof(attrs), FSOPT_NOFOLLOW) == 0) {
errno = serrno;
return (resolved);
}
+- return (NULL);
+#endif /* !__DARWIN_UNIX03 */
- return (NULL);
++ goto error_return;
}
- if (S_ISLNK(sb.st_mode)) {
+ if (dev != lastdev) {
+ * that each component of the mountpoint
+ * is a directory (and not a symlink)
+ */
-+ char temp[MNAMELEN];
++ char temp[sizeof(sfs.f_mntonname)];
+ char *cp;
+ int ok = 1;
+
+ if (islink) {
if (symlinks++ > MAXSYMLINKS) {
errno = ELOOP;
- return (NULL);
+- return (NULL);
++ goto error_return;
}
slen = readlink(resolved, symlink, sizeof(symlink) - 1);
- if (slen < 0)
+- return (NULL);
+ if (slen < 0) {
- return (NULL);
++ goto error_return;
+ }
symlink[slen] = '\0';
if (symlink[0] == '/') {
} else if (resolved_len > 1) {
/* Strip the last path component. */
resolved[resolved_len - 1] = '\0';
-@@ -184,7 +319,30 @@
+@@ -172,7 +322,7 @@ realpath(const char *path, char resolved
+ if (symlink[slen - 1] != '/') {
+ if (slen + 1 >= sizeof(symlink)) {
+ errno = ENAMETOOLONG;
+- return (NULL);
++ goto error_return;
+ }
+ symlink[slen] = '/';
+ symlink[slen + 1] = 0;
+@@ -180,11 +330,34 @@ realpath(const char *path, char resolved
+ left_len = strlcat(symlink, left, sizeof(left));
+ if (left_len >= sizeof(left)) {
+ errno = ENAMETOOLONG;
+- return (NULL);
++ goto error_return;
}
}
left_len = strlcpy(left, symlink, sizeof(left));
+ resolved_len = strlcat(resolved, (const char *)&attrs.name + attrs.name.attr_dataoffset, PATH_MAX);
+ if (resolved_len >= PATH_MAX) {
+ errno = ENAMETOOLONG;
-+ return (NULL);
++ goto error_return;
+ }
}
+ /*