#
/*
*       SCCS File Type - 
*
* 	What identification string
*/
static char *_S_edcomutilc = "@(#)edcom_util.c	1.17";
#include "emuniv.h"
#include "EDI.h"
#include "NKSRdefs.h"
#include "NKedit.h"
#include "KSOSpaths.h"

char *findusername();
char *findgroupname();

/*****************************************************************************

PURPOSE
		Find is a module to search the file for the name
		passed as an argument of the command.

DESCRIPTION
		Find will search the file for the requested record and
		will set a pointer to the beginning of that record
		if it is found. A message will be printed if not found.

CALLED BY
		main

CALLS
		scaner

*****************************************************************************/
find(list,num)
namearray list[];
register int num;
{
	extern  int     Index;          /* index into list */
	char    name[LINELENGTH];       /* user input */
	register int     start_index;   /* beginning position in list */

	if (Index == ERROR)
	{
		printf ("No record.\n");
		return;
	}

	while (FOREVER)
	{
		printf ("Enter name: ");
		scaner(name);

		/* check if input too long */
		if (strlen(name) > NAMELENGTH)
			printf("Name too long.\n");

		else if (name[0] == ENDSTRING);

		else if (prtable(name,NAMELENGTH) == FOUND)
			printf ("Non printable character in name.\n");

		else
			break;
	}

	/* save current position */
	start_index = Index;

	/* search through list */
	while (strlcmp(name,list[Index].Name,NAMELENGTH) != EQUAL)
	{
		Index++;

		if(Index == num)       /* go to top when end reached */
			Index = 0;

		if(Index == start_index)        /* name not found */
		{
			printf ("Record not found.\n\n");
			return;
		}
	}
  printf ("find_index = %d\n", Index);
	return;
}




/*****************************************************************************

PURPOSE
		Next is a module to set the Index to the
		next valid record in the database file.

DESCRIPTION
		Next will add one to the Index, looping until
		it has reached a valid field in namelist.

CALLED BY
		main

CALLS
		scaner

*****************************************************************************/
next(list,num)
namearray list[];
register int num;
{
	extern  int     Index;          /* index into list */
	register int     start_index;   /* beginning position in list */

	if (Index == ERROR)
	{
		printf ("No record.\n");
		return;
	}

	/* save current position in the database */
	start_index = Index;

	/* search through list */
	while (FOREVER)
	{
		Index++;

		if(Index == num)       /* go to top when end reached */
			Index = 0;

		if (list[Index].Name[0] != ENDSTRING)
		{
  printf ("next_index = %d\n", Index);
			return;                 /* valid name found */
		}

		if(Index == start_index)        /* no valid records found */
		{
			printf ("No other records.\n\n");
			return(ERROR);
		}
	}
	return;
}


/*****************************************************************************

PURPOSE
		Delete is a module to handle the deletion of a
		record from the file

DESCRIPTION
		The first fieldname of the record currently pointed to
		will be echoed and verifiction ask for to delete,
		A "y" will cause deletion of the entire record,
		anything else will be considered as no and procedure exited.

CALLED BY       main

CALLS		
		printf

*****************************************************************************/
delete(list,num)
namearray list[];
register int num;
{
	extern char     yesorno();
	extern int      Changed_flag;
	extern int      Index;
	register int     start_index;   /* beginning position in list */

	if (Index == ERROR)
	{
		printf ("No record.\n");
		return;
	}

	printf("Do you want to delete record %s (y or n) : ",list[Index].Name);

	if ( yesorno() == YES )
	{
		/* delete record from temp dpfile by blanking name field */
		list[Index].Name[0] = ENDSTRING;
		list[Index].Uid = -1;

		printf ("Record deleted.\n\n");
		Changed_flag = ON;

		/* move Index to next record */
		if (next(list,num) == ERROR)
			Index = -1;
	}
	else
		printf("Record not deleted.\n\n");

}



