X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/agedu/blobdiff_plain/bf53e756e0d26c9cdca601e65f2e652bc1bb1b21..7e25423cd18ab429b13c6e2ea920d9c44c0f263c:/html.c diff --git a/html.c b/html.c index 7633dec..d6d6ad1 100644 --- a/html.c +++ b/html.c @@ -2,18 +2,9 @@ * html.c: implementation of html.h. */ -#include -#include -#include -#include -#include -#include -#include -#include - #include "agedu.h" #include "html.h" -#include "malloc.h" +#include "alloc.h" #include "trie.h" #include "index.h" @@ -28,7 +19,8 @@ struct html { char *href; size_t hreflen; const char *format; - unsigned long long thresholds[MAXCOLOUR-1]; + unsigned long long thresholds[MAXCOLOUR]; + char *titletexts[MAXCOLOUR+1]; time_t now; }; @@ -36,9 +28,16 @@ static void vhtprintf(struct html *ctx, char *fmt, va_list ap) { va_list ap2; int size, size2; + char testbuf[2]; va_copy(ap2, ap); - size = vsnprintf(NULL, 0, fmt, ap2); + /* + * Some C libraries (Solaris, I'm looking at you) don't like + * an output buffer size of zero in vsnprintf, but will return + * sensible values given any non-zero buffer size. Hence, we + * use testbuf to gauge the length of the string. + */ + size = vsnprintf(testbuf, 1, fmt, ap2); va_end(ap2); if (ctx->buflen + size >= ctx->bufsize) { @@ -173,13 +172,10 @@ static void get_indices(const void *t, char *path, path[pathlen-1] = c2; } -static unsigned long long fetch_size(const void *t, char *path, +static unsigned long long fetch_size(const void *t, + unsigned long xi1, unsigned long xi2, unsigned long long atime) { - unsigned long xi1, xi2; - - get_indices(t, path, &xi1, &xi2); - return index_query(t, xi2, atime) - index_query(t, xi1, atime); } @@ -215,7 +211,6 @@ static void begin_colour_bar(struct html *ctx) static void add_to_colour_bar(struct html *ctx, int colour, int pixels) { int r, g, b; - char buf[80]; if (colour >= 0 && colour < 256) /* red -> yellow fade */ r = 255, g = colour, b = 0; @@ -224,26 +219,12 @@ static void add_to_colour_bar(struct html *ctx, int colour, int pixels) else /* background grey */ r = g = b = 240; - if (colour < 0) { - /* no title text here */ - } else if (colour == 0) { - strcpy(buf, "< "); - round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0); - } else if (colour == MAXCOLOUR) { - strcpy(buf, "> "); - round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0); - } else { - unsigned long long midrange = - (ctx->thresholds[colour] + ctx->thresholds[colour+1]) / 2; - round_and_format_age(ctx, midrange, buf, 0); - } - if (pixels > 0) { htprintf(ctx, "= 0) - htprintf(ctx, " title=\"%s\"", buf); + htprintf(ctx, " title=\"%s\"", ctx->titletexts[colour]); htprintf(ctx, ">\n"); } } @@ -302,7 +283,7 @@ static struct vector *make_vector(struct html *ctx, char *path, atime = ULLONG_MAX; else atime = ctx->thresholds[i]; - vec->sizes[i] = fetch_size(ctx->t, path, atime); + vec->sizes[i] = fetch_size(ctx->t, xi1, xi2, atime); } return vec; @@ -386,6 +367,18 @@ static void write_report_line(struct html *ctx, struct vector *vec) htprintf(ctx, "\n\n"); } +int strcmptrailingpathsep(const char *a, const char *b) +{ + while (*a == *b && *a) + a++, b++; + + if ((*a == pathsep && !a[1] && !*b) || + (*b == pathsep && !b[1] && !*a)) + return 0; + + return (int)(unsigned char)*a - (int)(unsigned char)*b; +} + char *html_query(const void *t, unsigned long index, const struct html_config *cfg) { @@ -461,7 +454,7 @@ char *html_query(const void *t, unsigned long index, *zp = '\0'; index2 = trie_before(t, path); trie_getpath(t, index2, path2); - if (!strcmp(path, path2) && cfg->format) { + if (!strcmptrailingpathsep(path, path2) && cfg->format) { snprintf(href, hreflen, cfg->format, index2); if (!*href) /* special case that we understand */ strcpy(href, "./"); @@ -494,9 +487,26 @@ char *html_query(const void *t, unsigned long index, ctx->oldest = round_and_format_age(ctx, ctx->oldest, agebuf1, 0); ctx->newest = round_and_format_age(ctx, ctx->newest, agebuf2, 0); } - for (i = 0; i < MAXCOLOUR-1; i++) { + for (i = 0; i < MAXCOLOUR; i++) { ctx->thresholds[i] = - ctx->oldest + (ctx->newest - ctx->oldest) * i / MAXCOLOUR; + ctx->oldest + (ctx->newest - ctx->oldest) * i / (MAXCOLOUR-1); + } + for (i = 0; i <= MAXCOLOUR; i++) { + char buf[80]; + + if (i == 0) { + strcpy(buf, "< "); + round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0); + } else if (i == MAXCOLOUR) { + strcpy(buf, "> "); + round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0); + } else { + unsigned long long midrange = + (ctx->thresholds[i-1] + ctx->thresholds[i]) / 2; + round_and_format_age(ctx, midrange, buf, 0); + } + + ctx->titletexts[i] = dupstr(buf); } htprintf(ctx, "

Key to colour coding (mouse over for more detail):\n"); htprintf(ctx, "

totalsize = fetch_size(t, path, ULLONG_MAX); + get_indices(t, path, &xi1, &xi2); + ctx->totalsize = fetch_size(t, xi1, xi2, ULLONG_MAX); /* * Generate a report line for the whole subdirectory.