/* FileMgrF.C -- All file functions for FILEMGR */

#include "windows.h"		/* all Windows functions		*/
#include "winexp.h"		/* all extra Windows functions	*/
#include "filemgr.h"		/* all file manager defines		*/
#include "string.h"		/* all string functions			*/
#include "stdio.h"		/* all file i/o functions		*/
#include "stdlib.h"		/* misc functions				*/
#include "dos.h"			/* all dos file functions		*/
#include "direct.h"		/* all dos directory functions	*/
#include "ctype.h"		/* all character type functions	*/

extern char *GetDir(char *);
extern BOOL FAR PASCAL Key(HANDLE,unsigned,WORD,LONG);

extern int		ntimes;
extern HANDLE	hinst,
			hnormcursor,
			hwaitcursor;
extern char		szkey[11+1],
			szerror1[49+1],
			szerror2[49+1];
extern BOOL		nencrypt,
			nendecrypt,
			ndup,
			nhide;
WORD	uoldsel;



/* procedure to copy contents of sxfile into
 * sznewfile
 */
int CopyFile(HWND hwnd,
             char *szfile,
             char *sznewfile)
{
  HANDLE 				hmem,
					hread_file,
					hwrite_file;
  OFSTRUCT				ofs;
  long				lfile_size,
					lbytes_left;
  LPSTR				lpszbuffer;
  static struct find_t		f_tbuffer;
  static unsigned			ufile_size,
			  		uact_bytes;

  hread_file = OpenFile((LPSTR)szfile,
					(LPOFSTRUCT)&ofs,
					OF_READ);
  hwrite_file = OpenFile((LPSTR)sznewfile,
					(LPOFSTRUCT)&ofs,
					OF_WRITE | OF_CREATE);
  if ((hread_file==-1)||(hwrite_file==-1))
	return FALSE;
  /* get file size */
  if(_dos_findfirst(szfile,
			_A_NORMAL|_A_HIDDEN,
			&f_tbuffer))
	return FALSE;
  lbytes_left = lfile_size = f_tbuffer.size;
  /* get memory to read file */
  if (!(hmem=GlobalAlloc(GHND, 65535)))
  {
	MessageBox(hwnd,
			(LPSTR)szerror1,
			(LPSTR)NULL,
			MB_ICONHAND | MB_OK);
	return FALSE;
  }
  else /* we've got memory to read file*/
  {
	/* lock memory and read file into mem */
	lpszbuffer = GlobalLock(hmem);
	do
       if (lbytes_left > 65535)
	  {
		_dos_read(hread_file,
				lpszbuffer,
				65535,
				&uact_bytes);
		_dos_write(hwrite_file,
				lpszbuffer,
				65535,
				&uact_bytes);
		lbytes_left -= uact_bytes;
	  }
	  else
	  {
		_dos_read(hread_file,
				lpszbuffer,
				(unsigned)LOWORD(lbytes_left),
				&uact_bytes);
		_dos_write(hwrite_file,
				lpszbuffer,
				(unsigned)LOWORD(lbytes_left),
				&uact_bytes);
		lbytes_left -= uact_bytes;
	  }
     while (lbytes_left > 0);
	/* unlock memory */
	GlobalUnlock(hmem);
	/* free memory */
	GlobalFree(hmem);
  }
  /* close the files */
  _dos_close(hread_file);
  _dos_close(hwrite_file);

  return TRUE;
} /* CopyFile */



/* Encypt/Decrypt file */
int EncryptFile(HANDLE hwnd,
			char *szfile,
			char *szkey)
{
  HANDLE				hread_file,
					hrw_file,
					hmem;
  OFSTRUCT				ofs;
  WORD				wkey_len,
					windex,
					wread_size;
  long				lbytes_left;
  LPSTR				lpszbuffer;
  static struct find_t		f_tbuffer;
  static unsigned			uact_bytes;

  SetCursor(hwaitcursor);
  hread_file = OpenFile((LPSTR)szfile,
					(LPOFSTRUCT)&ofs,
					OF_READ);
  hrw_file = OpenFile((LPSTR)szfile,
					(LPOFSTRUCT)&ofs,
					OF_READWRITE);
  if ((hread_file==-1)||(hrw_file==-1))
  {
	SetCursor(hnormcursor);
	return FALSE;
  }
  /* get file size */
  if (_dos_findfirst(szfile,
				_A_NORMAL|_A_HIDDEN,
				&f_tbuffer))
  {
	SetCursor(hnormcursor);
	return FALSE;
  }
  lbytes_left = f_tbuffer.size;
  wkey_len = (WORD)strlen(szkey);
  wread_size = (WORD)min(65535,lbytes_left);
  /* get memory to read file */
  if (!(hmem=GlobalAlloc(GHND, MAKELONG(wread_size,0))))
  {
	SetCursor(hnormcursor);
	MessageBox(hwnd,
			(LPSTR)szerror1,
			(LPSTR)NULL,
			MB_ICONHAND | MB_OK);
	return FALSE;
  }
  else /* we've got memory to read file*/
  {
	/* lock memory and read file into mem */
	lpszbuffer = GlobalLock(hmem);
	do
       if (lbytes_left > (long)wread_size)
	  {
		_dos_read(hread_file,
				lpszbuffer,
				wread_size,
				&uact_bytes);
		lbytes_left -= uact_bytes;
		for (windex=0;windex<uact_bytes;windex++)
		  lpszbuffer[windex] = 
				lpszbuffer[windex]^szkey[windex%wkey_len];
		_dos_write(hrw_file,
				lpszbuffer,
				uact_bytes,
				&uact_bytes);
	  }
	  else
	  {
		_dos_read(hread_file,
				lpszbuffer,
				(unsigned)LOWORD(lbytes_left),
				&uact_bytes);
		for (windex=0;windex<uact_bytes;windex++)
			lpszbuffer[windex] = 
				lpszbuffer[windex]^szkey[windex%wkey_len];
		_dos_write(hrw_file,
				lpszbuffer,
				uact_bytes,
				&uact_bytes);
		lbytes_left -= uact_bytes;
	  }
     while (lbytes_left > 0);
	/* unlock memory */
	GlobalUnlock(hmem);
	/* free memory */
	GlobalFree(hmem);
  }
  /* close the files */
  _dos_close(hread_file);
  _dos_close(hrw_file);
  SetCursor(hnormcursor);
  return TRUE;
} /* EncryptFile */


