improvements: - geomyidae - A small C-based gopherd.
Log
Files
Refs
README
LICENSE
---
commit c98811c741255305b3fbff36a7b06bfb263d3ebc
parent 9a2203506973a803e74ffa80a27f2bf1919b68cc
Author: Hiltjo Posthuma 
Date:   Sat, 10 Jun 2017 15:49:54 +0200

improvements:

- check all memory allocations, rename them to the common used names:
  xmalloc, xrealloc, xstrdup.
- show an error when a CGI program using execl() fails in some way.
- minor style and remove an unused variable (len = -1).

documentation improvements:
- document root directory must be absolute.
- typo fix

Signed-off-by: Christoph Lohmann <20h@r-36.net>

Diffstat:
  CGI                                 |       2 +-
  geomyidae.8                         |       3 ++-
  handlr.c                            |      14 ++++++++------
  ind.c                               |      73 +++++++++++++++++++------------
  ind.h                               |       4 +++-
  main.c                              |       4 ++--

6 files changed, 62 insertions(+), 38 deletions(-)
---
diff --git a/CGI b/CGI
@@ -51,7 +51,7 @@ is:
         -> $host = server host
         -> $port = server port
 
-If borth ways of input are combined, the variables are set as following:
+If both ways of input are combined, the variables are set as following:
 
         C: /test.cgi?hello=world        searchterm        (Beware! A Tab!)
         -> $search = »searchterm«
diff --git a/geomyidae.8 b/geomyidae.8
@@ -108,7 +108,8 @@ Loglevels:
 .Ed
 .
 .It Fl b Ar base
-Root directory to serve (default: /var/gopher)
+Root directory to serve (default: /var/gopher).
+This directory should be specified as an absolute path.
 .
 .It Fl p Ar port
 Port geomyidae should listen on (default: 70)
