Patch by W. Michael Petullo to use autotools for strncpy()
[infodrom/cgilib] / cgi.c
diff --git a/cgi.c b/cgi.c
index 59546df..5782d5b 100644 (file)
--- a/cgi.c
+++ b/cgi.c
 #include <cgi.h>
 #include "aux.h"
 
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n);
+#endif
+
 char *cgiHeaderString = NULL;
 char *cgiType = NULL;
 
@@ -663,7 +667,23 @@ s_cgi *cgiInit()
     if (res)
        res->cookies = cgiReadCookies ();
     else
-       return NULL;
+       {
+           /* In some cases, we might have no other CGI results.
+              But we still have cookies! */
+           s_cookie **cookies;
+           cookies = cgiReadCookies();
+           if (cookies) {
+               /* We need to create a s_cgi structure. */
+               if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
+                   return NULL;
+               res->vars = NULL;
+               res->cookies = cookies;
+               res->files = NULL;
+               
+           } else {
+               return NULL;
+           }
+       }
 
     if (!res->vars && !res->cookies && !res->files) {
        free (res);
@@ -841,6 +861,18 @@ void cgiFree (s_cgi *parms)
     }
 }
 
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n) {
+    char *fnval;
+
+    if ((fnval = (char *)malloc (n + 1)) == NULL)
+       return NULL;
+    strncpy(fnval, s, n);
+    fnval[n] = '\0';
+    return fnval;
+}
+#endif
+
 /*
  * Local variables:
  *  c-indent-level: 4