Fix whitespace (part 4/x) (convert tabs into 4-space indent).

svn path=/trunk/; revision=59383
This commit is contained in:
Hermès Bélusca-Maïto 2013-06-30 12:27:18 +00:00
parent 16ca7b2a1d
commit 3f69dc1614
10 changed files with 3077 additions and 3168 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -43,23 +43,18 @@
typedef struct tagHISTORY
{
struct tagHISTORY *prev;
struct tagHISTORY *next;
LPTSTR string;
struct tagHISTORY *prev;
struct tagHISTORY *next;
LPTSTR string;
} HIST_ENTRY, * LPHIST_ENTRY;
static INT size,
max_size=100;
static INT size, max_size=100;
static LPHIST_ENTRY Top;
static LPHIST_ENTRY Bottom;
static LPHIST_ENTRY curr_ptr=0;
VOID InitHistory(VOID);
VOID History_move_to_bottom(VOID);
VOID History (INT dir, LPTSTR commandline);
@ -73,297 +68,273 @@ static VOID add_at_bottom(LPTSTR string);
VOID set_size(INT new_size);
INT CommandHistory (LPTSTR param)
{
LPTSTR tmp;
INT tmp_int;
LPHIST_ENTRY h_tmp;
TCHAR szBuffer[2048];
LPTSTR tmp;
INT tmp_int;
LPHIST_ENTRY h_tmp;
TCHAR szBuffer[2048];
tmp=_tcschr(param,_T('/'));
tmp=_tcschr(param,_T('/'));
if (tmp)
{
param=tmp;
switch (_totupper(param[1]))
{
case _T('F'):/*delete history*/
CleanHistory();InitHistory();
break;
if (tmp)
{
param=tmp;
switch (_totupper(param[1]))
{
case _T('F'):/*delete history*/
CleanHistory();InitHistory();
break;
case _T('R'):/*read history from standard in*/
//hIn=GetStdHandle (STD_INPUT_HANDLE);
case _T('R'):/*read history from standard in*/
//hIn=GetStdHandle (STD_INPUT_HANDLE);
for(;;)
{
ConInString(szBuffer,sizeof(szBuffer)/sizeof(TCHAR));
if (*szBuffer!=_T('\0'))
History(0,szBuffer);
else
break;
}
break;
for(;;)
{
ConInString(szBuffer,sizeof(szBuffer)/sizeof(TCHAR));
if (*szBuffer!=_T('\0'))
History(0,szBuffer);
else
break;
}
break;
case _T('A'):/*add an antry*/
History(0,param+2);
break;
case _T('A'):/*add an antry*/
History(0,param+2);
break;
case _T('S'):/*set history size*/
if ((tmp_int=_ttoi(param+2)))
set_size(tmp_int);
break;
case _T('S'):/*set history size*/
if ((tmp_int=_ttoi(param+2)))
set_size(tmp_int);
break;
default:
return 1;
}
}
else
{
for (h_tmp = Top->prev; h_tmp != Bottom; h_tmp = h_tmp->prev)
ConErrPuts(h_tmp->string);
}
return 0;
default:
return 1;
}
}
else
{
for (h_tmp = Top->prev; h_tmp != Bottom; h_tmp = h_tmp->prev)
ConErrPuts(h_tmp->string);
}
return 0;
}
VOID set_size(INT new_size)
{
while (new_size<size)
del(Top->prev);
while (new_size<size)
del(Top->prev);
max_size=new_size;
max_size=new_size;
}
VOID InitHistory(VOID)
{
size=0;
size=0;
Top = cmd_alloc(sizeof(HIST_ENTRY));
Bottom = cmd_alloc(sizeof(HIST_ENTRY));
Top = cmd_alloc(sizeof(HIST_ENTRY));
Bottom = cmd_alloc(sizeof(HIST_ENTRY));
Top->prev = Bottom;
Top->next = NULL;
Top->string = NULL;
Top->prev = Bottom;
Top->next = NULL;
Top->string = NULL;
Bottom->prev = NULL;
Bottom->next = Top;
Bottom->string = NULL;
Bottom->prev = NULL;
Bottom->next = Top;
Bottom->string = NULL;
curr_ptr=Bottom;
curr_ptr=Bottom;
}
VOID CleanHistory(VOID)
{
while (Bottom->next!=Top)
del(Bottom->next);
while (Bottom->next!=Top)
del(Bottom->next);
cmd_free(Top);
cmd_free(Bottom);
cmd_free(Top);
cmd_free(Bottom);
}
VOID History_del_current_entry(LPTSTR str)
{
LPHIST_ENTRY tmp;
LPHIST_ENTRY tmp;
if (size == 0)
return;
if (size == 0)
return;
if (curr_ptr == Bottom)
curr_ptr=Bottom->next;
if (curr_ptr == Bottom)
curr_ptr=Bottom->next;
if (curr_ptr == Top)
curr_ptr=Top->prev;
if (curr_ptr == Top)
curr_ptr=Top->prev;
tmp = curr_ptr;
curr_ptr = curr_ptr->prev;
del(tmp);
History(-1, str);
tmp = curr_ptr;
curr_ptr = curr_ptr->prev;
del(tmp);
History(-1, str);
}
static
VOID del(LPHIST_ENTRY item)
{
if (item==NULL || item==Top || item==Bottom)
{
TRACE ("del in " __FILE__ ": returning\n"
"item is 0x%08x (Bottom is0x%08x)\n",
item, Bottom);
return;
}
if (item==NULL || item==Top || item==Bottom)
{
TRACE ("del in " __FILE__ ": returning\n"
"item is 0x%08x (Bottom is0x%08x)\n",
item, Bottom);
return;
}
/*free string's mem*/
if (item->string)
cmd_free(item->string);
/*set links in prev and next item*/
item->next->prev=item->prev;
item->prev->next=item->next;
/*free string's mem*/
if (item->string)
cmd_free(item->string);
cmd_free(item);
/*set links in prev and next item*/
item->next->prev=item->prev;
item->prev->next=item->next;
cmd_free(item);
size--;
size--;
}
static
VOID add_at_bottom(LPTSTR string)
{
LPHIST_ENTRY tmp;
/*delete first entry if maximum number of entries is reached*/
while(size>=max_size)
del(Top->prev);
LPHIST_ENTRY tmp;
while (_istspace(*string))
string++;
if (*string==_T('\0'))
return;
/*delete first entry if maximum number of entries is reached*/
while(size>=max_size)
del(Top->prev);
/*if new entry is the same than the last do not add it*/
if(size)
if(_tcscmp(string,Bottom->next->string)==0)
return;
while (_istspace(*string))
string++;
/*fill bottom with string, it will become Bottom->next*/
Bottom->string=cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR));
_tcscpy(Bottom->string,string);
if (*string==_T('\0'))
return;
/*save Bottom value*/
tmp=Bottom;
/*create new void Bottom*/
Bottom=cmd_alloc(sizeof(HIST_ENTRY));
Bottom->next=tmp;
Bottom->prev=NULL;
Bottom->string=NULL;
/*if new entry is the same than the last do not add it*/
if(size)
if(_tcscmp(string,Bottom->next->string)==0)
return;
/*fill bottom with string, it will become Bottom->next*/
Bottom->string=cmd_alloc((_tcslen(string)+1)*sizeof(TCHAR));
_tcscpy(Bottom->string,string);
/*save Bottom value*/
tmp=Bottom;
/*create new void Bottom*/
Bottom=cmd_alloc(sizeof(HIST_ENTRY));
Bottom->next=tmp;
Bottom->prev=NULL;
Bottom->string=NULL;
tmp->prev=Bottom;
/*set new size*/
size++;
tmp->prev=Bottom;
/*set new size*/
size++;
}
VOID History_move_to_bottom(VOID)
{
curr_ptr=Bottom;
curr_ptr=Bottom;
}
LPCTSTR PeekHistory(INT dir)
{
LPHIST_ENTRY entry = curr_ptr;
LPHIST_ENTRY entry = curr_ptr;
if (dir == 0)
return NULL;
if (dir == 0)
return NULL;
if (dir < 0)
{
/* key up */
if (entry->next == Top || entry == Top)
{
if (dir < 0)
{
/* key up */
if (entry->next == Top || entry == Top)
{
#ifdef WRAP_HISTORY
entry = Bottom;
entry = Bottom;
#else
return NULL;
return NULL;
#endif
}
entry = entry->next;
}
else
{
/* key down */
if (entry->next == Bottom || entry == Bottom)
{
}
entry = entry->next;
}
else
{
/* key down */
if (entry->next == Bottom || entry == Bottom)
{
#ifdef WRAP_HISTORY
entry = Top;
entry = Top;
#else
return NULL;
return NULL;
#endif
}
entry = entry->prev;
}
}
entry = entry->prev;
}
return entry->string;
return entry->string;
}
VOID History (INT dir, LPTSTR commandline)
{
if(dir==0)
{
add_at_bottom(commandline);
curr_ptr=Bottom;
return;
}
if(dir==0)
{
add_at_bottom(commandline);
curr_ptr=Bottom;
return;
}
if (size==0)
{
commandline[0]=_T('\0');
return;
}
if (size==0)
{
commandline[0]=_T('\0');
return;
}
if(dir<0)/*key up*/
{
if (curr_ptr->next==Top || curr_ptr==Top)
{
if(dir<0)/*key up*/
{
if (curr_ptr->next==Top || curr_ptr==Top)
{
#ifdef WRAP_HISTORY
curr_ptr=Bottom;
curr_ptr=Bottom;
#else
curr_ptr=Top;
commandline[0]=_T('\0');
return;
curr_ptr=Top;
commandline[0]=_T('\0');
return;
#endif
}
}
curr_ptr = curr_ptr->next;
if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string);
}
curr_ptr = curr_ptr->next;
if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string);
}
if(dir>0)
{
if (curr_ptr->prev==Bottom || curr_ptr==Bottom)
{
if(dir>0)
{
if (curr_ptr->prev==Bottom || curr_ptr==Bottom)
{
#ifdef WRAP_HISTORY
curr_ptr=Top;
curr_ptr=Top;
#else
curr_ptr=Bottom;
commandline[0]=_T('\0');
return;
curr_ptr=Bottom;
commandline[0]=_T('\0');
return;
#endif
}
}
curr_ptr=curr_ptr->prev;
if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string);
}
curr_ptr=curr_ptr->prev;
if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string);
}
}
#endif //#if FEATURE_HISTORY

