Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / system / libroot / posix / stdlib / realpath.cpp
blob9724c39d7e5f8d4fcf39b6ba46757e5668c88f3c
1 /*
2 * Copyright 2013, Olivier Coursière, olivier.coursiere@laposte.net.
3 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
5 */
8 #include <stdlib.h>
10 #include <errno.h>
11 #include <sys/stat.h>
13 #include <errno_private.h>
14 #include <syscalls.h>
17 char*
18 realpath(const char* path, char* resolved)
20 char* resolvedPath = resolved;
22 if (resolvedPath == NULL) {
23 resolvedPath = (char*)malloc(PATH_MAX + 1);
24 if (resolvedPath == NULL) {
25 __set_errno(B_NO_MEMORY);
26 return NULL;
30 status_t status = _kern_normalize_path(path, true, resolvedPath);
31 if (status != B_OK) {
32 __set_errno(status);
33 if (resolved == NULL)
34 free(resolvedPath);
35 return NULL;
38 // The path must actually exist, not just its parent directories
39 struct stat stat;
40 if (lstat(resolvedPath, &stat) != 0) {
41 if (resolved == NULL)
42 free(resolvedPath);
43 return NULL;
46 return resolvedPath;