Last active
January 15, 2018 22:06
-
-
Save SonoSooS/17aab115941883840627cefefdbf25bd to your computer and use it in GitHub Desktop.
the most overcomplicated chdir implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void chdir(const char* path) | |
{ | |
if(*path == '/') | |
{ | |
strncpy(root, path, 262); | |
} | |
else | |
{ | |
int len = 262 - strnlen(root, 262); | |
char* r = root + strnlen(root, 262); | |
const char* ptr = path; | |
const char* end = path; | |
while(1) | |
{ | |
if(!*end) break; | |
ptr = end; | |
while(*end && *end != '/') end++; | |
while(*end == '/') end++; | |
if(*ptr == '.') | |
{ | |
if(!ptr[1]) break; | |
if(ptr[1] == '/') continue; | |
if(ptr[1] == '.') | |
{ | |
if(r != root) | |
do | |
{ | |
*(--r) = '\0'; | |
len++; | |
while(r != root && *(--r) != '/') | |
{ | |
*r = 0; | |
len++; | |
} | |
} | |
while(0); | |
if(r == root) *(r++) = '/'; | |
continue; | |
} | |
} | |
while(len && *ptr) | |
{ | |
*(r++) = *ptr; | |
len--; | |
if(*(ptr++) == '/') | |
{ | |
ptr++; | |
break; | |
} | |
if(!*ptr) | |
{ | |
if(len) | |
{ | |
*(r++) = '/'; | |
len--; | |
} | |
break; | |
} | |
} | |
if(len) | |
{ | |
if(*(--r) != '/') | |
{ | |
*(++r) = '/'; | |
*(++r) = '\0'; | |
len--; | |
} | |
} | |
else | |
{ | |
*(r--) = 0; | |
*(r++) = '/'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment