/*static char *SCCSID = "@(#)fs_move.c	6.2 92/01/31";*/
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsd.h>
#include <fsh.h>
#include "heap.h"
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pb_ifs.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "shutdown.h"
#include "pr_HEAP.h"
#include "pr_MISC.h"
#include "pr_MOVE.h"
#include "pr_WRSUB.h"
#include "pr_DEBUG.h"


/*
 ***	FS_MOVE - rename a file
 *
 *	This routine changes a file's pathname.  The pathnames are copied
 *	into pinball buffers and converted to pinball's internal form in
 *	the process.
 *
 *	ENTRY	pcdfsi = ptr to file-independent current dir information
 *		pcdfsd = ptr to file-dependent current dir information
 *		pSrc = ASCIIZ string name of source file/directory
 *		iSrcCurDirEnd = index of end of current dir in pSrc
 *		pDst = ASCIIZ string name of destination file/directory
 *		iDstCurDirEnd = index of end of current dir in pDst
 *
 *	RETURNS NO_ERROR if successful, else error code
 *
 *	HISTORY
 *	    26-May-1989 by JR (John Rogers, JohnRo@Microsoft)
 *		- DCR 259.2: added counting of write threads for shutdown.
 *		- DCR 259.2: changed names of macros in "frame.h".
 */

int far pascal
FS_MOVE (struct cdfsi far *pcdfsi,
	 struct cdfsd far *pcdfsd,
	 PCHAR		   pSrc,
	 USHORT		   iSrcCurDirEnd,
	 PCHAR		   pDst,
	 USHORT		   iDstCurDirEnd,
	 USHORT		   flags)
{
	PCRACKEDPATH pcpSrc;
	PCRACKEDPATH pcpDst = NULL;	/* must be null since we may never
					   use this before we RetHeap */
	SHUTDOWN_LOCAL_VARS;
	USHORT usRet;
	FRAME  frm;

	INCR_WRITE_THREADS;		/* Tell shutdown we're writing. */

	frm.usHardErr = NO_ERROR;
	frm.fRecheckAddr = TRUE;

	usRet = CrackPath(pcdfsi->cdi_hVPB, pSrc, &pcpSrc, &frm);

	if (usRet == NO_ERROR) {

	    /* the destination must be cracked last, since it sets up a
	     * pointer in the frame to the mixed-case last name, which is
	     * needed later in LDECreate.
	     */

	    usRet = CrackPath(pcdfsi->cdi_hVPB, pDst, &pcpDst, &frm);

	    if (usRet == NO_ERROR) {
		usRet = Move (pcpDst, pcpSrc, flags, &frm);
	    }
	}

	if (pcpSrc != NULL)
	    RetHeap (pcpSrc);

	if (pcpDst != NULL)
	    RetHeap (pcpDst);

#ifdef DEBUG
	VBS ();
#endif

	DECR_WRITE_THREADS;		/* Tell shutdown we're done. */

	return (usRet);

	/* to prevent /W3 warnings */
	pcdfsd; iSrcCurDirEnd; iDstCurDirEnd;
}
