
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <qoent.h>

static struct qoent qo;
static char line[BUFSIZ+1];

/* Simple routine to parse non-whitespace strings */
static char *qostr(p)
     register char **p;
{
  char *cp = *p;
  char *retstr;

  while (*cp && isspace(*cp)) cp++;
  retstr = cp;
  while (*cp && !isspace(*cp)) cp++;
  if (*cp) { *cp = '\0'; cp++; }

  *p = cp;
  return(retstr);
}

/* Scan a line of input for all the fields of a qoent structure */
static qotabscan(qotabp, qo)
     FILE *qotabp;
     struct qoent *qo;
{
  char *cp;
  int n;

  do {
    cp = fgets(line, 256, qotabp);
    if (cp == NULL) return (EOF);
  } while (*cp == '#');

  qo->mnt_dir = qostr(&cp);
  if (*cp == '\0') return (1);

  qo->qo_type = qostr(&cp);
  if (*cp == '\0') return (2);

  n = sscanf(cp, "%d %d %d %d",
	     &qo->qo_bzsl, &qo->qo_bzhl,
	     &qo->qo_fzsl, &qo->qo_fzhl);

  return (2+n);
}

/* Get the next qoent structure from the table */
struct qoent *getqoent(qotabp)
     FILE *qotabp;
{
  int nfields;

  if (!qotabp) return ((struct qoent *)NULL);

  nfields = qotabscan(qotabp, &qo);
  if (nfields == EOF || nfields != 6) return ((struct qoent *)NULL);
  return (&qo);
}

/* Finished with quota options table */
int endqoent(qotab)
     FILE *qotab;
{
  if (qotab) fclose (qotab);
  return(1);
}
