#include <stdio.h>
#include <ndbm.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>


typedef struct list LIST;
typedef struct group GROUP;

struct list
{
char *machine;
LIST *next;
};

struct group
{
char *name;                /* name of the group */
LIST *resolved_list;       /* list of machine names & wildcard expr only */
LIST *unresolved_list;     /* list of groups contained in this group not yet 
			      resolved to machine namers */
GROUP *next;
};

main()
{
  char line[2048], *key, *data, nicer[2048];
  FILE *f;
  int  nptr,rc;
  DBM *d;
  datum ky, dt;

  d = dbm_open("hostauth", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (!d)
    {
      fprintf(stderr, "Couldn't touch hostauth.dir (%d).\n", errno);
      exit(1);
    }
 
  f = fopen("groups.txt", "r");
  if (f == NULL)
    {
      fprintf(stderr, "Couldn't open groups.txt.\n");
      exit(1);
    }

  dbminit("hostauth");

  while (!feof(f))
    {
      bzero(line,2048);
      if (fgets(line, sizeof(line), f))
	{
	  key = line;
	  while (isspace(*key) && (*key != '\0')) key++;
	  data = key;
	  while ((*data != ':') && (*data != '\0')) data++;
	  if (*data == '\0')
	    continue;
	  *data = '\0';
	  data++;
	  nptr = 0;
	  while (1)
	    {
	      while (isspace(*data)) data++;
	      if (*data == '\0')
		break;
	      while (!isspace(*data) && *data != '\0')
		nicer[nptr++] = *data++;
	      nicer[nptr++] = ' '; /* whats this doing ??*/
	    }
	  if (nptr == 0)
	    continue;
	  ky.dptr = key;
	  ky.dsize = strlen(key) + 1;
	  dt.dptr = nicer;
	  dt.dsize = nptr + 1;
	  printf("key = <%s>\n",ky.dptr);
	  printf("data= <%s>, size = %d\n",dt.dptr,dt.dsize);
	  rc = dbm_store(d,ky, dt,DBM_INSERT);
	  bzero(dt.dptr,dt.dsize);
	  if (rc < 0)
	    fprintf(stderr, "error storing %s\n", key);
	  if (rc == 1)
	    fprintf(stderr, "Found an entry with the same key: %s\n", key);
	}
    }

  fclose(f);
}
