(Brandon Turner) <turnerb7@msu.edu>) Fixed bug to allow MS style wildcards + code clean up added /y and /-y
Modified: trunk/reactos/subsys/system/cmd/move.c

Modified: trunk/reactos/subsys/system/cmd/move.c
--- trunk/reactos/subsys/system/cmd/move.c	2005-06-23 17:54:22 UTC (rev 16247)
+++ trunk/reactos/subsys/system/cmd/move.c	2005-06-23 18:12:17 UTC (rev 16248)
@@ -1,29 +1,33 @@
 /*
- *  MOVE.C - move internal command.
- *
- *
- *  History:
- *
- *    14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
- *        Started.
- *
- *    18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
- *        Unicode safe!
- *        Preliminary version!!!
- *
- *    20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
- *        Redirection safe!
- *
- *    27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
- *        Added help text ("/?").
- *        Added more error checks.
- *
- *    03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
- *        Added "/N" option.
- *
- *    30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
- *        Remove all hardcode string to En.rc
- */
+*  MOVE.C - move internal command.
+*
+*
+*  History:
+*
+*    14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
+*        Started.
+*
+*    18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
+*        Unicode safe!
+*        Preliminary version!!!
+*
+*    20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
+*        Redirection safe!
+*
+*    27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
+*        Added help text ("/?").
+*        Added more error checks.
+*
+*    03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
+*        Added "/N" option.
+*
+*    30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
+*        Remove all hardcode string to En.rc
+*
+*    24-Jun-2005 (Brandon Turner) <turnerb7@msu.edu>)
+*        Fixed bug to allow MS style wildcards + code clean up
+*        added /y and /-y
+*/
 
 #include "precomp.h"
 #include "resource.h"
@@ -31,32 +35,22 @@
 #ifdef INCLUDE_CMD_MOVE
 
 
-#define OVERWRITE_NO     0
-#define OVERWRITE_YES    1
-#define OVERWRITE_ALL    2
-#define OVERWRITE_CANCEL 3
+enum
+{
+	MOVE_NOTHING  = 0x001,   /* /N  */
+	MOVE_OVER_YES = 0x002,   /* /Y  */
+	MOVE_OVER_NO  = 0x004,   /* /-Y */
+};
 
-
 static INT Overwrite (LPTSTR fn)
 {
+	/*ask the user if they want to override*/
 	TCHAR szMsg[RC_STRING_MAX_SIZE];
-	TCHAR inp[10];
-	LPTSTR p;
-
+	INT res;
 	LoadString(CMD_ModuleHandle, STRING_MOVE_HELP1, szMsg, RC_STRING_MAX_SIZE);
-	ConOutPrintf(szMsg, fn);
-	ConInString(inp, 10);
-
-	_tcsupr (inp);
-	for (p = inp; _istspace(*p); p++)
-		;
-
-	if (*p != szMsg[0] && *p != szMsg[2])
-		return OVERWRITE_NO;
-	if (*p == szMsg[2])
-		return OVERWRITE_ALL;
-
-	return OVERWRITE_YES;
+	ConOutPrintf(szMsg,fn);
+	res = FilePromptYNA ("");
+	return res;
 }
 
 
@@ -68,29 +62,29 @@
 	INT argc, i, nFiles;
 	TCHAR szDestPath[MAX_PATH];
 	TCHAR szSrcPath[MAX_PATH];
-	BOOL bPrompt = TRUE;
-	LPTSTR p;
+	DWORD dwFlags = 0;
+	INT nOverwrite = 0;
 	WIN32_FIND_DATA findBuffer;
 	HANDLE hFile;
 	LPTSTR pszFile;
-	BOOL bNothing = FALSE;
 
