commit a551445fba3bcaad5d8aa3f74f0e1da22599a2aa
parent ee10f83391cea10260c7632eca3f8d1beb918741
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 27 Jun 2017 21:37:49 +0200
scandir: sort directories and show filetype
sort in order: directory or non-directory, filename (case-sensitive).
show filetypes after filename:
- / for directory
- @ for symlink
- | for pipe
- = for socket
- etc
Diffstat:
M | quark.c | | | 30 | +++++++++++++++++++++++++++--- |
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/quark.c b/quark.c
@@ -313,6 +313,30 @@ getrequest(int fd, struct request *r)
return 0;
}
+static int
+compareent(const struct dirent **d1, const struct dirent **d2)
+{
+ int v;
+
+ v = ((*d2)->d_type == DT_DIR ? 1 : -1) -
+ ((*d1)->d_type == DT_DIR ? 1 : -1);
+ if (v)
+ return v;
+ return strcmp((*d1)->d_name, (*d2)->d_name);
+}
+
+static char *
+filetype(int t)
+{
+ switch (t) {
+ case DT_FIFO: return "|";
+ case DT_DIR: return "/";
+ case DT_LNK: return "@";
+ case DT_SOCK: return "=";
+ }
+ return "";
+}
+
static enum status
senddir(int fd, char *name, struct request *r)
{
@@ -322,7 +346,7 @@ senddir(int fd, char *name, struct request *r)
static char t[TIMESTAMP_LEN];
/* read directory */
- if ((dirlen = scandir(name, &e, NULL, alphasort)) < 0) {
+ if ((dirlen = scandir(name, &e, NULL, compareent)) < 0) {
return sendstatus(fd, S_FORBIDDEN);
}
@@ -355,8 +379,8 @@ senddir(int fd, char *name, struct request *r)
}
/* entry line */
- if (dprintf(fd, "<br />\n\t\t<a href=\"%s\">%s</a>",
- e[i]->d_name, e[i]->d_name) < 0) {
+ if (dprintf(fd, "<br />\n\t\t<a href=\"%s\">%s%s</a>",
+ e[i]->d_name, e[i]->d_name, filetype(e[i]->d_type)) < 0) {
return S_REQUEST_TIMEOUT;
}
}