(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

@ -1,29 +1,33 @@
/* /*
* MOVE.C - move internal command. * MOVE.C - move internal command.
* *
* *
* History: * History:
* *
* 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>) * 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
* Started. * Started.
* *
* 18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>) * 18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
* Unicode safe! * Unicode safe!
* Preliminary version!!! * Preliminary version!!!
* *
* 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>) * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
* Redirection safe! * Redirection safe!
* *
* 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>) * 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
* Added help text ("/?"). * Added help text ("/?").
* Added more error checks. * Added more error checks.
* *
* 03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>) * 03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
* Added "/N" option. * Added "/N" option.
* *
* 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"
#include "resource.h" #include "resource.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,29 +62,29 @@ 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))
{ {
#if 0 #if 0
ConOutPuts (_T("Moves files and renames files and directories.\n\n" ConOutPuts (_T("Moves files and renames files and directories.\n\n"
"To move one or more files:\n" "To move one or more files:\n"
"MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n" "MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
"\n" "\n"
"To rename a directory:\n" "To rename a directory:\n"
"MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n" "MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
"\n" "\n"
" [drive:][path]filename1 Specifies the location and name of the file\n" " [drive:][path]filename1 Specifies the location and name of the file\n"
" or files you want to move.\n" " or files you want to move.\n"
" /N Nothing. Don everthing but move files or direcories.\n" " /N Nothing. Don everthing but move files or direcories.\n"
" /Y\n" " /Y\n"
" /-Y\n" " /-Y\n"
"...")); "..."));
#else #else
ConOutResPuts(STRING_MOVE_HELP2); ConOutResPuts(STRING_MOVE_HELP2);
#endif #endif
@ -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 (*p == _T('/'))
{ {
p++; if (_tcslen(arg[i]) >= 2)
if (*p == _T('-'))
{ {
p++; switch (_totupper(arg[i][1]))
if (_totupper (*p) == _T('Y')) {
bPrompt = TRUE; case _T('N'):
} dwFlags |= MOVE_NOTHING;
else break;
{
if (_totupper (*p) == _T('Y')) case _T('Y'):
bPrompt = FALSE; dwFlags |= MOVE_OVER_YES;
else if (_totupper (*p) == _T('N')) break;
bNothing = TRUE;
case _T('-'):
dwFlags |= MOVE_OVER_NO;
break;
}
} }
nFiles--; nFiles--;
} }
@ -132,13 +127,20 @@ 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
DebugPrintf (_T("Destination: %s\n"), szDestPath); DebugPrintf (_T("Destination: %s\n"), szDestPath);
#endif #endif
/* move it*/ /* move it */
for (i = 0; i < argc - 1; i++) for (i = 0; i < argc - 1; i++)
{ {
if (*arg[i] == _T('/')) if (*arg[i] == _T('/'))
@ -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)
@ -162,93 +166,100 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
#ifdef _DEBUG #ifdef _DEBUG
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 (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_DIRECTORY)
if (IsExistingFile (szDestPath))
{ {
/* destination exists */ /* destination is existing directory */
if (GetFileAttributes (szDestPath) & FILE_ATTRIBUTE_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); /*checks to make sure user wanted/wants the override*/
_tcscat (szFullDestPath, _T("\\")); if((dwFlags & MOVE_OVER_NO) && IsExistingFile (szFullDestPath))
_tcscat (szFullDestPath, pszFile); 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 ((dwFlags & MOVE_NOTHING))
{ continue;
if (MoveFile (szSrcPath, szFullDestPath))
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE); /*delete the file that might be there first*/
else DeleteFile(szFullDestPath);
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE); /*move the file*/
ConOutPrintf(szMsg); if (MoveFile (szSrcPath, szFullDestPath))
} LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
}
else else
{ LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
/* destination is existing file */
INT nOverwrite;
/* must get the overwrite code */ ConOutPrintf(szMsg);
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);
}
}
}
} }
else else
{ {
/* destination does not exist */ /* destination is a file */
TCHAR szFullDestPath[MAX_PATH];
GetFullPathName (szDestPath, MAX_PATH, szFullDestPath, NULL); if (_tcschr (arg[argc - 2], _T('*')) != NULL)
ConOutPrintf (_T("%s => %s"), szSrcPath, szFullDestPath);
if (!bNothing)
{ {
if (MoveFile (szSrcPath, szFullDestPath)) /*'*' in src but there should't be one in the dest*/
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE); error_parameter_format('1');
else return 1;
LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR2, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg);
} }
/*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);
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)); while (FindNextFile (hFile, &findBuffer));
FindClose (hFile); FindClose (hFile);
} }
freep (arg); freep (arg);
return 0; return 0;
} }