From 8a72d5be849f79c065a81a349a12b49c7e23dd03 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sun, 10 Nov 2013 22:02:12 +0000 Subject: Sort directory contents. --- content/fetchers/file.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/content/fetchers/file.c b/content/fetchers/file.c index 21cee79d5..4585f1710 100644 --- a/content/fetchers/file.c +++ b/content/fetchers/file.c @@ -499,15 +499,17 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, nsurl *up; /* url of parent */ char *path; /* url for list entries */ - DIR *scandir; /* handle for enumerating the directory */ - struct dirent* ent; /* leaf directory entry */ + struct dirent **listing = NULL; /* directory entry listing */ + struct dirent *ent; /* current directroy entry */ struct stat ent_stat; /* stat result of leaf entry */ char datebuf[64]; /* buffer for date text */ char timebuf[64]; /* buffer for time text */ char urlpath[PATH_MAX]; /* buffer for leaf entry path */ + int i; /* directory entry index */ + int n; /* number of directory entries */ - scandir = opendir(ctx->path); - if (scandir == NULL) { + n = scandir(ctx->path, &listing, 0, alphasort); + if (n < 0) { fetch_file_process_error(ctx, fetch_file_errno_to_http_code(errno)); return; @@ -565,7 +567,8 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; - while ((ent = readdir(scandir)) != NULL) { + for (i = 0; i < n; i++) { + ent = listing[i]; if (ent->d_name[0] == '.') continue; @@ -650,7 +653,12 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, fetch_file_process_dir_aborted: - closedir(scandir); + if (listing != NULL) { + for (i = 0; i < n; i++) { + free(listing[i]); + } + free(listing); + } } -- cgit v1.2.3