+
 	if (!_tcsncmp (param, _T("/?"), 2))
 	{
 #if 0
 		ConOutPuts (_T("Moves files and renames files and directories.\n\n"
-		               "To move one or more files:\n"
-		               "MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
-		               "\n"
-		               "To rename a directory:\n"
-		               "MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
-		               "\n"
-		               "  [drive:][path]filename1  Specifies the location and name of the file\n"
-		               "                           or files you want to move.\n"
-		               "  /N                       Nothing. Don everthing but move files or direcories.\n"
-		               "  /Y\n"
-		               "  /-Y\n"
-		               "..."));
+			"To move one or more files:\n"
+			"MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
+			"\n"
+			"To rename a directory:\n"
+			"MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
+			"\n"
+			"  [drive:][path]filename1  Specifies the location and name of the file\n"
+			"                           or files you want to move.\n"
+			"  /N                       Nothing. Don everthing but move files or direcories.\n"
+			"  /Y\n"
+			"  /-Y\n"
+			"..."));
 #else
 		ConOutResPuts(STRING_MOVE_HELP2);
 #endif
@@ -103,24 +97,25 @@
 	/* read options */
 	for (i = 0; i < argc; i++)
 	{
-		p = arg[i];
-
-		if (*p == _T('/'))
+		if (*arg[i] == _T('/'))
 		{
-			p++;
-			if (*p == _T('-'))
+			if (_tcslen(arg[i]) >= 2)
 			{
-				p++;
-				if (_totupper (*p) == _T('Y'))
-					bPrompt = TRUE;
+				switch (_totupper(arg[i][1]))
+				{
+				case _T('N'):
+					dwFlags |= MOVE_NOTHING;
+					break;
+
+				case _T('Y'):
+					dwFlags |= MOVE_OVER_YES;
+					break;
+
+				case _T('-'):
+					dwFlags |= MOVE_OVER_NO;
+					break;
+				}
 			}
-			else
-			{
-				if (_totupper (*p) == _T('Y'))
-					bPrompt = FALSE;
-				else if (_totupper (*p) == _T('N'))
-					bNothing = TRUE;
-			}
 			nFiles--;
 		}
 	}
@@ -132,13 +127,20 @@
 		return 1;
 	}
 
+	if (_tcschr (arg[argc - 1], _T('*')) != NULL)
+	{
+		/*'*' in dest, this doesnt happen.  give folder name instead*/
+		error_parameter_format('2');
+		return 1;
+	}
+
 	/* get destination */
 	GetFullPathName (arg[argc - 1], MAX_PATH, szDestPath, NULL);
 #ifdef _DEBUG
 	DebugPrintf (_T("Destination: %s\n"), szDestPath);
 #endif
 
-	/* move it*/
+	/* move it */
 	for (i = 0; i < argc - 1; i++)
 	{
 		if (*arg[i] == _T('/'))
@@ -154,6 +156,8 @@
 
 		do
 		{
+
+			nOverwrite = 1;
 			GetFullPathName (findBuffer.cFileName, MAX_PATH, szSrcPath, &pszFile);
 
 			if (GetFileAttributes (szSrcPath) & FILE_ATTRIBUTE_DIRECTORY)
@@ -162,93 +166,100 @@
 
 #ifdef _DEBUG
 				DebugPrintf (_T("Move directory \'%s\' to \'%s\'\n"),
-							 szSrcPath, szDestPath);
+					szSrcPath, szDestPath);
 #endif
-				if (!bNothing)
-				{
-					MoveFile (szSrcPath, szDestPath);
-				}
+				if (!(dwFlags & MOVE_NOTHING))
+					continue;
+				MoveFile (szSrcPath, szDestPath);
 			}
 			else
 			{
-				/* source is file */
-
-				if (IsExistingFile (szDestPath))
+				/* source is file */				
+				if (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_DIRECTORY)
 				{
-					/* destination exists */
-					if (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_DIRECTORY)
-					{
-						/* destination is existing directory */
+					/* destination is existing directory */
 
-						TCHAR szFullDestPath[MAX_PATH];
+					/*build the dest string(accounts for *)*/
+					TCHAR szFullDestPath[MAX_PATH];
+					_tcscpy (szFullDestPath, szDestPath);
+					/*this line causes a one to many slashes, GetFullPathName must
+					be adding on a \ when it sees that is a dir*/
+					//_tcscat (szFullDestPath, _T("\\"));
+					_tcscat (szFullDestPath, findBuffer.cFileName);
 
-						_tcscpy (szFullDestPath, szDestPath);
-						_tcscat (szFullDestPath, _T("\\"));
-						_tcscat (szFullDestPath, pszFile);
+					/*checks to make sure user wanted/wants the override*/
+					if((dwFlags & MOVE_OVER_NO) && IsExistingFile (szFullDestPath))
+						continue;
+					if(!(dwFlags & MOVE_OVER_YES) && IsExistingFile (szFullDestPath))
+						nOverwrite = Overwrite (szFullDestPath);
+					if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
+						continue;
+					if (nOverwrite == PROMPT_ALL)
+						dwFlags |= MOVE_OVER_YES;
 
-						ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath);
+					/*delete the file that might be there first*/
+					DeleteFile(szFullDestPath);
+					ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath);
 
-						if (!bNothing)
-						{
-							if (MoveFile (szSrcPath, szFullDestPath))
-								LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
-							else
-								LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
-							ConOutPrintf(szMsg);
-						}
-					}
+					if ((dwFlags & MOVE_NOTHING))
+						continue;
+
+					/*delete the file that might be there first*/
+					DeleteFile(szFullDestPath);
+					/*move the file*/
+					if (MoveFile (szSrcPath, szFullDestPath))
+						LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
 					else
