char *pathname = talloc_array(ctx, char, strlen(pathname_in)+2);
const char *s = pathname_in;
char *p = pathname;
- bool wrote_slash = false;
if (pathname == NULL) {
return NULL;
/* Always start with a '/'. */
*p++ = '/';
- wrote_slash = true;
while (*s) {
/* Deal with '/' or multiples of '/'. */
s++;
}
/* Update target with one '/' */
- if (!wrote_slash) {
+ if (p[-1] != '/') {
*p++ = '/';
- wrote_slash = true;
}
continue;
}
- if (wrote_slash) {
+ if (p[-1] == '/') {
/* Deal with "./" or ".\0" */
if (s[0] == '.' &&
(s[1] == '/' || s[1] == '\0')) {
s++;
}
/* Don't write anything to target. */
- /* wrote_slash is still true. */
continue;
}
/* Deal with "../" or "..\0" */
s++;
}
/*
- * As wrote_slash is true, we go back
- * one character to point p at the slash
- * we just saw.
+ * As we're on the slash, we go back
+ * one character to point p at the
+ * slash we just saw.
*/
if (p > pathname) {
p--;
p++;
/* Don't write anything to target. */
- /* wrote_slash is still true. */
continue;
}
}
/* Non-separator character, just copy. */
*p++ = *s++;
- wrote_slash = false;
}
- if (wrote_slash) {
+ if (p[-1] == '/') {
/*
* We finished on a '/'.
* Remove the trailing '/', but not if it's