From 3d78b14deba04f4ac81220f7b1ae6dc5cc1031b6 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 16:01:06 +0000 Subject: [PATCH] Fix #1396 - JS sorting code for dir listing --- mongoose.c | 13 +++++++------ src/http.c | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/mongoose.c b/mongoose.c index 0143aaff..3dcfe7ab 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1322,20 +1322,21 @@ static void printdirentry(const char *name, void *userdata) { struct printdirentrydata *d = (struct printdirentrydata *) userdata; struct mg_fs *fs = d->opts->fs == NULL ? &mg_fs_posix : d->opts->fs; size_t size = 0; - time_t mtime = 0; + time_t t = 0; char path[MG_PATH_MAX], sz[64], mod[64]; int flags, n = 0; // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &mtime)) == 0) { + } else if ((flags = fs->stat(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; - struct tm t; + struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); + size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -1345,12 +1346,12 @@ static void printdirentry(const char *name, void *userdata) { } else { snprintf(sz, sizeof(sz), "%.1fG", (double) size / 1073741824); } - strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&mtime, &t)); + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&t, &tm)); n = (int) mg_url_encode(name, strlen(name), path, sizeof(path)); mg_printf(d->c, " %s%s" - "%s%s\n", - n, path, slash, name, slash, mod, sz); + "%s%s\n", + n, path, slash, name, slash, (unsigned long) t, mod, size, sz); } } diff --git a/src/http.c b/src/http.c index 6b63e98b..4b5070a4 100644 --- a/src/http.c +++ b/src/http.c @@ -573,20 +573,21 @@ static void printdirentry(const char *name, void *userdata) { struct printdirentrydata *d = (struct printdirentrydata *) userdata; struct mg_fs *fs = d->opts->fs == NULL ? &mg_fs_posix : d->opts->fs; size_t size = 0; - time_t mtime = 0; + time_t t = 0; char path[MG_PATH_MAX], sz[64], mod[64]; int flags, n = 0; // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &mtime)) == 0) { + } else if ((flags = fs->stat(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; - struct tm t; + struct tm tm; if (flags & MG_FS_DIR) { snprintf(sz, sizeof(sz), "%s", "[DIR]"); + size = -1; } else if (size < 1024) { snprintf(sz, sizeof(sz), "%d", (int) size); } else if (size < 0x100000) { @@ -596,12 +597,12 @@ static void printdirentry(const char *name, void *userdata) { } else { snprintf(sz, sizeof(sz), "%.1fG", (double) size / 1073741824); } - strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&mtime, &t)); + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&t, &tm)); n = (int) mg_url_encode(name, strlen(name), path, sizeof(path)); mg_printf(d->c, " %s%s" - "%s%s\n", - n, path, slash, name, slash, mod, sz); + "%s%s\n", + n, path, slash, name, slash, (unsigned long) t, mod, size, sz); } }