client/runtime/linux_path.c (1342B)
1 #define _GNU_SOURCE 2 #include "linux_path.h" 3 #include <dirent.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <strings.h> 7 #include <sys/stat.h> 8 9 char *linux_path(const char *name) 10 { 11 char *path = strdup(name); 12 if (!path) abort(); 13 for (char *p = path; *p; ++p) if (*p == '\\') *p = '/'; 14 15 /* Exact names need no directory scan, including on case-insensitive mounts. */ 16 struct stat info; 17 if (!lstat(path, &info)) return path; 18 19 /* Resolve each existing component; keep missing leaves intact for creation. */ 20 for (char *part = path; *part;) { 21 if (*part == '/') { ++part; continue; } 22 char *end = strchrnul(part, '/'); 23 char separator = *end; 24 *end = 0; 25 if (lstat(path, &info)) { 26 char first = *part; 27 *part = 0; 28 DIR *directory = opendir(part == path ? "." : path); 29 *part = first; 30 if (directory) { 31 struct dirent *entry; 32 while ((entry = readdir(directory))) { 33 if (!strcasecmp(part, entry->d_name)) { 34 memcpy(part, entry->d_name, end - part); 35 break; 36 } 37 } 38 closedir(directory); 39 } 40 } 41 *end = separator; 42 part = end; 43 } 44 return path; 45 }