View file

@ -125,7 +125,7 @@
*
* 02-Apr-2004 (Magnus Olsen <magnus@greatlord.com>)
* Remove all hard code string so they can be
* translate to other langues.
* translate to other langues.
*
* 19-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
* Rewrite the CD, it working as Windows 2000 CMD
@ -151,61 +151,61 @@
INT GetRootPath(TCHAR *InPath,TCHAR *OutPath,INT size)
{
if (InPath[0] && InPath[1] == _T(':'))
{
INT t=0;
if (InPath[0] && InPath[1] == _T(':'))
{
INT t=0;
if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
{
t = (InPath[0] - _T('0')) +28;
}
if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
{
t = (InPath[0] - _T('0')) +28;
}
if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
{
t = (InPath[0] - _T('a')) +1;
InPath[0] = t + _T('A') - 1;
}
if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
{
t = (InPath[0] - _T('a')) +1;
InPath[0] = t + _T('A') - 1;
}
if ((InPath[0] >= _T('A')) && (InPath[0] <= _T('Z')))
{
t = (InPath[0] - _T('A')) +1;
}
if ((InPath[0] >= _T('A')) && (InPath[0] <= _T('Z')))
{
t = (InPath[0] - _T('A')) +1;
}
return _tgetdcwd(t,OutPath,size) == NULL;
}
return _tgetdcwd(t,OutPath,size) == NULL;
}
/* Get current directory */
return !GetCurrentDirectory(size,OutPath);
/* Get current directory */
return !GetCurrentDirectory(size,OutPath);
}
BOOL SetRootPath(TCHAR *oldpath, TCHAR *InPath)
{
TCHAR OutPath[MAX_PATH];
TCHAR OutPathTemp[MAX_PATH];
TCHAR OutPath[MAX_PATH];
TCHAR OutPathTemp[MAX_PATH];
/* The use of both of these together will correct the case of a path
/* The use of both of these together will correct the case of a path
where as one alone or GetFullPath will not. Exameple:
c:\windows\SYSTEM32 => C:\WINDOWS\system32 */
if (GetFullPathName(InPath, MAX_PATH, OutPathTemp, NULL))
{
GetPathCase(OutPathTemp, OutPath);
c:\windows\SYSTEM32 => C:\WINDOWS\system32 */
if (GetFullPathName(InPath, MAX_PATH, OutPathTemp, NULL))
{
GetPathCase(OutPathTemp, OutPath);
/* Use _tchdir, since unlike SetCurrentDirectory it updates
* the current-directory-on-drive environment variables. */
if (_tchdir(OutPath) != 0)
{
ConErrFormatMessage(GetLastError());
nErrorLevel = 1;
return FALSE;
}
/* Use _tchdir, since unlike SetCurrentDirectory it updates
* the current-directory-on-drive environment variables. */
if (_tchdir(OutPath) != 0)
{
ConErrFormatMessage(GetLastError());
nErrorLevel = 1;
return FALSE;
}
/* Keep original drive in ordinary CD/CHDIR (without /D switch). */
if (oldpath != NULL && _tcsncicmp(OutPath, oldpath, 2) != 0)
SetCurrentDirectory(oldpath);
}
/* Keep original drive in ordinary CD/CHDIR (without /D switch). */
if (oldpath != NULL && _tcsncicmp(OutPath, oldpath, 2) != 0)
SetCurrentDirectory(oldpath);
}
return TRUE;
return TRUE;
}
@ -215,63 +215,61 @@ BOOL SetRootPath(TCHAR *oldpath, TCHAR *InPath)
*/
INT cmd_chdir (LPTSTR param)
{
TCHAR szCurrent[MAX_PATH];
BOOL bChangeDrive = FALSE;
TCHAR szCurrent[MAX_PATH];
BOOL bChangeDrive = FALSE;
/* Filter out special cases first */
/* Filter out special cases first */
/* Print Help */
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_CD_HELP);
return 0;
}
/* Print Help */
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_CD_HELP);
return 0;
}
/* Remove " */
StripQuotes(param);
/* Remove " */
StripQuotes(param);
/* Set Error Level to Success */
nErrorLevel = 0;
/* Set Error Level to Success */
nErrorLevel = 0;
/* Print Current Directory on a disk */
if (_tcslen(param) == 2 && param[1] == _T(':'))
{
if (GetRootPath(param, szCurrent, MAX_PATH))
{
error_invalid_drive();
return 1;
}
ConOutPuts(szCurrent);
return 0;
}
/* Print Current Directory on a disk */
if (_tcslen(param) == 2 && param[1] == _T(':'))
{
if (GetRootPath(param, szCurrent, MAX_PATH))
{
error_invalid_drive();
return 1;
}
ConOutPuts(szCurrent);
return 0;
}
/* Get Current Directory */
GetCurrentDirectory(MAX_PATH, szCurrent);
if (param[0] == _T('\0'))
{
ConOutPuts(szCurrent);
return 0;
}
/* Get Current Directory */
GetCurrentDirectory(MAX_PATH, szCurrent);
if (param[0] == _T('\0'))
{
ConOutPuts(szCurrent);
return 0;
}
/* Input String Contains /D Switch */
if (!_tcsncicmp(param, _T("/D"), 2))
{
bChangeDrive = TRUE;
param += 2;
while (_istspace(*param))
param++;
}
/* Input String Contains /D Switch */
if (!_tcsncicmp(param, _T("/D"), 2))
{
bChangeDrive = TRUE;
param += 2;
while (_istspace(*param))
param++;
}
if (!SetRootPath(bChangeDrive ? NULL : szCurrent, param))
return 1;
if (!SetRootPath(bChangeDrive ? NULL : szCurrent, param))
return 1;
return 0;
return 0;
}
#endif
#ifdef INCLUDE_CMD_MKDIR
/* Helper funtion for mkdir to make directories in a path.
@ -295,13 +293,15 @@ MakeFullPath(TCHAR * DirPath)
p++; /* skip drive root */
do
{
p = _tcschr(p, _T('\\'));
n = p ? p++ - DirPath : _tcslen(DirPath);
_tcsncpy(path, DirPath, n);
path[n] = _T('\0');
if( !CreateDirectory(path, NULL) &&
(GetLastError() != ERROR_ALREADY_EXISTS))
return FALSE;
p = _tcschr(p, _T('\\'));
n = p ? p++ - DirPath : _tcslen(DirPath);
_tcsncpy(path, DirPath, n);
path[n] = _T('\0');
if( !CreateDirectory(path, NULL) &&
(GetLastError() != ERROR_ALREADY_EXISTS))
{
return FALSE;
}
} while (p != NULL);
return TRUE;
@ -309,46 +309,45 @@ MakeFullPath(TCHAR * DirPath)
/*
* MD / MKDIR
*
*/
INT cmd_mkdir (LPTSTR param)
{
LPTSTR *p;
INT argc, i;
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_MKDIR_HELP);
return 0;
}
LPTSTR *p;
INT argc, i;
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_MKDIR_HELP);
return 0;
}
p = split (param, &argc, FALSE, FALSE);
if (argc == 0)
{
ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
nErrorLevel = 1;
freep(p);
return 1;
}
p = split (param, &argc, FALSE, FALSE);
if (argc == 0)
{
ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING);
nErrorLevel = 1;
freep(p);
return 1;
}
nErrorLevel = 0;
for (i = 0; i < argc; i++)
{
if (!MakeFullPath(p[i]))
{
if(GetLastError() == ERROR_PATH_NOT_FOUND)
{
ConErrResPuts(STRING_MD_ERROR2);
}
else
{
ErrorMessage (GetLastError(), _T("MD"));
}
nErrorLevel = 1;
}
}
nErrorLevel = 0;
for (i = 0; i < argc; i++)
{
if (!MakeFullPath(p[i]))
{
if(GetLastError() == ERROR_PATH_NOT_FOUND)
{
ConErrResPuts(STRING_MD_ERROR2);
}
else
{
ErrorMessage (GetLastError(), _T("MD"));
}
nErrorLevel = 1;
}
}
freep (p);
return nErrorLevel;
freep (p);
return nErrorLevel;
}
#endif
@ -356,214 +355,211 @@ INT cmd_mkdir (LPTSTR param)
#ifdef INCLUDE_CMD_RMDIR
/*
* RD / RMDIR
*
*/
BOOL DeleteFolder(LPTSTR FileName)
{
TCHAR Base[MAX_PATH];
TCHAR TempFileName[MAX_PATH];
HANDLE hFile;
TCHAR Base[MAX_PATH];
TCHAR TempFileName[MAX_PATH];
HANDLE hFile;
WIN32_FIND_DATA f;
_tcscpy(Base,FileName);
_tcscat(Base,_T("\\*"));
hFile = FindFirstFile(Base, &f);
Base[_tcslen(Base) - 1] = _T('\0');
_tcscpy(Base,FileName);
_tcscat(Base,_T("\\*"));
hFile = FindFirstFile(Base, &f);
Base[_tcslen(Base) - 1] = _T('\0');
if (hFile != INVALID_HANDLE_VALUE)
{
do
{
if (!_tcscmp(f.cFileName, _T(".")) ||
if (!_tcscmp(f.cFileName, _T(".")) ||
!_tcscmp(f.cFileName, _T("..")))
continue;
_tcscpy(TempFileName,Base);
_tcscat(TempFileName,f.cFileName);
continue;
_tcscpy(TempFileName,Base);
_tcscat(TempFileName,f.cFileName);
if(f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
DeleteFolder(TempFileName);
else
{
SetFileAttributes(TempFileName,FILE_ATTRIBUTE_NORMAL);
if(!DeleteFile(TempFileName))
return 0;
}
if(f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
DeleteFolder(TempFileName);
else
{
SetFileAttributes(TempFileName,FILE_ATTRIBUTE_NORMAL);
if(!DeleteFile(TempFileName))
return 0;
}
}while (FindNextFile (hFile, &f));
FindClose (hFile);
FindClose (hFile);
}
return RemoveDirectory(FileName);
return RemoveDirectory(FileName);
}
INT cmd_rmdir (LPTSTR param)
{
TCHAR ch;
INT args;
INT dirCount;
LPTSTR *arg;
INT i;
BOOL RD_SUB = FALSE;
BOOL RD_QUIET = FALSE;
INT res;
INT nError = 0;
TCHAR szFullPath[MAX_PATH];
TCHAR ch;
INT args;
INT dirCount;
LPTSTR *arg;
INT i;
BOOL RD_SUB = FALSE;
BOOL RD_QUIET = FALSE;
INT res;
INT nError = 0;
TCHAR szFullPath[MAX_PATH];
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_RMDIR_HELP);
return 0;
}
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_RMDIR_HELP);
return 0;
}
arg = split (param, &args, FALSE, FALSE);
dirCount = 0;
arg = split (param, &args, FALSE, FALSE);
dirCount = 0;
/* check for options anywhere in command line */
for (i = 0; i < args; i++)
{
if (*arg[i] == _T('/'))
{
/*found a command, but check to make sure it has something after it*/
if (_tcslen (arg[i]) == 2)
{
ch = _totupper (arg[i][1]);
/* check for options anywhere in command line */
for (i = 0; i < args; i++)
{
if (*arg[i] == _T('/'))
{
/*found a command, but check to make sure it has something after it*/
if (_tcslen (arg[i]) == 2)
{
ch = _totupper (arg[i][1]);
if (ch == _T('S'))
{
RD_SUB = TRUE;
}
else if (ch == _T('Q'))
{
RD_QUIET = TRUE;
}
}
}
else
{
dirCount++;
}
}
if (ch == _T('S'))
{
RD_SUB = TRUE;
}
else if (ch == _T('Q'))
{
RD_QUIET = TRUE;
}
}
}
else
{
dirCount++;
}
}
if (dirCount == 0)
{
/* No folder to remove */
error_req_param_missing();
freep(arg);
return 1;
}
if (dirCount == 0)
{
/* No folder to remove */
error_req_param_missing();
freep(arg);
return 1;
}
for (i = 0; i < args; i++)
{
if (*arg[i] == _T('/'))
continue;
for (i = 0; i < args; i++)
{
if (*arg[i] == _T('/'))
continue;
if (RD_SUB)
{
/* ask if they want to delete evrything in the folder */
if (!RD_QUIET)
{
res = FilePromptYNA (STRING_DEL_HELP2);
if (res == PROMPT_NO || res == PROMPT_BREAK)
{
nError = 1;
continue;
}
if (res == PROMPT_ALL)
RD_QUIET = TRUE;
}
/* get the folder name */
GetFullPathName(arg[i],MAX_PATH,szFullPath,NULL);
if (RD_SUB)
{
/* ask if they want to delete evrything in the folder */
if (!RD_QUIET)
{
res = FilePromptYNA (STRING_DEL_HELP2);
if (res == PROMPT_NO || res == PROMPT_BREAK)
{
nError = 1;
continue;
}
if (res == PROMPT_ALL)
RD_QUIET = TRUE;
}
/* get the folder name */
GetFullPathName(arg[i],MAX_PATH,szFullPath,NULL);
/* remove trailing \ if any, but ONLY if dir is not the root dir */
if (_tcslen (szFullPath) >= 2 && szFullPath[_tcslen (szFullPath) - 1] == _T('\\'))
szFullPath[_tcslen(szFullPath) - 1] = _T('\0');
/* remove trailing \ if any, but ONLY if dir is not the root dir */
if (_tcslen (szFullPath) >= 2 && szFullPath[_tcslen (szFullPath) - 1] == _T('\\'))
szFullPath[_tcslen(szFullPath) - 1] = _T('\0');
res = DeleteFolder(szFullPath);
}
else
{
res = RemoveDirectory(arg[i]);
}
res = DeleteFolder(szFullPath);
}
else
{
res = RemoveDirectory(arg[i]);
}
if (!res)
{
/* Couldn't delete the folder, print out the error */
nError = GetLastError();
ErrorMessage(nError, _T("RD"));
}
}
if (!res)
{
/* Couldn't delete the folder, print out the error */
nError = GetLastError();
ErrorMessage(nError, _T("RD"));
}
}
freep (arg);
return nError;
freep (arg);
return nError;
}
#endif
/*
* set the exitflag to true
*
*/
INT CommandExit (LPTSTR param)
{
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_EXIT_HELP);
/* Just make sure */
bExit = FALSE;
/* Dont exit */
return 0;
}
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_EXIT_HELP);
/* Just make sure */
bExit = FALSE;
/* Dont exit */
return 0;
}
if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
{
param += 2;
while (_istspace (*param))
param++;
if (_istdigit(*param))
nErrorLevel = _ttoi(param);
ExitBatch();
}
else
bExit = TRUE;
return 0;
if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0)
{
param += 2;
while (_istspace (*param))
param++;
if (_istdigit(*param))
nErrorLevel = _ttoi(param);
ExitBatch();
}
else
{
bExit = TRUE;
}
return 0;
}
#ifdef INCLUDE_CMD_REM
/*
* does nothing
*
*/
INT CommandRem (LPTSTR param)
{
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_REM_HELP);
}
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_REM_HELP);
}
return 0;
return 0;
}
#endif /* INCLUDE_CMD_REM */
INT CommandShowCommands (LPTSTR param)
{
PrintCommandList ();
return 0;
PrintCommandList ();
return 0;
}
INT CommandShowCommandsDetail (LPTSTR param)
{
/* If a param was send, display help of correspondent command */
if (_tcslen(param))
{
DoCommand(param, _T("/?"), NULL);
}
/* Else, display detailed commands list */
else
{
PrintCommandListDetail ();
}
return 0;
/* If a param was send, display help of correspondent command */
if (_tcslen(param))
{
DoCommand(param, _T("/?"), NULL);
}
/* Else, display detailed commands list */
else
{
PrintCommandListDetail ();
}
return 0;
}
/* EOF */

