Skip to content
Snippets Groups Projects
Commit ef4abec6 authored by Yorhel's avatar Yorhel
Browse files

path.c: Don't throw away remaining components after a symlink

This is a quick hack to fix http://dev.yorhel.nl/ncdu/bug/18.
path_real() is both rather unreadable, fragile, hard to maintain and a
bit inefficient. It wouldn't be too surprising if I introduced a new bug
with this commit...

*makes a note to do a proper rewrite of that code later on*
parent f83ed07f
No related branches found
No related tags found
No related merge requests found
...@@ -124,7 +124,10 @@ static char *path_absolute(const char *path) { ...@@ -124,7 +124,10 @@ static char *path_absolute(const char *path) {
} }
/* NOTE: cwd and the memory cur points to are unreliable after calling this function */ /* NOTE: cwd and the memory cur points to are unreliable after calling this
* function.
* TODO: This code is rather fragile and inefficient. A rewrite is in order.
*/
static char *path_real_rec(char *cur, int *links) { static char *path_real_rec(char *cur, int *links) {
int i, n, tmpl, lnkl = 0; int i, n, tmpl, lnkl = 0;
char **arr, *tmp, *lnk = NULL, *ret = NULL; char **arr, *tmp, *lnk = NULL, *ret = NULL;
...@@ -159,17 +162,29 @@ static char *path_real_rec(char *cur, int *links) { ...@@ -159,17 +162,29 @@ static char *path_real_rec(char *cur, int *links) {
errno = ELOOP; errno = ELOOP;
goto path_real_done; goto path_real_done;
} }
lnk[n] = 0; lnk[n++] = 0;
/* create new path and call path_real_rec() again */ /* create new path */
if(lnk[0] != '/') { if(lnk[0] != '/')
n += strlen(tmp) + 1; n += strlen(tmp);
if(tmpl < n) { if(tmpl < n) {
tmpl = n; tmpl = n;
tmp = realloc(tmp, tmpl); tmp = realloc(tmp, tmpl);
} }
if(lnk[0] != '/')
strcat(tmp, lnk); strcat(tmp, lnk);
} else else
strcpy(tmp, lnk); strcpy(tmp, lnk);
/* append remaining directories */
while(--i>=0) {
n += strlen(arr[i])+1;
if(tmpl < n) {
tmpl = n;
tmp = realloc(tmp, tmpl);
}
strcat(tmp, "/");
strcat(tmp, arr[i]);
}
/* call path_real_rec() with the new path */
ret = path_real_rec(tmp, links); ret = path_real_rec(tmp, links);
goto path_real_done; goto path_real_done;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment