PERFORCE change 214871 for review
Brooks Davis
brooks at FreeBSD.org
Tue Jul 24 19:35:13 UTC 2012
http://p4web.freebsd.org/@@214871?ac=10
Change 214871 by brooks at brooks_ecr_current on 2012/07/24 19:34:53
Remove a level of indirection in the file list and correct an
overallocation in the realloc case in the process.
Sort the directory entries.
Affected files ...
.. //depot/projects/ctsrd/beribsd/src/ctsrd/browser/browser.c#15 edit
Differences ...
==== //depot/projects/ctsrd/beribsd/src/ctsrd/browser/browser.c#15 (text+ko) ====
@@ -123,7 +123,7 @@
const int colstart[] = {0, 267, 534};
struct dent {
- struct dirent *entry;
+ struct dirent entry;
char *desc;
u_int32_t *icon;
};
@@ -735,19 +735,35 @@
}
static int
+dentcmp(const void *v1, const void *v2)
+{
+ const struct dent *d1, *d2;
+
+ d1 = v1;
+ d2 = v2;
+
+ /* Sort .. first */
+ if (strcmp(d1->entry.d_name, "..") == 0)
+ return (-1);
+ if (strcmp(d2->entry.d_name, "..") == 0)
+ return (1);
+ return strcmp(d1->entry.d_name, d2->entry.d_name);
+}
+
+static int
browsedir(int dfd)
{
int action, topslot, j, curslot, maxdents, nfd, ndents, retfd;
DIR *dirp;
struct dirent *entry;
- struct dent **dents, *dent;
+ struct dent *dents, *dent;
if ((dirp = fdopendir(dfd)) == NULL)
err(1, "fdopendir()");
ndents = 0;
maxdents = 1024;
- dents = malloc(sizeof(struct dent *) * maxdents);
+ dents = malloc(sizeof(struct dent) * maxdents);
if (dents == NULL)
err(1, "malloc dents");
@@ -760,31 +776,28 @@
}
if (strcmp(".", entry->d_name) == 0)
continue;
- dents[ndents] = malloc(sizeof(struct dent));
- if (dents[ndents] == NULL)
- err(1, "malloc dent[%d]", ndents);
- memcpy(&(dents[ndents]->entry), &entry, sizeof(entry));
- dents[ndents]->desc = NULL;
- dents[ndents]->icon = NULL;
+ memcpy(&(dents[ndents].entry), entry, sizeof(*entry));
+ dents[ndents].desc = NULL;
+ dents[ndents].icon = NULL;
ndents++;
}
-
+ qsort(dents, ndents, sizeof(struct dent), &dentcmp);
topslot = 0;
render:
fb_fill_region(black, colstart[0], FROW, fb_width, NROW * RHEIGHT);
for(curslot = 0; curslot < NSLOTS && topslot + curslot < ndents;
curslot++) {
- dent = dents[topslot + curslot];
+ dent = &dents[topslot + curslot];
if (dent->desc == NULL)
- dent->desc = strdup(get_desc(dfd, dent->entry));
+ dent->desc = strdup(get_desc(dfd, &(dent->entry)));
if (dent->icon == NULL)
dent->icon = get_icon(dent->desc);
if (verbose)
- printf("%2d %20s %s\n", curslot, dent->entry->d_name,
+ printf("%2d %20s %s\n", curslot, dent->entry.d_name,
dent->desc);
- update_slot(curslot, dent->icon, dent->entry->d_name);
+ update_slot(curslot, dent->icon, dent->entry.d_name);
}
if (curslot == NSLOTS)
curslot--;
@@ -812,9 +825,9 @@
case ACT_REFRESH:
/* Reset descriptions and icons */
for (j = 0; j < ndents; j++) {
- free(dents[j]->desc);
- dents[j]->desc = NULL;
- dents[j]->icon = NULL;
+ free(dents[j].desc);
+ dents[j].desc = NULL;
+ dents[j].icon = NULL;
}
goto render;
default:
@@ -822,22 +835,22 @@
errx(1, "invalid action");
if (topslot + action >= ndents)
continue;
- if (dents[topslot + action]->entry->d_type == DT_DIR) {
+ if (dents[topslot + action].entry.d_type == DT_DIR) {
if ((nfd = openat(dfd,
- dents[topslot + action]->entry->d_name,
+ dents[topslot + action].entry.d_name,
O_RDONLY|O_DIRECTORY)) == -1)
goto render; /* XXX: display error */
retfd = nfd;
goto cleanup;
} else if (strcmp("image/png",
- dents[topslot + action]->desc) == 0) {
+ dents[topslot + action].desc) == 0) {
show_png(dfd,
- dents[topslot + action]->entry->d_name);
+ dents[topslot + action].entry.d_name);
goto render;
} else if (strcmp("text/plain",
- dents[topslot + action]->desc) == 0) {
+ dents[topslot + action].desc) == 0) {
show_text_file(dfd,
- dents[topslot + action]->entry->d_name);
+ dents[topslot + action].entry.d_name);
goto render;
} else {
if (verbose)
@@ -849,10 +862,8 @@
}
cleanup:
- for (j = 0; j < ndents; j++) {
- free(dents[j]->desc);
- free(dents[j]);
- }
+ for (j = 0; j < ndents; j++)
+ free(dents[j].desc);
free(dents);
if (closedir(dirp) == -1)
More information about the p4-projects
mailing list