From 0ad5165cf249826f13330c69e205f9a5dcc23933 Mon Sep 17 00:00:00 2001 From: insofaras Date: Thu, 1 Sep 2016 20:30:09 +0100 Subject: [PATCH 1/2] linux manual impl of get_canonical to handle non-existent paths --- linux_4ed.cpp | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/linux_4ed.cpp b/linux_4ed.cpp index fe608308..89a3c594 100644 --- a/linux_4ed.cpp +++ b/linux_4ed.cpp @@ -499,26 +499,45 @@ Sys_Set_File_List_Sig(system_set_file_list){ internal Sys_Get_Canonical_Sig(system_get_canonical){ - int32_t result = 0; + char* path = (char*) alloca(len + 1); + char* write_p = path; + const char* read_p = filename; - char* path = realpath(strndupa(filename, len), NULL); - if(path){ - size_t path_len = strlen(path); - if(max >= path_len){ - memcpy(buffer, path, path_len); - result = path_len; + while(read_p < filename + len){ + if(read_p == filename || read_p[0] == '/'){ + if(read_p[1] == '/'){ + ++read_p; + } else if(read_p[1] == '.'){ + if(read_p[2] == '/' || !read_p[2]){ + read_p += 2; + } else if(read_p[2] == '.' && (read_p[3] == '/' || !read_p[3])){ + while(write_p > path && *--write_p != '/'); + read_p += 3; + } else { + *write_p++ = *read_p++; + } + } else { + *write_p++ = *read_p++; + } + } else { + *write_p++ = *read_p++; } -#if FRED_INTERNAL - if(strncmp(filename, path, len) != 0){ - LINUX_FN_DEBUG("[%.*s] -> [%s]", len, filename, path); - } -#endif - free(path); + } + if(write_p == path) *write_p++ = '/'; + + if(max >= (write_p - path)){ + memcpy(buffer, path, write_p - path); } else { - LINUX_FN_DEBUG("[%.*s] -> [null]", len, filename); + write_p = path; } - return result; +#if FRED_INTERNAL + if(len != (write_p - path) || memcmp(filename, path, len) != 0){ + LINUX_FN_DEBUG("[%.*s] -> [%.*s]", len, filename, (int)(write_p - path), path); + } +#endif + + return write_p - path; } internal From f3a6b15deccbcdd7de68a13adbea06018c1ac602 Mon Sep 17 00:00:00 2001 From: insofaras Date: Thu, 1 Sep 2016 20:33:42 +0100 Subject: [PATCH 2/2] i messed up tabs/spaces again fml --- linux_4ed.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/linux_4ed.cpp b/linux_4ed.cpp index 89a3c594..4ca984cb 100644 --- a/linux_4ed.cpp +++ b/linux_4ed.cpp @@ -505,25 +505,25 @@ Sys_Get_Canonical_Sig(system_get_canonical){ while(read_p < filename + len){ if(read_p == filename || read_p[0] == '/'){ - if(read_p[1] == '/'){ - ++read_p; - } else if(read_p[1] == '.'){ - if(read_p[2] == '/' || !read_p[2]){ - read_p += 2; - } else if(read_p[2] == '.' && (read_p[3] == '/' || !read_p[3])){ - while(write_p > path && *--write_p != '/'); - read_p += 3; - } else { - *write_p++ = *read_p++; - } - } else { - *write_p++ = *read_p++; - } + if(read_p[1] == '/'){ + ++read_p; + } else if(read_p[1] == '.'){ + if(read_p[2] == '/' || !read_p[2]){ + read_p += 2; + } else if(read_p[2] == '.' && (read_p[3] == '/' || !read_p[3])){ + while(write_p > path && *--write_p != '/'); + read_p += 3; + } else { + *write_p++ = *read_p++; + } + } else { + *write_p++ = *read_p++; + } } else { *write_p++ = *read_p++; } } - if(write_p == path) *write_p++ = '/'; + if(write_p == path) *write_p++ = '/'; if(max >= (write_p - path)){ memcpy(buffer, path, write_p - path);