(Brandon Turner) <turnerb7@msu.edu>) Fixed bug to allow MS style wildcards + code clean up added /y and /-y

svn path=/trunk/; revision=16248
This commit is contained in:
Magnus Olsen 2005-06-23 18:12:17 +00:00
parent f887cf0f05
commit 366a911edc

View file

@ -23,6 +23,10 @@
* *
* 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>) * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
* Remove all hardcode string to En.rc * 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 "precomp.h"
@ -31,32 +35,22 @@
#ifdef INCLUDE_CMD_MOVE #ifdef INCLUDE_CMD_MOVE
#define OVERWRITE_NO 0 enum
#define OVERWRITE_YES 1 {
#define OVERWRITE_ALL 2 MOVE_NOTHING = 0x001, /* /N */
#define OVERWRITE_CANCEL 3 MOVE_OVER_YES = 0x002, /* /Y */
MOVE_OVER_NO = 0x004, /* /-Y */
};
static INT Overwrite (LPTSTR fn) static INT Overwrite (LPTSTR fn)
{ {
/*ask the user if they want to override*/
TCHAR szMsg[RC_STRING_MAX_SIZE]; TCHAR szMsg[RC_STRING_MAX_SIZE];
TCHAR inp[10]; INT res;
LPTSTR p;
LoadString(CMD_ModuleHandle, STRING_MOVE_HELP1, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_MOVE_HELP1, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg,fn); ConOutPrintf(szMsg,fn);
ConInString(inp, 10); res = FilePromptYNA ("");
return res;
_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;
} }
@ -68,12 +62,12 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
INT argc, i, nFiles; INT argc, i, nFiles;
TCHAR szDestPath[MAX_PATH]; TCHAR szDestPath[MAX_PATH];
TCHAR szSrcPath[MAX_PATH]; TCHAR szSrcPath[MAX_PATH];
BOOL bPrompt = TRUE; DWORD dwFlags = 0;
LPTSTR p; INT nOverwrite = 0;
WIN32_FIND_DATA findBuffer; WIN32_FIND_DATA findBuffer;
HANDLE hFile; HANDLE hFile;
LPTSTR pszFile; LPTSTR pszFile;
BOOL bNothing = FALSE;
if (!_tcsncmp (param, _T("/?"), 2)) if (!_tcsncmp (param, _T("/?"), 2))
{ {
@ -103,23 +97,24 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
/* read options */ /* read options */
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
p = arg[i]; if (*arg[i] == _T('/'))
{
if (_tcslen(arg[i]) >= 2)
{
switch (_totupper(arg[i][1]))
{
case _T('N'):
dwFlags |= MOVE_NOTHING;
break;
if (*p == _T('/')) case _T('Y'):
{ dwFlags |= MOVE_OVER_YES;
p++; break;
if (*p == _T('-'))
{ case _T('-'):
p++; dwFlags |= MOVE_OVER_NO;
if (_totupper (*p) == _T('Y')) break;
bPrompt = TRUE;
} }
else
{
if (_totupper (*p) == _T('Y'))
bPrompt = FALSE;
else if (_totupper (*p) == _T('N'))
bNothing = TRUE;
} }
nFiles--; nFiles--;
} }
@ -132,6 +127,13 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
return 1; 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 */ /* get destination */
GetFullPathName (arg[argc - 1], MAX_PATH, szDestPath, NULL); GetFullPathName (arg[argc - 1], MAX_PATH, szDestPath, NULL);
#ifdef _DEBUG #ifdef _DEBUG
@ -154,6 +156,8 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
do do
{ {
nOverwrite = 1;
GetFullPathName (findBuffer.cFileName, MAX_PATH, szSrcPath, &pszFile); GetFullPathName (findBuffer.cFileName, MAX_PATH, szSrcPath, &pszFile);
if (GetFileAttributes (szSrcPath) & FILE_ATTRIBUTE_DIRECTORY) if (GetFileAttributes (szSrcPath) & FILE_ATTRIBUTE_DIRECTORY)
@ -164,91 +168,98 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
DebugPrintf (_T("Move directory \'%s\' to \'%s\'\n"), DebugPrintf (_T("Move directory \'%s\' to \'%s\'\n"),
szSrcPath, szDestPath); szSrcPath, szDestPath);
#endif #endif
if (!bNothing) if (!(dwFlags & MOVE_NOTHING))
{ continue;
MoveFile (szSrcPath, szDestPath); MoveFile (szSrcPath, szDestPath);
} }
}
else else
{ {
/* source is file */ /* source is file */
if (IsExistingFile (szDestPath))
{
/* destination exists */
if (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_DIRECTORY) if (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_DIRECTORY)
{ {
/* destination is existing directory */ /* destination is existing directory */
/*build the dest string(accounts for *)*/
TCHAR szFullDestPath[MAX_PATH]; TCHAR szFullDestPath[MAX_PATH];
_tcscpy (szFullDestPath, szDestPath); _tcscpy (szFullDestPath, szDestPath);
_tcscat (szFullDestPath, _T("\\")); /*this line causes a one to many slashes, GetFullPathName must
_tcscat (szFullDestPath, pszFile); be adding on a \ when it sees that is a dir*/
//_tcscat (szFullDestPath, _T("\\"));
_tcscat (szFullDestPath, findBuffer.cFileName);
/*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;
/*delete the file that might be there first*/
DeleteFile(szFullDestPath);
ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath); ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath);
if (!bNothing) if ((dwFlags & MOVE_NOTHING))
{ continue;
/*delete the file that might be there first*/
DeleteFile(szFullDestPath);
/*move the file*/
if (MoveFile (szSrcPath, szFullDestPath)) if (MoveFile (szSrcPath, szFullDestPath))
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
else else
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg); ConOutPrintf(szMsg);
} }
}
else else
{ {
/* destination is existing file */ /* destination is a file */
INT nOverwrite;
/* must get the overwrite code */ if (_tcschr (arg[argc - 2], _T('*')) != NULL)
if ((nOverwrite = Overwrite (szDestPath)))
{ {
#if 0 /*'*' in src but there should't be one in the dest*/
if (nOverwrite == OVERWRITE_ALL) error_parameter_format('1');
*lpFlags |= FLAG_OVERWRITE_ALL; return 1;
#endif }
/*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;
ConOutPrintf (_T("%s => %s"), szSrcPath, szDestPath); ConOutPrintf (_T("%s => %s"), szSrcPath, szDestPath);
if (!bNothing) 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)) if (MoveFile (szSrcPath, szDestPath))
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
else else
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg); ConOutPrintf(szMsg);
}
}
}
}
else
{
/* destination does not exist */
TCHAR szFullDestPath[MAX_PATH];
GetFullPathName (szDestPath, MAX_PATH, szFullDestPath, NULL);
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);
}
} }
} }
} }
while (FindNextFile (hFile, &findBuffer)); while (FindNextFile (hFile, &findBuffer));
FindClose (hFile); FindClose (hFile);
} }
freep (arg); freep (arg);
return 0; return 0;
} }