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

main(argc, argv)
int argc;
char **argv;
{
  char *hn, s[5000];
  int port, sock, fd, dummy;
  long t;

  if (argc != 3) {
    fprintf(stderr, "usage: catmotd host port\n");
    exit(1);
  }

  hn = argv[1];
  port = atoi(argv[2]);
  if (port < 5000) {
    fprintf(stderr, "usage: catmotd host port\n");
    fprintf(stderr, "       port must be > 5000\n");
    exit(1);
  }

  sock = serve_socket(hn, port);
  while (1) {
    fd = accept_connection(sock);
    t = time(0);
    printf("Got a connection %s\n", ctime(&t));
    fflush(stdout);
    if (fork() == 0) {
      dup2(fd, 1);
      close(fd);
      close(sock);
      execlp("cat", "cat", "motd", 0);
      fprintf(stderr, "Exec failed.  Drag\n");
      exit(1);
    } else {
      close(fd);
      wait(&dummy);
    }
  }
}

