/*				LGR.C
 *
 *	This program takes from input, A username and data
 *	The username is looked for in [1,2]USERS.DAT, and
 *	if found, the coresponding file is opened,
 *	and the data input is written into the file.
 *
 *	The format of USERS.DAT is:
 *	username file
 *	where 'username' is any valid ascii string terminated by a white space
 *	      'file' is a valid Filename
 *
 *	The GCL module from GCL.ULB will have to be linked to this to
 *	get it to run
 */

#define iswhite(c)	index(" \t\n\r",c)
#include <ctype.h>
#include <gcl.h>
#include <stdio.h>
#include <cx.h>
#include <tparbf.h>

main()
{
	FILE *lf;
	char *cmdlin;
	char name[50];
	char fname[50];
	char *scan;
	int l;
	char line[132];
	char *lp;
	int x;
	long timevec;
	char *tp;
	struct tparbf tskdat;
	word status;
	int memuic, gruic;
	char tsknam[20];
	char tni[20];

	concat(tsknam,"\n\r",argv[0],"> ",0);
	concat(tni,tsknam,"[username ?]> ",0);

	cmdlin=gcl(tni);

	/* Skip any initial white spaces */
	while(iswhite(*cmdlin) && (*cmdlin))
		cmdlin++;

	/* Copy the first word into a buffer */
	for(scan=name ; ((!iswhite(*cmdlin))&&(*cmdlin)) ; )
		*(scan++) = toupper(*(cmdlin++));
	*scan=0;

	/* Skip the white spaces in the command line */
	while(iswhite(*cmdlin) && (*cmdlin))
		cmdlin++;

	/* try to open the user name file */
	if(!(lf=fopen("lb:[1,2]users.dat","r")))
		error("Can't open username file\n");

	/* Look for the user */
	l=strlen(name);
	x=fgetss(line,132,lf);
	while((strncmp(line,name,l)) && x) {
		x=fgetss(line,132,lf);
	}

	if (!x)
		error("\nCan't find username \n");

	for(lp=line ; ((!iswhite(*lp))&&(*lp)) ; lp++ );
	while(iswhite(*lp) && (*lp))
		lp++;
	for(scan=fname ; ((!iswhite(*lp))&&(*lp)) ; ) {
		*(scan++) = toupper(*(lp++));
	}
	*scan=0;

	fclose(lf);
	if (!(lf=fopen(fname,"a")))
		error("\nCan't open log file\n");

	time(&timevec);
	tp = ctime(&timevec);	
	tp[24]=0;

	status = gtsk(&tskdat);
	memuic = tskdat.g_tspc;
	gruic = tskdat.g_tsgc&0377;

	fprintf(lf,"\n\n>> %s  logged from [%o,%o]\n",tp, gruic, memuic);

	while(!GCLEOF) {
		fprintf(lf,"\t%s\n",cmdlin);
		cmdlin=gcl(tsknam);
	}

	fclose(lf);	
	
}

	
