diff --git a/Nexus/fcaseopen.c b/Nexus/fcaseopen.c index 5b0a23b..0bdefbd 100644 --- a/Nexus/fcaseopen.c +++ b/Nexus/fcaseopen.c @@ -30,75 +30,68 @@ THE SOFTWARE. #include #include -// r must have strlen(path) + 2 bytes +// r must have strlen(path) + 3 bytes static int casepath(char const *path, char *r) { size_t l = strlen(path); - char *p = (char*)alloca(l + 1); + char *p = (char *)alloca(l + 1); strcpy(p, path); size_t rl = 0; - + DIR *d; - if (p[0] == '/') - { + if (p[0] == '/') { d = opendir("/"); p = p + 1; } - else - { - d = opendir("."); + else { + d = opendir("."); r[0] = '.'; r[1] = 0; - rl = 1; + rl = 1; } - + int last = 0; - char *c = strsep(&p, "/"); - while (c) - { - if (!d) - { + char *c = strsep(&p, "/"); + while (c) { + if (!d) { return 0; } - - if (last) - { + + if (last) { closedir(d); return 0; } - + r[rl] = '/'; rl += 1; r[rl] = 0; - + struct dirent *e = readdir(d); - while (e) - { - if (strcasecmp(c, e->d_name) == 0) - { + while (e) { + if (strcasecmp(c, e->d_name) == 0) { strcpy(r + rl, e->d_name); rl += strlen(e->d_name); closedir(d); d = opendir(r); - + break; } - + e = readdir(d); } - - if (!e) - { + + if (!e) { strcpy(r + rl, c); rl += strlen(c); last = 1; } - + c = strsep(&p, "/"); } - - if (d) closedir(d); + + if (d) + closedir(d); return 1; } #endif @@ -107,11 +100,9 @@ FILE *fcaseopen(char const *path, char const *mode) { FILE *f = fopen(path, mode); #if !defined(_WIN32) - if (!f) - { - char *r = (char*)alloca(strlen(path) + 2); - if (casepath(path, r)) - { + if (!f) { + char *r = (char *)alloca(strlen(path) + 3); + if (casepath(path, r)) { f = fopen(r, mode); } } @@ -122,13 +113,11 @@ FILE *fcaseopen(char const *path, char const *mode) void casechdir(char const *path) { #if !defined(_WIN32) - char *r = (char*)alloca(strlen(path) + 2); - if (casepath(path, r)) - { + char *r = (char *)alloca(strlen(path) + 3); + if (casepath(path, r)) { chdir(r); } - else - { + else { errno = ENOENT; } #else