* Sync up to trunk head (r64894).
[reactos.git] / base / shell / cmd / copy.c
1 /*
2 * COPY.C -- copy internal command.
3 *
4 *
5 * History:
6 *
7 * 01-Aug-98 (Rob Lake z63rrl@morgan.ucs.mun.ca)
8 * started
9 *
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
13 *
14 * 13-Dec-1998 (Eric Kohl)
15 * Added COPY command to CMD.
16 *
17 * 26-Jan-1998 (Eric Kohl)
18 * Replaced CRT io functions by Win32 io functions.
19 *
20 * 27-Oct-1998 (Eric Kohl)
21 * Disabled prompting when used in batch mode.
22 *
23 * 03-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
24 * Remove all hardcode string to En.rc
25 *
26 * 13-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
27 * Rewrite to clean up copy and support wildcard.
28 *
29 * 20-Jul-2005 (Brandon Turner <turnerb7@msu.edu>)
30 * Add touch syntax. "copy arp.exe+,,"
31 * Copy command is now completed.
32 */
33
34 #include "precomp.h"
35
36 #ifdef INCLUDE_CMD_COPY
37
38 enum
39 {
40 COPY_ASCII = 0x001, /* /A */
41 COPY_DECRYPT = 0x004, /* /D */
42 COPY_VERIFY = 0x008, /* /V : Dummy, Never will be Impleneted */
43 COPY_SHORTNAME = 0x010, /* /N : Dummy, Never will be Impleneted */
44 COPY_NO_PROMPT = 0x020, /* /Y */
45 COPY_PROMPT = 0x040, /* /-Y */
46 COPY_RESTART = 0x080, /* /Z */
47 COPY_BINARY = 0x100, /* /B */
48 };
49
50 INT
51 copy(TCHAR source[MAX_PATH],
52 TCHAR dest[MAX_PATH],
53 INT append,
54 DWORD lpdwFlags,
55 BOOL bTouch)
56 {
57 FILETIME srctime,NewFileTime;
58 HANDLE hFileSrc;
59 HANDLE hFileDest;
60 LPBYTE buffer;
61 DWORD dwAttrib;
62 DWORD dwRead;
63 DWORD dwWritten;
64 BOOL bEof = FALSE;
65 TCHAR TrueDest[MAX_PATH];
66 TCHAR TempSrc[MAX_PATH];
67 TCHAR * FileName;
68 SYSTEMTIME CurrentTime;
69
70 /* Check Breaker */
71 if (CheckCtrlBreak(BREAK_INPUT))
72 return 0;
73
74 TRACE ("checking mode\n");
75
76 if (bTouch)
77 {
78 hFileSrc = CreateFile (source, GENERIC_WRITE, FILE_SHARE_READ,
79 NULL, OPEN_EXISTING, 0, NULL);
80 if (hFileSrc == INVALID_HANDLE_VALUE)
81 {
82 ConOutResPrintf(STRING_COPY_ERROR1, source);
83 nErrorLevel = 1;
84 return 0;
85 }
86
87 GetSystemTime(&CurrentTime);
88 SystemTimeToFileTime(&CurrentTime, &NewFileTime);
89 if (SetFileTime(hFileSrc,(LPFILETIME) NULL, (LPFILETIME) NULL, &NewFileTime))
90 {
91 CloseHandle(hFileSrc);
92 nErrorLevel = 1;
93 return 1;
94
95 }
96 else
97 {
98 CloseHandle(hFileSrc);
99 return 0;
100 }
101 }
102
103 dwAttrib = GetFileAttributes (source);
104
105 hFileSrc = CreateFile (source, GENERIC_READ, FILE_SHARE_READ,
106 NULL, OPEN_EXISTING, 0, NULL);
107 if (hFileSrc == INVALID_HANDLE_VALUE)
108 {
109 ConOutResPrintf(STRING_COPY_ERROR1, source);
110 nErrorLevel = 1;
111 return 0;
112 }
113
114 TRACE ("getting time\n");
115
116 GetFileTime (hFileSrc, &srctime, NULL, NULL);
117
118 TRACE ("copy: flags has %s\n",
119 lpdwFlags & COPY_ASCII ? "ASCII" : "BINARY");
120
121 /* Check to see if /D or /Z are true, if so we need a middle
122 man to copy the file too to allow us to use CopyFileEx later */
123 if (lpdwFlags & COPY_DECRYPT)
124 {
125 GetEnvironmentVariable(_T("TEMP"),TempSrc,MAX_PATH);
126 _tcscat(TempSrc,_T("\\"));
127 FileName = _tcsrchr(source,_T('\\'));
128 FileName++;
129 _tcscat(TempSrc,FileName);
130 /* This is needed to be on the end to prevent an error
131 if the user did "copy /D /Z foo bar then it would be copied
132 too %TEMP%\foo here and when %TEMP%\foo when it sets it up
133 for COPY_RESTART, this would mean it is copying to itself
134 which would error when it tried to open the handles for ReadFile
135 and WriteFile */
136 _tcscat(TempSrc,_T(".decrypt"));
137 if (!CopyFileEx(source, TempSrc, NULL, NULL, FALSE, COPY_FILE_ALLOW_DECRYPTED_DESTINATION))
138 {
139 CloseHandle (hFileSrc);
140 nErrorLevel = 1;
141 return 0;
142 }
143 _tcscpy(source, TempSrc);
144 }
145
146
147 if (lpdwFlags & COPY_RESTART)
148 {
149 _tcscpy(TrueDest, dest);
150 GetEnvironmentVariable(_T("TEMP"),dest,MAX_PATH);
151 _tcscat(dest,_T("\\"));
152 FileName = _tcsrchr(TrueDest,_T('\\'));
153 FileName++;
154 _tcscat(dest,FileName);
155 }
156
157
158 if (!IsExistingFile (dest))
159 {
160 TRACE ("opening/creating\n");
161 hFileDest =
162 CreateFile (dest, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
163 }
164 else if (!append)
165 {
166 TRACE ("SetFileAttributes (%s, FILE_ATTRIBUTE_NORMAL);\n", debugstr_aw(dest));
167 SetFileAttributes (dest, FILE_ATTRIBUTE_NORMAL);
168
169 TRACE ("DeleteFile (%s);\n", debugstr_aw(dest));
170 DeleteFile (dest);
171
172 hFileDest = CreateFile (dest, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
173 }
174 else
175 {
176 LONG lFilePosHigh = 0;
177
178 if (!_tcscmp (dest, source))
179 {
180 CloseHandle (hFileSrc);
181 return 0;
182 }
183
184 TRACE ("opening/appending\n");
185 SetFileAttributes (dest, FILE_ATTRIBUTE_NORMAL);
186
187 hFileDest =
188 CreateFile (dest, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
189
190 /* Move to end of file to start writing */
191 SetFilePointer (hFileDest, 0, &lFilePosHigh,FILE_END);
192 }
193
194
195 if (hFileDest == INVALID_HANDLE_VALUE)
196 {
197 CloseHandle (hFileSrc);
198 ConOutResPuts(STRING_ERROR_PATH_NOT_FOUND);
199 nErrorLevel = 1;
200 return 0;
201 }
202
203 /* A page-aligned buffer usually give more speed */
204 buffer = VirtualAlloc(NULL, BUFF_SIZE, MEM_COMMIT, PAGE_READWRITE);
205 if (buffer == NULL)
206 {
207 CloseHandle (hFileDest);
208 CloseHandle (hFileSrc);
209 ConOutResPuts(STRING_ERROR_OUT_OF_MEMORY);
210 nErrorLevel = 1;
211 return 0;
212 }
213
214 do
215 {
216 ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL);
217 if (lpdwFlags & COPY_ASCII)
218 {
219 LPBYTE pEof = memchr(buffer, 0x1A, dwRead);
220 if (pEof != NULL)
221 {
222 bEof = TRUE;
223 dwRead = pEof-buffer+1;
224 break;
225 }
226 }
227
228 if (dwRead == 0)
229 break;
230
231 WriteFile (hFileDest, buffer, dwRead, &dwWritten, NULL);
232 if (dwWritten != dwRead || CheckCtrlBreak(BREAK_INPUT))
233 {
234 ConOutResPuts(STRING_COPY_ERROR3);
235
236 VirtualFree (buffer, 0, MEM_RELEASE);
237 CloseHandle (hFileDest);
238 CloseHandle (hFileSrc);
239 nErrorLevel = 1;
240 return 0;
241 }
242 }
243 while (!bEof);
244
245 TRACE ("setting time\n");
246 SetFileTime (hFileDest, &srctime, NULL, NULL);
247
248 if ((lpdwFlags & COPY_ASCII) && !bEof)
249 {
250 /* we're dealing with ASCII files! */
251 buffer[0] = 0x1A;
252 TRACE ("appending ^Z\n");
253 WriteFile (hFileDest, buffer, sizeof(CHAR), &dwWritten, NULL);
254 }
255
256 VirtualFree (buffer, 0, MEM_RELEASE);
257 CloseHandle (hFileDest);
258 CloseHandle (hFileSrc);
259
260 TRACE ("setting mode\n");
261 SetFileAttributes (dest, dwAttrib);
262
263 /* Now finish off the copy if needed with CopyFileEx */
264 if (lpdwFlags & COPY_RESTART)
265 {
266 if (!CopyFileEx(dest, TrueDest, NULL, NULL, FALSE, COPY_FILE_RESTARTABLE))
267 {
268 nErrorLevel = 1;
269 DeleteFile(dest);
270 return 0;
271 }
272 /* Take care of file in the temp folder */
273 DeleteFile(dest);
274
275 }
276
277 if (lpdwFlags & COPY_DECRYPT)
278 DeleteFile(TempSrc);
279
280 return 1;
281 }
282
283
284 static INT CopyOverwrite (LPTSTR fn)
285 {
286 /*ask the user if they want to override*/
287 INT res;
288 ConOutResPrintf(STRING_COPY_HELP1, fn);
289 res = FilePromptYNA (0);
290 return res;
291 }
292
293 /* The following lines of copy were written by someone else
294 (most likely Eric Kohl) and it was taken from ren.c */
295 static void
296 BuildFileName(
297 LPTSTR pszSource,
298 LPTSTR pszTarget,
299 LPTSTR pszOutput)
300 {
301 /* build destination file name */
302 while (*pszTarget != 0)
303 {
304 if (*pszTarget == _T('*'))
305 {
306 pszTarget++;
307 while ((*pszSource != 0) && (*pszSource != *pszTarget))
308 {
309 *pszOutput++ = *pszSource++;
310 }
311 }
312 else if (*pszTarget == _T('?'))
313 {
314 pszTarget++;
315 if (*pszSource != 0)
316 {
317 *pszOutput++ = *pszSource++;
318 }
319 }
320 else
321 {
322 *pszOutput++ = *pszTarget++;
323 if (*pszSource != 0)
324 pszSource++;
325 }
326 }
327
328 *pszOutput = 0;
329 }
330
331 INT cmd_copy(LPTSTR param)
332 {
333 LPTSTR *arg;
334 INT argc, i, nFiles, nOverwrite = 0, nSrc = -1, nDes = -1;
335 /* this is the path up to the folder of the src and dest ie C:\windows\ */
336 TCHAR szDestPath[MAX_PATH];
337 TCHAR szSrcPath[MAX_PATH];
338 DWORD dwFlags = 0;
339 /* If this is the type of copy where we are adding files */
340 BOOL bAppend = FALSE;
341 WIN32_FIND_DATA findBuffer;
342 HANDLE hFile = NULL;
343 BOOL bTouch = FALSE;
344 /* Used when something like "copy c*.exe d*.exe" during the process of
345 figuring out the new name */
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 */
354 TCHAR * UseThisName;
355 /* for CMDCOPY env */
356 TCHAR *evar;
357 int size;
358 TCHAR * szTouch;
359 BOOL bHasWildcard, bDone = FALSE, bMoreFiles = FALSE;
360 BOOL bMultipleSource = FALSE, bMultipleDest = FALSE;
361
362
363 /* Show help/usage info */
364 if (!_tcsncmp(param, _T("/?"), 2))
365 {
366 ConOutResPaging(TRUE, STRING_COPY_HELP2);
367 return 0;
368 }
369
370 nErrorLevel = 0;
371
372 /* Get the envor value if it exists */
373 evar = cmd_alloc(512 * sizeof(TCHAR));
374 if (evar == NULL)
375 size = 0;
376 else
377 size = GetEnvironmentVariable (_T("COPYCMD"), evar, 512);
378
379 if (size > 512)
380 {
381 TCHAR *old_evar = evar;
382 evar = cmd_realloc(evar,size * sizeof(TCHAR) );
383 if (evar!=NULL)
384 size = GetEnvironmentVariable (_T("COPYCMD"), evar, size);
385 else
386 {
387 size=0;
388 evar = old_evar;
389 }
390 }
391
392 /* check see if we did get any env variable */
393 if (size != 0)
394 {
395 int t = 0;
396
397 /* scan and set the flags */
398 for (t = 0; t < size; t++)
399 {
400 if (_tcsncicmp(_T("/A"),&evar[t],2) == 0)
401 {
402 dwFlags |=COPY_ASCII;
403 t++;
404 }
405 else if (_tcsncicmp(_T("/B"),&evar[t],2) == 0)
406 {
407 dwFlags |= COPY_BINARY;
408 t++;
409 }
410 else if (_tcsncicmp(_T("/D"),&evar[t],2) == 0)
411 {
412 dwFlags |= COPY_DECRYPT;
413 t++;
414 }
415 else if (_tcsncicmp(_T("/V"),&evar[t],2) == 0)
416 {
417 dwFlags |= COPY_VERIFY;
418 t++;
419 }
420 else if (_tcsncicmp(_T("/N"),&evar[t],2) == 0)
421 {
422 dwFlags |= COPY_SHORTNAME;
423 t++;
424 }
425 else if (_tcsncicmp(_T("/Y"),&evar[t],2) == 0)
426 {
427 dwFlags |= COPY_NO_PROMPT;
428 t++;
429 }
430 else if (_tcsncicmp(_T("/-Y"),&evar[t],3) == 0)
431 {
432 dwFlags |= COPY_PROMPT;
433 t+=2;
434 }
435 else if (_tcsncicmp(_T("/Z"),&evar[t],2) == 0)
436 {
437 dwFlags |= COPY_PROMPT;
438 t++;
439 }
440 }
441 }
442 cmd_free(evar);
443
444
445 /* Split the user input into array */
446 arg = split(param, &argc, FALSE, TRUE);
447 nFiles = argc;
448
449 /* Read switches and count files */
450 for (i = 0; i < argc; i++)
451 {
452 if (*arg[i] == _T('/'))
453 {
454 if (_tcslen(arg[i]) >= 2)
455 {
456 switch (_totupper(arg[i][1]))
457 {
458 case _T('A'):
459 dwFlags |= COPY_ASCII;
460 break;
461
462 case _T('B'):
463 dwFlags |= COPY_BINARY;
464 break;
465
466 case _T('D'):
467 dwFlags |= COPY_DECRYPT;
468 break;
469
470 case _T('V'):
471 dwFlags |= COPY_VERIFY;
472 break;
473
474 case _T('N'):
475 dwFlags |= COPY_SHORTNAME;
476 break;
477
478 case _T('Y'):
479 dwFlags |= COPY_NO_PROMPT;
480 dwFlags &= ~COPY_PROMPT;
481 break;
482
483 case _T('-'):
484 if (_tcslen(arg[i]) >= 3)
485 if (_totupper(arg[i][2]) == _T('Y'))
486 {
487 dwFlags &= ~COPY_NO_PROMPT;
488 dwFlags |= COPY_PROMPT;
489 }
490
491 break;
492
493 case _T('Z'):
494 dwFlags |= COPY_RESTART;
495 break;
496
497 default:
498 /* Invalid switch */
499 ConOutResPrintf(STRING_ERROR_INVALID_SWITCH, _totupper(arg[i][1]));
500 nErrorLevel = 1;
501 freep (arg);
502 return 1;
503 break;
504 }
505 }
506 /* If it was a switch, subtract from total arguments */
507 nFiles--;
508 }
509 else
510 {
511 /* If it isn't a switch then it is the source or destination */
512 if (nSrc == -1)
513 {
514 nSrc = i;
515 }
516 else if (*arg[i] == _T('+'))
517 {
518 /* Next file should be appended */
519 bMoreFiles = TRUE;
520 nFiles -= 1;
521 }
522 else if (bMoreFiles)
523 {
524 /* Add this file to the source string
525 this way we can do all checks
526 directly on source string later on */
527 TCHAR * ptr;
528 int length = (_tcslen(arg[nSrc]) + _tcslen(arg[i]) + 2) * sizeof(TCHAR);
529 ptr = cmd_alloc(length);
530 if (ptr)
531 {
532 _tcscpy(ptr, arg[nSrc]);
533 _tcscat(ptr, _T("|"));
534 _tcscat(ptr, arg[i]);
535 cmd_free(arg[nSrc]);
536 arg[nSrc] = ptr;
537 nFiles -= 1;
538 }
539
540 bMoreFiles = FALSE;
541 }
542 else if (nDes == -1)
543 {
544 nDes = i;
545 }
546 }
547 }
548
549 /* keep quiet within batch files */
550 if (bc != NULL)
551 {
552 dwFlags |= COPY_NO_PROMPT;
553 dwFlags &= ~COPY_PROMPT;
554 }
555
556 if (nFiles < 1)
557 {
558 /* There are not enough files, there has to be at least 1 */
559 ConOutResPuts(STRING_ERROR_REQ_PARAM_MISSING);
560 freep(arg);
561 return 1;
562 }
563
564 if (nFiles > 2)
565 {
566 /* There are too many file names in command */
567 ConErrResPrintf(STRING_ERROR_TOO_MANY_PARAMETERS,_T(""));
568 nErrorLevel = 1;
569 freep(arg);
570 return 1;
571 }
572
573 if ((_tcschr(arg[nSrc], _T('|')) != NULL) ||
574 (_tcschr(arg[nSrc], _T('*')) != NULL) ||
575 (_tcschr(arg[nSrc], _T('?')) != NULL) ||
576 IsExistingDirectory(arg[nSrc]))
577 {
578 bMultipleSource = TRUE;
579 }
580
581 /* Reusing the number of files variable */
582 nFiles = 0;
583
584 /* Check if no destination argument is passed */
585 if (nDes == -1)
586 {
587 /* If no destination was entered then just use
588 the current directory as the destination */
589 GetCurrentDirectory(MAX_PATH, szDestPath);
590 }
591 else
592 {
593 /* Check if the destination is 'x:' */
594 if ((arg[nDes][1] == _T(':')) && (arg[nDes][2] == _T('\0')))
595 {
596 GetRootPath(arg[nDes], szDestPath, MAX_PATH);
597 }
598 else
599 {
600 /* If the user entered two file names then form the full string path */
601 GetFullPathName(arg[nDes], MAX_PATH, szDestPath, NULL);
602 }
603
604 /* Make sure there is an ending slash to the path if the dest is a folder */
605 if ((_tcschr(szDestPath, _T('*')) == NULL) &&
606 IsExistingDirectory(szDestPath))
607 {
608 bMultipleDest = TRUE;
609 if (szDestPath[_tcslen(szDestPath) - 1] != _T('\\'))
610 _tcscat(szDestPath, _T("\\"));
611 }
612
613 /* Check if the destination uses wildcards */
614 if ((_tcschr(arg[nDes], _T('*')) != NULL) ||
615 (_tcschr(arg[nDes], _T('?')) != NULL))
616 {
617 bMultipleDest = TRUE;
618 }
619 }
620
621 if (nDes != -1) /* you can only append files when there is a destination */
622 {
623 if (bMultipleSource && !bMultipleDest)
624 {
625 /* We have multiple source files, but not multiple destination
626 files. This means we are appending the soruce files. */
627 bAppend = TRUE;
628 if (_tcschr(arg[nSrc], _T('|')) != NULL)
629 appendPointer = arg[nSrc];
630 }
631 }
632
633 /* Save the name the user entered */
634 UseThisName = _tcsrchr(szDestPath,_T('\\'));
635 if (UseThisName)
636 {
637 /* Split the name from the path */
638 *UseThisName++ = _T('\0');
639
640 /* Check if the dest path ends with '\*' or '\' */
641 if (((UseThisName[0] == _T('*')) && (UseThisName[1] == _T('\0'))) ||
642 (UseThisName[0] == _T('\0')))
643 {
644 /* In this case we will be using the same name as the source file
645 for the destination file because destination is a folder */
646 bSrcName = TRUE;
647 UseThisName = NULL;
648 }
649 }
650 else
651 {
652 /* Something's seriously wrong! */
653 UseThisName = szDestPath;
654 }
655
656 do
657 {
658 /* Get the full string of the path to the source file */
659 if (_tcschr(arg[nSrc], _T('|')) != NULL)
660 {
661 /* Reset the source path */
662 szSrcPath[0] = _T('\0');
663
664 /* Loop through the source file name and copy all
665 the chars one at a time until it gets too + */
666 while(TRUE)
667 {
668 if (appendPointer[0] == _T('|'))
669 {
670 /* Skip the | and go to the next file name */
671 appendPointer++;
672 break;
673 }
674 else if (appendPointer[0] == _T('\0'))
675 {
676 bDone = TRUE;
677 break;
678 }
679
680 _tcsncat(szSrcPath, appendPointer, 1);
681 appendPointer++;
682 }
683
684 if (_tcschr(arg[nSrc], _T(',')) != NULL)
685 {
686 /* Only time there is a , in the source is when they are using touch
687 Cant have a destination and can only have on ,, at the end of the string
688 Cant have more then one file name */
689 szTouch = _tcsstr(arg[nSrc], _T("|"));
690 if (_tcsncmp(szTouch,_T("|,,\0"), 4) || (nDes != -1))
691 {
692 ConErrResPrintf(STRING_ERROR_INVALID_PARAM_FORMAT,arg[nSrc]);
693 nErrorLevel = 1;
694 freep (arg);
695 return 1;
696 }
697 bTouch = TRUE;
698 bDone = TRUE;
699 }
700 }
701 else
702 {
703 bDone = TRUE;
704 _tcscpy(szSrcPath, arg[nSrc]);
705 }
706
707 /* "x:" is not a valid source path format. */
708 if ((szSrcPath[1] == _T(':')) && (szSrcPath[2] == _T('\0')))
709 {
710 ConOutPrintf(_T("%s\n"), szSrcPath);
711 ConOutFormatMessage(ERROR_FILE_NOT_FOUND, szSrcPath);
712 nErrorLevel = 1;
713 break;
714 }
715
716
717 /* From this point on, we can assume that the shortest path is 3 letters long
718 and that would be [DriveLetter]:\ */
719
720 /* Check if the path has a wildcard */
721 bHasWildcard = (_tcschr(szSrcPath, _T('*')) != NULL);
722
723 /* If there is no * in the path name and it is a folder then we will
724 need to add a wildcard to the pathname so FindFirstFile comes up
725 with all the files in that folder */
726 if (!bHasWildcard && IsExistingDirectory(szSrcPath))
727 {
728 /* If it doesnt have a \ at the end already then on needs to be added */
729 if (szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\'))
730 _tcscat(szSrcPath, _T("\\"));
731 _tcscat(szSrcPath, _T("*"));
732 bHasWildcard = TRUE;
733 }
734
735 /* If the path ends with '\' add a wildcard at the end */
736 if (szSrcPath[_tcslen(szSrcPath) - 1] == _T('\\'))
737 {
738 _tcscat(szSrcPath, _T("*"));
739 bHasWildcard = TRUE;
740 }
741
742 /* Get a list of all the files */
743 hFile = FindFirstFile(szSrcPath, &findBuffer);
744
745 /* If it couldnt open the file handle, print out the error */
746 if (hFile == INVALID_HANDLE_VALUE)
747 {
748 /* only print source name when more then one file */
749 if (bMultipleSource)
750 ConOutPrintf(_T("%s\n"), szSrcPath);
751
752 ConOutFormatMessage(GetLastError(), szSrcPath);
753 freep(arg);
754 nErrorLevel = 1;
755 return 1;
756 }
757
758 /* Strip the paths back to the folder they are in */
759 for (i = (_tcslen(szSrcPath) - 1); i > -1; i--)
760 if (szSrcPath[i] != _T('\\'))
761 szSrcPath[i] = _T('\0');
762 else
763 break;
764
765 do
766 {
767 /* Check Breaker */
768 if (CheckCtrlBreak(BREAK_INPUT))
769 {
770 FindClose(hFile);
771 freep(arg);
772 return 1;
773 }
774
775 /* Set the override to yes each new file */
776 nOverwrite = 1;
777
778 /* Ignore the . and .. files */
779 if (!_tcscmp(findBuffer.cFileName, _T(".")) ||
780 !_tcscmp(findBuffer.cFileName, _T("..")) ||
781 findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
782 {
783 continue;
784 }
785
786 /* Copy the base folder over to a tmp string */
787 _tcscpy(tmpDestPath, szDestPath);
788 _tcscat(tmpDestPath, _T("\\"));
789
790 /* Can't put a file into a folder that isn't there */
791 if (_tcscmp(tmpDestPath, _T("\\\\.\\")) &&
792 !IsExistingDirectory(tmpDestPath))
793 {
794 FindClose(hFile);
795 ConOutFormatMessage(GetLastError(), szSrcPath);
796 freep(arg);
797 nErrorLevel = 1;
798 return 1;
799 }
800
801 /* Copy over the destination path name */
802 if (bSrcName)
803 _tcscat(tmpDestPath, findBuffer.cFileName);
804 else
805 {
806 /* If there is no wildcard you can use the name the user entered */
807 if ((_tcschr(UseThisName, _T('*')) == NULL) &&
808 (_tcschr(UseThisName, _T('?')) == NULL))
809 {
810 _tcscat(tmpDestPath, UseThisName);
811 }
812 else
813 {
814 TCHAR DoneFile[MAX_PATH];
815
816 BuildFileName(findBuffer.cFileName,
817 UseThisName,
818 DoneFile);
819
820
821 /* Add the filename to the tmp string path */
822 _tcscat(tmpDestPath, DoneFile);
823 }
824 }
825
826 /* Build the string path to the source file */
827 _tcscpy(tmpSrcPath,szSrcPath);
828 _tcscat (tmpSrcPath, findBuffer.cFileName);
829
830 /* Check to see if the file is the same file */
831 if (!bTouch && !_tcscmp(tmpSrcPath, tmpDestPath))
832 {
833 ConOutResPrintf(STRING_COPY_ERROR2);
834
835 nErrorLevel = 1;
836 break;
837 }
838
839 /* only print source name when more then one file */
840 if (bMultipleSource)
841 ConOutPrintf(_T("%s\n"), tmpSrcPath);
842
843 /* Handle any overriding / prompting that needs to be done */
844 if (((!(dwFlags & COPY_NO_PROMPT) && IsExistingFile (tmpDestPath)) || dwFlags & COPY_PROMPT) && !bTouch)
845 nOverwrite = CopyOverwrite(tmpDestPath);
846 if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
847 continue;
848 if (nOverwrite == PROMPT_ALL || (nOverwrite == PROMPT_YES && bAppend))
849 dwFlags |= COPY_NO_PROMPT;
850
851 /* Tell weather the copy was successful or not */
852 if (copy(tmpSrcPath,tmpDestPath, bAppend, dwFlags, bTouch))
853 {
854 nFiles++;
855 //LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
856 }
857 else
858 {
859 /* print out the error message */
860 ConOutResPrintf(STRING_COPY_ERROR3);
861 ConOutFormatMessage (GetLastError(), szSrcPath);
862 nErrorLevel = 1;
863 }
864
865 /* Loop through all wildcard files */
866 } while (FindNextFile(hFile, &findBuffer));
867
868 /* Loop through all files in src string with a + */
869 } while(!bDone);
870
871 /* print out the number of files copied */
872 ConOutResPrintf(STRING_COPY_FILE, bAppend ? 1 : nFiles);
873
874 if (hFile) FindClose(hFile);
875
876 if (arg != NULL)
877 freep(arg);
878
879 return 0;
880 }
881
882 #endif /* INCLUDE_CMD_COPY */