/* hostread.c */

#include <stdio.h>
#include <string.h>    /* This contains the definition for strdup() -- read
                          the man page */ 

#include "dlist.h"    /* This is in /blugreen/homes/plank/cs360/include, 
                         which we enable the compiler to find by using the
                         flag -I/blugreen/homes/plank/cs360/include in the 
                         makefile */
#include "fields.h"    /* Also in /blugreen/homes/plank/cs360/include */

struct hostip {
  char *ip;
  char *host;
}

main()
{
  Dlist d, tmp;
  int i;
  struct hostip *hip;
  IS is;

  /* Create the dlist */

  d = make_dl();

  /* read in the host file */

  is = new_inputstruct(NULL);
  i = 0;
  while (get_line(is) == 2) {
    hip = (struct hostip *) malloc (sizeof(struct hostip));
    hip->ip = is->fields[0];
    hip->host = is->fields[1];
    dl_insert_b(d, hip);
    i++;
  }

  /* Print the dlist backwards */

  for (tmp = last(d); tmp != nil(d); tmp = prev(tmp)) {
    hip = (struct hostip *) tmp->val;
    printf("Entry %3d:   Ip: %16s,    Host: %s\n", 
            i, hip->ip, hip->host);
    i--;
  }
}

