
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "cus.h"
#include "defines.h"
#include "lbio.h"

int inlsys(char *lsysfile, char *hostname)
{
	int rc;
	FILE *fp = NULL;
	char *buf = NULL;
	long len;
	LB *lb = NULL;
	int i;
	char *p;

	if ((fp = fopen(lsysfile, "r")) == NULL)
		CU(ERR);
	if (fseek(fp, 0L, SEEK_END) == -1L)
		CU(ERR);
	if ((len = ftell(fp)) == -1L)
		CU(ERR);
	if (fseek(fp, 0L, SEEK_SET) == -1L)
		CU(ERR);
	if ((buf = (char *)malloc(len)) == NULL)
		CU(ERR);
	if ((fread(buf, len, 1, fp) != 1) && ferror(fp))
		CU(ERR);
	if ((lb = lbopen(buf, buf+len-1)) == NULL)
		CU(ERR);
	if (lbseek(lb, 0, LBSEEK_SET) == NULL)
		CU(ERR);

	do {
		if ((lb->lb_wLnCur != 0) && (*(lb->lb_cpLnCur) != '#')) {
			for (i=0, p = lb->lb_cpLnCur; i < lb->lb_wLnCur; i++, p++)
				if (*p == SP)
					break;
			if ((*p == SP) && (i == strlen(hostname)))
				if (!(strnicmp(lb->lb_cpLnCur, hostname, i)))
					CU(TRUE);
		}
	} while (lbseek(lb, 1, LBSEEK_CUR));

	rc = FALSE;

	CUS:
	if (lb)	lbfree(lb);
	if (fp) fclose(fp);
	if (buf) free(buf);
	return rc;
}

