From owner-p4-projects@FreeBSD.ORG Tue Jul 24 19:35:13 2012 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 9DE571065689; Tue, 24 Jul 2012 19:35:11 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4CCEE106567D for ; Tue, 24 Jul 2012 19:35:11 +0000 (UTC) (envelope-from brooks@freebsd.org) Received: from skunkworks.freebsd.org (skunkworks.freebsd.org [IPv6:2001:4f8:fff6::2d]) by mx1.freebsd.org (Postfix) with ESMTP id 33FB18FC19 for ; Tue, 24 Jul 2012 19:35:11 +0000 (UTC) Received: from skunkworks.freebsd.org (localhost [127.0.0.1]) by skunkworks.freebsd.org (8.14.4/8.14.4) with ESMTP id q6OJZBnq005532 for ; Tue, 24 Jul 2012 19:35:11 GMT (envelope-from brooks@freebsd.org) Received: (from perforce@localhost) by skunkworks.freebsd.org (8.14.4/8.14.4/Submit) id q6OJZA4J005529 for perforce@freebsd.org; Tue, 24 Jul 2012 19:35:10 GMT (envelope-from brooks@freebsd.org) Date: Tue, 24 Jul 2012 19:35:10 GMT Message-Id: <201207241935.q6OJZA4J005529@skunkworks.freebsd.org> X-Authentication-Warning: skunkworks.freebsd.org: perforce set sender to brooks@freebsd.org using -f From: Brooks Davis To: Perforce Change Reviews Precedence: bulk Cc: Subject: PERFORCE change 214871 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Jul 2012 19:35:13 -0000 http://p4web.freebsd.org/@@214871?ac=10 Change 214871 by brooks@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)