/* Happy program to tell you where the xtrek daemon is running */

#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define SEEK_END 2
#define SEEK_CUR 1


main()
{
  FILE *file;
  register struct hostent *host = (struct hostent *) NULL;
  char name[10],place[80],foo;
  struct sockaddr_in sin;
  int s, n=1;
  char buf[80];
  
  file = fopen("/afs/sipb/project/xtrek/foobar", "r");
  if (file == NULL) exit(5);
  if (fseek(file, -2L,SEEK_END)) exit(10);
  for (;(foo != '\n');fseek(file,-2L,SEEK_CUR))
    foo =  fgetc(file);
  fscanf(file,"%c%s%s", &foo, name, place);
  fprintf(stdout, "Last xtrek game at: %s\t\tRun by: %s\n", place, name);
  fclose(file);

  if (host = gethostbyname(place))
    {
      sin.sin_family = host->h_addrtype;
      bcopy(host->h_addr, (caddr_t) & sin.sin_addr, host->h_length);
    }
  sin.sin_port = htons(25876);
  s = socket(PF_INET, SOCK_STREAM, 0);
  if ((s < 0) || (connect(s, (struct sockaddr *) & sin, sizeof (sin)) < 0))
    {fprintf(stdout, "But no game is currently at that host\n");}
  else
    {fprintf(stdout, "The game is still running\n");}

/*
 *  for (;;) 
 *   sprintf(buf, "Display: %s Login: %s","xtrek:0", "me");
 *   write(s, buf, strlen(buf));
 *   write(s, "\015\012", 2);
 */

  close(s);
}


