From 44aa54e35beafb479f298c031880097785fb362e Mon Sep 17 00:00:00 2001 From: Joey Schulze Date: Sat, 14 Feb 1998 22:25:00 +0000 Subject: [PATCH 1/1] Import of IN-GS source --- cgi.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cgi.h | 40 ++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 cgi.c create mode 100644 cgi.h diff --git a/cgi.c b/cgi.c new file mode 100644 index 0000000..5bdb2f1 --- /dev/null +++ b/cgi.c @@ -0,0 +1,97 @@ +/* + cgi.c - Ein paar Routinen fuer die Programmierung von CGI-Programmen + Copyright (c) 1996 Martin Schulze + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include "cgi.h" + +c_var *cgiParseVar (char *binding) +{ + c_var *result; + char *ip; + + if ((ip = (char *)index(binding, '=')) == NULL) + return NULL; + if (((result = (c_var *)malloc(sizeof(c_var))) == NULL) + ||((result->name = (char *)malloc(ip - binding +1)) == NULL) + ||((result->value = (char *)malloc(strlen(ip))) == NULL)) + return NULL; + + bzero(result->name,sizeof(result->name)); + bzero(result->value,sizeof(result->value)); + strncpy (result->name, binding, ip++ - binding); + strcpy (result->value, ip); + + return result; +} + +c_var **cgiParseInput () +{ + int length; + char *line; + char frag[50]; + int numargs; + char *cp, *ip; + c_var **result; + int i; + + if (!getenv("CONTENT_LENGTH")) + return NULL; + length = atoi(getenv("CONTENT_LENGTH")); + if ((line = (char *)malloc (length+2)) == NULL) + return NULL; + + fgets(line, length+1, stdin); + + /* line contains all values stored like okz=0923&plz=23 */ + for (numargs=1,cp=line; *cp; cp++) + if (*cp == '&') numargs++; + if ((result = (c_var **)malloc((numargs * sizeof(c_var *))+1)) == NULL) + return NULL; + + cp = line; + i=0; + while ((ip = (char *)index(cp, '&')) != NULL) { + bzero (frag, sizeof(frag)); + strncpy (frag, cp, ip - cp); + result[i++] = cgiParseVar(frag); + cp = ++ip; + } + result[i++] = cgiParseVar(cp); + result[i] = NULL; + +/* + for (i=0;result[i]; i++) + printf ("%s=%s
\n", result[i]->name,result[i]->value); +*/ + + return result; +} + +char *cgiGetValue(c_var **parms, const char *var) +{ + int i; + + if (parms) + for (i=0;parms[i]; i++) + if (!strcmp(var,parms[i]->name)) + return parms[i]->value; + return NULL; +} diff --git a/cgi.h b/cgi.h new file mode 100644 index 0000000..34b5751 --- /dev/null +++ b/cgi.h @@ -0,0 +1,40 @@ +/* + cgi.h - Ein paar Routinen fuer die Programmierung von CGI-Programmen + Copyright (c) 1996 Martin Schulze + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef _CGI_H_ +#define _CGI_H_ + +typedef struct var_s { + char *name, + *value; +} c_var; + +/* + cgiParseInput + liest die Variablen eines POST-Aufrufes und gibt sie zurueck. + */ +c_var **cgiParseInput (); + +/* + cgiGetValue + gibt den Wert der Variablen zurueck oder NULL. + */ +char *cgiGetValue(c_var **parms, const char *var); + +#endif /* _CGI_H_ */ -- 2.20.1