// This function needs a 4KiB (PATH_MAX) buffer. // The alternative is to heap allocate and then trim, but that's 2x the code. // (Remember that readlink(2) won't tell you the needed size, so the multi-pass // algorithm isn't even an option unless you want to just guess, in which case // you're back needing to trim again.) #pragma clang diagnostic ignored "-Wframe-larger-than="
char* realpath(constchar* path, char* result) { // Weird special case. if (!path) {
errno = EINVAL; return nullptr;
}
// Get an O_PATH fd, and...
ScopedFd fd(open(path, O_PATH | O_CLOEXEC)); if (fd.get() == -1) return nullptr;
// (...remember the device/inode that we're talking about and...) struct stat sb_before; if (fstat(fd.get(), &sb_before) == -1) return nullptr;
// ...ask the kernel to do the hard work for us.
FdPath fd_path(fd.get()); char dst[PATH_MAX];
ssize_t l = readlink(fd_path.c_str(), dst, sizeof(dst) - 1); if (l == -1) return nullptr;
dst[l] = '\0';
// What if the file was removed in the meantime? readlink(2) will have // returned "/a/b/c (deleted)", and we want to return ENOENT instead. struct stat sb_after; if (stat(dst, &sb_after) == -1 ||
sb_before.st_dev != sb_after.st_dev ||
sb_before.st_ino != sb_after.st_ino) {
errno = ENOENT; return nullptr;
}
return result ? strcpy(result, dst) : strdup(dst);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.20 Sekunden
(vorverarbeitet am 2026-06-28)
¤
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.