/****************

spill.c
dumps contents of QUERY_STRING and STDIN from GET and POST methods respectively
*****************/

#include <fcntl.h>
#ifdef WIN32
    #include <io.h>
#endif
#include <stdio.h>
#include <stdlib.h>

#define MAXLINELENGTH 4096

int main () {
   FILE *fin = NULL;
   char *input;
   char input_line[MAXLINELENGTH];
   int bytesRead, intHandle;

   input = getenv("QUERY_STRING");

   printf("Content-type: text/html\n\n");
   printf("<HTML><HEAD><TITLE>Data from GET and POST</TITLE></HEAD><BODY><PRE>\n\n");

   if (input == NULL)/*we are testing from the command line*/
      printf("No data in QUERY_STRING\n\n");
   else{
      printf("Below is data in QUERY_STRING:\n\n");
      printf("[%s]", input);
      printf("\n\n");
   }

#ifdef WIN32
    /* Set "stdin" to have binary mode:  */
    intHandle = _setmode(_fileno(stdin), _O_BINARY);
    if(intHandle == -1){
        printf("Cannot set mode for stdin\n");
        return 1;
    }
    /* Set "stdout" to have binary mode: */
    intHandle = _setmode(_fileno(stdout), _O_BINARY);
    if(intHandle == -1){
        printf("Cannot set mode for stdout\n");
        return 1;
    }
#endif

   printf("Below is data from STDIN:\n\n[");
   while ((bytesRead = fread(input_line, 1, MAXLINELENGTH, stdin)) > 0)
      fwrite(input_line, 1, bytesRead, stdout);

   printf("]\n\n</PRE></BODY></HTML>\n");
   return 0;

}/*end main*/

