/*
 * $Header: /home/gene/library/website/docsrc/cgx/RCS/ekko.c,v 395.1 2008/04/20 17:25:46 gene Exp $
 *
 * Copyright (c) 2004 Gene Michael Stover.  All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
extern char **environ;
extern int getpid ();

static void
S_Init ()
{
  srand (time (NULL) + getpid ());
}

static void
S_Send (char c)
{
  switch (c) {
  case '"':
    printf ("&quot;");
    break;
  case '&':
    printf ("&amp;");
    break;
  case '<':
    printf ("&lt;");
    break;
  case '>':
    printf ("&gt;");
    break;
  default:
    printf ("%c", c);
  }
}

static void
S_SendStr (char str[])
{
  char *i;

  for (i = str; *i != '\0'; ++i) {
    S_Send (*i);
  }
}

int main (argc, argv)
     int argc;
     char *argv[];
{
  int rc = 0, c, j;
  char **i;

  S_Init ();
  printf ("Content-Type: text/html");
  for (j = 0; j < 5; ++j) {
    printf ("\r\nSet-Cookie: ekko%d=%d; path=/", j, rand ());
  }
  printf ("\r\n");
  printf ("\r\n<html>\n<head></head>\n<body>");
  printf ("\n<h2>Environment</h2>");
  printf ("\n<p>");
  for (i = environ; *i != NULL && **i != '\0'; ++i) {
    S_SendStr (*i);
    printf ("<br />\n");
  }
  printf ("\n</p>");
  printf ("\n<h2>From stdin</h2>");
  printf ("\n<p>Here is what your browser sent:</p>");
  printf ("\n<pre>\n");
  while ((c = getchar ()) != -1) {
    S_Send (c);
  }
  printf ("\n</pre>");
  printf ("\n<p><a href=\"index.html\">again</a></p>");
  printf ("\n<p align=\"center\">End.</p>\n</body>\n</html>\n");
  return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

/* --- end of file --- */