diff --git a/handlr.c b/handlr.c
@@ -32,12 +32,12 @@ handledir(int sock, char *path, char *port, char *base, char *args,
         USED(args);
         USED(sear);
 
-        pa = gstrdup(path);
+        pa = xstrdup(path);
         e = pa + strlen(pa) - 1;
         if(e[0] == '/')
                 *e = '\0';
 
-        par = gstrdup(pa);
+        par = xstrdup(pa);
         b = strrchr(par + strlen(base), '/');
         if(b != nil) {
                 *b = '\0';
@@ -107,7 +107,6 @@ handlebin(int sock, char *file, char *port, char *base, char *args,
         char sendb[1024];
         int len, fd, sent;
 
-        len = -1;
         USED(port);
         USED(base);
         USED(args);
@@ -137,7 +136,7 @@ handlecgi(int sock, char *file, char *port, char *base, char *args,
         USED(base);
         USED(port);
 
-        path = gstrdup(file);
+        path = xstrdup(file);
         p = strrchr(path, '/');
         if (p != nil)
                 p[1] = '\0';
@@ -165,7 +164,10 @@ handlecgi(int sock, char *file, char *port, char *base, char *args,
                                 break;
                 }
 
-                execl(file, p, sear, args, ohost, port, (char *)nil);
+                if (execl(file, p, sear, args, ohost, port, (char *)nil) == -1) {
+                        perror(NULL);
+                        _exit(1);
+                }
         case -1:
                 break;
         default:
@@ -189,7 +191,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
         if(pipe(outpipe) < 0)
                 return;
 
-        path = gstrdup(file);
+        path = xstrdup(file);
         p = strrchr(path, '/');
         if (p != nil)
                 p[1] = '\0';
diff --git a/ind.c b/ind.c
@@ -38,6 +38,43 @@ filetype type[] = {
         {nil, nil, nil},
 };
 
+void *
+xmalloc(size_t size)
+{
+        void *p;
+
+        if (!(p = malloc(size))) {
+                perror("malloc");
+                exit(1);
+        }
+
+        return p;
+}
+
+void *
+xrealloc(void *ptr, size_t size)
+{
+        if (!(ptr = realloc(ptr, size))) {
+                perror("realloc");
+                exit(1);
+        }
+
+        return ptr;
+}
+
+char *
+xstrdup(const char *str)
+{
+        char *ret;
+
+        if (!(ret = strdup(str))) {
+                perror("strdup");
+                exit(1);
+        }
+
+        return ret;
+}
+
 filetype *
 gettype(char *filename)
 {
@@ -61,11 +98,7 @@ gmallocz(int l, int d)
 {
         char *ret;
 
-        ret = malloc(l);
-        if(ret == nil) {
-                perror("malloc");
-                exit(1);
-        }
+        ret = xmalloc(l);
 
         if(d)
                 memset(ret, 0, l);
@@ -74,20 +107,6 @@ gmallocz(int l, int d)
 }
 
 char *
-gstrdup(char *str)
-{
-        char *ret;
-
-        ret = strdup(str);
-        if(ret == nil) {
-                perror("strdup");
-                exit(1);
-        }
-
-        return ret;
-}
-
-char *
 readln(int fd)
 {
         char *ret;
@@ -95,9 +114,9 @@ readln(int fd)
 
         len = 1;
 
-        ret = malloc(2);
+        ret = xmalloc(2);
         while(read(fd, &ret[len - 1], 1) > 0 && ret[len - 1] != '\n')
-                ret = realloc(ret, ++len + 1);
+                ret = xrealloc(ret, ++len + 1);
         if(ret[len - 1] != '\n') {
                 free(ret);
                 return nil;
@@ -148,7 +167,7 @@ addelem(Elems *e, char *s)
         slen = strlen(s) + 1;
 
         e->num++;
-        e->e = realloc(e->e, sizeof(char *) * e->num);
+        e->e = xrealloc(e->e, sizeof(char *) * e->num);
         e->e[e->num - 1] = gmallocz(slen, 2);
         strncpy(e->e[e->num - 1], s, slen - 1);
 
@@ -201,7 +220,7 @@ addindexs(Indexs *idx, Elems *el)
 {
 
         idx->num++;
-        idx->n = realloc(idx->n, sizeof(Elems) * idx->num);
+        idx->n = xrealloc(idx->n, sizeof(Elems) * idx->num);
         idx->n[idx->num - 1] = el;
 
         return;
@@ -246,11 +265,11 @@ printelem(int fd, Elems *el, char *addr, char *port)
 
         if(!strncmp(el->e[3], "server", 6)) {
                 free(el->e[3]);
-                el->e[3] = gstrdup(addr);
+                el->e[3] = xstrdup(addr);
         }
         if(!strncmp(el->e[4], "port", 4)) {
                 free(el->e[4]);
-                el->e[4] = gstrdup(port);
+                el->e[4] = xstrdup(port);
         }
         tprintf(fd, "%.1s%s\t%s\t%s\t%s\r\n", el->e[0], el->e[1], el->e[2],
                         el->e[3], el->e[4]);
@@ -332,11 +351,11 @@ reverselookup(char *host)
                 client = gethostbyaddr((const void *)&hoststr,
                                 sizeof(hoststr), AF_INET);
                 if(client != NULL)
-                        rethost = strdup(client->h_name);
+                        rethost = xstrdup(client->h_name);
         }
 
         if(rethost == NULL)
-                rethost = gstrdup(host);
+                rethost = xstrdup(host);
 
         return rethost;
 }
diff --git a/ind.h b/ind.h
@@ -32,7 +32,9 @@ struct filetype {
 
 filetype *gettype(char *filename);
 void *gmallocz(int l, int d);
-char *gstrdup(char *str);
+void *xmalloc(size_t);
+void *xrealloc(void *, size_t);
+char *xstrdup(const char *str);
 Indexs *scanfile(char *fname);
 Elems *getadv(char *str);
 void printelem(int fd, Elems *el, char *addr, char *port);
diff --git a/main.c b/main.c
@@ -32,7 +32,7 @@ enum {
         FILES        = 1,
         DIRS        = 2,
         HTTP        = 4,
-        ERRORS        = 8,
+        ERRORS        = 8
 };
 
 int glfd = -1;
@@ -381,7 +381,7 @@ main(int argc, char *argv[])
                         return 1;
                 }
         } else {
-                ohost = gstrdup(ohost);
+                ohost = xstrdup(ohost);
         }
 
         if(group != nil) {