ReactOS  0.4.13-dev-551-gf37fb1f
copy.c File Reference
#include "precomp.h"
Include dependency graph for copy.c:

Go to the source code of this file.

Enumerations

enum  {
  COPY_ASCII = 0x001, COPY_DECRYPT = 0x004, COPY_VERIFY = 0x008, COPY_SHORTNAME = 0x010,
  COPY_NO_PROMPT = 0x020, COPY_PROMPT = 0x040, COPY_RESTART = 0x080, COPY_BINARY = 0x100
}
 

Functions

INT copy (TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
 
static INT CopyOverwrite (LPTSTR fn)
 
static void BuildFileName (LPTSTR pszSource, LPTSTR pszTarget, LPTSTR pszOutput)
 
INT cmd_copy (LPTSTR param)
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
COPY_ASCII 
COPY_DECRYPT 
COPY_VERIFY 
COPY_SHORTNAME 
COPY_NO_PROMPT 
COPY_PROMPT 
COPY_RESTART 
COPY_BINARY 

Definition at line 38 of file copy.c.

39 {
40  COPY_ASCII = 0x001, /* /A */
41  COPY_DECRYPT = 0x004, /* /D */
42  COPY_VERIFY = 0x008, /* /V : Dummy, Never will be Implemented */
43  COPY_SHORTNAME = 0x010, /* /N : Dummy, Never will be Implemented */
44  COPY_NO_PROMPT = 0x020, /* /Y */
45  COPY_PROMPT = 0x040, /* /-Y */
46  COPY_RESTART = 0x080, /* /Z */
47  COPY_BINARY = 0x100, /* /B */
48 };

Function Documentation

◆ BuildFileName()

static void BuildFileName ( LPTSTR  pszSource,
LPTSTR  pszTarget,
LPTSTR  pszOutput 
)
static

Definition at line 296 of file copy.c.

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 }
#define _T(x)
Definition: vfdio.h:22

Referenced by cmd_copy().

◆ cmd_copy()

INT cmd_copy ( LPTSTR  param)

