order-directories-by-date.patch - geomyidae - A small C-based gopherd.
HTML git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
DIR LICENSE
---
order-directories-by-date.patch (2037B)
---
1 diff --git a/handlr.c b/handlr.c
2 index 0c230d3..5f5a767 100644
3 --- a/handlr.c
4 +++ b/handlr.c
5 @@ -3,6 +3,7 @@
6 * by 20h
7 */
8
9 +#include <limits.h>
10 #include <unistd.h>
11 #include <memory.h>
12 #include <netdb.h>
13 @@ -21,13 +22,48 @@
14 #include "ind.h"
15 #include "arg.h"
16
17 +char dir_path[PATH_MAX];
18 +int datesort(const struct dirent **, const struct dirent **);
19 +
20 +int
21 +datesort(const struct dirent **a, const struct dirent **b)
22 +{
23 + struct stat sbuf1, sbuf2;
24 + char path1[PATH_MAX], path2[PATH_MAX];
25 + int rv;
26 +
27 + rv = snprintf(path1, sizeof(path1), "%s/%s", dir_path, (*a)->d_name);
28 + if (rv < 0 || (size_t)rv >= sizeof(path1)) {
29 + perror("snprintf");
30 + return 0;
31 + }
32 + rv = snprintf(path2, sizeof(path2), "%s/%s", dir_path, (*b)->d_name);
33 + if (rv < 0 || (size_t)rv >= sizeof(path2)) {
34 + perror("snprintf");
35 + return 0;
36 + }
37 +
38 + if (stat(path1, &sbuf1)) {
39 + perror("stat");
40 + return 0;
41 + }
42 + if (stat(path2, &sbuf2)) {
43 + perror("stat");
44 + return 0;
45 + }
46 +
47 + return sbuf1.st_mtime < sbuf2.st_mtime ? -1 : sbuf1.st_mtime > sbuf2.st_mtime;
48 +}
49 +
50 void
51 handledir(int sock, char *path, char *port, char *base, char *args,
52 char *sear, char *ohost, char *chost, int istls)
53 {
54 + int (*sortorder) (const struct dirent **, const struct dirent **);
55 + char ds[PATH_MAX];
56 char *pa, *file, *e, *par, *b;
57 struct dirent **dirent;
58 - int ndir, i, ret = 0;
59 + int ndir, i, ret = 0, rv;
60 struct stat st;
61 filetype *type;
62
63 @@ -48,7 +84,21 @@ handledir(int sock, char *path, char *port, char *base, char *args,
64 }
65 free(par);
66
67 - ndir = scandir(pa[0] ? pa : ".", &dirent, 0, alphasort);
68 + rv = snprintf(dir_path, sizeof(dir_path), "%s", pa);
69 + if (rv < 0 || (size_t)rv >= sizeof(dir_path)) {
70 + perror("snprintf");
71 + return;
72 + }
73 +
74 + rv = snprintf(ds, sizeof(ds), "%s/.datesort", pa);
75 + if (rv < 0 || (size_t)rv >= sizeof(ds)) {
76 + perror("snprintf");
77 + return;
78 + }
79 +
80 + sortorder = access(ds, F_OK) != -1 ? datesort : alphasort;
81 +
82 + ndir = scandir(pa[0] ? pa : ".", &dirent, 0, sortorder);
83 if (ndir < 0) {
84 perror("scandir");
85 free(pa);