| 34 | } |
| 35 | |
| 36 | char *path(char *s) |
| 37 | { |
| 38 | char *c = s; |
| 39 | // skip "<decal>" |
| 40 | if(c[0] == '<') |
| 41 | { |
| 42 | char *enddecal = strrchr(c, '>'); |
| 43 | if(!enddecal) return s; |
| 44 | c = enddecal + 1; |
| 45 | } |
| 46 | // substitute with single, proper path delimiters |
| 47 | for(char *t = c; (t = strpbrk(t, "/\\")); ) |
| 48 | { |
| 49 | *t++ = PATHDIV; |
| 50 | size_t d = strspn(t, "/\\"); |
| 51 | if(d) memmove(t, t + d, strlen(t + d) + 1); // remove multiple path delimiters |
| 52 | } |
| 53 | // collapse ".."-parts |
| 54 | for(char *prevdir = NULL, *curdir = s;;) |
| 55 | { |
| 56 | prevdir = curdir[0] == PATHDIV ? curdir + 1 : curdir; |
| 57 | curdir = strchr(prevdir, PATHDIV); |
| 58 | if(!curdir) break; |
| 59 | if(prevdir + 1 == curdir && prevdir[0]=='.') |
| 60 | { // simply remove "./" |
| 61 | memmove(prevdir, curdir + 1, strlen(curdir)); |
| 62 | curdir = prevdir; |
| 63 | } |
| 64 | else if(curdir[1] == '.' && curdir[2] == '.' && curdir[3] == PATHDIV) |
| 65 | { // collapse "/foo/../" to "/" |
| 66 | if(prevdir + 2 == curdir && prevdir[0] == '.' && prevdir[1] == '.') continue; // foo is also ".." -> skip |
| 67 | memmove(prevdir, curdir + 4, strlen(curdir + 3)); |
| 68 | if(prevdir >= c + 2 && prevdir[-1] == PATHDIV) |
| 69 | { |
| 70 | prevdir -= 2; |
| 71 | while(prevdir > c && prevdir[-1] != PATHDIV) --prevdir; |
| 72 | } |
| 73 | curdir = prevdir; |
| 74 | } |
| 75 | } |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | char *unixpath(char *s) |
| 80 | { |
no test coverage detected