#include #include #include #include "cgi.h" /* a generic cgi handler that displays the environment and the values of all of the variables on the user's browser */ int main(int argc, char *argv[], char *envp[]) { char *contentLength; char *buffer; int length; int n; int i = 0; printf("Content-type: text/html\n"); printf("Set-cookie: ID=12345\n\n"); printf("\n"); printf("Print the Environment\n"); printf("\n"); printf("

Here's the environment

\n"); printf("

\n"); while (envp[i]) { printf("%s
\n",envp[i]); i++; } printf("

\n"); contentLength = getenv("CONTENT_LENGTH"); if (!contentLength) length = 0; else length = atoi(contentLength); if (length == 0) { printf("Error, no data, please try again

\n"); printf("\n"); exit(0); } buffer = (char *) malloc(length +2); if (buffer == NULL) { printf("Error, Malloc failed\n"); printf("\n"); exit(0); } memset(buffer,0,length+2); n = read(0,buffer,length); if (n != length) { printf("

Error reading, n is %d

",n); printf("\n"); exit(0); } buffer[length]='\0'; printf("

Here's the input buffer

%s\n",buffer); parseline(buffer); /* parses the input line */ printf("
There are %d variables. Here they are
\n", count); for (i=0;i\n",vars[i][0], vars[i][1]); printf("\n\n"); return 0; }