Update Debian Vcs-* fields to point to git repository
[onak.git] / getcgi.c
index 47a94b294090c9a21da443247168aa706a72030a..203584149b1e38320b397916a7ea161aae7d6162 100644 (file)
--- a/getcgi.c
+++ b/getcgi.c
@@ -1,14 +1,28 @@
 /*
  * getcgivars.c - routine to read CGI input variables into an array.
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002 Jonathan McDowell <noodles@earth.li>
  *
  * The x2c() and unescape_url() routines were lifted directly
  * from NCSA's sample program util.c, packaged with their HTTPD.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include <stdio.h>
 #include <string.h>
+#include <strings.h>
 #include <stdlib.h>
 
 #include "getcgi.h"
@@ -25,7 +39,7 @@ char *txt2html(const char *string)
        char *ptr = NULL;
        char *nextptr = NULL;
 
-       buf[0] = 0;
+       memset(buf, 0, 1024);
 
        ptr = strchr(string, '<');
        if (ptr != NULL) {
@@ -57,8 +71,42 @@ char *txt2html(const char *string)
        return buf;
 }
 
+/*
+ *     start_html - Start HTML output.
+ *     @title: The title for the HTML.
+ *
+ *     Takes a title string and starts HTML output, including the
+ *     Content-Type header all the way up to <BODY>.
+ */
+void start_html(const char *title)
+{
+       puts("Content-Type: text/html; charset=UTF-8\n");
+       puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
+       puts("<HTML>");
+       puts("<HEAD>");
+       printf("<TITLE>%s</TITLE>\n", title);
+       puts("</HEAD>");
+       puts("<BODY>");
+
+       return;
+}
+
+/*
+ *     end_html - End HTML output.
+ *
+ *     Ends HTML output - closes the BODY and HTML tags.
+ */
+void end_html(void)
+{
+       puts("</BODY>");
+       puts("</HTML>");
+
+       return;
+}
+
+
 /* Convert a two-char hex string into the char it represents */
-char x2c(char *what) 
+char x2c(const char *what) 
 {
        register char digit;
 
@@ -94,25 +142,26 @@ char **getcgivars(int argc, char *argv[])
        int i;
        char *request_method;
        int content_length, paircount;
-       char *cgiinput;
-       char **cgivars;
-       char **pairlist;
+       char *cgiinput = NULL;
+       char **cgivars = NULL;
+       char **pairlist = NULL;
        char *nvpair,*eqpos;
 
        /* Depending on the request method, read all CGI input into cgiinput */
        /* (really should produce HTML error messages, instead of exit()ing) */
 
-       request_method=getenv("REQUEST_METHOD");
+       request_method = getenv("REQUEST_METHOD");
        
        if (request_method == NULL) {
                if (argc > 1) {
-                       cgiinput = argv[1];
+                       cgiinput = strdup(argv[1]);
                } else {
                        return NULL;
                }
        } else if (strlen(request_method)==0) {
                return NULL;
-       } else if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) {
+       } else if (!strcmp(request_method, "GET") ||
+                       !strcmp(request_method, "HEAD")) {
                cgiinput=strdup(getenv("QUERY_STRING"));
        } else if (!strcmp(request_method, "POST")) {
                if (getenv("CONTENT_TYPE") != NULL &&
@@ -123,12 +172,14 @@ char **getcgivars(int argc, char *argv[])
                }
                
                if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
-                       printf("getcgivars(): No Content-Length was sent with the POST request.\n");
+                       printf("getcgivars(): No Content-Length was sent with"
+                                       " the POST request.\n");
                        exit(1);
                }
                
                if (!(cgiinput= (char *) malloc(content_length+1))) {
-                       printf("getcgivars(): Could not malloc for cgiinput.\n");
+                       printf("getcgivars(): Could not malloc for "
+                                       "cgiinput.\n");
                        exit(1);
                }
                
@@ -154,7 +205,10 @@ char **getcgivars(int argc, char *argv[])
        nvpair=strtok(cgiinput, "&");
        while (nvpair) {
                pairlist[paircount++]= strdup(nvpair) ;
-               if (!(paircount%256)) pairlist=(char **) realloc(pairlist,(paircount+256)*sizeof(char **));
+               if (!(paircount%256)) {
+                       pairlist=(char **) realloc(pairlist,
+                                       (paircount+256)*sizeof(char **));
+               }
                nvpair=strtok(NULL, "&") ;
        }
 
@@ -184,3 +238,27 @@ char **getcgivars(int argc, char *argv[])
        /* Return the list of name-value strings */
        return cgivars;
 }
+
+
+/**
+ *     cleanupcgi - free the memory allocated for our CGI parameters.
+ *     @cgivars: The CGI parameter list to free.
+ *
+ *     Frees up the elements of the CGI parameter array and then frees the
+ *     array.
+ */
+void cleanupcgi(char **cgivars)
+{
+       int i;
+
+       if (cgivars != NULL) {
+               for (i = 0; cgivars[i] != NULL; i++) {
+                       free(cgivars[i]);
+                       cgivars[i] = NULL;
+               }
+               free(cgivars);
+               cgivars = NULL;
+       }
+
+       return;
+}