Fixing some dir handling. A path does not need to end in '/'. - geomyidae - A small C-based gopherd.
git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/
Log
Files
Refs
Tags
README
LICENSE
---
commit 7b14b6e74778cf0ddb328cf3e049a302be614b36
parent 5be8d044ed4e832c4c6a6b7f748b81a30695dc39
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed,  4 Jan 2017 21:54:49 +0100

Fixing some dir handling. A path does not need to end in '/'.

Diffstat:
  M handlr.c                            |      67 +++++++++++++++----------------

1 file changed, 33 insertions(+), 34 deletions(-)
---
diff --git a/handlr.c b/handlr.c
@@ -33,46 +33,45 @@ handledir(int sock, char *path, char *port, char *base, char *args,
         USED(sear);
 
         pa = gstrdup(path);
-        e = strrchr(pa, '/');
-        if(e != nil) {
+        e = pa + strlen(pa) - 1;
+        if(e[0] == '/')
                 *e = '\0';
 
-                par = gstrdup(pa);
-                b = strrchr(par + strlen(base), '/');
-                if(b != nil) {
-                        *b = '\0';
-                        tprintf(sock, "1..\t%s\t%s\t%s\r\n",
-                                par + strlen(base), ohost, port);
-                }
-                free(par);
-
-                ndir = scandir(pa, &dirent, 0, alphasort);
-                if(ndir < 0) {
-                        perror("scandir");
-                        free(pa);
-                        return;
-                } else {
-                        for(i = 0; i < ndir; i++) {
-                                if(dirent[i]->d_name[0] == '.') {
-                                        free(dirent[i]);
-                                        continue;
-                                }
-
-                                type = gettype(dirent[i]->d_name);
-                                file = smprintf("%s/%s", pa,
-                                                dirent[i]->d_name);
-                                if(stat(file, &st) >= 0 && S_ISDIR(st.st_mode))
-                                        type = gettype("index.gph");
-                                e = file + strlen(base);
-                                tprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type,
-                                        dirent[i]->d_name, e, ohost, port);
-                                free(file);
+        par = gstrdup(pa);
+        b = strrchr(par + strlen(base), '/');
+        if(b != nil) {
+                *b = '\0';
+                tprintf(sock, "1..\t%s\t%s\t%s\r\n",
+                        par + strlen(base), ohost, port);
+        }
+        free(par);
+
+        ndir = scandir(pa, &dirent, 0, alphasort);
+        if(ndir < 0) {
+                perror("scandir");
+                free(pa);
+                return;
+        } else {
+                for(i = 0; i < ndir; i++) {
+                        if(dirent[i]->d_name[0] == '.') {
                                 free(dirent[i]);
+                                continue;
                         }
-                        free(dirent);
+
+                        type = gettype(dirent[i]->d_name);
+                        file = smprintf("%s/%s", pa,
+                                        dirent[i]->d_name);
+                        if(stat(file, &st) >= 0 && S_ISDIR(st.st_mode))
+                                type = gettype("index.gph");
+                        e = file + strlen(base);
+                        tprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type,
+                                dirent[i]->d_name, e, ohost, port);
+                        free(file);
+                        free(dirent[i]);
                 }
-                tprintf(sock, ".\r\n");
+                free(dirent);
         }
+        tprintf(sock, ".\r\n");
 
         free(pa);
 }