/* Note: The comments in this file use the terminology
defined in the java.io.File class */
/* Convert a pathname to canonical form. The input path is assumed to contain noduplicateslashes.OnSolariswecanuserealpath()todomostofthe work,thoughoncethat'sdonewestillmustcollapseanyremaining"."and
".." names by hand. */
JNIEXPORT int
JDK_Canonicalize(constchar *orig, char *out, int len)
{ if (len < PATH_MAX) {
errno = EINVAL; return -1;
}
/* First try realpath() on the entire path */ if (realpath(orig, out)) { /* That worked, so return it */
collapse(out); return0;
} else { /* Something's bogus in the original path, so remove names from the end
until either some subpath works or we run out of names */ char *p, *end, *r = NULL; char path[PATH_MAX + 1];
// strlen(orig) <= PATH_MAX, see above
strncpy(path, orig, PATH_MAX); // append null for == case
path[PATH_MAX] = '\0';
end = path + strlen(path);
for (p = end; p > path;) {
/* Skip last element */ while ((--p > path) && (*p != '/')); if (p == path) break;
/* Try realpath() on this subpath */
*p = '\0';
r = realpath(path, out);
*p = (p == end) ? '\0' : '/';
if (r != NULL) { /* The subpath has a canonical path */ break;
} elseif (errno == ENOENT || errno == ENOTDIR || errno == EACCES) { /* If the lookup of a particular subpath fails because the file doesnotexist,becauseitisofthewrongtype,orbecause accessisdenied,thenremoveitslastnameandtryagain.
Other I/O problems cause an error return. */ continue;
} else { return -1;
}
}
if (r != NULL) { /* Append unresolved subpath to resolved subpath */ int rn = strlen(r); if (rn + (int)strlen(p) >= len) { /* Buffer overflow */
errno = ENAMETOOLONG; return -1;
} if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) { /* Avoid duplicate slashes */
p++;
}
strcpy(r + rn, p);
collapse(r); return0;
} else { /* Nothing resolved, so just return the original path */
strcpy(out, path);
collapse(out); return0;
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.