Add size and date/time to dir listing. - geomyidae - A small C-based gopherd. (HTM) git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/ (DIR) Log (DIR) Files (DIR) Refs (DIR) Tags (DIR) README (DIR) LICENSE --- (DIR) commit 7f132215d36985b9a17a338fb5bb451a09116303 (DIR) parent f9779c166558b9b0e70754fa9ce6026e5c10fffa (HTM) Author: Christoph Lohmann <20h@r-36.net> Date: Fri, 12 Jun 2020 21:07:14 +0200 Add size and date/time to dir listing. This applies and old patch by Evil_Bob. Dank u! Diffstat: M handlr.c | 9 +++++++-- M ind.c | 35 +++++++++++++++++++++++++++++++ M ind.h | 2 ++ M main.c | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) --- (DIR) diff --git a/handlr.c b/handlr.c @@ -66,8 +66,13 @@ handledir(int sock, char *path, char *port, char *base, char *args, if (stat(file, &st) >= 0 && S_ISDIR(st.st_mode)) type = gettype("index.gph"); e = file + strlen(base); - ret = dprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type, - dirent[i]->d_name, e, ohost, port); + ret = dprintf(sock, + "%c%-50.50s %10s %16s\t%s\t%s\t%s\r\n", + *type->type, + dirent[i]->d_name, + humansize(st.st_size), + humantime(&(st.st_mtim.tv_sec)), + e, ohost, port); free(file); free(dirent[i]); } (DIR) diff --git a/ind.c b/ind.c @@ -10,6 +10,8 @@ #include <fcntl.h> #include <stdio.h> #include <stdlib.h> +#include <stdint.h> +#include <time.h> #include <netdb.h> #include <sys/socket.h> #include <sys/stat.h> @@ -513,3 +515,36 @@ setcgienviron(char *file, char *path, char *port, char *base, char *args, } +char * +humansize(off_t n) +{ + static char buf[16]; + const char postfixes[] = "BKMGTPE"; + double size; + int i = 0; + + for (size = n; size >= 1024 && i < strlen(postfixes); i++) + size /= 1024; + + if (!i) { + snprintf(buf, sizeof(buf), "%ju%c", (uintmax_t)n, + postfixes[i]); + } else { + snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]); + } + + return buf; +} + +char * +humantime(const time_t *clock) +{ + static char buf[32]; + struct tm *tm; + + tm = localtime(clock); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M %Z", tm); + + return buf; +} + (DIR) diff --git a/ind.h b/ind.h @@ -50,6 +50,8 @@ char *reverselookup(char *host); void setcgienviron(char *file, char *path, char *port, char *base, char *args, char *sear, char *ohost, char *chost, int istls); +char *humansize(off_t n); +char *humantime(const time_t *clock); #endif (DIR) diff --git a/main.c b/main.c @@ -202,7 +202,7 @@ handlerequest(int sock, char *req, int rlen, char *base, char *ohost, } if (recvb[0] != '/' || strstr(recvb, "..")){ - dprintf(sock, selinval); + dprintf(sock, "%s", selinval); return; }