2 * html.c: implementation of html.h.
15 size_t buflen
, bufsize
;
17 unsigned long long totalsize
, oldest
, newest
;
21 const char *uriformat
;
22 unsigned long long thresholds
[MAXCOLOUR
];
23 char *titletexts
[MAXCOLOUR
+1];
27 static void vhtprintf(struct html
*ctx
, const char *fmt
, va_list ap
)
35 * Some C libraries (Solaris, I'm looking at you) don't like
36 * an output buffer size of zero in vsnprintf, but will return
37 * sensible values given any non-zero buffer size. Hence, we
38 * use testbuf to gauge the length of the string.
40 size
= vsnprintf(testbuf
, 1, fmt
, ap2
);
43 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
44 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
45 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
47 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
49 assert(size
== size2
);
53 static void htprintf(struct html
*ctx
, const char *fmt
, ...)
57 vhtprintf(ctx
, fmt
, ap
);
61 static unsigned long long round_and_format_age(struct html
*ctx
,
62 unsigned long long age
,
63 char *buf
, int direction
)
67 unsigned long long ret
, newret
;
70 static const int minutes
[] = { 5, 10, 15, 30, 45 };
72 tm
= *localtime(&ctx
->now
);
73 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
78 for (i
= 0; i
< lenof(minutes
); i
++) {
79 newret
= ctx
->now
- minutes
[i
] * 60;
80 sprintf(newbuf
, "%d minutes", minutes
[i
]);
87 for (i
= 1; i
< 24; i
++) {
88 newret
= ctx
->now
- i
* (60*60);
89 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
96 for (i
= 1; i
< 7; i
++) {
97 newret
= ctx
->now
- i
* (24*60*60);
98 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
105 for (i
= 1; i
< 4; i
++) {
106 newret
= ctx
->now
- i
* (7*24*60*60);
107 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
114 for (i
= 1; i
< 11; i
++) {
115 tm2
= tm
; /* structure copy */
116 tm2
.tm_year
= (ym
- i
) / 12;
117 tm2
.tm_mon
= (ym
- i
) % 12;
118 newret
= mktime(&tm2
);
119 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
127 tm2
= tm
; /* structure copy */
128 tm2
.tm_year
= (ym
- i
*12) / 12;
129 tm2
.tm_mon
= (ym
- i
*12) % 12;
130 newret
= mktime(&tm2
);
131 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
141 * Round toward newest, i.e. use the existing (buf,ret).
143 } else if (direction
< 0) {
145 * Round toward oldest, i.e. use (newbuf,newret);
153 if (ret
- age
> age
- newret
) {
161 static void get_indices(const void *t
, char *path
,
162 unsigned long *xi1
, unsigned long *xi2
)
164 size_t pathlen
= strlen(path
);
165 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
167 *xi1
= trie_before(t
, path
);
168 make_successor(path
);
169 *xi2
= trie_before(t
, path
);
172 path
[pathlen
-1] = c2
;
175 static unsigned long long fetch_size(const void *t
,
176 unsigned long xi1
, unsigned long xi2
,
177 unsigned long long atime
)
179 if (xi2
- xi1
== 1) {
181 * We are querying an individual file, so we should not
182 * depend on the index entries either side of the node,
183 * since they almost certainly don't both exist. Instead,
184 * just look up the file's size and atime in the main trie.
186 const struct trie_file
*f
= trie_getfile(t
, xi1
);
187 if (f
->atime
< atime
)
192 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
196 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
198 while (n
> 0 && *s
) {
199 unsigned char c
= (unsigned char)*s
++;
202 htprintf(ctx
, "&");
204 htprintf(ctx
, "<");
206 htprintf(ctx
, ">");
207 else if (c
>= ' ' && c
< '\177')
208 htprintf(ctx
, "%c", c
);
210 if (italics
) htprintf(ctx
, "<i>");
211 htprintf(ctx
, "[%02x]", c
);
212 if (italics
) htprintf(ctx
, "</i>");
219 static void begin_colour_bar(struct html
*ctx
)
221 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
222 " style=\"border:0\">\n<tr>\n");
225 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
229 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
230 r
= 255, g
= colour
, b
= 0;
231 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
232 r
= 511 - colour
, g
= 255, b
= 0;
233 else /* background grey */
237 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
238 "background-color:#%02x%02x%02x\"",
241 htprintf(ctx
, " title=\"%s\"", ctx
->titletexts
[colour
]);
242 htprintf(ctx
, "></td>\n");
246 static void end_colour_bar(struct html
*ctx
)
248 htprintf(ctx
, "</tr>\n</table>\n");
252 int want_href
, essential
;
254 int literal
; /* should the name be formatted in fixed-pitch? */
256 unsigned long long sizes
[MAXCOLOUR
+1];
259 int vec_compare(const void *av
, const void *bv
)
261 const struct vector
*a
= *(const struct vector
**)av
;
262 const struct vector
*b
= *(const struct vector
**)bv
;
264 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
266 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
268 else if (a
->want_href
< b
->want_href
)
270 else if (a
->want_href
> b
->want_href
)
272 else if (a
->want_href
)
273 return strcmp(a
->name
, b
->name
);
274 else if (a
->index
< b
->index
)
276 else if (a
->index
> b
->index
)
278 else if (a
->essential
< b
->essential
)
280 else if (a
->essential
> b
->essential
)
285 static struct vector
*make_vector(struct html
*ctx
, char *path
,
286 int want_href
, int essential
,
287 char *name
, int literal
)
289 unsigned long xi1
, xi2
;
290 struct vector
*vec
= snew(struct vector
);
293 vec
->want_href
= want_href
;
294 vec
->essential
= essential
;
295 vec
->name
= name ?
dupstr(name
) : NULL
;
296 vec
->literal
= literal
;
298 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
302 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
303 unsigned long long atime
;
307 atime
= ctx
->thresholds
[i
];
308 vec
->sizes
[i
] = fetch_size(ctx
->t
, xi1
, xi2
, atime
);
314 static void print_heading(struct html
*ctx
, const char *title
)
316 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
317 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
320 static void compute_display_size(unsigned long long size
,
321 const char **fmt
, double *display_size
)
323 static const char *const fmts
[] = {
324 "%g b", "%g Kb", "%#.1f Mb", "%#.1f Gb", "%#.1f Tb",
325 "%#.1f Pb", "%#.1f Eb", "%#.1f Zb", "%#.1f Yb"
328 unsigned long long tmpsize
;
333 while (tmpsize
>= 1024 && shift
< lenof(fmts
)-1) {
335 denominator
*= 1024.0;
338 *display_size
= size
/ denominator
;
342 struct format_option
{
343 const char *prefix
, *suffix
; /* may include '%%' */
344 int prefixlen
, suffixlen
; /* does not count '%%' */
345 char fmttype
; /* 0 for none, or 'n' or 'p' */
346 int translate_pathsep
; /* pathsep rendered as '/'? */
347 int shorten_path
; /* omit common prefix? */
351 * Gets the next format option from a format string. Advances '*fmt'
352 * past it, or sets it to NULL if nothing is left.
354 struct format_option
get_format_option(const char **fmt
)
356 struct format_option ret
;
359 * Scan for prefix of format.
366 * No formatting directive, and this is the last option.
373 } else if (**fmt
== '%') {
374 if ((*fmt
)[1] == '%') {
375 (*fmt
) += 2; /* just advance one extra */
377 } else if ((*fmt
)[1] == '|') {
379 * No formatting directive.
384 (*fmt
) += 2; /* advance to start of next option */
390 (*fmt
)++; /* normal character */
396 * Interpret formatting directive with flags.
399 ret
.translate_pathsep
= ret
.shorten_path
= 1;
404 ret
.translate_pathsep
= 0;
405 } else if (c
== '-') {
406 ret
.shorten_path
= 0;
408 assert(c
== 'n' || c
== 'p');
422 * This is the last option.
426 } else if (**fmt
!= '%') {
427 (*fmt
)++; /* normal character */
430 if ((*fmt
)[1] == '%') {
431 (*fmt
) += 2; /* just advance one extra */
434 assert((*fmt
)[1] == '|');
435 (*fmt
) += 2; /* advance to start of next option */
442 char *format_string(const char *fmt
, unsigned long index
, const void *t
)
445 char *ret
= NULL
, *p
= NULL
;
446 char *path
= NULL
, *q
= NULL
;
447 char pathsep
= trie_pathsep(t
);
448 int maxpathlen
= trie_maxpathlen(t
);
452 struct format_option opt
= get_format_option(&fmt
);
453 if (index
&& !opt
.fmttype
)
454 continue; /* option is only good for the root, which this isn't */
456 maxlen
= opt
.prefixlen
+ opt
.suffixlen
+ 1;
457 switch (opt
.fmttype
) {
459 maxlen
+= 40; /* generous length for an integer */
462 maxlen
+= 3*maxpathlen
; /* might have to escape everything */
465 ret
= snewn(maxlen
, char);
467 while (opt
.prefixlen
-- > 0) {
468 if ((*p
++ = *opt
.prefix
++) == '%')
471 switch (opt
.fmttype
) {
473 p
+= sprintf(p
, "%lu", index
);
476 path
= snewn(1+trie_maxpathlen(t
), char);
477 if (opt
.shorten_path
) {
478 trie_getpath(t
, 0, path
);
479 q
= path
+ strlen(path
);
480 trie_getpath(t
, index
, path
);
484 trie_getpath(t
, index
, path
);
490 if (c
== pathsep
&& opt
.translate_pathsep
) {
493 } else if (!isalnum((unsigned char)c
) &&
494 ((leading
&& c
=='.') || !strchr("-.@_", c
))) {
495 p
+= sprintf(p
, "=%02X", (unsigned char)c
);
505 while (opt
.suffixlen
-- > 0) {
506 if ((*p
++ = *opt
.suffix
++) == '%')
510 assert(p
- ret
< maxlen
);
513 assert(!"Getting here implies an incomplete set of formats");
516 char *html_format_path(const void *t
, const struct html_config
*cfg
,
519 return format_string(cfg
->uriformat
, index
, t
);
522 int html_parse_path(const void *t
, const char *path
,
523 const struct html_config
*cfg
, unsigned long *index
)
525 int len
= strlen(path
);
529 char pathsep
= trie_pathsep(t
);
530 const char *fmt
= cfg
->uriformat
;
533 struct format_option opt
= get_format_option(&fmt
);
536 * Check prefix and suffix.
538 midlen
= len
- opt
.prefixlen
- opt
.suffixlen
;
540 continue; /* prefix and suffix don't even fit */
543 while (opt
.prefixlen
> 0) {
544 char c
= *opt
.prefix
++;
552 if (opt
.prefixlen
> 0)
553 continue; /* prefix didn't match */
555 q
= path
+ len
- opt
.suffixlen
;
556 while (opt
.suffixlen
> 0) {
557 char c
= *opt
.suffix
++;
565 if (opt
.suffixlen
> 0)
566 continue; /* suffix didn't match */
569 * Check the data in between. p points at it, and it's midlen
572 if (opt
.fmttype
== '\0') {
575 * Successful match against a root format.
580 } else if (opt
.fmttype
== 'n') {
583 if (*p
>= '0' && *p
<= '9')
584 *index
= *index
* 10 + (*p
- '0');
592 * Successful match against a numeric format.
597 assert(opt
.fmttype
== 'p');
599 int maxoutlen
= trie_maxpathlen(t
) + 1;
600 int maxinlen
= midlen
+ 1;
601 char triepath
[maxinlen
+maxoutlen
];
603 if (opt
.shorten_path
) {
604 trie_getpath(t
, 0, triepath
);
605 r
= triepath
+ strlen(triepath
);
606 if (r
> triepath
&& r
[-1] != pathsep
)
613 if (*p
== '/' && opt
.translate_pathsep
) {
617 } else if (*p
== '=') {
619 * We intentionally do not check whether the
620 * escaped character _should_ have been escaped
621 * according to the rules in html_format_path.
623 * All clients of this parsing function, after a
624 * successful parse, call html_format_path to find
625 * the canonical URI for the same index and return
626 * an HTTP redirect if the provided URI was not
627 * exactly equal to that canonical form. This is
628 * critical when the correction involves adding or
629 * removing a trailing slash (because then
630 * relative hrefs on the generated page can be
631 * computed with respect to the canonical URI
632 * instead of having to remember what the actual
633 * URI was), but also has the useful effect that
634 * if a user attempts to type in (guess) a URI by
635 * hand they don't have to remember the escaping
636 * rules - as long as they type _something_ that
637 * this code can parse into a recognisable
638 * pathname, it will be automatically 301ed into
639 * the canonical form.
642 !isxdigit((unsigned char)p
[1]) ||
643 !isxdigit((unsigned char)p
[2]))
644 break; /* faulty escape encoding */
650 sscanf(x
, "%x", &cval
);
661 continue; /* something went wrong in that loop */
662 assert(r
- triepath
< maxinlen
+maxoutlen
);
665 unsigned long gotidx
= trie_before(t
, triepath
);
666 if (gotidx
>= trie_count(t
))
667 continue; /* index out of range */
668 char retpath
[1+maxoutlen
];
669 trie_getpath(t
, gotidx
, retpath
);
670 if (strcmp(triepath
, retpath
))
671 continue; /* exact path not found in trie */
672 if (!index_has_root(t
, gotidx
))
673 continue; /* path is not a directory */
676 * Successful path-based match.
683 return 0; /* no match from any format option */
686 char *make_href(const char *source
, const char *target
)
689 * We insist that both source and target URIs start with a /, or
690 * else we won't be reliably able to construct relative hrefs
691 * between them (e.g. because we've got a suffix on the end of
692 * some CGI pathname that this function doesn't know the final
695 assert(*source
== '/');
696 assert(*target
== '/');
699 * Find the last / in source. Everything up to but not including
700 * that is the directory to which the output href will be
701 * relative. We enforce by assertion that there must be a /
702 * somewhere in source, or else we can't construct a relative href
705 const char *sourceend
= strrchr(source
, '/');
706 assert(sourceend
!= NULL
);
709 * See how far the target URI agrees with the source one, up to
710 * and including that /.
712 const char *s
= source
, *t
= target
;
713 while (s
<= sourceend
&& *s
== *t
)
717 * We're only interested in agreement of complete path components,
718 * so back off until we're sitting just after a shared /.
720 while (s
> source
&& s
[-1] != '/')
725 * Now we need some number of levels of "../" to get from source
726 * to here, and then we just replicate the rest of 'target'.
729 while (s
<= sourceend
) {
734 int len
= 3*levels
+ strlen(t
);
736 /* One last special case: if target has no tail _and_ we
737 * haven't written out any "../". */
740 char *ret
= snewn(len
+1, char);
742 while (levels
-- > 0) {
752 #define PIXEL_SIZE 600 /* FIXME: configurability? */
753 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
755 unsigned long long size
, asize
, divisor
;
759 const char *unitsfmt
;
762 * A line with literally zero space usage should not be
763 * printed at all if it's a link to a subdirectory (since it
764 * probably means the whole thing was excluded by some
765 * --exclude-path wildcard). If it's [files] or the top-level
766 * line, though, we must always print _something_, and in that
767 * case we must fiddle about to prevent divisions by zero in
770 if (!vec
->sizes
[MAXCOLOUR
] && !vec
->essential
)
772 divisor
= ctx
->totalsize
;
778 * Find the total size of this subdirectory.
780 size
= vec
->sizes
[MAXCOLOUR
];
781 compute_display_size(size
, &unitsfmt
, &display_size
);
782 htprintf(ctx
, "<tr>\n"
783 "<td style=\"padding: 0.2em; text-align: right\">");
784 htprintf(ctx
, unitsfmt
, display_size
);
785 htprintf(ctx
, "</td>\n");
788 * Generate a colour bar.
790 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
791 begin_colour_bar(ctx
);
793 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
794 asize
= vec
->sizes
[i
];
795 newpix
= asize
* PIXEL_SIZE
/ divisor
;
796 add_to_colour_bar(ctx
, i
, newpix
- pix
);
799 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
801 htprintf(ctx
, "</td>\n");
804 * Output size as a percentage of totalsize.
806 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
807 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
810 * Output a subdirectory marker.
812 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
816 if (ctx
->uriformat
&& vec
->want_href
) {
817 char *targeturi
= format_string(ctx
->uriformat
, vec
->index
,
819 char *link
= make_href(ctx
->oururi
, targeturi
);
820 htprintf(ctx
, "<a href=\"%s\">", link
);
826 htprintf(ctx
, "<code>");
827 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
829 htprintf(ctx
, "</code>");
831 htprintf(ctx
, "</a>");
833 htprintf(ctx
, "</td>\n</tr>\n");
836 int strcmptrailingpathsep(const char *a
, const char *b
)
838 while (*a
== *b
&& *a
)
841 if ((*a
== pathsep
&& !a
[1] && !*b
) ||
842 (*b
== pathsep
&& !b
[1] && !*a
))
845 return (int)(unsigned char)*a
- (int)(unsigned char)*b
;
848 char *html_query(const void *t
, unsigned long index
,
849 const struct html_config
*cfg
, int downlink
)
851 struct html actx
, *ctx
= &actx
;
852 char *path
, *path2
, *p
, *q
;
853 char agebuf1
[80], agebuf2
[80];
854 size_t pathlen
, subdirpos
;
855 unsigned long index2
;
857 struct vector
**vecs
;
859 unsigned long xi1
, xi2
, xj1
, xj2
;
861 if (index
>= trie_count(t
))
865 ctx
->buflen
= ctx
->bufsize
= 0;
867 ctx
->uriformat
= cfg
->uriformat
;
868 htprintf(ctx
, "<html>\n");
870 path
= snewn(1+trie_maxpathlen(t
), char);
871 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
872 ctx
->oururi
= format_string(cfg
->uriformat
, index
, t
);
877 htprintf(ctx
, "<head>\n");
878 trie_getpath(t
, index
, path
);
879 htprintf(ctx
, "<title>%s: ", PNAME
);
880 htescape(ctx
, path
, strlen(path
), 0);
881 htprintf(ctx
, "</title>\n");
882 htprintf(ctx
, "</head>\n");
885 * Begin BODY section.
887 htprintf(ctx
, "<body>\n");
888 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
889 " last-access time</h3>\n");
892 * Show the pathname we're centred on, with hyperlinks to
893 * parent directories where available.
895 htprintf(ctx
, "<p align=center>\n<code>");
897 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
902 * See if this path prefix exists in the trie. If so,
903 * generate a hyperlink.
906 if (p
== path
) /* special case for "/" at start */
913 index2
= trie_before(t
, path
);
914 trie_getpath(t
, index2
, path2
);
915 if (!strcmptrailingpathsep(path
, path2
) && cfg
->uriformat
) {
916 char *targeturi
= format_string(cfg
->uriformat
, index2
, t
);
917 char *link
= make_href(ctx
->oururi
, targeturi
);
918 htprintf(ctx
, "<a href=\"%s\">", link
);
924 htescape(ctx
, q
, zp
- q
, 1);
926 htprintf(ctx
, "</a>");
927 htescape(ctx
, zp
, p
- zp
, 1);
930 htescape(ctx
, q
, strlen(q
), 1);
931 htprintf(ctx
, "</code>\n");
934 * Decide on the age limit of our colour coding, establish the
935 * colour thresholds, and write out a key.
937 ctx
->now
= time(NULL
);
939 ctx
->oldest
= index_order_stat(t
, 0.05);
940 ctx
->newest
= index_order_stat(t
, 1.0);
941 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
942 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
944 ctx
->oldest
= cfg
->oldest
;
945 ctx
->newest
= cfg
->newest
;
946 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
947 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
949 for (i
= 0; i
< MAXCOLOUR
; i
++) {
951 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ (MAXCOLOUR
-1);
953 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
957 strcpy(buf
, "< ");
958 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
959 } else if (i
== MAXCOLOUR
) {
960 strcpy(buf
, "> ");
961 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
963 unsigned long long midrange
=
964 (ctx
->thresholds
[i
-1] + ctx
->thresholds
[i
]) / 2;
965 round_and_format_age(ctx
, midrange
, buf
, 0);
968 ctx
->titletexts
[i
] = dupstr(buf
);
970 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
971 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
972 "margin-bottom:1em\">");
973 begin_colour_bar(ctx
);
974 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
975 for (i
= 0; i
< MAXCOLOUR
; i
++)
976 add_to_colour_bar(ctx
, i
, 1);
977 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
981 * Begin the main table.
983 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
986 * Find the total size of our entire subdirectory. We'll use
987 * that as the scale for all the colour bars in this report.
989 get_indices(t
, path
, &xi1
, &xi2
);
990 ctx
->totalsize
= fetch_size(t
, xi1
, xi2
, ULLONG_MAX
);
993 * Generate a report line for the whole subdirectory.
996 vecs
= snewn(vecsize
, struct vector
*);
998 vecs
[0] = make_vector(ctx
, path
, 0, 1, NULL
, 0);
999 print_heading(ctx
, "Overall");
1000 write_report_line(ctx
, vecs
[0]);
1003 * Now generate report lines for all its children, and the
1004 * files contained in it.
1006 print_heading(ctx
, "Subdirectories");
1008 vecs
[0]->name
= dupstr("[files]");
1009 get_indices(t
, path
, &xi1
, &xi2
);
1011 pathlen
= strlen(path
);
1012 subdirpos
= pathlen
+ 1;
1013 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
1016 trie_getpath(t
, xi1
, path2
);
1017 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
1019 if (!cfg
->showfiles
&& xj2
- xj1
<= 1)
1020 continue; /* skip individual files */
1021 if (nvecs
>= vecsize
) {
1022 vecsize
= nvecs
* 3 / 2 + 64;
1023 vecs
= sresize(vecs
, vecsize
, struct vector
*);
1025 assert(strlen(path2
) > pathlen
);
1026 vecs
[nvecs
] = make_vector(ctx
, path2
, downlink
&& (xj2
- xj1
> 1), 0,
1027 path2
+ subdirpos
, 1);
1028 for (i
= 0; i
<= MAXCOLOUR
; i
++)
1029 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
1033 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
1035 for (i
= 0; i
< nvecs
; i
++)
1036 write_report_line(ctx
, vecs
[i
]);
1039 * Close the main table.
1041 htprintf(ctx
, "</table>\n");
1044 * Finish up and tidy up.
1046 htprintf(ctx
, "</body>\n");
1047 htprintf(ctx
, "</html>\n");
1051 for (i
= 0; i
< nvecs
; i
++) {
1052 sfree(vecs
[i
]->name
);
1060 int html_dump(const void *t
, unsigned long index
, unsigned long endindex
,
1061 int maxdepth
, const struct html_config
*cfg
,
1062 const char *pathprefix
)
1065 * Determine the filename for this file.
1067 assert(cfg
->fileformat
!= NULL
);
1068 char *filename
= format_string(cfg
->fileformat
, index
, t
);
1069 char *path
= dupfmt("%s%s", pathprefix
, filename
);
1073 * Create the HTML itself. Don't write out downlinks from our
1076 char *html
= html_query(t
, index
, cfg
, maxdepth
!= 0);
1081 FILE *fp
= fopen(path
, "w");
1083 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
, path
, strerror(errno
));
1086 if (fputs(html
, fp
) < 0) {
1087 fprintf(stderr
, "%s: %s: write: %s\n", PNAME
, path
, strerror(errno
));
1091 if (fclose(fp
) < 0) {
1092 fprintf(stderr
, "%s: %s: fclose: %s\n", PNAME
, path
, strerror(errno
));
1100 if (maxdepth
!= 0) {
1101 unsigned long subindex
, subendindex
;
1102 int newdepth
= (maxdepth
> 0 ? maxdepth
- 1 : maxdepth
);
1103 char rpath
[1+trie_maxpathlen(t
)];
1106 while (index
< endindex
) {
1107 trie_getpath(t
, index
, rpath
);
1108 get_indices(t
, rpath
, &subindex
, &subendindex
);
1109 index
= subendindex
;
1110 if (subendindex
- subindex
> 1) {
1111 if (html_dump(t
, subindex
, subendindex
, newdepth
,