~ubuntu-branches/ubuntu/trusty/netrek-client-cow/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/****************************************************************************/
/***  File:  check.c                                                      ***/
/***  Function:                                                           ***/
/***  Revisions:                                                          ***/
/***   9/20/93  VEG changed last while loop of file to improve clarity.   ***/
/***   Also re-indented and spaced to make source easier to read.         ***/
/***   Lastly, changed size of declared character buffer from 4096 + 1    ***/
/***   to just 4096, since read only reads up to the number of characters ***/
/***   requested.  Decided to make this a local define, BUF_SIZE.         ***/
/***                                                                      ***/
/****************************************************************************/

/****************************************************************************/
/***                           Include Files                              ***/
/****************************************************************************/
#include "config.h"
#include "copyright2.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <errno.h>
#include INC_NETINET_IN
#include <arpa/inet.h>
#include "Wlib.h"
#include "defs.h"
#include "struct.h"
#include "data.h"
#include "check.h"

/* For close() and read(), at least in MSVC -SAC */
#include INC_IO

extern void terminate(int error);

/******************************************************************************/
/***     Size lof character data buffer                                     ***/
/******************************************************************************/
#define  BUF_SIZE    ( 4096 )

/******************************************************************************/
/***                          Static Character Buffer                       ***/
/******************************************************************************/
static char buf[BUF_SIZE];

/******************************************************************************/
/***  check()  will obtain the connection to the server and will forever    ***/
/***  read server data into a 4096 byte buffer area, unless an error is     ***/
/***  detected.                                                             ***/
/******************************************************************************/
void check(void)
{
  struct sockaddr_in addr;
  struct hostent *hp;
  int     cc, sock;

  /* get numeric form */
  if ((addr.sin_addr.s_addr = inet_addr(serverName)) == -1)
    {
      if ((hp = gethostbyname(serverName)) == NULL)
	{
	  fprintf(stderr, "unknown host '%s'\n", serverName);
	  terminate(0);
	}
      else
	{
	  addr.sin_addr.s_addr = *(LONG *) hp->h_addr;
	}
    }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(xtrekPort);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
      perror("socket");
      terminate(0);
    }
  printf("checking %s on port %d\n", serverName, xtrekPort);
  if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    {
      perror("connect");
      close(sock);
      terminate(0);
    }

  while ((cc = read(sock, buf, BUF_SIZE)) > 0)
    {
      if (fwrite(buf, cc, 1, stdout) != 1) {
	perror("fwrite");
	close(sock);
	terminate(0);
      }
    }
  if (cc < 0)
    {
      perror("read");
    }
  close(sock);
  terminate(0);
}