libroot: Accept NULL for the resolved_name argument of realpath()

as specified in IEEE Std 1003.1, 2013 Edition, see
http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html

In this case, the returned buffer is allocated with realpath() and can be
deallocated by the caller with free().
The behavior was only "implementation defined" in previous revisions like
IEEE Std 1003.1, 2004 Edition, see
http://pubs.opengroup.org/onlinepubs/000095399/functions/realpath.html

Signed-off-by: Jérôme Duval <[email protected]>
This commit is contained in:
Olivier Coursière
2013-11-10 23:26:18 +01:00
committed by Jérôme Duval
parent 1bb144a926
commit be6d2f97cd
+19 -3
View File
@@ -1,4 +1,5 @@
/*
* Copyright 2013, Olivier Coursière, [email protected].
* Copyright 2009, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
@@ -16,16 +17,31 @@
char*
realpath(const char* path, char* resolved)
{
status_t status = _kern_normalize_path(path, true, resolved);
char* resolvedPath = resolved;
if (resolvedPath == NULL) {
resolvedPath = (char*)malloc(PATH_MAX + 1);
if (resolvedPath == NULL) {
__set_errno(B_NO_MEMORY);
return NULL;
}
}
status_t status = _kern_normalize_path(path, true, resolvedPath);
if (status != B_OK) {
__set_errno(status);
if (resolved == NULL)
free(resolvedPath);
return NULL;
}
// The path must actually exist, not just its parent directories
struct stat stat;
if (lstat(resolved, &stat) != 0)
if (lstat(resolvedPath, &stat) != 0) {
if (resolved == NULL)
free(resolvedPath);
return NULL;
}
return resolved;
return resolvedPath;
}