/*
 * Really Simple Threads package.  Copyright Robert S. Thau 1996.
 * This code may be used for any purpose.  It may be redistributed on
 * the following terms: 1) this paragraph and the next must appear
 * unaltered in any source distribution, and be prominently displayed
 * in the supporting documentation for any source or binary
 * distribution; 2) if you wish to distribute modified or derived
 * works, each such work must bear a notice of who last altered the
 * included version of Really Simple Threads, and when.  The notice of
 * alterations shall be attached to the derivative works in the same
 * manner as these two paragraphs, and shall be at least as prominent.
 * Redistribution for any purpose is otherwise unrestricted.
 *
 * THIS CODE IS IN AN UNFINISHED STATE, AND HAS NOT BEEN TESTED
 * TO THE POINT THAT THE AUTHOR BELIEVES IT CAN BE EXPECTED TO
 * WORK RELIABLY.  IT IS SUITABLE, AT THIS POINT, FOR EXPERIMENTATION
 * AND DEBUGGING ONLY.  TREAT ACCORDINGLY.
 */

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifdef SCO5
#include <strings.h>
#endif
#include <netdb.h>
#include "rsthreads.h"
#include "rsthreads_dns.h"

void *test_it (void *fname)
{
    FILE *fp = fopen ((char *)fname, "r");
    char buf[512];

    if (fp == NULL) {
	perror ((char *)fname);
	return -1;
    }

    while (fgets (buf, sizeof(buf), fp) != NULL) {
	struct hostent *hptr;
	struct in_addr addr;
	char *cp = buf + strlen(buf) - 1;
	unsigned char *addrptr = (unsigned char *)&addr;

	while (cp >= buf && isspace(*cp))
	    *cp-- = '\0';

	if ((hptr = rst_gethostbyname (buf)) == NULL) {
	    fprintf (stderr, "error on gethostaddr for %s\n", buf);
	    continue;
	}

	addr = *(struct in_addr *)hptr->h_addr;

	if ((hptr = rst_gethostbyaddr ((char *)&addr, sizeof (addr), AF_INET))
	    == NULL)
	{
	    fprintf (stderr, "error on gethostname for %u.%u.%u.%u, from %s\n",
		     addrptr[0], addrptr[1], addrptr[2], addrptr[3],
		     buf);
	    continue;
	}

	printf ("%s: %u.%u.%u.%u\n",
		buf, addrptr[0], addrptr[1], addrptr[2], addrptr[3]);

	if (strcasecmp (buf, hptr->h_name)) {
	    fprintf (stderr, "Name mismatch: %s != %s\n", buf, hptr->h_name);
	}
    }

    fclose (fp);
    return 0;
}

void main(int argc, char **argv) {
    int bogo_count = 0;
    
    rst_init();
    
    while (*++argv)
	rst_spawn (test_it, *argv);

    while (1) {
	printf ("Mark: %d\n", ++bogo_count);
	rst_sleep (1);
    }
}
