📄 copy.c
字号:
}
else if (_tcsncicmp(_T("/Z"),&evar[t],2)==0)
{
dwFlags |= COPY_PROMPT;
t++;
}
}
}
cmd_free(evar);
/*Split the user input into array*/
arg = split (param, &argc, FALSE);
nFiles = argc;
/*Read switches and count files*/
for (i = 0; i < argc; i++)
{
if (*arg[i] == _T('/'))
{
if (_tcslen(arg[i]) >= 2)
{
switch (_totupper(arg[i][1]))
{
case _T('A'):
dwFlags |= COPY_ASCII;
break;
case _T('B'):
dwFlags |= COPY_BINARY;
break;
case _T('D'):
dwFlags |= COPY_DECRYPT;
break;
case _T('V'):
dwFlags |= COPY_VERIFY;
break;
case _T('N'):
dwFlags |= COPY_SHORTNAME;
break;
case _T('Y'):
dwFlags |= COPY_NO_PROMPT;
dwFlags &= ~COPY_PROMPT;
break;
case _T('-'):
if(_tcslen(arg[i]) >= 3)
if(_totupper(arg[i][2]) == _T('Y'))
{
dwFlags &= ~COPY_NO_PROMPT;
dwFlags |= COPY_PROMPT;
}
break;
case _T('Z'):
dwFlags |= COPY_RESTART;
break;
default:
/* invaild switch */
LoadString(CMD_ModuleHandle, STRING_ERROR_INVALID_SWITCH, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg, _totupper(arg[i][1]));
nErrorLevel = 1;
return 1;
break;
}
}
/*If it was a switch, subtract from total arguments*/
nFiles--;
}
else
{
/*if it isnt a switch then it is the source or destination*/
if(nSrc == -1)
{
nSrc = i;
}
else if(*arg[i] == _T('+') || *arg[i] == _T(','))
{
/* Add these onto the source string
this way we can do all checks
directly on source string later on */
_tcscat(arg[nSrc],arg[i]);
nFiles--;
}
else if(nDes == -1)
{
nDes = i;
}
}
}
/* keep quiet within batch files */
if (bc != NULL)
{
dwFlags |= COPY_NO_PROMPT;
dwFlags &= ~COPY_PROMPT;
}
if(nFiles < 1)
{
/* There is not enough files, there has to be at least 1 */
ConOutResPuts(STRING_ERROR_REQ_PARAM_MISSING);
freep (arg);
return 1;
}
if(nFiles > 2)
{
/* there is too many file names in command */
LoadString(CMD_ModuleHandle, STRING_ERROR_TOO_MANY_PARAMETERS, szMsg, RC_STRING_MAX_SIZE);
ConErrPrintf(szMsg,_T(""));
nErrorLevel = 1;
freep (arg);
return 1;
}
if(((_tcschr (arg[nSrc], _T('+')) != NULL) ||
(_tcschr (arg[nSrc], _T('*')) != NULL && _tcschr (arg[nDes], _T('*')) == NULL) ||
(IsExistingDirectory (arg[nSrc]) && (_tcschr (arg[nDes], _T('*')) == NULL && !IsExistingDirectory (arg[nDes])))
))
{
/* There is a + in the source filename, this means
that there is more then one file being put into
one file. */
bAppend = TRUE;
if(_tcschr (arg[nSrc], _T('+')) != NULL)
appendPointer = arg[nSrc];
}
/* Reusing the number of files variable */
nFiles = 0;
do
{
/* Set up the string that is the path to the destination */
if(nDes != -1)
{
if(_tcslen(arg[nDes]) == 2 && arg[nDes][1] == _T(':'))
{
GetRootPath(arg[nDes],szDestPath,MAX_PATH);
}
else
/* If the user entered two file names then form the full string path*/
GetFullPathName (arg[nDes], MAX_PATH, szDestPath, NULL);
}
else
{
/* If no destination was entered then just use
the current directory as the destination */
GetCurrentDirectory (MAX_PATH, szDestPath);
}
/* Get the full string of the path to the source file*/
if(_tcschr (arg[nSrc], _T('+')) != NULL)
{
_tcscpy(tmpName,_T("\0"));
/* Loop through the source file name and copy all
the chars one at a time until it gets too + */
while(TRUE)
{
if(!_tcsncmp (appendPointer,_T("+"),1) || !_tcsncmp (appendPointer,_T("\0"),1))
{
/* Now that the pointer is on the + we
need to go to the start of the next filename */
if(!_tcsncmp (appendPointer,_T("+"),1))
appendPointer++;
else
bDone = TRUE;
break;
}
_tcsncat(tmpName,appendPointer,1);
appendPointer++;
}
/* Finish the string off with a null char */
_tcsncat(tmpName,_T("\0"),1);
if(_tcschr (arg[nSrc], _T(',')) != NULL)
{
/* Only time there is a , in the source is when they are using touch
Cant have a destination and can only have on ,, at the end of the string
Cant have more then one file name */
szTouch = _tcsstr (arg[nSrc], _T("+"));
if(_tcsncmp (szTouch,_T("+,,\0"),4) || nDes != -1)
{
LoadString(CMD_ModuleHandle, STRING_ERROR_INVALID_PARAM_FORMAT, szMsg, RC_STRING_MAX_SIZE);
ConErrPrintf(szMsg,arg[nSrc]);
nErrorLevel = 1;
freep (arg);
return 1;
}
bTouch = TRUE;
bDone = TRUE;
}
if(_tcslen(tmpName) == 2)
{
if(tmpName[1] == _T(':'))
{
GetRootPath(tmpName,szSrcPath,MAX_PATH);
}
}
else
/* Get the full path to first file in the string of file names */
GetFullPathName (tmpName, MAX_PATH, szSrcPath, NULL);
}
else
{
bDone = TRUE;
if(_tcslen(arg[nSrc]) == 2 && arg[nSrc][1] == _T(':'))
{
GetRootPath(arg[nSrc],szSrcPath,MAX_PATH);
}
else
/* Get the full path of the source file */
GetFullPathName (arg[nSrc], MAX_PATH, szSrcPath, NULL);
}
/* From this point on, we can assume that the shortest path is 3 letters long
and that would be [DriveLetter]:\ */
/* If there is no * in the path name and it is a folder
then we will need to add a wildcard to the pathname
so FindFirstFile comes up with all the files in that
folder */
if(_tcschr (szSrcPath, _T('*')) == NULL &&
IsExistingDirectory (szSrcPath))
{
/* If it doesnt have a \ at the end already then on needs to be added */
if(szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\'))
_tcscat (szSrcPath, _T("\\"));
/* Add a wildcard after the \ */
_tcscat (szSrcPath, _T("*"));
}
/* Make sure there is an ending slash to the path if the dest is a folder */
if(_tcschr (szDestPath, _T('*')) == NULL &&
IsExistingDirectory(szDestPath))
{
if(szDestPath[_tcslen(szDestPath) - 1] != _T('\\'))
_tcscat (szDestPath, _T("\\"));
}
/* Get a list of all the files */
hFile = FindFirstFile (szSrcPath, &findBuffer);
/* We need to figure out what the name of the file in the is going to be */
if((szDestPath[_tcslen(szDestPath) - 1] == _T('*') && szDestPath[_tcslen(szDestPath) - 2] == _T('\\')) ||
szDestPath[_tcslen(szDestPath) - 1] == _T('\\'))
{
/* In this case we will be using the same name as the source file
for the destination file because destination is a folder */
bSrcName = TRUE;
}
else
{
/* Save the name the user entered */
UseThisName = _tcsrchr(szDestPath,_T('\\'));
UseThisName++;
_tcscpy(PreserveName,UseThisName);
}
/* Strip the paths back to the folder they are in */
for(i = (_tcslen(szSrcPath) - 1); i > -1; i--)
if(szSrcPath[i] != _T('\\'))
szSrcPath[i] = _T('\0');
else
break;
for(i = (_tcslen(szDestPath) - 1); i > -1; i--)
if(szDestPath[i] != _T('\\'))
szDestPath[i] = _T('\0');
else
break;
do
{
/* Check Breaker */
if(CheckCtrlBreak(BREAK_INPUT))
{
freep(arg);
return 1;
}
/* Set the override to yes each new file */
nOverwrite = 1;
/* If it couldnt open the file handle, print out the error */
if(hFile == INVALID_HANDLE_VALUE)
{
ConOutFormatMessage (GetLastError(), szSrcPath);
freep (arg);
nErrorLevel = 1;
return 1;
}
/* Ignore the . and .. files */
if(!_tcscmp (findBuffer.cFileName, _T(".")) ||
!_tcscmp (findBuffer.cFileName, _T(".."))||
findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
/* Copy the base folder over to a tmp string */
_tcscpy(tmpDestPath,szDestPath);
/* Can't put a file into a folder that isnt there */
if(!IsExistingDirectory(szDestPath))
{
ConOutFormatMessage (GetLastError (), szSrcPath);
freep (arg);
nErrorLevel = 1;
return 1;
}
/* Copy over the destination path name */
if(bSrcName)
_tcscat (tmpDestPath, findBuffer.cFileName);
else
{
/* If there is no wildcard you can use the name the user entered */
if(_tcschr (PreserveName, _T('*')) == NULL)
{
_tcscat (tmpDestPath, PreserveName);
}
else
{
/* The following lines of copy were written by someone else
(most likely Eric Khoul) and it was taken from ren.c */
LPTSTR p,q,r;
TCHAR DoneFile[MAX_PATH];
/* build destination file name */
p = findBuffer.cFileName;
q = PreserveName;
r = DoneFile;
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;
/* Add the filename to the tmp string path */
_tcscat (tmpDestPath, DoneFile);
}
}
/* Build the string path to the source file */
_tcscpy(tmpSrcPath,szSrcPath);
_tcscat (tmpSrcPath, findBuffer.cFileName);
/* Check to see if the file is the same file */
if(!bTouch && !_tcscmp (tmpSrcPath, tmpDestPath))
continue;
/* Handle any overriding / prompting that needs to be done */
if(((!(dwFlags & COPY_NO_PROMPT) && IsExistingFile (tmpDestPath)) || dwFlags & COPY_PROMPT) && !bTouch)
nOverwrite = CopyOverwrite(tmpDestPath);
if(nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
continue;
if(nOverwrite == PROMPT_ALL || (nOverwrite == PROMPT_YES && bAppend))
dwFlags |= COPY_NO_PROMPT;
/* Tell weather the copy was successful or not */
if(copy(tmpSrcPath,tmpDestPath, bAppend, dwFlags, bTouch))
{
nFiles++;
/* only print source name when more then one file */
if(_tcschr (arg[nSrc], _T('+')) != NULL || _tcschr (arg[nSrc], _T('*')) != NULL)
ConOutPrintf(_T("%s\n"),findBuffer.cFileName);
//LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
}
else
{
/* print out the error message */
LoadString(CMD_ModuleHandle, STRING_COPY_ERROR3, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg);
ConOutFormatMessage (GetLastError(), szSrcPath);
nErrorLevel = 1;
}
/* Loop through all wildcard files */
}while(FindNextFile (hFile, &findBuffer));
/* Loop through all files in src string with a + */
}while(!bDone);
/* print out the number of files copied */
LoadString(CMD_ModuleHandle, STRING_COPY_FILE, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg, nFiles);
FindClose(hFile);
if (arg!=NULL)
freep(arg);
return 0;
}
#endif /* INCLUDE_CMD_COPY */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -