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

#define expiration_date "26-Dec-1998 23:59:59 PST"

typedef struct
{
	char name[128];
	char val[128];
} entry;

void getword(char *word, char *line, char stop);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

void main(int argc, char *argv[])
{
	entry entries[10000];
	register int x,m=0;
	char *query_string;
	char *cookie;
	int user_id;

	query_string = getenv("QUERY_STRING");

	for (x=0; query_string[0] != '\0'; x++)
	{
		m=x;
		getword(entries[x].val,query_string,'&');
		plustospace(entries[x].val);
		unescape_url(entries[x].val);
		getword(entries[x].name,entries[x].val,'=');
	};

	if (strcmp(entries[0].name,"name") != 0)
	{
		printf("Content-type: text/html%c%c",10,10);
		printf("<title>Login Error</title>\n");
		printf("Error...supply a name, please\n");
		exit(0);
	}

/* This assumes, for purposes of testing, four character names */
	user_id = * ((int *) entries[0].val);

	printf("Set-cookie: user=%i; expires=%s; path=/\n",user_id,
		expiration_date);
	printf("Content-type: text/html%c%c",10,10);

	printf("<html>\n");
	printf("<head>\n");
	printf("<title>Login</title>\n");
	printf("</head>\n");

	printf("<body>\n");
	printf("Welcome %s!\n", entries[0].val);
	printf("<p>\n");
	printf("<a href=\"list.cgi\">View the list</a> or\n");
	printf("<a href=\"login.html\">login as another user</a>.\n");

	printf("</body>\n");
	printf("</html>\n");
};
