[hackers] [quark] scandir: sort directories and show filetype || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Tue, 27 Jun 2017 23:31:28 +0200 (CEST)

commit a551445fba3bcaad5d8aa3f74f0e1da22599a2aa
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
AuthorDate: Tue Jun 27 21:37:49 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Jun 27 23:31:08 2017 +0200

    scandir: sort directories and show filetype
    
    sort in order: directory or non-directory, filename (case-sensitive).
    show filetypes after filename:
    
    - / for directory
    - _AT_ for symlink
    - | for pipe
    - = for socket
    - etc

diff --git a/quark.c b/quark.c
index 8d151a1..f5fc6b0 100644
--- a/quark.c
+++ b/quark.c
_AT_@ -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 "_AT_";
+ case DT_SOCK: return "=";
+ }
+ return "";
+}
+
 static enum status
 senddir(int fd, char *name, struct request *r)
 {
_AT_@ -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);
         }
 
_AT_@ -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;
                         }
                 }
Received on Tue Jun 27 2017 - 23:31:28 CEST

This archive was generated by hypermail 2.3.0 : Tue Jun 27 2017 - 23:36:37 CEST