Definition at line 331 of file copy.c.

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  /* Pointer to keep track of how far through the append input(file1+file2+file3) we are */
345  TCHAR * appendPointer = _T("\0");
346  /* The full path to src and dest. This has drive letter, folders, and filename */
347  TCHAR tmpDestPath[MAX_PATH];
348  TCHAR tmpSrcPath[MAX_PATH];
349  /* A bool to know whether or not the destination name will be taken from the input */
350  BOOL bSrcName = FALSE;
351  /* Seems like a waste but it is a pointer used to copy from input to PreserveName */
352  TCHAR * UseThisName;
353  /* for CMDCOPY env */
354  TCHAR *evar;
355  int size;
356  TCHAR * szTouch;
357  BOOL bHasWildcard, bDone = FALSE, bMoreFiles = FALSE;
358  /* Used for something like "copy c*.exe d*.exe" */
359  BOOL bMultipleSource = FALSE, bMultipleDest = FALSE;
360 
361 
362  /* Show help/usage info */
363  if (!_tcsncmp(param, _T("/?"), 2))
364  {
366  return 0;
367  }
368 
369  nErrorLevel = 0;
370 
371  /* Get the env variable value if it exists */
372  evar = cmd_alloc(512 * sizeof(TCHAR));
373  if (evar == NULL)
374  size = 0;
375  else
376  size = GetEnvironmentVariable (_T("COPYCMD"), evar, 512);
377 
378  if (size > 512)
379  {
380  TCHAR *old_evar = evar;
381  evar = cmd_realloc(evar,size * sizeof(TCHAR) );
382  if (evar!=NULL)
383  size = GetEnvironmentVariable (_T("COPYCMD"), evar, size);
384  else
385  {
386  size=0;
387  evar = old_evar;
388  }
389  }
390 
391  /* check see if we did get any env variable */
392  if (size != 0)
393  {
394  int t = 0;
395 
396  /* scan and set the flags */
397  for (t = 0; t < size; t++)
398  {
399  if (_tcsncicmp(_T("/A"),&evar[t],2) == 0)
400  {
402  t++;
403  }
404  else if (_tcsncicmp(_T("/B"),&evar[t],2) == 0)
405  {
406  dwFlags |= COPY_BINARY;
407  t++;
408  }
409  else if (_tcsncicmp(_T("/D"),&evar[t],2) == 0)
410  {
412  t++;
413  }
414  else if (_tcsncicmp(_T("/V"),&evar[t],2) == 0)
415  {
416  dwFlags |= COPY_VERIFY;
417  t++;
418  }
419  else if (_tcsncicmp(_T("/N"),&evar[t],2) == 0)
420  {
422  t++;
423  }
424  else if (_tcsncicmp(_T("/Y"),&evar[t],2) == 0)
425  {
427  t++;
428  }
429  else if (_tcsncicmp(_T("/-Y"),&evar[t],3) == 0)
430  {
431  dwFlags |= COPY_PROMPT;
432  t+=2;
433  }
434  else if (_tcsncicmp(_T("/Z"),&evar[t],2) == 0)
435  {
436  dwFlags |= COPY_PROMPT;
437  t++;
438  }
439  }
440  }
441  cmd_free(evar);
442 
443 
444  /* Split the user input into array */
445  arg = split(param, &argc, FALSE, TRUE);
446  nFiles = argc;
447 
448  /* Read switches and count files */
449  for (i = 0; i < argc; i++)
450  {
451  if (*arg[i] == _T('/'))
452  {
453  if (_tcslen(arg[i]) >= 2)
454  {
455  switch (_totupper(arg[i][1]))
456  {
457  case _T('A'):
458  dwFlags |= COPY_ASCII;
459  break;
460 
461  case _T('B'):
462  dwFlags |= COPY_BINARY;
463  break;
464 
465  case _T('D'):
467  break;
468 
469  case _T('V'):
470  dwFlags |= COPY_VERIFY;
471  break;
472 
473  case _T('N'):
475  break;
476 
477  case _T('Y'):
479  dwFlags &= ~COPY_PROMPT;
480  break;
481 
482  case _T('-'):
483  if (_tcslen(arg[i]) >= 3)
484  {
485  if (_totupper(arg[i][2]) == _T('Y'))
486  {
488  dwFlags |= COPY_PROMPT;
489  }
490  }
491 
492  break;
493 
494  case _T('Z'):
496  break;
497 
498  default:
499  /* Invalid switch */
501  nErrorLevel = 1;
502  freep (arg);
503  return 1;
504  break;
505  }
506  }
507  /* If it was a switch, subtract from total arguments */
508  nFiles--;
509  }
510  else
511  {
512  /* If it isn't a switch then it is the source or destination */
513  if (nSrc == -1)
514  {
515  nSrc = i;
516  }
517  else if (*arg[i] == _T('+'))
518  {
519  /* Next file should be appended */
520  bMoreFiles = TRUE;
521  nFiles -= 1;
522  }
523  else if (bMoreFiles)
524  {
525  /* Add this file to the source string
526  this way we can do all checks
527  directly on source string later on */
528  TCHAR * ptr;
529  int length = (_tcslen(arg[nSrc]) + _tcslen(arg[i]) + 2) * sizeof(TCHAR);
530  ptr = cmd_alloc(length);
531  if (ptr)
532  {
533  _tcscpy(ptr, arg[nSrc]);
534  _tcscat(ptr, _T("|"));
535  _tcscat(ptr, arg[i]);
536  cmd_free(arg[nSrc]);
537  arg[nSrc] = ptr;
538  nFiles -= 1;
539  }
540 
541  bMoreFiles = FALSE;
542  }
543  else if (nDes == -1)
544  {
545  nDes = i;
546  }
547  }
548  }
549 
550  /* keep quiet within batch files */
551  if (bc != NULL)
552  {
554  dwFlags &= ~COPY_PROMPT;
555  }
556 
557  if (nFiles < 1)
558  {
559  /* There are not enough files, there has to be at least 1 */
561  freep(arg);
562  return 1;
563  }
564 
565  if (nFiles > 2)
566  {
567  /* There are too many file names in command */
569  nErrorLevel = 1;
570  freep(arg);
571  return 1;
572  }
573 
574  if ((_tcschr(arg[nSrc], _T('|')) != NULL) ||
575  (_tcschr(arg[nSrc], _T('*')) != NULL) ||
576  (_tcschr(arg[nSrc], _T('?')) != NULL) ||
577  IsExistingDirectory(arg[nSrc]))
578  {
579  bMultipleSource = TRUE;
580  }
581 
582  /* Reuse the number of files variable */
583  nFiles = 0;
584 
585  /* Check if no destination argument is passed */
586  if (nDes == -1)
587  {
588  /* If no destination was entered then just use
589  the current directory as the destination */
590  GetCurrentDirectory(ARRAYSIZE(szDestPath), szDestPath);
591  }
592  else
593  {
594  /* Check if the destination is 'x:' */
595  if ((arg[nDes][1] == _T(':')) && (arg[nDes][2] == _T('\0')))
596  {
597  GetRootPath(arg[nDes], szDestPath, ARRAYSIZE(szDestPath));
598  }
599  else
600  {
601  /* If the user entered two file names then form the full string path */
602  GetFullPathName(arg[nDes], ARRAYSIZE(szDestPath), szDestPath, NULL);
603  }
604 
605  /* Make sure there is an ending slash to the path if the dest is a folder */
606  if ((_tcschr(szDestPath, _T('*')) == NULL) &&
607  IsExistingDirectory(szDestPath))
608  {
609  bMultipleDest = TRUE;
610  if (szDestPath[_tcslen(szDestPath) - 1] != _T('\\'))
611  _tcscat(szDestPath, _T("\\"));
612  }
613 
614  /* Check if the destination uses wildcards */
615  if ((_tcschr(arg[nDes], _T('*')) != NULL) ||
616  (_tcschr(arg[nDes], _T('?')) != NULL))
617  {
618  bMultipleDest = TRUE;
619  }
620  }
621 
622  if (nDes != -1) /* Append files only when there is a destination */
623  {
624  if (bMultipleSource && !bMultipleDest)
625  {
626  /* We have multiple source files, but not multiple destination
627  files. This means we are appending the source files. */
628  bAppend = TRUE;
629  if (_tcschr(arg[nSrc], _T('|')) != NULL)
630  appendPointer = arg[nSrc];
631  }
632  }
633 
634  /* Save the name the user entered */
635  UseThisName = _tcsrchr(szDestPath,_T('\\'));
636  if (UseThisName)
637  {
638  /* Split the name from the path */
639  *UseThisName++ = _T('\0');
640 
641  /* Check if the dest path ends with '\*' or '\' */
642  if (((UseThisName[0] == _T('*')) && (UseThisName[1] == _T('\0'))) ||
643  (UseThisName[0] == _T('\0')))
644  {
645  /* In this case we will be using the same name as the source file
646  for the destination file because destination is a folder */
647  bSrcName = TRUE;
648  UseThisName = NULL;
649  }
650  }
651  else
652  {
653  /* Something's seriously wrong! */
654  UseThisName = szDestPath;
655  }
656 
657  do
658  {
659  /* Get the full string of the path to the source file */
660  if (_tcschr(arg[nSrc], _T('|')) != NULL)
661  {
662  /* Reset the source path */
663  szSrcPath[0] = _T('\0');
664 
665  /* Loop through the source file name and copy all
666  the chars one at a time until we reach the separator */
667  while(TRUE)
668  {
669  if (appendPointer[0] == _T('|'))
670  {
671  /* Skip the | and go to the next file name */
672  appendPointer++;
673  break;
674  }
675  else if (appendPointer[0] == _T('\0'))
676  {
677  bDone = TRUE;
678  break;
679  }
680 
681  _tcsncat(szSrcPath, appendPointer, 1);
682  appendPointer++;
683  }
684 
685  if (_tcschr(arg[nSrc], _T(',')) != NULL)
686  {
687  /* Only time there is a , in the source is when they are using touch
688  Cant have a destination and can only have on ,, at the end of the string
689  Cant have more than one file name */
690  szTouch = _tcsstr(arg[nSrc], _T("|"));
691  if (_tcsncmp(szTouch,_T("|,,\0"), 4) || (nDes != -1))
692  {
694  nErrorLevel = 1;
695  freep (arg);
696  return 1;
697  }
698  bTouch = TRUE;
699  bDone = TRUE;
700  }
701  }
702  else
703  {
704  bDone = TRUE;
705  _tcscpy(szSrcPath, arg[nSrc]);
706  }
707 
708  /* "x:" is not a valid source path format. */
709  if ((szSrcPath[1] == _T(':')) && (szSrcPath[2] == _T('\0')))
710  {
711  ConOutPrintf(_T("%s\n"), szSrcPath);
713  nErrorLevel = 1;
714  break;
715  }
716 
717 
718  /* From this point on, we can assume that the shortest path is
719  3 letters long and that would be [DriveLetter]:\ */
720 
721  /* Check if the path has a wildcard */
722  bHasWildcard = (_tcschr(szSrcPath, _T('*')) != NULL);
723 
724  /* If there is no * in the path name and it is a folder then we will
725  need to add a wildcard to the pathname so FindFirstFile comes up
726  with all the files in that folder */
727  if (!bHasWildcard && IsExistingDirectory(szSrcPath))
728  {
729  /* If it doesnt have a \ at the end already then on needs to be added */
730  if (szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\'))
731  _tcscat(szSrcPath, _T("\\"));
732  _tcscat(szSrcPath, _T("*"));
733  bHasWildcard = TRUE;
734  }
735 
736  /* If the path ends with '\' add a wildcard at the end */
737  if (szSrcPath[_tcslen(szSrcPath) - 1] == _T('\\'))
738  {
739  _tcscat(szSrcPath, _T("*"));
740  bHasWildcard = TRUE;
741  }
742 
743  /* Get a list of all the files */
744  hFile = FindFirstFile(szSrcPath, &findBuffer);
745 
746  /* If we could not open the file handle, print out the error */
748  {
749  /* only print source name when more than one file */
750  if (bMultipleSource)
751  ConOutPrintf(_T("%s\n"), szSrcPath);
752 
753  ConOutFormatMessage(GetLastError(), szSrcPath);
754  freep(arg);
755  nErrorLevel = 1;
756  return 1;
757  }
758 
759  /* Strip the paths back to the folder they are in */
760  for (i = (_tcslen(szSrcPath) - 1); i > -1; i--)
761  {
762  if (szSrcPath[i] != _T('\\'))
763  szSrcPath[i] = _T('\0');
764  else
765  break;
766  }
767 
768  do
769  {
770  /* Check Breaker */
772  {
773  FindClose(hFile);
774  freep(arg);
775  return 1;
776  }
777 
778  /* Set the override to yes each new file */
779  nOverwrite = 1;
780 
781  /* Ignore the . and .. files */
782  if (!_tcscmp(findBuffer.cFileName, _T(".")) ||
783  !_tcscmp(findBuffer.cFileName, _T("..")) ||
784  findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
785  {
786  continue;
787  }
788 
789  /* Copy the base folder over to a tmp string */
790  _tcscpy(tmpDestPath, szDestPath);
791  _tcscat(tmpDestPath, _T("\\"));
792 
793  /* Can't put a file into a folder that isn't there */
794  if (_tcscmp(tmpDestPath, _T("\\\\.\\")) &&
795  !IsExistingDirectory(tmpDestPath))
796  {
797  FindClose(hFile);
798  ConOutFormatMessage(GetLastError(), szSrcPath);
799  freep(arg);
800  nErrorLevel = 1;
801  return 1;
802  }
803 
804  /* Copy over the destination path name */
805  if (bSrcName)
806  _tcscat(tmpDestPath, findBuffer.cFileName);
807  else
808  {
809  /* If there is no wildcard, use the name the user entered */
810  if ((_tcschr(UseThisName, _T('*')) == NULL) &&
811  (_tcschr(UseThisName, _T('?')) == NULL))
812  {
813  _tcscat(tmpDestPath, UseThisName);
814  }
815  else
816  {
817  TCHAR DoneFile[MAX_PATH];
818 
819  BuildFileName(findBuffer.cFileName,
820  UseThisName,
821  DoneFile);
822 
823 
824  /* Add the filename to the tmp string path */
825  _tcscat(tmpDestPath, DoneFile);
826  }
827  }
828 
829  /* Build the string path to the source file */
830  _tcscpy(tmpSrcPath,szSrcPath);
831  _tcscat (tmpSrcPath, findBuffer.cFileName);
832 
833  /* Check to see if the file is the same file */
834  if (!bTouch && !_tcscmp(tmpSrcPath, tmpDestPath))
835  {
837 
838  nErrorLevel = 1;
839  break;
840  }
841 
842  /* only print source name when more than one file */
843  if (bMultipleSource)
844  ConOutPrintf(_T("%s\n"), tmpSrcPath);
845 
846  /* Handle any overriding / prompting that needs to be done */
847  if (((!(dwFlags & COPY_NO_PROMPT) && IsExistingFile (tmpDestPath)) || dwFlags & COPY_PROMPT) && !bTouch)
848  nOverwrite = CopyOverwrite(tmpDestPath);
849  if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
850  continue;
851  if (nOverwrite == PROMPT_ALL || (nOverwrite == PROMPT_YES && bAppend))
853 
854  /* Tell whether the copy was successful or not */
855  if (copy(tmpSrcPath,tmpDestPath, bAppend, dwFlags, bTouch))
856  {
857  nFiles++;
858  }
859  else
860  {
861  /* print out the error message */
863  ConOutFormatMessage (GetLastError(), szSrcPath);
864  nErrorLevel = 1;
865  }
866 
867  /* Loop through all wildcard files */
868  } while (FindNextFile(hFile, &findBuffer));
869 
870  FindClose(hFile);
871 
872  /* Loop through all files in src string with a + */
873  } while (!bDone);
874 
875  /* print out the number of files copied */
876  ConOutResPrintf(STRING_COPY_FILE, bAppend ? 1 : nFiles);
877 
878  if (arg != NULL)
879  freep(arg);
880 
881  return 0;
882 }
INT nErrorLevel
Definition: cmd.c:157
#define STRING_COPY_ERROR2
Definition: resource.h:36
#define ConOutFormatMessage(MessageId,...)
Definition: console.h:54
static int argc
Definition: ServiceArgs.c:12
BOOL CheckCtrlBreak(INT)
Definition: misc.c:132
#define TRUE
Definition: types.h:120
#define BREAK_INPUT
Definition: cmd.h:33
LPBATCH_CONTEXT bc
Definition: batch.c:66
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
GLdouble GLdouble t
Definition: gl.h:2047
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
#define PROMPT_BREAK
Definition: cmd.h:285
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define STRING_ERROR_REQ_PARAM_MISSING
Definition: resource.h:10
#define ConOutPrintf(szStr,...)
Definition: console.h:42
void * arg
Definition: msvc.h:12
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
#define PROMPT_NO
Definition: cmd.h:282
#define _totupper
Definition: tchar.h:1509
#define PROMPT_ALL
Definition: cmd.h:284
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int BOOL
Definition: ntddk_ex.h:94
#define _tcsncicmp
Definition: tchar.h:1429
#define ConErrResPrintf(uID,...)
Definition: console.h:51
static PVOID ptr
Definition: dispmode.c:27
#define GetCurrentDirectory
Definition: winbase.h:3619
static INT CopyOverwrite(LPTSTR fn)
Definition: copy.c:284
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define STRING_COPY_FILE
Definition: resource.h:215
static VOID freep(LPSTR *p)
Definition: cmdcons.c:98
smooth NULL
Definition: ftsmooth.c:416
#define FindFirstFile
Definition: winbase.h:3596
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define _tcsstr
Definition: xmlstorage.h:199
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
_TCHAR * _tcsncat(_TCHAR *dst, const _TCHAR *src, size_t n)
Definition: tcsncat.h:5
BOOL IsExistingDirectory(LPCTSTR)
Definition: misc.c:507
GLsizeiptr size
Definition: glext.h:5919
if(!(yy_init))
Definition: macro.lex.yy.c:714
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define FindNextFile
Definition: winbase.h:3602
GLfloat param
Definition: glext.h:5796
#define MAX_PATH
Definition: compat.h:26
static LPSTR * split(LPSTR s, LPINT args)
Definition: cmdcons.c:163
#define ConOutResPrintf(uID,...)
Definition: console.h:48
unsigned long DWORD
Definition: ntddk_ex.h:95
#define STRING_ERROR_INVALID_PARAM_FORMAT
Definition: resource.h:5
static void BuildFileName(LPTSTR pszSource, LPTSTR pszTarget, LPTSTR pszOutput)
Definition: copy.c:296
#define cmd_realloc(ptr, size)
Definition: cmddbg.h:30
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define GetFullPathName
Definition: winbase.h:3635
_In_ HANDLE hFile
Definition: mswsock.h:90
INT GetRootPath(TCHAR *InPath, TCHAR *OutPath, INT size)
Definition: internal.c:152
#define cmd_alloc(size)
Definition: cmddbg.h:29
#define PROMPT_YES
Definition: cmd.h:283
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define STRING_ERROR_INVALID_SWITCH
Definition: resource.h:6
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
#define STRING_ERROR_TOO_MANY_PARAMETERS
Definition: resource.h:7
#define ConOutResPuts(uID)
Definition: console.h:36
#define STRING_COPY_HELP2
Definition: resource.h:95
_TCHAR * _tcsrchr(const _TCHAR *s, _XINT c)
Definition: tcsrchr.h:4
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
#define GetEnvironmentVariable
Definition: winbase.h:3628
#define STRING_COPY_ERROR3
Definition: resource.h:37
BOOL IsExistingFile(LPCTSTR)
Definition: misc.c:500
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502

◆ copy()

INT copy ( TCHAR  source[MAX_PATH],
TCHAR  dest[MAX_PATH],
INT  append,
DWORD  lpdwFlags,
BOOL  bTouch 
)

Definition at line 51 of file copy.c.

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 */
72  return 0;
73 
74  TRACE ("checking mode\n");
75 
76  if (bTouch)
77  {
79  NULL, OPEN_EXISTING, 0, NULL);
80  if (hFileSrc == INVALID_HANDLE_VALUE)
81  {
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 
106  NULL, OPEN_EXISTING, 0, NULL);
107  if (hFileSrc == INVALID_HANDLE_VALUE)
108  {
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"));
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);
151  _tcscat(dest,_T("\\"));
152  FileName = _tcsrchr(TrueDest,_T('\\'));
153  FileName++;
155  }
156 
157 
158  if (!IsExistingFile (dest))
159  {
160  TRACE ("opening/creating\n");
161  hFileDest =
163  }
164  else if (!append)
165  {
166  TRACE ("SetFileAttributes (%s, FILE_ATTRIBUTE_NORMAL);\n", debugstr_aw(dest));
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");
186 
187  hFileDest =
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);
199  nErrorLevel = 1;
200  return 0;
201  }
202 
203  /* A page-aligned buffer usually give more speed */
205  if (buffer == NULL)
206  {
207  CloseHandle (hFileDest);
208  CloseHandle (hFileSrc);
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  {
235 
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 
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 }
INT nErrorLevel
Definition: cmd.c:157
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
BOOL CheckCtrlBreak(INT)
Definition: misc.c:132
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
#define BREAK_INPUT
Definition: cmd.h:33
#define memchr(s, c, n)
Definition: mkisofs.h:875
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
char CHAR
Definition: xmlstorage.h:175
#define debugstr_aw
Definition: precomp.h:43
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
GLuint buffer
Definition: glext.h:5915
static void append(struct dump_context *dc, const void *data, unsigned size)
Definition: minidump.c:380
#define DeleteFile
Definition: winbase.h:3578
#define MEM_COMMIT
Definition: nt_native.h:1313
#define COPY_FILE_RESTARTABLE
Definition: winbase.h:206
#define CopyFileEx
Definition: winbase.h:3556
#define FILE_SHARE_READ
Definition: compat.h:125
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
BOOL WINAPI SetFileTime(IN HANDLE hFile, CONST FILETIME *lpCreationTime OPTIONAL, CONST FILETIME *lpLastAccessTime OPTIONAL, CONST FILETIME *lpLastWriteTime OPTIONAL)
Definition: fileinfo.c:1098
LPVOID NTAPI VirtualAlloc(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
Definition: virtmem.c:74
unsigned char * LPBYTE
Definition: typedefs.h:52
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:426
#define STRING_ERROR_OUT_OF_MEMORY
Definition: resource.h:14
#define STRING_COPY_ERROR1
Definition: resource.h:35
char TCHAR
Definition: xmlstorage.h:189
VOID WINAPI GetSystemTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:317
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
#define FILE_END
Definition: winbase.h:114
#define SetFileAttributes
Definition: winbase.h:3723
#define MAX_PATH
Definition: compat.h:26
#define ConOutResPrintf(uID,...)
Definition: console.h:48
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define COPY_FILE_ALLOW_DECRYPTED_DESTINATION
Definition: winbase.h:204
#define STRING_ERROR_PATH_NOT_FOUND
Definition: resource.h:8
#define GENERIC_READ
Definition: compat.h:124
#define GetFileAttributes
Definition: winbase.h:3629
struct _FileName FileName
Definition: fatprocs.h:884
#define CREATE_ALWAYS
Definition: disk.h:72
BOOL WINAPI SystemTimeToFileTime(IN CONST SYSTEMTIME *lpSystemTime, OUT LPFILETIME lpFileTime)
Definition: time.c:148
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
#define ConOutResPuts(uID)
Definition: console.h:36
#define MEM_RELEASE
Definition: nt_native.h:1316
_TCHAR * _tcsrchr(const _TCHAR *s, _XINT c)
Definition: tcsrchr.h:4
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
#define GetEnvironmentVariable
Definition: winbase.h:3628
static char * dest
Definition: rtl.c:135
BOOL NTAPI VirtualFree(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType)
Definition: virtmem.c:128
#define STRING_COPY_ERROR3
Definition: resource.h:37
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
BOOL IsExistingFile(LPCTSTR)
Definition: misc.c:500
BOOL WINAPI GetFileTime(IN HANDLE hFile, OUT LPFILETIME lpCreationTime OPTIONAL, OUT LPFILETIME lpLastAccessTime OPTIONAL, OUT LPFILETIME lpLastWriteTime OPTIONAL)
Definition: fileinfo.c:1046
#define BUFF_SIZE
Definition: cmd.h:44
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by __BVECTOR_QUALIFIED::__BVECTOR(), __copy_float_and_fill(), __copy_integer_and_fill(), __do_put_bool(), __do_widen(), __merge_adaptive(), __money_do_put(), __put_integer(), __read_buffered(), __rotate_adaptive(), __set_difference(), __set_symmetric_difference(), __set_union(), __stable_partition_adaptive(), __write_integer(), basic_string< char, char_traits< char >, allocator< char > >::_M_assign(), basic_string< char, char_traits< char >, allocator< char > >::_M_copy(), deque< IncompleteClass >::_M_erase(), __BVECTOR_QUALIFIED::_M_fill_insert(), deque< IncompleteClass >::_M_fill_insert_aux(), __BVECTOR_QUALIFIED::_M_insert_aux(), deque< IncompleteClass >::_M_insert_range_aux(), vector< CTestInfo >::_M_range_insert_aux(), deque< IncompleteClass >::_M_reallocate_map(), basic_filebuf< char, char_traits< char > >::_M_underflow_aux(), _MmuInit(), _S_string_copy(), _Stl_copy_array(), add_local_oid_text_to_control(), SEALED_::Advise(), Arguments_idx_put(), vector< CTestInfo >::assign(), deque< IncompleteClass >::assign(), BinsertTest::binsert1(), BinsertTest::binsert2(), CDecodeMsg_SaveAlgorithmID(), cmd_copy(), container_matches_cert(), basic_string< char, char_traits< char >, allocator< char > >::copy(), CopyTest::copy_array(), copy_handle(), CopyTest::copy_insert(), CopyTest::copy_vector(), CopyTest::copy_volatile(), CRYPT_CopyChainToElement(), CRYPT_CopySimpleChainToElement(), valarray< bool >::cshift(), d3dcompiler_strdup(), DataCacheEntry_SetData(), deflate(), codecvt< wchar_t, char, mbstate_t >::do_in(), codecvt< wchar_t, char, mbstate_t >::do_out(), time_put< _Ch, _OutIt >::do_put(), EnumSTATDATA_Construct(), EqualTest::equal1(), __BVECTOR_QUALIFIED::erase(), expect(), XMLStorage::XMLNode::filter(), FinsertTest::finsert1(), FinsertTest::finsert2(), SEALED_::FireChannelReceivedData(), FT_Glyph_Copy(), FT_Glyph_Stroke(), FT_Glyph_StrokeBorder(), GdipCreateMetafileFromWmf(), GdipInvertMatrix(), get_data_from_bitmap(), get_data_from_enhmetafile(), get_data_from_metafilepict(), gz_write(), inflate(), inflateBack(), inflateCopy(), ATL::CComEnumImpl< Base, piid, T, Copy >::Init(), __BVECTOR_QUALIFIED::insert(), InsertTest::insert1(), InsertTest::insert2(), interp_local(), main(), mapStructEntryToValue(), ME_SetParaFormat(), ME_StreamInText(), merge(), MismatchTest::mismatch2(), ATL::CComEnumImpl< Base, piid, T, Copy >::Next(), valarray< bool >::operator=(), sequence_buffer< _Sequence, _Buf_sz >::operator=(), deque< IncompleteClass >::operator=(), __BVECTOR_QUALIFIED::operator=(), OstreamIteratorTest::ostmit0(), parseAndPrintFile(), PtrSpecTest::ptr_specialization_test(), RealDefWindowProcA(), __BVECTOR_QUALIFIED::reserve(), rotate_copy(), SaveCursorScheme(), SearchTest::search2(), sequence_buffer< _Sequence, _Buf_sz >::sequence_buffer(), shader_arb_handle_instruction(), valarray< bool >::shift(), snapshot_GetDataHere(), StrongCheck(), test_CloneBitmapArea(), test_CoInternetParseIUri_InvalidArgs(), test_CopyImage_Check(), test_height_selection_vdmx(), test_validtypes(), testMemStore(), updatewindow(), VerifyInsertion(), basic_streambuf< char, char_traits< char > >::xsgetn(), xsltApplySequenceConstructor(), xsltAttrListTemplateProcess(), xsltCopy(), xsltCopyAttrListNoOverwrite(), xsltCopyText(), xsltCopyTextString(), xsltCopyTree(), xsltCopyTreeList(), xsltDefaultProcessOneNode(), xsltElement(), xsltShallowCopyAttr(), xsltShallowCopyElem(), xsltText(), basic_stringbuf< _CharT, _Traits, _Alloc >::xsputn(), and basic_streambuf< char, char_traits< char > >::xsputn().

◆ CopyOverwrite()

static INT CopyOverwrite ( LPTSTR  fn)
static

Definition at line 284 of file copy.c.

285 {
286  /*ask the user if they want to override*/
287  INT res;
289  res = FilePromptYNA (0);
290  return res;
291 }
static GLenum _GLUfuncptr fn
Definition: wgl_font.c:159
int32_t INT
Definition: typedefs.h:56
INT FilePromptYNA(UINT)
Definition: misc.c:665
#define ConOutResPrintf(uID,...)
Definition: console.h:48
#define STRING_COPY_HELP1
Definition: resource.h:94
GLuint res
Definition: glext.h:9613

Referenced by cmd_copy().