/* Split the names in the given name sequence,
replacing slashes with nulls and filling in the given index array */
staticvoid
splitNames(char *names, char **ix)
{ char *p = names; int i = 0;
while (*p) {
ix[i++] = p++; while (*p) { if (*p == '/') {
*p++ = '\0'; break;
}
p++;
}
}
}
/* Join the names in the given name sequence, ignoring names whose index
entries have been cleared and replacing nulls with slashes as needed */
staticvoid
joinNames(char *names, int nc, char **ix)
{ int i; char *p;
for (i = 0, p = names; i < nc; i++) { if (!ix[i]) continue; if (i > 0) {
p[-1] = '/';
} if (p == ix[i]) {
p += strlen(p) + 1;
} else { char *q = ix[i]; while ((*p++ = *q++));
}
}
*p = '\0';
}
/* Collapse "." and ".." names in the given path wherever possible. A"."namemayalwaysbeeliminated;a".."namemaybeeliminatedifit followsanamethatisneither"."nor"..".Thisisasyntacticoperation thatperformsnofilesystemqueries,soitshouldonlybeusedtocleanup
after invoking the realpath() procedure. */
void
collapse(char *path)
{ char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */ int nc; char **ix; int i, j;
nc = collapsible(names); if (nc < 2) return; /* Nothing to do */
ix = (char **)alloca(nc * sizeof(char *));
splitNames(names, ix);
for (i = 0; i < nc; i++) { int dots = 0;
/* Find next occurrence of "." or ".." */ do { char *p = ix[i]; if (p[0] == '.') { if (p[1] == '\0') {
dots = 1; break;
} if ((p[1] == '.') && (p[2] == '\0')) {
dots = 2; break;
}
}
i++;
} while (i < nc); if (i >= nc) break;
/* At this point i is the index of either a "." or a "..", so take the
appropriate action and then continue the outer loop */ if (dots == 1) { /* Remove this instance of "." */
ix[i] = 0;
} else { /* If there is a preceding name, remove both that name and this
instance of ".."; otherwise, leave the ".." as is */ for (j = i - 1; j >= 0; j--) { if (ix[j]) break;
} if (j < 0) continue;
ix[j] = 0;
ix[i] = 0;
} /* i will be incremented at the top of the loop */
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.