File diff suppressed because it is too large Load diff

View file

@ -9,211 +9,216 @@
/* There is no API for creating junctions, so we must do it the hard way */
static BOOL CreateJunction(LPCTSTR LinkName, LPCTSTR TargetName)
{
/* Structure for reparse point daya when ReparseTag is one of
* the tags defined by Microsoft. Copied from MinGW winnt.h */
typedef struct _REPARSE_DATA_BUFFER {
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
_ANONYMOUS_UNION union {
struct {
WORD SubstituteNameOffset;
WORD SubstituteNameLength;
WORD PrintNameOffset;
WORD PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
WORD SubstituteNameOffset;
WORD SubstituteNameLength;
WORD PrintNameOffset;
WORD PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
BYTE DataBuffer[1];
} GenericReparseBuffer;
} DUMMYUNIONNAME;
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
/* Structure for reparse point daya when ReparseTag is one of
* the tags defined by Microsoft. Copied from MinGW winnt.h */
typedef struct _REPARSE_DATA_BUFFER
{
DWORD ReparseTag;
WORD ReparseDataLength;
WORD Reserved;
_ANONYMOUS_UNION union
{
struct
{
WORD SubstituteNameOffset;
WORD SubstituteNameLength;
WORD PrintNameOffset;
WORD PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct
{
WORD SubstituteNameOffset;
WORD SubstituteNameLength;
WORD PrintNameOffset;
WORD PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct
{
BYTE DataBuffer[1];
} GenericReparseBuffer;
} DUMMYUNIONNAME;
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
HMODULE hNTDLL = GetModuleHandle(_T("NTDLL"));
BOOLEAN (WINAPI *RtlDosPathNameToNtPathName_U)(PCWSTR, PUNICODE_STRING, PCWSTR *, CURDIR *)
= (BOOLEAN (WINAPI *)(PCWSTR, PUNICODE_STRING, PCWSTR *, CURDIR *))GetProcAddress(hNTDLL, "RtlDosPathNameToNtPathName_U");
VOID (WINAPI *RtlFreeUnicodeString)(PUNICODE_STRING)
= (VOID (WINAPI *)(PUNICODE_STRING))GetProcAddress(hNTDLL, "RtlFreeUnicodeString");
HMODULE hNTDLL = GetModuleHandle(_T("NTDLL"));
BOOLEAN (WINAPI *RtlDosPathNameToNtPathName_U)(PCWSTR, PUNICODE_STRING, PCWSTR *, CURDIR *)
= (BOOLEAN (WINAPI *)(PCWSTR, PUNICODE_STRING, PCWSTR *, CURDIR *))GetProcAddress(hNTDLL, "RtlDosPathNameToNtPathName_U");
VOID (WINAPI *RtlFreeUnicodeString)(PUNICODE_STRING)
= (VOID (WINAPI *)(PUNICODE_STRING))GetProcAddress(hNTDLL, "RtlFreeUnicodeString");
TCHAR TargetFullPath[MAX_PATH];
TCHAR TargetFullPath[MAX_PATH];
#ifdef UNICODE
#define TargetFullPathW TargetFullPath
#define TargetFullPathW TargetFullPath
#else
WCHAR TargetFullPathW[MAX_PATH];
WCHAR TargetFullPathW[MAX_PATH];
#endif
UNICODE_STRING TargetNTPath;
HANDLE hJunction;
UNICODE_STRING TargetNTPath;
HANDLE hJunction;
/* The data for this kind of reparse point has two strings:
* The first ("SubstituteName") is the full target path in NT format,
* the second ("PrintName") is the full target path in Win32 format.
* Both of these must be wide-character strings. */
if (!RtlDosPathNameToNtPathName_U ||
!RtlFreeUnicodeString ||
!GetFullPathName(TargetName, MAX_PATH, TargetFullPath, NULL) ||
/* The data for this kind of reparse point has two strings:
* The first ("SubstituteName") is the full target path in NT format,
* the second ("PrintName") is the full target path in Win32 format.
* Both of these must be wide-character strings. */
if (!RtlDosPathNameToNtPathName_U ||
!RtlFreeUnicodeString ||
!GetFullPathName(TargetName, MAX_PATH, TargetFullPath, NULL) ||
#ifndef UNICODE
!MultiByteToWideChar(CP_ACP, 0, TargetFullPath, -1, TargetFullPathW, -1) ||
!MultiByteToWideChar(CP_ACP, 0, TargetFullPath, -1, TargetFullPathW, -1) ||
#endif
!RtlDosPathNameToNtPathName_U(TargetFullPathW, &TargetNTPath, NULL, NULL))
{
return FALSE;
}
!RtlDosPathNameToNtPathName_U(TargetFullPathW, &TargetNTPath, NULL, NULL))
{
return FALSE;
}
/* We have both the names we need, so time to create the junction.
* Start with an empty directory */
if (!CreateDirectory(LinkName, NULL))
{
RtlFreeUnicodeString(&TargetNTPath);
return FALSE;
}
/* We have both the names we need, so time to create the junction.
* Start with an empty directory */
if (!CreateDirectory(LinkName, NULL))
{
RtlFreeUnicodeString(&TargetNTPath);
return FALSE;
}
/* Open the directory we just created */
hJunction = CreateFile(LinkName, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hJunction != INVALID_HANDLE_VALUE)
{
/* Allocate a buffer large enough to hold both strings, including trailing NULs */
SIZE_T TargetLen = wcslen(TargetFullPathW) * sizeof(WCHAR);
DWORD DataSize = (DWORD)(FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer)
+ TargetNTPath.Length + sizeof(WCHAR)
+ TargetLen + sizeof(WCHAR));
PREPARSE_DATA_BUFFER Data = _alloca(DataSize);
/* Open the directory we just created */
hJunction = CreateFile(LinkName, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hJunction != INVALID_HANDLE_VALUE)
{
/* Allocate a buffer large enough to hold both strings, including trailing NULs */
SIZE_T TargetLen = wcslen(TargetFullPathW) * sizeof(WCHAR);
DWORD DataSize = (DWORD)(FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer)
+ TargetNTPath.Length + sizeof(WCHAR)
+ TargetLen + sizeof(WCHAR));
PREPARSE_DATA_BUFFER Data = _alloca(DataSize);
/* Fill it out and use it to turn the directory into a reparse point */
Data->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
Data->ReparseDataLength = (WORD)(DataSize - FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer));
Data->Reserved = 0;
Data->MountPointReparseBuffer.SubstituteNameOffset = 0;
Data->MountPointReparseBuffer.SubstituteNameLength = TargetNTPath.Length;
wcscpy(Data->MountPointReparseBuffer.PathBuffer,
TargetNTPath.Buffer);
Data->MountPointReparseBuffer.PrintNameOffset = TargetNTPath.Length + sizeof(WCHAR);
Data->MountPointReparseBuffer.PrintNameLength = (USHORT)TargetLen;
wcscpy((WCHAR *)((BYTE *)Data->MountPointReparseBuffer.PathBuffer
+ Data->MountPointReparseBuffer.PrintNameOffset),
TargetFullPathW);
if (DeviceIoControl(hJunction, FSCTL_SET_REPARSE_POINT,
Data, DataSize, NULL, 0, &DataSize, NULL))
{
/* Success */
CloseHandle(hJunction);
RtlFreeUnicodeString(&TargetNTPath);
return TRUE;
}
CloseHandle(hJunction);
}
RemoveDirectory(LinkName);
RtlFreeUnicodeString(&TargetNTPath);
return FALSE;
/* Fill it out and use it to turn the directory into a reparse point */
Data->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
Data->ReparseDataLength = (WORD)(DataSize - FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer));
Data->Reserved = 0;
Data->MountPointReparseBuffer.SubstituteNameOffset = 0;
Data->MountPointReparseBuffer.SubstituteNameLength = TargetNTPath.Length;
wcscpy(Data->MountPointReparseBuffer.PathBuffer,
TargetNTPath.Buffer);
Data->MountPointReparseBuffer.PrintNameOffset = TargetNTPath.Length + sizeof(WCHAR);
Data->MountPointReparseBuffer.PrintNameLength = (USHORT)TargetLen;
wcscpy((WCHAR *)((BYTE *)Data->MountPointReparseBuffer.PathBuffer
+ Data->MountPointReparseBuffer.PrintNameOffset),
TargetFullPathW);
if (DeviceIoControl(hJunction, FSCTL_SET_REPARSE_POINT,
Data, DataSize, NULL, 0, &DataSize, NULL))
{
/* Success */
CloseHandle(hJunction);
RtlFreeUnicodeString(&TargetNTPath);
return TRUE;
}
CloseHandle(hJunction);
}
RemoveDirectory(LinkName);
RtlFreeUnicodeString(&TargetNTPath);
return FALSE;
}
INT
cmd_mklink(LPTSTR param)
{
HMODULE hKernel32 = GetModuleHandle(_T("KERNEL32"));
DWORD Flags = 0;
enum { SYMBOLIC, HARD, JUNCTION } LinkType = SYMBOLIC;
INT NumFiles = 0;
LPTSTR Name[2];
INT argc, i;
LPTSTR *arg;
HMODULE hKernel32 = GetModuleHandle(_T("KERNEL32"));
DWORD Flags = 0;
enum { SYMBOLIC, HARD, JUNCTION } LinkType = SYMBOLIC;
INT NumFiles = 0;
LPTSTR Name[2];
INT argc, i;
LPTSTR *arg;
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPuts(STRING_MKLINK_HELP);
return 0;
}
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPuts(STRING_MKLINK_HELP);
return 0;
}
arg = split(param, &argc, FALSE, FALSE);
for (i = 0; i < argc; i++)
{
if (arg[i][0] == _T('/'))
{
if (!_tcsicmp(arg[i], _T("/D")))
Flags |= 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
else if (!_tcsicmp(arg[i], _T("/H")))
LinkType = HARD;
else if (!_tcsicmp(arg[i], _T("/J")))
LinkType = JUNCTION;
else
{
error_invalid_switch(arg[i][1]);
freep(arg);
return 1;
}
}
else
{
if (NumFiles == 2)
{
error_too_many_parameters(arg[i]);
freep(arg);
return 1;
}
Name[NumFiles++] = arg[i];
}
}
freep(arg);
arg = split(param, &argc, FALSE, FALSE);
for (i = 0; i < argc; i++)
{
if (arg[i][0] == _T('/'))
{
if (!_tcsicmp(arg[i], _T("/D")))
Flags |= 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
else if (!_tcsicmp(arg[i], _T("/H")))
LinkType = HARD;
else if (!_tcsicmp(arg[i], _T("/J")))
LinkType = JUNCTION;
else
{
error_invalid_switch(arg[i][1]);
freep(arg);
return 1;
}
}
else
{
if (NumFiles == 2)
{
error_too_many_parameters(arg[i]);
freep(arg);
return 1;
}
Name[NumFiles++] = arg[i];
}
}
freep(arg);
if (NumFiles != 2)
{
error_req_param_missing();
return 1;
}
if (NumFiles != 2)
{
error_req_param_missing();
return 1;
}
nErrorLevel = 0;
nErrorLevel = 0;
if (LinkType == SYMBOLIC)
{
/* CreateSymbolicLink doesn't exist in old versions of Windows,
* so load dynamically */
BOOL (WINAPI *CreateSymbolicLink)(LPCTSTR, LPCTSTR, DWORD)
if (LinkType == SYMBOLIC)
{
/* CreateSymbolicLink doesn't exist in old versions of Windows,
* so load dynamically */
BOOL (WINAPI *CreateSymbolicLink)(LPCTSTR, LPCTSTR, DWORD)
#ifdef UNICODE
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hKernel32, "CreateSymbolicLinkW");
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hKernel32, "CreateSymbolicLinkW");
#else
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hKernel32, "CreateSymbolicLinkA");
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, DWORD))GetProcAddress(hKernel32, "CreateSymbolicLinkA");
#endif
if (CreateSymbolicLink && CreateSymbolicLink(Name[0], Name[1], Flags))
{
ConOutResPrintf(STRING_MKLINK_CREATED_SYMBOLIC, Name[0], Name[1]);
return 0;
}
}
else if (LinkType == HARD)
{
/* CreateHardLink doesn't exist in old versions of Windows,
* so load dynamically */
BOOL (WINAPI *CreateHardLink)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES)
if (CreateSymbolicLink && CreateSymbolicLink(Name[0], Name[1], Flags))
{
ConOutResPrintf(STRING_MKLINK_CREATED_SYMBOLIC, Name[0], Name[1]);
return 0;
}
}
else if (LinkType == HARD)
{
/* CreateHardLink doesn't exist in old versions of Windows,
* so load dynamically */
BOOL (WINAPI *CreateHardLink)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES)
#ifdef UNICODE
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES))GetProcAddress(hKernel32, "CreateHardLinkW");
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES))GetProcAddress(hKernel32, "CreateHardLinkW");
#else
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES))GetProcAddress(hKernel32, "CreateHardLinkA");
= (BOOL (WINAPI *)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES))GetProcAddress(hKernel32, "CreateHardLinkA");
#endif
if (CreateHardLink && CreateHardLink(Name[0], Name[1], NULL))
{
ConOutResPrintf(STRING_MKLINK_CREATED_HARD, Name[0], Name[1]);
return 0;
}
}
else
{
if (CreateJunction(Name[0], Name[1]))
{
ConOutResPrintf(STRING_MKLINK_CREATED_JUNCTION, Name[0], Name[1]);
return 0;
}
}
if (CreateHardLink && CreateHardLink(Name[0], Name[1], NULL))
{
ConOutResPrintf(STRING_MKLINK_CREATED_HARD, Name[0], Name[1]);
return 0;
}
}
else
{
if (CreateJunction(Name[0], Name[1]))
{
ConOutResPrintf(STRING_MKLINK_CREATED_JUNCTION, Name[0], Name[1]);
return 0;
}
}
ErrorMessage(GetLastError(), _T("MKLINK"));
return 1;
ErrorMessage(GetLastError(), _T("MKLINK"));
return 1;
}
#endif /* INCLUDE_CMD_MKLINK */

