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 */ 6 7 8 #include <stdlib.h> 9 10 #include <errno.h> 11 #include <sys/stat.h> 12 13 #include <errno_private.h> 14 #include <syscalls.h> 15 16 17 char* 18 realpath(const char* path, char* resolved) 19 { 20 char* resolvedPath = resolved; 21 22 if (resolvedPath == NULL) { 23 resolvedPath = (char*)malloc(PATH_MAX + 1); 24 if (resolvedPath == NULL) { 25 __set_errno(B_NO_MEMORY); 26 return NULL; 27 } 28 } 29 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; 36 } 37 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; 44 } 45 46 return resolvedPath; 47 } 48