5
* In place, rewrite name to compress multiple /, eliminate ., and process ..
7
#define SEP(x) ((x)=='/' || (x) == 0)
14
rooted = name[0] == '/';
18
* p points at beginning of path element we're considering.
19
* q points just past the last path element we wrote (no slash).
20
* dotdot points just past the point where .. cannot backtrack
21
* any further (no slash).
23
p = q = dotdot = name+rooted;
25
if(p[0] == '/') /* null element */
27
else if(p[0] == '.' && SEP(p[1]))
28
p += 1; /* don't count the separator in case it is nul */
29
else if(p[0] == '.' && p[1] == '.' && SEP(p[2])) {
31
if(q > dotdot) { /* can backtrack */
32
while(--q > dotdot && *q != '/')
34
} else if(!rooted) { /* /.. is / but ./../ is .. */
41
} else { /* real path element */
44
while((*q = *p) != '/' && *q != 0)
48
if(q == name) /* empty string is really ``.'' */