Keep local copies of dir entries, just keeping pointers was a bug

This commit is contained in:
lostd 2014-10-08 15:50:39 +03:00
parent bc766bc4a8
commit 7028eb1838

32
noice.c
View file

@ -75,10 +75,6 @@ struct assoc assocs[] = {
int die = 0; int die = 0;
struct entry {
char name[MAXNAMLEN + 1];
};
char * char *
openwith(char *file) openwith(char *file)
{ {
@ -104,8 +100,8 @@ dentcmp(const void *va, const void *vb)
{ {
const struct dirent *a, *b; const struct dirent *a, *b;
a = *(struct dirent **)va; a = (struct dirent *)va;
b = *(struct dirent **)vb; b = (struct dirent *)vb;
return strcmp(a->d_name, b->d_name); return strcmp(a->d_name, b->d_name);
} }
@ -229,7 +225,7 @@ browse(const char *ipath)
{ {
DIR *dirp; DIR *dirp;
struct dirent *dp; struct dirent *dp;
struct dirent **dents; struct dirent *dents;
int i, n, cur; int i, n, cur;
int r, ret; int r, ret;
char *path = strdup(ipath); char *path = strdup(ipath);
@ -255,7 +251,7 @@ begin:
dents = realloc(dents, (n + 1) * sizeof(*dents)); dents = realloc(dents, (n + 1) * sizeof(*dents));
if (dents == NULL) if (dents == NULL)
printerr(1, "realloc"); printerr(1, "realloc");
dents[n] = dp; memcpy(&dents[n], dp, sizeof(*dents));
n++; n++;
} }
@ -263,7 +259,7 @@ begin:
for (;;) { for (;;) {
int nlines; int nlines;
struct entry *tmpents; struct dirent *tmpents;
int odd; int odd;
redraw: redraw:
@ -289,11 +285,9 @@ redraw:
/* No text wrapping in entries */ /* No text wrapping in entries */
tmpents = malloc(n * sizeof(*tmpents)); tmpents = malloc(n * sizeof(*tmpents));
for (i = 0; i < n; i++) { memcpy(tmpents, dents, n * sizeof(*tmpents));
strlcpy(tmpents[i].name, dents[i]->d_name, for (i = 0; i < n; i++)
sizeof(tmpents[i].name)); tmpents[i].d_name[COLS - strlen(CURSR) - 1] = '\0';
tmpents[i].name[COLS - strlen(CURSR) - 1] = '\0';
}
/* Print cwd. If empty we are on the root. We store it /* Print cwd. If empty we are on the root. We store it
* as an empty string so that when we navigate in /mnt * as an empty string so that when we navigate in /mnt
@ -308,18 +302,18 @@ redraw:
for (i = 0; i < nlines; i++) for (i = 0; i < nlines; i++)
printw("%s%s\n", printw("%s%s\n",
i == cur ? CURSR : EMPTY, i == cur ? CURSR : EMPTY,
tmpents[i].name); tmpents[i].d_name);
} else if (cur >= n - nlines / 2) { } else if (cur >= n - nlines / 2) {
for (i = n - nlines; i < n; i++) for (i = n - nlines; i < n; i++)
printw("%s%s\n", printw("%s%s\n",
i == cur ? CURSR : EMPTY, i == cur ? CURSR : EMPTY,
tmpents[i].name); tmpents[i].d_name);
} else { } else {
for (i = cur - nlines / 2; for (i = cur - nlines / 2;
i < cur + nlines / 2 + odd; i++) i < cur + nlines / 2 + odd; i++)
printw("%s%s\n", printw("%s%s\n",
i == cur ? CURSR : EMPTY, i == cur ? CURSR : EMPTY,
tmpents[i].name); tmpents[i].d_name);
} }
free(tmpents); free(tmpents);
@ -358,8 +352,8 @@ nochange:
if (n == 0) if (n == 0)
goto nochange; goto nochange;
name = dents[cur]->d_name; name = dents[cur].d_name;
type = dents[cur]->d_type; type = dents[cur].d_type;
pathsiz = strlen(path) + 1 + strlen(name) + 1; pathsiz = strlen(path) + 1 + strlen(name) + 1;
pathnew = malloc(pathsiz); pathnew = malloc(pathsiz);