/*****************************************************************************

PURPOSE
		Quit is the module which ends execution of the
		editor.

DESCRIPTION
		When ending execution, the user has the option to
		save the editing session updates. The files are 
		closed and execution of the editor teminated.

ASSERT
		Assume a temp file is open for read

CALLED BY       main

CALLS           yesorno,
		N_close, N_open, N_read, N_write, printf

*****************************************************************************/
quit(list,num,filepath,record,size)
namearray list[];
register int num;
char	 *filepath;
char     *record;
register int size;
{
	extern char   *Tempfile;        /* pointer to temp file */
	extern char   yesorno();
	extern int    F1,F2;            /* file descriptors */
	extern int    Index;            /* offset into input buffer */
	extern int    Changed_flag;     /* flag set if changes made */
	register int    file_read;      /* number of bytes received by read */
	seid parent;
	seid fseid;
	char entry_name[SIZE_NAME];

	if (Changed_flag == ON)
	{
		printf("Do you want to save the updates? ");

		if (yesorno() == YES)
		{
			creatdb(filepath);

			if (Index != ERROR)
				writedb(list,num,record,size);
		}
	}

	N_close (F1);
	N_close (F2);

	if (UDM_find (ROOT, Tempfile, po_REMOVE, &parent, &fseid, &entry_name)
	    != UDM_no_error)
	{
	    printf ("Can't remove temp file\n");
	    K_release_process (null_seid);
	}
	if (UDM_rm (parent, &entry_name, TRUE) != UDM_no_error)
	    printf ("Removal of temp file failed\n");

	printf ("exit ...\n");
	K_release_process (null_seid);
}
creatdb(filepath)
char	 *filepath;
{
	extern int    F1;               /* file descriptor */
	seid parent;
	seid fseid;
	char entry_name[SIZE_NAME];
	int  result;

	/* close database for reading and open for writing */
	N_close (F1);

	if (UDM_find (ROOT, filepath, po_REMOVE, &parent, &fseid, &entry_name)
	    != UDM_no_error)
	{
	    printf ("Can't remove file\n");
	    K_release_process (null_seid);
	}

	if ((result = UDM_rm (parent, &entry_name, TRUE)) != UDM_no_error)
	{
	    printf ("rm returned %d\n", result);
	    printf ("Removal of file %s failed\n", entry_name);
	    K_release_process (null_seid);
	}

	if ((F1 = N_create (filepath, WRITE, PMODE)) == -1)
	{
		printf("Can't recreate file for writing\n");
		K_release_process (null_seid);
	}
}

writedb(list,num,record,size)
namearray list[];
register int num;
char     *record;
register int size;
{
	extern int    F1,F2;            /* file descriptors */
	extern int    Index;            /* offset into input buffer */
	register int  file_read;        /* number of bytes received by read */

	for (Index = 0; Index < num; Index++)
		if (list[Index].Name[0] != ENDSTRING)
		{
			N_seek (F2,(long) Index * size,0);
			file_read = N_read (F2,record,size);
			if (file_read < 0 )
			{
				printf ("error writing temp file\n");
				K_release_process (null_seid);
			}
			N_write (F1,record,file_read);
		}
}

mknamelist()
{
	extern  namearray Namelist[];
	extern  int     Numofnames;             /* number of entries in the name list */
	extern  int     F1;                     /* file descriptors */
	uaastruct uaarecord;                    /* stores a uaadb record */
	int     lindex;                         /* local index */


	/* open uaadb */
	if ((F1 = N_open(uaadbPath, READ)) == -1)
		printf("can't open uaadb\n");

	lindex = 0;
	while((N_read(F1,&uaarecord,sizeof(uaarecord))) > 0)
	{
		if (uaarecord.userName == ENDSTRING)
			break;

		strcopy(Namelist[lindex].Name, uaarecord.userName,NAMELENGTH);
		Namelist[lindex++].Uid = uaarecord.tii.tii_owner;
	}
  
	Numofnames = lindex;
	N_close(F1);
}

