


#include <stdio.h>
#include <stdlib.h>

#define MAX_ENTRIES 10000

typedef struct {
    char *name;
    char *val;
} entry;

char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);


main(int argc, char *argv[]) {
    entry entries[MAX_ENTRIES];
    register int x,m=0;
    int cl;
		FILE *fp;

    printf("Content-type: text/html%c%c",10,10);

    if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
        printf("This script should be referenced with a METHOD of POST.\n");
        printf("If you don't understand this, see this ");
        printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
        exit(1);
    }
    if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
        printf("This script can only be used to decode form results. \n");
        exit(1);
    }
    cl = atoi(getenv("CONTENT_LENGTH"));

    for(x=0;cl && (!feof(stdin));x++) {
        m=x;
        entries[x].val = fmakeword(stdin,'&',&cl);
        plustospace(entries[x].val);
        unescape_url(entries[x].val);
        entries[x].name = makeword(entries[x].val,'=');
    }
		m++;

	if (fp = fopen("/home/vinz/formdata.dat","a"))
		{
		fprintf(fp,"----%c",10);
		for(x=0;x<m;x++)
			{
			fprintf(fp,"** %s:%c%s%c",entries[x].name,10,entries[x].val,10);
			}

		fclose(fp);

		printf("<H1>Data Received Successfully</H1>");

		}
	else
		{
		printf("<H1>Error Opening Data File</H1>");
		printf("Please e-mail <ADDRESS>dylan@takoyaki.demon.co.uk</ADDRESS>%c",10);
		printf("or <ADDRESS>vinz@pclsp2.kuicr.kyoto-u.ac.jp</ADDRESS>%c",10);
		}

		printf("Click <A HREF=\"index.html\">here</A> to return to Kansai-WWW index.<p>%c",10);

    printf("</ul>%c",10);
}
