2 * COPY.C -- copy internal command.
7 * 01-Aug-98 (Rob Lake z63rrl@morgan.ucs.mun.ca)
10 * 13-Aug-1998 (John P. Price)
11 * fixed memory leak problem in copy function.
12 * fixed copy function so it would work with wildcards in the source
14 * 13-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
15 * Added COPY command to CMD.
17 * 26-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
18 * Replaced CRT io functions by Win32 io functions.
20 * 27-Oct-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
21 * Disabled prompting when used in batch mode.
23 * 03-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
24 * Remove all hardcode string to En.rc
26 * 13-Jul-2005 (Brandon Turner) <turnerb7@msu.edu>)
27 * Rewrite to clean up copy and support wildcard.
29 * 20-Jul-2005 (Brandon Turner) <turnerb7@msu.edu>)
30 * Add touch syntax. "copy arp.exe+,,"
31 * Copy command is now completed.
37 #ifdef INCLUDE_CMD_COPY
41 COPY_ASCII
= 0x001, /* /A */
42 COPY_DECRYPT
= 0x004, /* /D */
43 COPY_VERIFY
= 0x008, /* /V : Dummy, Never will be Impleneted */
44 COPY_SHORTNAME
= 0x010, /* /N : Dummy, Never will be Impleneted */
45 COPY_NO_PROMPT
= 0x020, /* /Y */
46 COPY_PROMPT
= 0x040, /* /-Y */
47 COPY_RESTART
= 0x080, /* /Z */
48 COPY_BINARY
= 0x100, /* /B */
51 #define BUFF_SIZE 16384 /* 16k = max buffer size */
54 int copy (TCHAR source
[MAX_PATH
], TCHAR dest
[MAX_PATH
], INT append
, DWORD lpdwFlags
, BOOL bTouch
)
56 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
66 TCHAR TrueDest
[MAX_PATH
];
67 TCHAR TempSrc
[MAX_PATH
];
72 DebugPrintf (_T("checking mode\n"));
77 hFileSrc
= CreateFile (source
, GENERIC_WRITE
, FILE_SHARE_READ
,
78 NULL
, OPEN_EXISTING
, 0, NULL
);
79 if (hFileSrc
== INVALID_HANDLE_VALUE
)
81 LoadString(CMD_ModuleHandle
, STRING_COPY_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
82 ConOutPrintf(szMsg
, source
);
88 SYSTEMTIME CurrentTime
;
90 GetSystemTime(&CurrentTime
);
91 SystemTimeToFileTime(&CurrentTime
, &NewFileTime
);
92 if(SetFileTime(hFileSrc
,(LPFILETIME
) NULL
, (LPFILETIME
) NULL
, &NewFileTime
))
94 CloseHandle(hFileSrc
);
100 CloseHandle(hFileSrc
);
105 dwAttrib
= GetFileAttributes (source
);
107 hFileSrc
= CreateFile (source
, GENERIC_READ
, FILE_SHARE_READ
,
108 NULL
, OPEN_EXISTING
, 0, NULL
);
109 if (hFileSrc
== INVALID_HANDLE_VALUE
)
111 LoadString(CMD_ModuleHandle
, STRING_COPY_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
112 ConOutPrintf(szMsg
, source
);
118 DebugPrintf (_T("getting time\n"));
121 GetFileTime (hFileSrc
, &srctime
, NULL
, NULL
);
124 DebugPrintf (_T("copy: flags has %s\n"),
125 lpdwFlags
& COPY_ASCII
? "ASCII" : "BINARY");
128 /* Check to see if /D or /Z are true, if so we need a middle
129 man to copy the file too to allow us to use CopyFileEx later */
130 if(lpdwFlags
& COPY_DECRYPT
)
132 GetEnvironmentVariable(_T("TEMP"),TempSrc
,MAX_PATH
);
133 _tcscat(TempSrc
,_T("\\"));
134 FileName
= _tcsrchr(source
,_T('\\'));
136 _tcscat(TempSrc
,FileName
);
137 /* This is needed to be on the end to prevent an error
138 if the user did "copy /D /Z foo bar then it would be copied
139 too %TEMP%\foo here and when %TEMP%\foo when it sets it up
140 for COPY_RESTART, this would mean it is copying to itself
141 which would error when it tried to open the handles for ReadFile
143 _tcscat(TempSrc
,_T(".decrypt"));
144 if(!CopyFileEx(source
, TempSrc
, NULL
, NULL
, FALSE
, COPY_FILE_ALLOW_DECRYPTED_DESTINATION
))
149 _tcscpy(source
, TempSrc
);
153 if(lpdwFlags
& COPY_RESTART
)
155 _tcscpy(TrueDest
, dest
);
156 GetEnvironmentVariable(_T("TEMP"),dest
,MAX_PATH
);
157 _tcscat(dest
,_T("\\"));
158 FileName
= _tcsrchr(TrueDest
,_T('\\'));
160 _tcscat(dest
,FileName
);
165 if (!IsExistingFile (dest
))
168 DebugPrintf (_T("opening/creating\n"));
171 CreateFile (dest
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
175 if (!_tcscmp (dest
, source
))
177 LoadString(CMD_ModuleHandle
, STRING_COPY_ERROR2
, szMsg
, RC_STRING_MAX_SIZE
);
178 ConOutPrintf(szMsg
, source
);
180 CloseHandle (hFileSrc
);
186 DebugPrintf (_T("SetFileAttributes (%s, FILE_ATTRIBUTE_NORMAL);\n"), dest
);
188 SetFileAttributes (dest
, FILE_ATTRIBUTE_NORMAL
);
191 DebugPrintf (_T("DeleteFile (%s);\n"), dest
);
195 hFileDest
= CreateFile (dest
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
199 LONG lFilePosHigh
= 0;
201 if (!_tcscmp (dest
, source
))
203 CloseHandle (hFileSrc
);
208 DebugPrintf (_T("opening/appending\n"));
210 SetFileAttributes (dest
, FILE_ATTRIBUTE_NORMAL
);
213 CreateFile (dest
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
215 /* Move to end of file to start writing */
216 SetFilePointer (hFileDest
, 0, &lFilePosHigh
,FILE_END
);
220 if (hFileDest
== INVALID_HANDLE_VALUE
)
222 CloseHandle (hFileSrc
);
223 ConOutResPuts(STRING_ERROR_PATH_NOT_FOUND
);
227 buffer
= (LPBYTE
)malloc (BUFF_SIZE
);
230 CloseHandle (hFileDest
);
231 CloseHandle (hFileSrc
);
232 ConOutResPuts(STRING_ERROR_OUT_OF_MEMORY
);
239 ReadFile (hFileSrc
, buffer
, BUFF_SIZE
, &dwRead
, NULL
);
240 if (lpdwFlags
& COPY_ASCII
)
242 for (i
= 0; i
< dwRead
; i
++)
244 if (((LPTSTR
)buffer
)[i
] == 0x1A)
256 WriteFile (hFileDest
, buffer
, dwRead
, &dwWritten
, NULL
);
257 if (dwWritten
!= dwRead
)
259 ConOutResPuts(STRING_COPY_ERROR3
);
262 CloseHandle (hFileDest
);
263 CloseHandle (hFileSrc
);
268 while (dwRead
&& !bEof
);
271 DebugPrintf (_T("setting time\n"));
273 SetFileTime (hFileDest
, &srctime
, NULL
, NULL
);
275 if (lpdwFlags
& COPY_ASCII
)
277 ((LPTSTR
)buffer
)[0] = 0x1A;
278 ((LPTSTR
)buffer
)[1] = _T('\0');
280 DebugPrintf (_T("appending ^Z\n"));
282 WriteFile (hFileDest
, buffer
, sizeof(TCHAR
), &dwWritten
, NULL
);
286 CloseHandle (hFileDest
);
287 CloseHandle (hFileSrc
);
290 DebugPrintf (_T("setting mode\n"));
292 SetFileAttributes (dest
, dwAttrib
);
294 /* Now finish off the copy if needed with CopyFileEx */
295 if(lpdwFlags
& COPY_RESTART
)
297 if(!CopyFileEx(dest
, TrueDest
, NULL
, NULL
, FALSE
, COPY_FILE_RESTARTABLE
))
303 /* Take care of file in the temp folder */
308 if(lpdwFlags
& COPY_DECRYPT
)
317 static INT
Overwrite (LPTSTR fn
)
319 /*ask the user if they want to override*/
320 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
322 LoadString(CMD_ModuleHandle
, STRING_COPY_HELP1
, szMsg
, RC_STRING_MAX_SIZE
);
323 ConOutPrintf(szMsg
,fn
);
324 res
= FilePromptYNA (_T(""));
329 INT
cmd_copy (LPTSTR cmd
, LPTSTR param
)
331 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
333 INT argc
, i
, nFiles
, nOverwrite
= 0, nSrc
= -1, nDes
= -1;
334 /* this is the path up to the folder of the src and dest ie C:\windows\ */
335 TCHAR szDestPath
[MAX_PATH
];
336 TCHAR szSrcPath
[MAX_PATH
];
338 /* If this is the type of copy where we are adding files */
339 BOOL bAppend
= FALSE
;
340 WIN32_FIND_DATA findBuffer
;
343 /* Used when something like "copy c*.exe d*.exe" during the process of
344 figuring out the new name */
345 TCHAR tmpName
[MAX_PATH
] = _T("");
346 /* Pointer to keep track of how far through the append input(file1+file2+file3) we are */
347 TCHAR
* appendPointer
= _T("\0");
348 /* The full path to src and dest. This has drive letter, folders, and filename */
349 TCHAR tmpDestPath
[MAX_PATH
];
350 TCHAR tmpSrcPath
[MAX_PATH
];
351 /* A bool on weather or not the destination name will be taking from the input */
352 BOOL bSrcName
= FALSE
;
353 /* Seems like a waste but it is a pointer used to copy from input to PreserveName */
355 /* Stores the name( i.e. blah.txt or blah*.txt) which later we might need */
356 TCHAR PreserveName
[MAX_PATH
];
357 /* for CMDCOPY env */
364 /*Show help/usage info*/
365 if (!_tcsncmp (param
, _T("/?"), 2))
367 ConOutResPaging(TRUE
, STRING_COPY_HELP2
);
373 /* Get the envor value if it exists */
374 evar
= malloc(512 * sizeof(TCHAR
));
375 if (evar
==NULL
) size
= 0;
378 size
= GetEnvironmentVariable (_T("COPYCMD"), evar
, 512);
382 evar
= realloc(evar
,size
* sizeof(TCHAR
) );
385 size
= GetEnvironmentVariable (_T("COPYCMD"), evar
, size
);
393 /* check see if we did get any env variable */
397 /* scan and set the flags */
400 if (_tcsncicmp(_T("/A"),&evar
[t
],2)==0)
402 dwFlags
|=COPY_ASCII
;
406 else if (_tcsncicmp(_T("/B"),&evar
[t
],2)==0)
408 dwFlags
|= COPY_BINARY
;
411 else if (_tcsncicmp(_T("/D"),&evar
[t
],2)==0)
413 dwFlags
|= COPY_DECRYPT
;
417 else if (_tcsncicmp(_T("/V"),&evar
[t
],2)==0)
419 dwFlags
|= COPY_VERIFY
;
423 else if (_tcsncicmp(_T("/N"),&evar
[t
],2)==0)
425 dwFlags
|= COPY_SHORTNAME
;
429 else if (_tcsncicmp(_T("/Y"),&evar
[t
],2)==0)
431 dwFlags
|= COPY_NO_PROMPT
;
435 else if (_tcsncicmp(_T("/-Y"),&evar
[t
],3)==0)
437 dwFlags
|= COPY_PROMPT
;
441 else if (_tcsncicmp(_T("/Z"),&evar
[t
],2)==0)
443 dwFlags
|= COPY_PROMPT
;
451 /*Split the user input into array*/
452 arg
= split (param
, &argc
, FALSE
);
456 /*Read switches and count files*/
457 for (i
= 0; i
< argc
; i
++)
459 if (*arg
[i
] == _T('/'))
461 if (_tcslen(arg
[i
]) >= 2)
463 switch (_totupper(arg
[i
][1]))
467 dwFlags
|= COPY_ASCII
;
471 dwFlags
|= COPY_BINARY
;
475 dwFlags
|= COPY_DECRYPT
;
479 dwFlags
|= COPY_VERIFY
;
483 dwFlags
|= COPY_SHORTNAME
;
487 dwFlags
|= COPY_NO_PROMPT
;
488 dwFlags
&= ~COPY_PROMPT
;
492 if(_tcslen(arg
[i
]) >= 3)
493 if(_totupper(arg
[i
][2]) == _T('Y'))
495 dwFlags
&= ~COPY_NO_PROMPT
;
496 dwFlags
|= COPY_PROMPT
;
502 dwFlags
|= COPY_RESTART
;
507 LoadString(CMD_ModuleHandle
, STRING_ERROR_INVALID_SWITCH
, szMsg
, RC_STRING_MAX_SIZE
);
508 ConOutPrintf(szMsg
, _totupper(arg
[i
][1]));
514 /*If it was a switch, subtract from total arguments*/
519 /*if it isnt a switch then it is the source or destination*/
524 else if(*arg
[i
] == _T('+') || *arg
[i
] == _T(','))
526 /* Add these onto the source string
527 this way we can do all checks
528 directly on source string later on */
529 _tcscat(arg
[nSrc
],arg
[i
]);
540 /* keep quiet within batch files */
543 dwFlags
|= COPY_NO_PROMPT
;
544 dwFlags
&= ~COPY_PROMPT
;
549 /* There is not enough files, there has to be at least 1 */
550 ConOutResPuts(STRING_ERROR_REQ_PARAM_MISSING
);
557 /* there is too many file names in command */
558 LoadString(CMD_ModuleHandle
, STRING_ERROR_TOO_MANY_PARAMETERS
, szMsg
, RC_STRING_MAX_SIZE
);
559 ConErrPrintf(szMsg
,_T(""));
565 if(((_tcschr (arg
[nSrc
], _T('+')) != NULL
) ||
566 (_tcschr (arg
[nSrc
], _T('*')) != NULL
&& _tcschr (arg
[nDes
], _T('*')) == NULL
) ||
567 (IsExistingDirectory (arg
[nSrc
]) && (_tcschr (arg
[nDes
], _T('*')) == NULL
&& !IsExistingDirectory (arg
[nDes
])))
570 /* There is a + in the source filename, this means
571 that there is more then one file being put into
574 if(_tcschr (arg
[nSrc
], _T('+')) != NULL
)
575 appendPointer
= arg
[nSrc
];
578 /* Reusing the number of files variable */
583 /* Set up the string that is the path to the destination */
586 if(_tcslen(arg
[nDes
]) == 2 && arg
[nDes
][1] == _T(':'))
588 GetRootPath(arg
[nDes
],szDestPath
,MAX_PATH
);
591 /* If the user entered two file names then form the full string path*/
592 GetFullPathName (arg
[nDes
], MAX_PATH
, szDestPath
, NULL
);
597 /* If no destination was entered then just use
598 the current directory as the destination */
599 GetCurrentDirectory (MAX_PATH
, szDestPath
);
603 /* Get the full string of the path to the source file*/
604 if(_tcschr (arg
[nSrc
], _T('+')) != NULL
)
606 _tcscpy(tmpName
,_T("\0"));
607 /* Loop through the source file name and copy all
608 the chars one at a time until it gets too + */
611 if(!_tcsncmp (appendPointer
,_T("+"),1) || !_tcsncmp (appendPointer
,_T("\0"),1))
613 /* Now that the pointer is on the + we
614 need to go to the start of the next filename */
615 if(!_tcsncmp (appendPointer
,_T("+"),1))
623 _tcsncat(tmpName
,appendPointer
,1);
627 /* Finish the string off with a null char */
628 _tcsncat(tmpName
,_T("\0"),1);
630 if(_tcschr (arg
[nSrc
], _T(',')) != NULL
)
632 /* Only time there is a , in the source is when they are using touch
633 Cant have a destination and can only have on ,, at the end of the string
634 Cant have more then one file name */
635 szTouch
= _tcsstr (arg
[nSrc
], _T("+"));
636 if(_tcsncmp (szTouch
,_T("+,,\0"),4) || nDes
!= -1)
638 LoadString(CMD_ModuleHandle
, STRING_ERROR_INVALID_PARAM_FORMAT
, szMsg
, RC_STRING_MAX_SIZE
);
639 ConErrPrintf(szMsg
,arg
[nSrc
]);
648 if(_tcslen(tmpName
) == 2)
650 if(tmpName
[1] == _T(':'))
653 GetRootPath(tmpName
,szSrcPath
,MAX_PATH
);
657 /* Get the full path to first file in the string of file names */
658 GetFullPathName (tmpName
, MAX_PATH
, szSrcPath
, NULL
);
663 if(_tcslen(arg
[nSrc
]) == 2 && arg
[nSrc
][1] == _T(':'))
665 GetRootPath(arg
[nSrc
],szSrcPath
,MAX_PATH
);
668 /* Get the full path of the source file */
669 GetFullPathName (arg
[nSrc
], MAX_PATH
, szSrcPath
, NULL
);
673 /* From this point on, we can assume that the shortest path is 3 letters long
674 and that would be [DriveLetter]:\ */
676 /* If there is no * in the path name and it is a folder
677 then we will need to add a wildcard to the pathname
678 so FindFirstFile comes up with all the files in that
680 if(_tcschr (szSrcPath
, _T('*')) == NULL
&&
681 IsExistingDirectory (szSrcPath
))
683 /* If it doesnt have a \ at the end already then on needs to be added */
684 if(szSrcPath
[_tcslen(szSrcPath
) - 1] != _T('\\'))
685 _tcscat (szSrcPath
, _T("\\"));
686 /* Add a wildcard after the \ */
687 _tcscat (szSrcPath
, _T("*"));
689 /* Make sure there is an ending slash to the path if the dest is a folder */
690 if(_tcschr (szDestPath
, _T('*')) == NULL
&&
691 IsExistingDirectory(szDestPath
))
693 if(szDestPath
[_tcslen(szDestPath
) - 1] != _T('\\'))
694 _tcscat (szDestPath
, _T("\\"));
698 /* Get a list of all the files */
699 hFile
= FindFirstFile (szSrcPath
, &findBuffer
);
702 /* We need to figure out what the name of the file in the is going to be */
703 if((szDestPath
[_tcslen(szDestPath
) - 1] == _T('*') && szDestPath
[_tcslen(szDestPath
) - 2] == _T('\\')) ||
704 szDestPath
[_tcslen(szDestPath
) - 1] == _T('\\'))
706 /* In this case we will be using the same name as the source file
707 for the destination file because destination is a folder */
712 /* Save the name the user entered */
713 UseThisName
= _tcsrchr(szDestPath
,_T('\\'));
715 _tcscpy(PreserveName
,UseThisName
);
718 /* Strip the paths back to the folder they are in */
719 for(i
= (_tcslen(szSrcPath
) - 1); i
> -1; i
--)
720 if(szSrcPath
[i
] != _T('\\'))
721 szSrcPath
[i
] = _T('\0');
725 for(i
= (_tcslen(szDestPath
) - 1); i
> -1; i
--)
726 if(szDestPath
[i
] != _T('\\'))
727 szDestPath
[i
] = _T('\0');
733 /* Set the override to yes each new file */
736 /* If it couldnt open the file handle, print out the error */
737 if(hFile
== INVALID_HANDLE_VALUE
)
739 ConOutFormatMessage (GetLastError(), szSrcPath
);
745 /* Ignore the . and .. files */
746 if(!_tcscmp (findBuffer
.cFileName
, _T(".")) ||
747 !_tcscmp (findBuffer
.cFileName
, _T(".."))||
748 findBuffer
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
751 /* Copy the base folder over to a tmp string */
752 _tcscpy(tmpDestPath
,szDestPath
);
754 /* Can't put a file into a folder that isnt there */
755 if(!IsExistingDirectory(szDestPath
))
757 ConOutFormatMessage (GetLastError (), szSrcPath
);
762 /* Copy over the destination path name */
764 _tcscat (tmpDestPath
, findBuffer
.cFileName
);
767 /* If there is no wildcard you can use the name the user entered */
768 if(_tcschr (PreserveName
, _T('*')) == NULL
)
770 _tcscat (tmpDestPath
, PreserveName
);
774 /* The following lines of copy were written by someone else
775 (most likely Eric Khoul) and it was taken from ren.c */
777 TCHAR DoneFile
[MAX_PATH
];
778 /* build destination file name */
779 p
= findBuffer
.cFileName
;
787 while (*p
!= 0 && *p
!= *q
)
814 /* Add the filename to the tmp string path */
815 _tcscat (tmpDestPath
, DoneFile
);
821 /* Build the string path to the source file */
822 _tcscpy(tmpSrcPath
,szSrcPath
);
823 _tcscat (tmpSrcPath
, findBuffer
.cFileName
);
825 /* Check to see if the file is the same file */
826 if(!bTouch
&& !_tcscmp (tmpSrcPath
, tmpDestPath
))
829 /* Handle any overriding / prompting that needs to be done */
830 if(((!(dwFlags
& COPY_NO_PROMPT
) && IsExistingFile (tmpDestPath
)) || dwFlags
& COPY_PROMPT
) && !bTouch
)
831 nOverwrite
= Overwrite(tmpDestPath
);
832 if(nOverwrite
== PROMPT_NO
|| nOverwrite
== PROMPT_BREAK
)
834 if(nOverwrite
== PROMPT_ALL
|| (nOverwrite
== PROMPT_YES
&& bAppend
))
835 dwFlags
|= COPY_NO_PROMPT
;
837 /* Tell weather the copy was successful or not */
838 if(copy(tmpSrcPath
,tmpDestPath
, bAppend
, dwFlags
, bTouch
))
841 /* only print source name when more then one file */
842 if(_tcschr (arg
[nSrc
], _T('+')) != NULL
|| _tcschr (arg
[nSrc
], _T('*')) != NULL
)
843 ConOutPrintf(_T("%s\n"),findBuffer
.cFileName
);
844 //LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
848 /* print out the error message */
849 LoadString(CMD_ModuleHandle
, STRING_COPY_ERROR3
, szMsg
, RC_STRING_MAX_SIZE
);
851 ConOutFormatMessage (GetLastError(), szSrcPath
);
855 /* Loop through all wildcard files */
856 }while(FindNextFile (hFile
, &findBuffer
));
857 /* Loop through all files in src string with a + */
860 /* print out the number of files copied */
861 LoadString(CMD_ModuleHandle
, STRING_COPY_FILE
, szMsg
, RC_STRING_MAX_SIZE
);
862 ConOutPrintf(szMsg
, nFiles
);
872 #endif /* INCLUDE_CMD_COPY */