int fgetline(s,max,fp)
char s[];
int max;
FILE *fp;
{
  int i,c;
  short q;
  q=0; /* quote flag */
  i=0;
  while ( (c=fgetc(fp)) != EOF){
/*     putchar(c); /* DEBUG */
    /* Comments begin with # and end with newline */
     if (c=='#'){
      while( (c=fgetc(fp))!='\n' ){
      }
      continue;
    }

    /* Strings enclosed by double quotes treat spaces as any other char */
    if (c==QUOTE ){
      if(q==0){
/*         printf("quote on\n"); /* DEBUG */
        q=1;
      }
      else{
/*        printf("quote off\n"); /* DEBUG */
        q=0;
      }
      continue;
    }
    if ( c==' ' && q==0 ){
/*       printf("FS\n"); /* DEBUG */
      break;
    }

    /* Tabs and newlines are field separators */
    if (c=='\n' || i>=max || c=='\t' ){
/*       printf("FS\n"); /* DEBUG */
      break;
    }
    s[i]=c;
    i++;
  }

  s[i]='\0';
/*   printf("[%s\]\n",s); /* DEBUG */
  return(i);
}