-					{
-						/* destination is existing file */
-						INT nOverwrite;
+						LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
 
-						/* must get the overwrite code */
-						if ((nOverwrite = Overwrite (szDestPath)))
-						{
-#if 0
-							if (nOverwrite == OVERWRITE_ALL)
-								*lpFlags |= FLAG_OVERWRITE_ALL;
-#endif
-							ConOutPrintf (_T("%s => %s"), szSrcPath, szDestPath);
-
-							if (!bNothing)
-							{
-								if (MoveFile (szSrcPath, szDestPath))
-									LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
-								else
-									LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
-								ConOutPrintf(szMsg);
-							}
-						}
-					}
+					ConOutPrintf(szMsg);
 				}
 				else
 				{
-					/* destination does not exist */
-					TCHAR szFullDestPath[MAX_PATH];
+					/* destination is a file */
 
-					GetFullPathName (szDestPath, MAX_PATH, szFullDestPath, NULL);
+					if (_tcschr (arg[argc - 2], _T('*')) != NULL)
+					{
+						/*'*' in src but there should't be one in the dest*/
+						error_parameter_format('1');
+						return 1;
+					}
 
-					ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath);
+					/*bunch of checks to see if we the user wanted/wants
+					to really override the files*/
+					if((dwFlags & MOVE_OVER_NO) && IsExistingFile (szDestPath))
+						continue;
+					if(!(dwFlags & MOVE_OVER_YES) && IsExistingFile (szDestPath))
+						nOverwrite = Overwrite (szDestPath);
+					if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
+						continue;
+					if (nOverwrite == PROMPT_ALL)
+						dwFlags |= MOVE_OVER_YES;
 
-					if (!bNothing)
-					{
-						if (MoveFile (szSrcPath, szFullDestPath))
-							LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
-						else
-							LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
-						ConOutPrintf(szMsg);
-					}
+					ConOutPrintf (_T("%s => %s"), szSrcPath, szDestPath);
+
+					if ((dwFlags & MOVE_NOTHING))
+						continue;
+
+					/*delete the file first just to get ride of it
+					if it was already there*/
+					DeleteFile(szDestPath);
+					/*do the moving*/
+					if (MoveFile (szSrcPath, szDestPath))
+						LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
+					else
+						LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
+
+					ConOutPrintf(szMsg);
+
 				}
 			}
 		}
 		while (FindNextFile (hFile, &findBuffer));
-
 		FindClose (hFile);
 	}
 
 	freep (arg);
-
 	return 0;
 }