/* Procedure to get next highlighted selection */
char *GetNextSel(HANDLE hsel)
{
  WORD utemp,ucount;
  static char sztemp[29+1];

  strset(sztemp,0);
  ucount = (WORD)SendMessage(hsel,LB_GETCOUNT,0,0L);
  for(utemp=uoldsel;
      utemp<ucount;
      utemp++)
  {
	if (SendMessage(hsel,LB_GETSEL,utemp,0L))
	{
	  uoldsel = utemp;
	  SendMessage(hsel,LB_GETTEXT,utemp,(long)(LPSTR)sztemp);
	  SendMessage(hsel,LB_SETSEL,0,MAKELONG(utemp,0));
       utemp = 65534;
	}
  }
  return sztemp;
}


/* Procedure to Encrypt and Decrypt files */
void EnDecryptFiles(HANDLE hsel)
{
  static char szfile[29+1],
              szcopy[29+1];
  static unsigned uatt;
  FARPROC lpproc;

  ntimes = 0;
  lpproc = MakeProcInstance(Key,hinst);
  DialogBox(hinst,
			MAKEINTRESOURCE(KEYBOX),
			hsel,
			lpproc);
  FreeProcInstance(lpproc);

  if (nendecrypt)
  {
	uoldsel = 0;
	strset(szfile,0);
	strcpy(szfile,GetNextSel(hsel));
	while (szfile[0]!=0)
	{
	  if (ndup==MF_CHECKED)
	  {
		strset(szcopy,0);
		strcpy(szcopy,szfile);
		szcopy[strlen(szcopy)-1]='2';
		CopyFile(hsel,szfile,szcopy);
	  }
	  if ((nhide==MF_CHECKED)&&(nencrypt))
	  {
		_dos_getfileattr(szfile,&uatt);
		uatt = uatt|_A_HIDDEN;
		_dos_setfileattr(szfile,uatt);
	  }
	  if (!EncryptFile(hsel,szfile,szkey))
	  {
		MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
		if ((nhide==MF_CHECKED)&&(nencrypt))
		{
		  _dos_getfileattr(szfile,&uatt);
		  uatt = uatt-_A_HIDDEN;
		  _dos_setfileattr(szfile,uatt);
		}
	  }
	  strcpy(szfile,GetNextSel(hsel));
	}
  }
}


/* Procedure to make files invisible */
void MakeFilesInv(HANDLE hsel)
{
  static char szfile[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strcpy(szfile,GetNextSel(hsel));
  
  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt|_A_HIDDEN;
	if (_dos_setfileattr(szfile,uatt))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
  }
}


/* Procedure to make files visible */
void MakeFilesVis(HANDLE hsel)
{
  static char szfile[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strcpy(szfile,GetNextSel(hsel));
  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt-_A_HIDDEN;
	if (_dos_setfileattr(szfile,uatt))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
  }
}


/* Procedure to make files read-only */
void MakeFilesRead(HANDLE hsel)
{
  static char szfile[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strcpy(szfile,GetNextSel(hsel));
  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt|_A_RDONLY;
	if (_dos_setfileattr(szfile,uatt))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
  }
}


/* Procedure to make files read\write */
void MakeFilesRW(HANDLE hsel)
{
  static char szfile[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strcpy(szfile,GetNextSel(hsel));
  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt-_A_RDONLY;
	if (_dos_setfileattr(szfile,uatt))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
  }
}


/* Procedure to make directories invisible */
void MakeDirsInv(HANDLE hsel)
{
  static char szfile[29+1],
              szdir[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strset(szdir,0);
  strcpy(szfile,GetNextSel(hsel));
  strcat(szdir,GetDir(szfile));
  
  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt|_A_HIDDEN;
	if (_dos_setfileattr(szdir,uatt))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
	strcat(szdir,GetDir(szfile));
  }
}


/* Procedure to make directories visible */
void MakeDirsVis(HANDLE hsel)
{
  static char szfile[29+1],
              szdir[29+1];
  static unsigned uatt;

  uoldsel = 0;
  strset(szfile,0);
  strset(szdir,0);
  strcpy(szfile,GetNextSel(hsel));
  strcat(szdir,GetDir(szfile));

  while (szfile[0]!=0)
  {
	_dos_getfileattr(szfile,&uatt);
	uatt = uatt-_A_HIDDEN;
	if (_dos_setfileattr(szdir,_A_NORMAL))
	  MessageBox(hsel,
				(LPSTR)szerror2,
				(LPSTR)szfile,
				MB_ICONASTERISK|MB_OK);
	strcpy(szfile,GetNextSel(hsel));
	strcat(szdir,GetDir(szfile));
  }
}


