
/* Test CGI Script */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define CGI_311

void strcvrt(char * cStr,char cOld,char cNew);
void urlDecode(char *p);

char * parseStringField( char *Item)
    {
    char *p;

    p = strchr(Item,'=');
    *p++ = '\0';
    urlDecode(p);
    strcvrt(p,'\n',' ');
    strcvrt(p,'\r',' ');
    strcvrt(p,'+',' ');

    return p;
    }


main(int argc, char *argv[])
{
    char ch;
    char TempStr[1024];
    char *value;
    int k=0;

#if 1    
    /* Check for 2 files as args */
    if(argc<5)
        {
        printf("Error Invalid Args! %d\n", argc);
        exit(1);
        }
#endif     

#ifdef CGI_311
    freopen(argv[7],"r",stdin);
    freopen(argv[8],"w",stdout);
#endif    

    /* Print header */
    printf("<html>\n");
    printf("<head>\n");
    printf("<title>CGI Test Script</title>\n");
    printf("</head>\n");

    /* Print Body */
    printf("<body>\n");
    printf("<h2>CGI Test Script</h2>");

    printf("You entered the following information.<P>\n");

    while ( fread(&ch,1,1,stdin )>0 && ch!='\r' && ch!='\n') {
      if (ch=='&') {
         TempStr[k]=0;
         value = parseStringField(TempStr);
         printf(value);
         printf("<br>\n");
         k=0;
      }
      else 
         TempStr[k++] = ch;
   }

   TempStr[k]=0;
   value = parseStringField(TempStr);
   printf(value);
   printf("<p>\n");
   printf("<hr>\n");
   printf("The following information was passed to this script ");
   printf("as command line parameters.<p>\n");
   for(k=9;k<argc;k++)
       printf("%s ",argv[k]);
   printf("<p>\n");
   printf("<hr>\n");
   printf("Here is a list of supported \"environment variables\" and their ");
   printf("values.<p>\n");
   printf("SCRIPT_NAME = %s<br>\n", argv[0]);
   printf("REQUEST_METHOD = %s<br>\n", argv[1]);
   printf("SERVER_PROTOCOL = %s<br>\n", argv[2]);
   printf("REMOTE_NAME = %s<br>\n", argv[3]);
   printf("REMOTE_ADDRESS = %s<br>\n", argv[4]);
   printf("CONTENT_LENGTH = %s<br>\n", argv[5]);
   printf("QUERY_STRING = %s<br>\n", argv[6]);
   
   printf("</body>");
   printf("</html>\n");
}

    