mkgrplist()
{
	extern  namearray Grouplist[];
	extern  int	Numofgroups;		/* number of entries in the group list */
	extern  int     F1;                     /* file descriptors */
	gaastruct gaarecord;                    /* group record buffer */
	char    grp[3];                         /* stores groupid to be converted */
	int     lindex;                         /* local index */

	/* open gaadb for reading */
	if ((F1 = N_open(gaadbPath, READ)) == -1)
		printf("can't open gaadb\n");
  
	/* create grouplist */
	lindex = 0;
	while (N_read(F1,&gaarecord,sizeof(gaarecord)) > 0)
	{
		if (gaarecord.groupName == ENDSTRING)
			break;

		strcopy(Grouplist[lindex].Name,gaarecord.groupName,NAMELENGTH);
		Grouplist[lindex++].Uid = gaarecord.groupid;
	}

	Numofgroups = lindex;
	N_close(F1);
}

/*****************************************************************************

PURPOSE
		Readarec reads a record from the temp file
		and stores it in record;

CALLED BY       prtrec, prtfield, change

CALLS           N_seek, N_read

*****************************************************************************/
readarec(record,size)
char    *record;
register int    size;
{
	extern  int    Index;           /* index into temp */
	extern  int    F2;              /* temp file descriptor */
	extern	char	notvalid;	/* invalid record test value */

	/* read record from file into structure */
	if (N_seek (F2,(long) Index * size,0) != 0)
	{
		printf ("error seeking in temp file\n");
		K_release_process (null_seid);
	}
	if (N_read (F2,record,size) < 0)
	{
		printf ("error reading temp file\n");
		K_release_process (null_seid);
	}

	if (*record == notvalid)
	{
		printf ("No valid records in database.\n\n");
		return(ERROR);
	}
	return(OFF);

}
/*****************************************************************************

PURPOSE
		Wrtarec writes a record to temp file

CALLED BY       change

CALLS           N_seek, N_write

*****************************************************************************/
wrtarec(record,size)
char    *record;
register int    size;
{
	extern  int     Index;          /* index into temp file */
	extern  int     F2;             /* temp file descriptor */

	/* write a record to the temp file */
	if (N_seek (F2,(long) Index * size,0) != 0)
	{
		printf ("error seeking in temp file\n");
		K_release_process (null_seid);
	}
	if (N_write (F2,record,size) < 0)
	{
		printf ("error writing in temp file\n");
		K_release_process (null_seid);
	}
	return;

}


/*****************************************************************************

PURPOSE
		Write out the newly created record

CALLED BY       add

CALLS           N_seek, N_write

*****************************************************************************/
wrtnewrec(record,size)
char    *record;
register int    size;
{
	extern  int     Index;                  /* position in temp */
	extern  int     F2;                     /* file descriptor */

	/* write newrecord to end of temp file */
	if (N_seek (F2,(long) Index * size,0) != 0)
	{
		printf ("error seeking in temp file\n");
		K_release_process (null_seid);
	}
	if (N_write (F2,record,size) < 0)
	{
		printf ("error writing in temp file\n");
		K_release_process (null_seid);
	}

}


char *findgroupname(groupid)
register int    groupid;
{
	extern  int        Numofgroups;
	extern  namearray  Grouplist[];
	extern  char    Notvalmessage[];
	register int    nindex;               /* array increment */

	/* index through Grouplist */
	for(nindex =0; nindex < Numofgroups; nindex++)

		if (Grouplist[nindex].Uid == groupid)
			return(&Grouplist[nindex].Name);

	/* group id number not found */
	return(&Notvalmessage);
}

char *findusername(nameid)
register int nameid;
{
	extern  int       Numofnames;
	extern  namearray Namelist[];
	extern  char      Notvalmessage[];
	register int      nindex;               /* array increment */

	/* index through Namelist */
	for(nindex =0; nindex < Numofnames; nindex++)

		if (Namelist[nindex].Uid == nameid)
			return(&Namelist[nindex].Name);

	/* group id number not found */
	return(&Notvalmessage);
}