View file

@ -35,486 +35,478 @@
enum
{
MOVE_NOTHING = 0x001, /* /N */
MOVE_OVER_YES = 0x002, /* /Y */
MOVE_OVER_NO = 0x004, /* /-Y */
MOVE_NOTHING = 0x001, /* /N */
MOVE_OVER_YES = 0x002, /* /Y */
MOVE_OVER_NO = 0x004, /* /-Y */
};
enum
{ /* Move status flags */
MOVE_SOURCE_IS_DIR = 0x001,
MOVE_SOURCE_IS_FILE = 0x002,
MOVE_DEST_IS_DIR = 0x004,
MOVE_DEST_IS_FILE = 0x008,
MOVE_SOURCE_HAS_WILD = 0x010, /* source has wildcard */
MOVE_SRC_CURRENT_IS_DIR = 0x020, /* source is file but at the current round we found a directory */
MOVE_DEST_EXISTS = 0x040,
MOVE_PATHS_ON_DIF_VOL = 0x080 /* source and destination paths are on different volume */
{
/* Move status flags */
MOVE_SOURCE_IS_DIR = 0x001,
MOVE_SOURCE_IS_FILE = 0x002,
MOVE_DEST_IS_DIR = 0x004,
MOVE_DEST_IS_FILE = 0x008,
MOVE_SOURCE_HAS_WILD = 0x010, /* source has wildcard */
MOVE_SRC_CURRENT_IS_DIR = 0x020, /* source is file but at the current round we found a directory */
MOVE_DEST_EXISTS = 0x040,
MOVE_PATHS_ON_DIF_VOL = 0x080 /* source and destination paths are on different volume */
};
static INT MoveOverwrite (LPTSTR fn)
{
/*ask the user if they want to override*/
INT res;
ConOutResPrintf(STRING_MOVE_HELP1, fn);
res = FilePromptYNA (0);
return res;
/* ask the user if they want to override */
INT res;
ConOutResPrintf(STRING_MOVE_HELP1, fn);
res = FilePromptYNA (0);
return res;
}
void GetDirectory (LPTSTR wholepath, LPTSTR directory, BOOL CheckExisting)
{
/* returns only directory part of path with backslash */
/* TODO: make code unc aware */
/* Is there a better alternative to this? */
LPTSTR last;
if (CheckExisting && IsExistingDirectory(wholepath))
{
_tcscpy(directory, wholepath);
}
else if ((last = _tcsrchr(wholepath,_T('\\'))) != NULL)
{
_tcsncpy(directory, wholepath, last - wholepath + 1);
directory[last - wholepath + 1] = 0;
}
else
{
GetRootPath(wholepath,directory, MAX_PATH);
}
/* returns only directory part of path with backslash */
/* TODO: make code unc aware */
/* Is there a better alternative to this? */
LPTSTR last;
if (CheckExisting && IsExistingDirectory(wholepath))
{
_tcscpy(directory, wholepath);
}
else if ((last = _tcsrchr(wholepath,_T('\\'))) != NULL)
{
_tcsncpy(directory, wholepath, last - wholepath + 1);
directory[last - wholepath + 1] = 0;
}
else
{
GetRootPath(wholepath,directory, MAX_PATH);
}
}
INT cmd_move (LPTSTR param)
{
LPTSTR *arg;
INT argc, i, nFiles;
LPTSTR pszDest;
TCHAR szDestPath[MAX_PATH];
TCHAR szFullDestPath[MAX_PATH];
TCHAR szSrcDirPath[MAX_PATH];
TCHAR szSrcPath[MAX_PATH];
TCHAR szFullSrcPath[MAX_PATH];
DWORD dwFlags = 0;
INT nOverwrite = 0;
WIN32_FIND_DATA findBuffer;
HANDLE hFile;
/* used only when source and destination directories are on different volume*/
HANDLE hDestFile;
WIN32_FIND_DATA findDestBuffer;
TCHAR szMoveDest[MAX_PATH];
TCHAR szMoveSrc[MAX_PATH];
LPTSTR pszDestDirPointer;
LPTSTR pszSrcDirPointer;
INT nDirLevel = 0;
LPTSTR pszFile;
BOOL OnlyOneFile;
BOOL FoundFile;
BOOL MoveStatus;
DWORD dwMoveFlags = 0;
DWORD dwMoveStatusFlags = 0;
LPTSTR *arg;
INT argc, i, nFiles;
LPTSTR pszDest;
TCHAR szDestPath[MAX_PATH];
TCHAR szFullDestPath[MAX_PATH];
TCHAR szSrcDirPath[MAX_PATH];
TCHAR szSrcPath[MAX_PATH];
TCHAR szFullSrcPath[MAX_PATH];
DWORD dwFlags = 0;
INT nOverwrite = 0;
WIN32_FIND_DATA findBuffer;
HANDLE hFile;
/* used only when source and destination directories are on different volume */
HANDLE hDestFile;
WIN32_FIND_DATA findDestBuffer;
TCHAR szMoveDest[MAX_PATH];
TCHAR szMoveSrc[MAX_PATH];
LPTSTR pszDestDirPointer;
LPTSTR pszSrcDirPointer;
INT nDirLevel = 0;
if (!_tcsncmp (param, _T("/?"), 2))
{
LPTSTR pszFile;
BOOL OnlyOneFile;
BOOL FoundFile;
BOOL MoveStatus;
DWORD dwMoveFlags = 0;
DWORD dwMoveStatusFlags = 0;
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"
"..."));
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"
"..."));
#else
ConOutResPaging(TRUE,STRING_MOVE_HELP2);
ConOutResPaging(TRUE,STRING_MOVE_HELP2);
#endif
return 0;
}
return 0;
}
nErrorLevel = 0;
arg = splitspace(param, &argc);
nErrorLevel = 0;
arg = splitspace(param, &argc);
/* read options */
for (i = 0; i < argc; i++)
{
if (!_tcsicmp(arg[i], _T("/N")))
dwFlags |= MOVE_NOTHING;
else if (!_tcsicmp(arg[i], _T("/Y")))
dwFlags |= MOVE_OVER_YES;
else if (!_tcsicmp(arg[i], _T("/-Y")))
dwFlags |= MOVE_OVER_NO;
else
break;
}
nFiles = argc - i;
/* read options */
for (i = 0; i < argc; i++)
{
if (!_tcsicmp(arg[i], _T("/N")))
dwFlags |= MOVE_NOTHING;
else if (!_tcsicmp(arg[i], _T("/Y")))
dwFlags |= MOVE_OVER_YES;
else if (!_tcsicmp(arg[i], _T("/-Y")))
dwFlags |= MOVE_OVER_NO;
else
break;
}
nFiles = argc - i;
if (nFiles < 1)
{
/* there must be at least one pathspec */
error_req_param_missing();
freep(arg);
return 1;
}
if (nFiles < 1)
{
/* there must be at least one pathspec */
error_req_param_missing();
freep(arg);
return 1;
}
if (nFiles > 2)
{
/* there are more than two pathspecs */
error_too_many_parameters(param);
freep(arg);
return 1;
}
if (nFiles > 2)
{
/* there are more than two pathspecs */
error_too_many_parameters(param);
freep(arg);
return 1;
}
/* If no destination is given, default to current directory */
pszDest = (nFiles == 1) ? _T(".") : arg[i + 1];
/* If no destination is given, default to current directory */
pszDest = (nFiles == 1) ? _T(".") : arg[i + 1];
/* check for wildcards in source and destination */
if (_tcschr(pszDest, _T('*')) != NULL || _tcschr(pszDest, _T('?')) != NULL)
{
/* '*'/'?' in dest, this doesnt happen. give folder name instead*/
error_invalid_parameter_format(pszDest);
freep(arg);
return 1;
}
if (_tcschr(arg[i], _T('*')) != NULL || _tcschr(arg[i], _T('?')) != NULL)
{
dwMoveStatusFlags |= MOVE_SOURCE_HAS_WILD;
}
/* get destination */
GetFullPathName (pszDest, MAX_PATH, szDestPath, NULL);
TRACE ("Destination: %s\n", debugstr_aw(szDestPath));
/* get source folder */
GetFullPathName(arg[i], MAX_PATH, szSrcDirPath, &pszFile);
if (pszFile != NULL)
*pszFile = _T('\0');
TRACE ("Source Folder: %s\n", debugstr_aw(szSrcDirPath));
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[i]);
freep (arg);
return 1;
}
/* check for wildcards in source and destination */
if (_tcschr(pszDest, _T('*')) != NULL || _tcschr(pszDest, _T('?')) != NULL)
{
/* '*'/'?' in dest, this doesnt happen. give folder name instead*/
error_invalid_parameter_format(pszDest);
freep(arg);
return 1;
}
if (_tcschr(arg[i], _T('*')) != NULL || _tcschr(arg[i], _T('?')) != NULL)
{
dwMoveStatusFlags |= MOVE_SOURCE_HAS_WILD;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* what? we don't have anything to move? */
error_file_not_found();
FindClose(hFile);
freep(arg);
return 1;
}
OnlyOneFile = TRUE;
/* check if there can be found files as files have first priority */
if (findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
dwMoveStatusFlags |= MOVE_SOURCE_IS_DIR;
else
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
while(OnlyOneFile && FindNextFile(hFile,&findBuffer))
{
if (!(findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
ConOutPrintf(_T(""));
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) OnlyOneFile = FALSE;
else
{ /* this has been done this way so that we don't disturb other settings if they have been set before this */
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
dwMoveStatusFlags &= ~MOVE_SOURCE_IS_DIR;
}
}
}
FindClose(hFile);
/* get destination */
GetFullPathName (pszDest, MAX_PATH, szDestPath, NULL);
TRACE ("Destination: %s\n", debugstr_aw(szDestPath));
TRACE ("Do we have only one file: %s\n", OnlyOneFile ? "TRUE" : "FALSE");
/* get source folder */
GetFullPathName(arg[i], MAX_PATH, szSrcDirPath, &pszFile);
if (pszFile != NULL)
*pszFile = _T('\0');
TRACE ("Source Folder: %s\n", debugstr_aw(szSrcDirPath));
/* we have to start again to be sure we don't miss any files or folders*/
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[i]);
freep (arg);
return 1;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* huh? somebody removed files and/or folders which were there */
error_file_not_found();
FindClose(hFile);
freep(arg);
return 1;
}
/* check if source and destination paths are on different volumes */
if (szSrcDirPath[0] != szDestPath[0])
dwMoveStatusFlags |= MOVE_PATHS_ON_DIF_VOL;
/* move it */
do
{
TRACE ("Found file/directory: %s\n", debugstr_aw(findBuffer.cFileName));
nOverwrite = 1;
dwMoveFlags = 0;
dwMoveStatusFlags &= ~MOVE_DEST_IS_FILE &
~MOVE_DEST_IS_DIR &
~MOVE_SRC_CURRENT_IS_DIR &
~MOVE_DEST_EXISTS;
_tcscpy(szFullSrcPath,szSrcDirPath);
if(szFullSrcPath[_tcslen(szFullSrcPath) - 1] != _T('\\'))
_tcscat (szFullSrcPath, _T("\\"));
_tcscat(szFullSrcPath,findBuffer.cFileName);
_tcscpy(szSrcPath, szFullSrcPath);
if (IsExistingDirectory(szSrcPath))
{
/* source is directory */
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE)
{
dwMoveStatusFlags |= MOVE_SRC_CURRENT_IS_DIR; /* source is file but at the current round we found a directory */
continue;
}
TRACE ("Source is dir: %s\n", debugstr_aw(szSrcPath));
dwMoveFlags = MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
/* if source is file we don't need to do anything special */
if (IsExistingDirectory(szDestPath))
{
/* destination is existing directory */
TRACE ("Destination is directory: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
/*build the dest string(accounts for *)*/
_tcscpy (szFullDestPath, szDestPath);
/*check to see if there is an ending slash, if not add one*/
if(szFullDestPath[_tcslen(szFullDestPath) - 1] != _T('\\'))
_tcscat (szFullDestPath, _T("\\"));
_tcscat (szFullDestPath, findBuffer.cFileName);
if (IsExistingFile(szFullDestPath) || IsExistingDirectory(szFullDestPath))
dwMoveStatusFlags |= MOVE_DEST_EXISTS;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (IsExistingFile(szDestPath))
{
/* destination is a file */
TRACE ("Destination is file: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_FILE | MOVE_DEST_EXISTS;
_tcscpy (szFullDestPath, szDestPath);
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
TRACE ("Move Status Flags: 0x%X\n",dwMoveStatusFlags);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_DEST_IS_DIR &&
dwMoveStatusFlags & MOVE_SOURCE_HAS_WILD)
{
/* We are not allowed to have existing source and destination dir when there is wildcard in source */
error_syntax(NULL);
FindClose(hFile);
freep(arg);
return 1;
}
if (!(dwMoveStatusFlags & (MOVE_DEST_IS_FILE | MOVE_DEST_IS_DIR)))
{
/* destination doesn't exist */
_tcscpy (szFullDestPath, szDestPath);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) dwMoveStatusFlags |= MOVE_DEST_IS_FILE;
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE &&
dwMoveStatusFlags & MOVE_DEST_IS_FILE &&
!OnlyOneFile)
{
/*source has many files but there is only one destination file*/
error_invalid_parameter_format(pszDest);
FindClose(hFile);
freep (arg);
return 1;
}
/*checks to make sure user wanted/wants the override*/
if((dwFlags & MOVE_OVER_NO) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
continue;
if(!(dwFlags & MOVE_OVER_YES) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
nOverwrite = MoveOverwrite (szFullDestPath);
if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
continue;
if (nOverwrite == PROMPT_ALL)
dwFlags |= MOVE_OVER_YES;
ConOutPrintf (_T("%s => %s "), szSrcPath, szFullDestPath);
/* are we really supposed to do something */
if (dwFlags & MOVE_NOTHING)
continue;
/*move the file*/
if (!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_PATHS_ON_DIF_VOL))
/* we aren't moving source folder to different drive */
MoveStatus = MoveFileEx (szSrcPath, szFullDestPath, dwMoveFlags);
else
{ /* we are moving source folder to different drive */
_tcscpy(szMoveDest, szFullDestPath);
_tcscpy(szMoveSrc, szSrcPath);
DeleteFile(szMoveDest);
MoveStatus = CreateDirectory(szMoveDest, NULL); /* we use default security settings */
if (MoveStatus)
{
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel = 0;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
MoveStatus = FALSE;
else
{
BOOL FirstTime = TRUE;
FoundFile = TRUE;
MoveStatus = FALSE;
while(FoundFile)
{
if (FirstTime)
FirstTime = FALSE;
else
FoundFile = FindNextFile (hDestFile, &findDestBuffer);
if (!FoundFile)
{ /* Nothing to do in this folder so we stop working on it */
FindClose(hDestFile);
(pszSrcDirPointer)--;
(pszDestDirPointer)--;
_tcscpy(pszSrcDirPointer,_T(""));
_tcscpy(pszDestDirPointer,_T(""));
if (nDirLevel > 0)
{
TCHAR szTempPath[MAX_PATH];
INT_PTR nDiff;
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[i]);
freep (arg);
return 1;
}
FoundFile = TRUE; /* we need to continue our seek for files */
nDirLevel--;
RemoveDirectory(szMoveSrc);
GetDirectory(szMoveSrc,szTempPath,0);
nDiff = _tcslen(szMoveSrc) - _tcslen(szTempPath);
pszSrcDirPointer = pszSrcDirPointer - nDiff;
_tcscpy(pszSrcDirPointer,_T(""));
GetDirectory(szMoveDest,szTempPath,0);
nDiff = _tcslen(szMoveDest) - _tcslen(szTempPath);
pszDestDirPointer = pszDestDirPointer - nDiff;
_tcscpy(pszDestDirPointer,_T(""));
if(szMoveSrc[_tcslen(szMoveSrc) - 1] != _T('\\'))
_tcscat (szMoveSrc, _T("\\"));
if(szMoveDest[_tcslen(szMoveDest) - 1] != _T('\\'))
_tcscat (szMoveDest, _T("\\"));
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
continue;
FirstTime = TRUE;
}
else
{
MoveStatus = TRUE; /* we moved everything so lets tell user about it */
RemoveDirectory(szMoveSrc);
}
continue;
}
/* if we find "." or ".." we'll skip them */
if (_tcscmp(findDestBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findDestBuffer.cFileName,_T("..")) == 0)
continue;
_tcscpy(pszSrcDirPointer, findDestBuffer.cFileName);
_tcscpy(pszDestDirPointer, findDestBuffer.cFileName);
if (IsExistingFile(szMoveSrc))
{
FoundFile = CopyFile(szMoveSrc, szMoveDest, FALSE);
if (!FoundFile) continue;
DeleteFile(szMoveSrc);
}
else
{
FindClose(hDestFile);
CreateDirectory(szMoveDest, NULL);
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel++;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
{
FoundFile = FALSE;
continue;
}
FirstTime = TRUE;
}
}
}
}
}
if (MoveStatus)
ConOutResPrintf(STRING_MOVE_ERROR1);
else
ConOutResPrintf(STRING_MOVE_ERROR2);
}
while ((!OnlyOneFile || dwMoveStatusFlags & MOVE_SRC_CURRENT_IS_DIR ) &&
!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) &&
FindNextFile (hFile, &findBuffer));
FindClose (hFile);
freep (arg);
return 0;
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* what? we don't have anything to move? */
error_file_not_found();
FindClose(hFile);
freep(arg);
return 1;
}
OnlyOneFile = TRUE;
/* check if there can be found files as files have first priority */
if (findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
dwMoveStatusFlags |= MOVE_SOURCE_IS_DIR;
else
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
while(OnlyOneFile && FindNextFile(hFile,&findBuffer))
{
if (!(findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
ConOutPrintf(_T(""));
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) OnlyOneFile = FALSE;
else
{ /* this has been done this way so that we don't disturb other settings if they have been set before this */
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
dwMoveStatusFlags &= ~MOVE_SOURCE_IS_DIR;
}
}
}
FindClose(hFile);
TRACE ("Do we have only one file: %s\n", OnlyOneFile ? "TRUE" : "FALSE");
/* we have to start again to be sure we don't miss any files or folders*/
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[i]);
freep (arg);
return 1;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* huh? somebody removed files and/or folders which were there */
error_file_not_found();
FindClose(hFile);
freep(arg);
return 1;
}
/* check if source and destination paths are on different volumes */
if (szSrcDirPath[0] != szDestPath[0])
dwMoveStatusFlags |= MOVE_PATHS_ON_DIF_VOL;
/* move it */
do
{
TRACE ("Found file/directory: %s\n", debugstr_aw(findBuffer.cFileName));
nOverwrite = 1;
dwMoveFlags = 0;
dwMoveStatusFlags &= ~MOVE_DEST_IS_FILE &
~MOVE_DEST_IS_DIR &
~MOVE_SRC_CURRENT_IS_DIR &
~MOVE_DEST_EXISTS;
_tcscpy(szFullSrcPath,szSrcDirPath);
if(szFullSrcPath[_tcslen(szFullSrcPath) - 1] != _T('\\'))
_tcscat (szFullSrcPath, _T("\\"));
_tcscat(szFullSrcPath,findBuffer.cFileName);
_tcscpy(szSrcPath, szFullSrcPath);
if (IsExistingDirectory(szSrcPath))
{
/* source is directory */
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE)
{
dwMoveStatusFlags |= MOVE_SRC_CURRENT_IS_DIR; /* source is file but at the current round we found a directory */
continue;
}
TRACE ("Source is dir: %s\n", debugstr_aw(szSrcPath));
dwMoveFlags = MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
/* if source is file we don't need to do anything special */
if (IsExistingDirectory(szDestPath))
{
/* destination is existing directory */
TRACE ("Destination is directory: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
/*build the dest string(accounts for *)*/
_tcscpy (szFullDestPath, szDestPath);
/*check to see if there is an ending slash, if not add one*/
if(szFullDestPath[_tcslen(szFullDestPath) - 1] != _T('\\'))
_tcscat (szFullDestPath, _T("\\"));
_tcscat (szFullDestPath, findBuffer.cFileName);
if (IsExistingFile(szFullDestPath) || IsExistingDirectory(szFullDestPath))
dwMoveStatusFlags |= MOVE_DEST_EXISTS;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (IsExistingFile(szDestPath))
{
/* destination is a file */
TRACE ("Destination is file: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_FILE | MOVE_DEST_EXISTS;
_tcscpy (szFullDestPath, szDestPath);
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
TRACE ("Move Status Flags: 0x%X\n",dwMoveStatusFlags);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_DEST_IS_DIR &&
dwMoveStatusFlags & MOVE_SOURCE_HAS_WILD)
{
/* We are not allowed to have existing source and destination dir when there is wildcard in source */
error_syntax(NULL);
FindClose(hFile);
freep(arg);
return 1;
}
if (!(dwMoveStatusFlags & (MOVE_DEST_IS_FILE | MOVE_DEST_IS_DIR)))
{
/* destination doesn't exist */
_tcscpy (szFullDestPath, szDestPath);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) dwMoveStatusFlags |= MOVE_DEST_IS_FILE;
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE &&
dwMoveStatusFlags & MOVE_DEST_IS_FILE &&
!OnlyOneFile)
{
/*source has many files but there is only one destination file*/
error_invalid_parameter_format(pszDest);
FindClose(hFile);
freep (arg);
return 1;
}
/*checks to make sure user wanted/wants the override*/
if((dwFlags & MOVE_OVER_NO) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
continue;
if(!(dwFlags & MOVE_OVER_YES) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
nOverwrite = MoveOverwrite (szFullDestPath);
if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
continue;
if (nOverwrite == PROMPT_ALL)
dwFlags |= MOVE_OVER_YES;
ConOutPrintf (_T("%s => %s "), szSrcPath, szFullDestPath);
/* are we really supposed to do something */
if (dwFlags & MOVE_NOTHING)
continue;
/*move the file*/
if (!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_PATHS_ON_DIF_VOL))
/* we aren't moving source folder to different drive */
MoveStatus = MoveFileEx (szSrcPath, szFullDestPath, dwMoveFlags);
else
{ /* we are moving source folder to different drive */
_tcscpy(szMoveDest, szFullDestPath);
_tcscpy(szMoveSrc, szSrcPath);
DeleteFile(szMoveDest);
MoveStatus = CreateDirectory(szMoveDest, NULL); /* we use default security settings */
if (MoveStatus)
{
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel = 0;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
MoveStatus = FALSE;
else
{
BOOL FirstTime = TRUE;
FoundFile = TRUE;
MoveStatus = FALSE;
while(FoundFile)
{
if (FirstTime)
FirstTime = FALSE;
else
FoundFile = FindNextFile (hDestFile, &findDestBuffer);
if (!FoundFile)
{
/* Nothing to do in this folder so we stop working on it */
FindClose(hDestFile);
(pszSrcDirPointer)--;
(pszDestDirPointer)--;
_tcscpy(pszSrcDirPointer,_T(""));
_tcscpy(pszDestDirPointer,_T(""));
if (nDirLevel > 0)
{
TCHAR szTempPath[MAX_PATH];
INT_PTR nDiff;
FoundFile = TRUE; /* we need to continue our seek for files */
nDirLevel--;
RemoveDirectory(szMoveSrc);
GetDirectory(szMoveSrc,szTempPath,0);
nDiff = _tcslen(szMoveSrc) - _tcslen(szTempPath);
pszSrcDirPointer = pszSrcDirPointer - nDiff;
_tcscpy(pszSrcDirPointer,_T(""));
GetDirectory(szMoveDest,szTempPath,0);
nDiff = _tcslen(szMoveDest) - _tcslen(szTempPath);
pszDestDirPointer = pszDestDirPointer - nDiff;
_tcscpy(pszDestDirPointer,_T(""));
if(szMoveSrc[_tcslen(szMoveSrc) - 1] != _T('\\'))
_tcscat (szMoveSrc, _T("\\"));
if(szMoveDest[_tcslen(szMoveDest) - 1] != _T('\\'))
_tcscat (szMoveDest, _T("\\"));
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
continue;
FirstTime = TRUE;
}
else
{
MoveStatus = TRUE; /* we moved everything so lets tell user about it */
RemoveDirectory(szMoveSrc);
}
continue;
}
/* if we find "." or ".." we'll skip them */
if (_tcscmp(findDestBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findDestBuffer.cFileName,_T("..")) == 0)
continue;
_tcscpy(pszSrcDirPointer, findDestBuffer.cFileName);
_tcscpy(pszDestDirPointer, findDestBuffer.cFileName);
if (IsExistingFile(szMoveSrc))
{
FoundFile = CopyFile(szMoveSrc, szMoveDest, FALSE);
if (!FoundFile) continue;
DeleteFile(szMoveSrc);
}
else
{
FindClose(hDestFile);
CreateDirectory(szMoveDest, NULL);
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel++;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
{
FoundFile = FALSE;
continue;
}
FirstTime = TRUE;
}
}
}
}
}
if (MoveStatus)
ConOutResPrintf(STRING_MOVE_ERROR1);
else
ConOutResPrintf(STRING_MOVE_ERROR2);
}
while ((!OnlyOneFile || dwMoveStatusFlags & MOVE_SRC_CURRENT_IS_DIR ) &&
!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) &&
FindNextFile (hFile, &findBuffer));
FindClose (hFile);
freep (arg);
return 0;
}
#endif /* INCLUDE_CMD_MOVE */

View file

@ -1,6 +1,5 @@
/*
* PARSER.C - command parsing.
*
*/
#include "precomp.h"

View file

@ -51,318 +51,287 @@ enum
*/
INT cmd_rename (LPTSTR param)
{
LPTSTR *arg = NULL;
INT args = 0;
INT nSlash = 0;
INT nEvalArgs = 0; /* nunber of evaluated arguments */
DWORD dwFlags = 0;
DWORD dwFiles = 0; /* number of renamedd files */
INT i;
LPTSTR *arg = NULL;
INT args = 0;
INT nSlash = 0;
INT nEvalArgs = 0; /* nunber of evaluated arguments */
DWORD dwFlags = 0;
DWORD dwFiles = 0; /* number of renamedd files */
INT i;
LPTSTR srcPattern = NULL; /* Source Argument*/
TCHAR srcPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR srcFILE = NULL; /*Contains the files name(s)*/
TCHAR srcFinal[MAX_PATH];
LPTSTR srcPattern = NULL; /* Source Argument*/
TCHAR srcPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR srcFILE = NULL; /*Contains the files name(s)*/
TCHAR srcFinal[MAX_PATH];
LPTSTR dstPattern = NULL; /*Destiny Argument*/
TCHAR dstPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR dstFILE = NULL; /*Contains the files name(s)*/
LPTSTR dstPattern = NULL; /*Destiny Argument*/
TCHAR dstPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR dstFILE = NULL; /*Contains the files name(s)*/
TCHAR dstLast[MAX_PATH]; /*It saves the File name after unmasked with wildcarts*/
TCHAR dstFinal[MAX_PATH]; /*It saves the Final destiny Path*/
TCHAR dstLast[MAX_PATH]; /*It saves the File name after unmasked with wildcarts*/
TCHAR dstFinal[MAX_PATH]; /*It saves the Final destiny Path*/
BOOL bDstWildcard = FALSE;
BOOL bPath = FALSE;
BOOL bDstWildcard = FALSE;
BOOL bPath = FALSE;
LPTSTR p,q,r;
LPTSTR p,q,r;
HANDLE hFile;
WIN32_FIND_DATA f;
/*If the PARAM=/? then show the help*/
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPaging(TRUE,STRING_REN_HELP1);
return 0;
}
nErrorLevel = 0;
/* Split the argument list.Args will be saved in arg vector*/
arg = split(param, &args, FALSE, FALSE);
if (args < 2)
HANDLE hFile;
WIN32_FIND_DATA f;
/*If the PARAM=/? then show the help*/
if (!_tcsncmp(param, _T("/?"), 2))
{
if (!(dwFlags & REN_ERROR))
error_req_param_missing();
freep(arg);
return 1;
ConOutResPaging(TRUE,STRING_REN_HELP1);
return 0;
}
/* Read options */
for (i = 0; i < args; i++)
nErrorLevel = 0;
/* Split the argument list.Args will be saved in arg vector*/
arg = split(param, &args, FALSE, FALSE);
if (args < 2)
{
/* Lets check if we have a special option choosen and set the flag(s)*/
if (*arg[i] == _T('/'))
{
if (_tcslen(arg[i]) >= 2)
{
switch (_totupper(arg[i][1]))
{
case _T('E'):
dwFlags |= REN_ERROR;
break;
case _T('N'):
dwFlags |= REN_NOTHING;
break;
case _T('P'):
dwFlags |= REN_PROMPT;
break;
case _T('Q'):
dwFlags |= REN_QUIET;
break;
case _T('S'):
dwFlags |= REN_SUBDIR;
break;
case _T('T'):
dwFlags |= REN_TOTAL;
break;
}
}
nEvalArgs++;//Save the number of the options.
}
if (!(dwFlags & REN_ERROR))
error_req_param_missing();
freep(arg);
return 1;
}
/* keep quiet within batch files */
if (bc != NULL)
dwFlags |= REN_QUIET;
/* there are only options on the command line --> error!!! */
if (args < nEvalArgs + 2)
/* Read options */
for (i = 0; i < args; i++)
{
if (!(dwFlags & REN_ERROR))
error_req_param_missing();
freep(arg);
return 1;
/* Lets check if we have a special option choosen and set the flag(s)*/
if (*arg[i] == _T('/'))
{
if (_tcslen(arg[i]) >= 2)
{
switch (_totupper(arg[i][1]))
{
case _T('E'):
dwFlags |= REN_ERROR;
break;
case _T('N'):
dwFlags |= REN_NOTHING;
break;
case _T('P'):
dwFlags |= REN_PROMPT;
break;
case _T('Q'):
dwFlags |= REN_QUIET;
break;
case _T('S'):
dwFlags |= REN_SUBDIR;
break;
case _T('T'):
dwFlags |= REN_TOTAL;
break;
}
}
nEvalArgs++;//Save the number of the options.
}
}
/* keep quiet within batch files */
if (bc != NULL)
dwFlags |= REN_QUIET;
/* Get destination pattern and source pattern*/
for (i = 0; i < args; i++)
/* there are only options on the command line --> error!!! */
if (args < nEvalArgs + 2)
{
if (*arg[i] == _T('/'))//We have find an Option.Jump it.
continue;
dstPattern = arg[i]; //we save the Last argument as dstPattern
srcPattern = arg[i-1];
if (!(dwFlags & REN_ERROR))
error_req_param_missing();
freep(arg);
return 1;
}
/* Get destination pattern and source pattern*/
for (i = 0; i < args; i++)
{
if (*arg[i] == _T('/'))//We have find an Option.Jump it.
continue;
dstPattern = arg[i]; //we save the Last argument as dstPattern
srcPattern = arg[i-1];
}
if (_tcschr(srcPattern, _T('\\'))) //Checking if the Source (srcPattern) is a Path to the file
{
bPath= TRUE;
if (_tcschr(srcPattern, _T('\\'))) //Checking if the Source (srcPattern) is a Path to the file
{
bPath= TRUE;
//Splitting srcPath and srcFile.
srcFILE = _tcschr(srcPattern, _T('\\'));
nSlash++;
while(_tcschr(srcFILE, _T('\\')))
{
srcFILE++;
if(*srcFILE==_T('\\')) nSlash++ ;
if(!_tcschr(srcFILE, _T('\\'))) break;
}
_tcsncpy(srcPath,srcPattern,_tcslen(srcPattern)-_tcslen(srcFILE));
srcFILE = _tcschr(srcPattern, _T('\\'));
nSlash++;
while(_tcschr(srcFILE, _T('\\')))
{
srcFILE++;
if(*srcFILE==_T('\\')) nSlash++ ;
if(!_tcschr(srcFILE, _T('\\'))) break;
}
_tcsncpy(srcPath,srcPattern,_tcslen(srcPattern)-_tcslen(srcFILE));
if(_tcschr(dstPattern, _T('\\'))) //Checking if the Destiny (dstPattern)is also a Path.And splitting dstPattern in dstPath and srcPath.
{
dstFILE = _tcschr(dstPattern, _T('\\'));
nSlash=0;
while(_tcschr(dstFILE, _T('\\')))
{
dstFILE++;
if(*dstFILE==_T('\\')) nSlash++ ;
if(!_tcschr(dstFILE, _T('\\'))) break;
}
_tcsncpy(dstPath,dstPattern,_tcslen(dstPattern)-_tcslen(dstFILE));
if((_tcslen(dstPath)!=_tcslen(srcPath))||(_tcsncmp(srcPath,dstPath,_tcslen(srcPath))!=0)) //If it has a Path,then MUST be equal than srcPath
{
error_syntax(dstPath);
freep(arg);
return 1;
}
}
else
{ //If Destiny hasnt a Path,then (MSDN says) srcPath is its Path.
_tcscpy(dstPath,srcPath);
dstFILE=dstPattern;
}
}
if(_tcschr(dstPattern, _T('\\'))) //Checking if the Destiny (dstPattern)is also a Path.And splitting dstPattern in dstPath and srcPath.
{
dstFILE = _tcschr(dstPattern, _T('\\'));
nSlash=0;
while(_tcschr(dstFILE, _T('\\')))
{
dstFILE++;
if(*dstFILE==_T('\\')) nSlash++ ;
if(!_tcschr(dstFILE, _T('\\'))) break;
}
_tcsncpy(dstPath,dstPattern,_tcslen(dstPattern)-_tcslen(dstFILE));
if (!_tcschr(srcPattern, _T('\\'))) //If srcPattern isn't a Path but a name:
{
srcFILE=srcPattern;
if(_tcschr(dstPattern, _T('\\')))
{
error_syntax(dstPattern);
freep(arg);
return 1;
}
else
{
dstFILE=dstPattern;
}
}
if((_tcslen(dstPath)!=_tcslen(srcPath))||(_tcsncmp(srcPath,dstPath,_tcslen(srcPath))!=0)) //If it has a Path,then MUST be equal than srcPath
{
error_syntax(dstPath);
freep(arg);
return 1;
}
}else { //If Destiny hasnt a Path,then (MSDN says) srcPath is its Path.
//Checking Wildcards.
if (_tcschr(dstFILE, _T('*')) || _tcschr(dstFILE, _T('?')))
bDstWildcard = TRUE;
_tcscpy(dstPath,srcPath);
TRACE("\n\nSourcePattern: %s SourcePath: %s SourceFile: %s", debugstr_aw(srcPattern),debugstr_aw(srcPath),debugstr_aw(srcFILE));
TRACE("\n\nDestinationPattern: %s Destination Path:%s Destination File: %s\n", debugstr_aw(dstPattern),debugstr_aw(dstPath),debugstr_aw(dstFILE));
dstFILE=dstPattern;
hFile = FindFirstFile(srcPattern, &f);
}
if (hFile == INVALID_HANDLE_VALUE)
{
if (!(dwFlags & REN_ERROR)) error_file_not_found();
}
do
{
/* ignore "." and ".." */
if (!_tcscmp (f.cFileName, _T(".")) || !_tcscmp (f.cFileName, _T("..")))
continue;
/* do not rename hidden or system files */
if (f.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
continue;
/* do not rename directories when the destination pattern contains
* wildcards, unless option /S is used */
if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
bDstWildcard && !(dwFlags & REN_SUBDIR))
{
continue;
}
}
TRACE("Found source name: %s\n", debugstr_aw(f.cFileName));
/* So here we have splitted the dstFILE and we have find a f.cFileName(thanks to srcPattern)
* Now we have to use the mask (dstFILE) (which can have Wildcards) with f.cFileName to find destination file name(dstLast) */
p = f.cFileName;
q = dstFILE;
r = dstLast;
while(*q != 0)
{
if (*q == '*')
{
q++;
while (*p != 0 && *p != *q)
{
*r = *p;
p++;
r++;
}
}
else if (*q == '?')
{
q++;
if (*p != 0)
{
*r = *p;
p++;
r++;
}
}
else
{
*r = *q;
if (*p != 0) p++;
q++;
r++;
}
}
*r = 0;
//Well we have splitted the Paths,so now we have to paste them again(if needed),thanks bPath.
if(bPath == TRUE)
{
_tcscpy(srcFinal,srcPath);
_tcscat(srcFinal,f.cFileName);
_tcscpy(dstFinal,dstPath);
_tcscat(dstFinal,dstLast);
}
else
{
_tcscpy(srcFinal,f.cFileName);
_tcscpy(dstFinal,dstLast);
}
if (!_tcschr(srcPattern, _T('\\'))) //If srcPattern isn't a Path but a name:
{
srcFILE=srcPattern;
if(_tcschr(dstPattern, _T('\\')))
{
error_syntax(dstPattern);
TRACE("DestinationPath: %s\n", debugstr_aw(dstFinal));
freep(arg);
return 1;
}else dstFILE=dstPattern;
}
if (!(dwFlags & REN_QUIET) && !(dwFlags & REN_TOTAL))
ConOutPrintf(_T("%s -> %s\n"),srcFinal , dstFinal);
//Checking Wildcards.
if (_tcschr(dstFILE, _T('*')) || _tcschr(dstFILE, _T('?')))
bDstWildcard = TRUE;
/* Rename the file */
if (!(dwFlags & REN_NOTHING))
{
if (MoveFile(srcFinal, dstFinal))
{
dwFiles++;
}
else
{
if (!(dwFlags & REN_ERROR))
ConErrResPrintf(STRING_REN_ERROR1, GetLastError());
}
}
}
while (FindNextFile(hFile, &f));
//Closing and Printing errors.
FindClose(hFile);
if (!(dwFlags & REN_QUIET))
{
if (dwFiles == 1)
ConOutResPrintf(STRING_REN_HELP2, dwFiles);
else
ConOutResPrintf(STRING_REN_HELP3, dwFiles);
}
TRACE("\n\nSourcePattern: %s SourcePath: %s SourceFile: %s", debugstr_aw(srcPattern),debugstr_aw(srcPath),debugstr_aw(srcFILE));
TRACE("\n\nDestinationPattern: %s Destination Path:%s Destination File: %s\n", debugstr_aw(dstPattern),debugstr_aw(dstPath),debugstr_aw(dstFILE));
hFile = FindFirstFile(srcPattern, &f);
if (hFile == INVALID_HANDLE_VALUE)
{
if (!(dwFlags & REN_ERROR))
error_file_not_found();
}
do
{
/* ignore "." and ".." */
if (!_tcscmp (f.cFileName, _T(".")) ||
!_tcscmp (f.cFileName, _T("..")))
continue;
/* do not rename hidden or system files */
if (f.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
continue;
/* do not rename directories when the destination pattern contains
* wildcards, unless option /S is used */
if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& bDstWildcard
&& !(dwFlags & REN_SUBDIR))
continue;
TRACE("Found source name: %s\n", debugstr_aw(f.cFileName));
/* So here we have splitted the dstFILE and we have find a f.cFileName(thanks to srcPattern)
* Now we have to use the mask (dstFILE) (which can have Wildcards) with f.cFileName to find destination file name(dstLast) */
p = f.cFileName;
q = dstFILE;
r = dstLast;
while(*q != 0)
{
if (*q == '*')
{
q++;
while (*p != 0 && *p != *q)
{
*r = *p;
p++;
r++;
}
}
else if (*q == '?')
{
q++;
if (*p != 0)
{
*r = *p;
p++;
r++;
}
}
else
{
*r = *q;
if (*p != 0)
p++;
q++;
r++;
}
}
*r = 0;
//Well we have splitted the Paths,so now we have to paste them again(if needed),thanks bPath.
if( bPath == TRUE)
{
_tcscpy(srcFinal,srcPath);
_tcscat(srcFinal,f.cFileName);
_tcscpy(dstFinal,dstPath);
_tcscat(dstFinal,dstLast);
}else{
_tcscpy(srcFinal,f.cFileName);
_tcscpy(dstFinal,dstLast);
}
TRACE("DestinationPath: %s\n", debugstr_aw(dstFinal));
if (!(dwFlags & REN_QUIET) && !(dwFlags & REN_TOTAL))
ConOutPrintf(_T("%s -> %s\n"),srcFinal , dstFinal);
/* Rename the file */
if (!(dwFlags & REN_NOTHING))
{
if (MoveFile(srcFinal, dstFinal))
{
dwFiles++;
}
else
{
if (!(dwFlags & REN_ERROR))
{
ConErrResPrintf(STRING_REN_ERROR1, GetLastError());
}
}
}
}
while (FindNextFile(hFile, &f));
//Closing and Printing errors.
FindClose(hFile);
if (!(dwFlags & REN_QUIET))
{
if (dwFiles == 1)
ConOutResPrintf(STRING_REN_HELP2, dwFiles);
else
ConOutResPrintf(STRING_REN_HELP3, dwFiles);
}
freep(arg);
return 0;
freep(arg);
return 0;
}
#endif

View file

@ -112,7 +112,7 @@ INT cmd_set (LPTSTR param)
Success = seta_eval ( skip_ws(param+2) );
if(!Success)
{
/*might seem random but this is what windows xp does */
/* might seem random but this is what windows xp does */
nErrorLevel = 9165;
}
return !Success;
@ -218,11 +218,11 @@ ident_len(LPCTSTR p)
}
#define PARSE_IDENT(ident,identlen,p) \
identlen = ident_len(p); \
ident = (LPTSTR)alloca ( ( identlen + 1 ) * sizeof(TCHAR) ); \
memmove ( ident, p, identlen * sizeof(TCHAR) ); \
ident[identlen] = 0; \
p += identlen;
identlen = ident_len(p); \
ident = (LPTSTR)alloca ( ( identlen + 1 ) * sizeof(TCHAR) ); \
memmove ( ident, p, identlen * sizeof(TCHAR) ); \
ident[identlen] = 0; \
p += identlen;
static BOOL
seta_identval(LPCTSTR ident, INT* result)