ReactOS  0.4.15-dev-1033-gd7d716a
replace.c File Reference
#include "precomp.h"
Include dependency graph for replace.c:

Go to the source code of this file.

Enumerations

enum  {
  REPLACE_ADD = 0x001, REPLACE_CONFIRM = 0x002, REPLACE_READ_ONLY = 0x004, REPLACE_SUBDIR = 0x008,
  REPLACE_DISK = 0x010, REPLACE_UPDATE = 0x020
}
 

Functions

void invalid_switch (LPTSTR is)
 
void getPath (TCHAR *out, LPTSTR in)
 
INT replace (TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], DWORD dwFlags, BOOL *doMore)
 
INT recReplace (DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
 
INT recFindSubDirs (DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
 
INT cmd_replace (LPTSTR param)
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
REPLACE_ADD 
REPLACE_CONFIRM 
REPLACE_READ_ONLY 
REPLACE_SUBDIR 
REPLACE_DISK 
REPLACE_UPDATE 

Definition at line 17 of file replace.c.

18 {
19  REPLACE_ADD = 0x001, /* /A */
20  REPLACE_CONFIRM = 0x002, /* /P */
21  REPLACE_READ_ONLY = 0x004, /* /R */
22  REPLACE_SUBDIR = 0x008, /* /S */
23  REPLACE_DISK = 0x010, /* /W */
24  REPLACE_UPDATE = 0x020, /* /U */
25 };

Function Documentation

◆ cmd_replace()

INT cmd_replace ( LPTSTR  param)

Definition at line 392 of file replace.c.

393 {
394  LPTSTR *arg;
395  INT argc, i,filesReplaced = 0, nFiles, srcIndex = -1, destIndex = -1;
396  DWORD dwFlags = 0;
397  TCHAR szDestPath[MAX_PATH], szSrcPath[MAX_PATH], tmpSrcPath[MAX_PATH];
398  BOOL doMore = TRUE;
399 
400  /* Help wanted? */
401  if (!_tcsncmp (param, _T("/?"), 2))
402  {
404  return 0;
405  }
406 
407  /* Divide the argument in to an array of c-strings */
408  arg = split (param, &argc, FALSE, FALSE);
409  nFiles = argc;
410 
411  /* Read options */
412  for (i = 0; i < argc; i++)
413  {
414  if (arg[i][0] == _T('/'))
415  {
416  if (_tcslen(arg[i]) == 2)
417  {
418  switch (_totupper(arg[i][1]))
419  {
420  case _T('A'):
421  dwFlags |= REPLACE_ADD;
422  break;
423  case _T('P'):
425  break;
426  case _T('R'):
428  break;
429  case _T('S'):
431  break;
432  case _T('W'):
434  break;
435  case _T('U'):
437  break;
438  default:
439  invalid_switch(arg[i]);
440  freep(arg);
441  return 0;
442  }
443  }
444  else
445  {
446  invalid_switch(arg[i]);
447  freep(arg);
448  return 0;
449  }
450  nFiles--;
451  }
452  else
453  {
454  if (srcIndex == -1)
455  {
456  srcIndex = i;
457  }
458  else if (destIndex == -1)
459  {
460  destIndex = i;
461  }
462  else
463  {
464  invalid_switch(arg[i]);
465  freep(arg);
466  return 0;
467  }
468  }
469  }
470 
471  /* See so that at least source is there */
472  if (nFiles < 1)
473  {
476  freep(arg);
477  return 1;
478  }
479  /* Check so that not both update and add switch is added and subdir */
481  {
484  freep(arg);
485  return 1;
486  }
487 
488  /* If we have a destination get the full path */
489  if (destIndex != -1)
490  {
491  if (_tcslen(arg[destIndex]) == 2 && arg[destIndex][1] == ':')
492  GetRootPath(arg[destIndex],szDestPath,MAX_PATH);
493  else
494  {
495  /* Check for wildcards in destination directory */
496  if (_tcschr (arg[destIndex], _T('*')) != NULL ||
497  _tcschr (arg[destIndex], _T('?')) != NULL)
498  {
501  freep(arg);
502  return 1;
503  }
504  getPath(szDestPath, arg[destIndex]);
505  /* Make sure that destination exists */
506  if (!IsExistingDirectory(szDestPath))
507  {
510  freep(arg);
511  return 1;
512  }
513  }
514  }
515  else
516  {
517  /* Dest is current dir */
518  GetCurrentDirectory(MAX_PATH,szDestPath);
519  }
520 
521  /* Get the full source path */
522  if (!(_tcslen(arg[srcIndex]) == 2 && arg[srcIndex][1] == ':'))
523  getPath(szSrcPath, arg[srcIndex]);
524  else
525  _tcscpy(szSrcPath,arg[srcIndex]);
526 
527  /* Source does not have wildcards */
528  if (_tcschr (arg[srcIndex], _T('*')) == NULL &&
529  _tcschr (arg[srcIndex], _T('?')) == NULL)
530  {
531  /* Check so that source is not a directory, because that is not allowed */
532  if (IsExistingDirectory(szSrcPath))
533  {
536  freep(arg);
537  return 1;
538  }
539  /* Check if the file exists */
540  if (!IsExistingFile(szSrcPath))
541  {
543  freep(arg);
544  return 1;
545  }
546  }
547  /* /w switch is set so wait for any key to be pressed */
548  if (dwFlags & REPLACE_DISK)
549  {
550  msg_pause();
551  cgetchar();
552  }
553 
554  /* Add an extra \ to the destination path if needed */
555  if (szDestPath[_tcslen(szDestPath) - 1] != _T('\\'))
556  _tcscat(szDestPath, _T("\\"));
557 
558  /* Save source path */
559  _tcscpy(tmpSrcPath,szSrcPath);
560  /* Replace in dest dir */
561  filesReplaced += recReplace(dwFlags, tmpSrcPath, szDestPath, &doMore);
562  /* If subdir switch is set replace in the subdirs to */
563  if (dwFlags & REPLACE_SUBDIR && doMore)
564  {
565  filesReplaced += recFindSubDirs(dwFlags, szSrcPath, szDestPath, &doMore);
566  }
567 
568  /* If source == dest write no more */
569  if (filesReplaced != -1)
570  {
571  /* No files replaced */
572  if (filesReplaced==0)
573  {
574  /* Add switch dependent output */
575  if (dwFlags & REPLACE_ADD)
577  else
579  }
580  /* Some files replaced */
581  else
582  {
583  /* Add switch dependent output */
584  if (dwFlags & REPLACE_ADD)
585  ConOutResPrintf(STRING_REPLACE_HELP8, filesReplaced);
586  else
587  ConOutResPrintf(STRING_REPLACE_HELP4, filesReplaced);
588  }
589  }
590  /* Return memory */
591  freep(arg);
592  return 1;
593 }
static int argc
Definition: ServiceArgs.c:12
#define STRING_REPLACE_ERROR4
Definition: resource.h:63
#define TRUE
Definition: types.h:120
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: misc.c:498
INT recReplace(DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
Definition: replace.c:215
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
#define STRING_REPLACE_ERROR6
Definition: resource.h:65
void * arg
Definition: msvc.h:10
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
#define _totupper
Definition: tchar.h:1509
int32_t INT
Definition: typedefs.h:58
CHAR * LPTSTR
Definition: xmlstorage.h:192
void invalid_switch(LPTSTR is)
Definition: replace.c:30
#define STRING_REPLACE_HELP3
Definition: resource.h:225
#define STRING_REPLACE_HELP4
Definition: resource.h:226
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
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GetCurrentDirectory
Definition: winbase.h:3645
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
static VOID freep(LPSTR *p)
Definition: cmdcons.c:98
smooth NULL
Definition: ftsmooth.c:416
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
void getPath(TCHAR *out, LPTSTR in)
Definition: replace.c:37
#define STRING_REPLACE_HELP8
Definition: resource.h:230
GLfloat param
Definition: glext.h:5796
#define MAX_PATH
Definition: compat.h:34
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_REPLACE_HELP7
Definition: resource.h:229
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
VOID msg_pause(VOID)
Definition: error.c:167
INT GetRootPath(IN LPCTSTR InPath, OUT LPTSTR OutPath, IN INT size)
Definition: internal.c:152
#define STRING_REPLACE_ERROR2
Definition: resource.h:61
#define STRING_REPLACE_HELP1
Definition: resource.h:223
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
TCHAR cgetchar(VOID)
Definition: misc.c:41
INT recFindSubDirs(DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
Definition: replace.c:315
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9
BOOL IsExistingDirectory(IN LPCTSTR pszPath)
Definition: misc.c:504
#define STRING_REPLACE_HELP2
Definition: resource.h:224

◆ getPath()

void getPath ( TCHAR out,
LPTSTR  in 
)

Definition at line 37 of file replace.c.

38 {
39  if (_tcslen(in) == 2 && in[1] == _T(':'))
41  else
43 }
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
#define _T(x)
Definition: vfdio.h:22
#define MAX_PATH
Definition: compat.h:34
static FILE * out
Definition: regtests2xml.c:44
#define GetFullPathName
Definition: winbase.h:3661
INT GetRootPath(IN LPCTSTR InPath, OUT LPTSTR OutPath, IN INT size)
Definition: internal.c:152
GLuint in
Definition: glext.h:9616

Referenced by cmd_replace().

◆ invalid_switch()

void invalid_switch ( LPTSTR  is)

Definition at line 30 of file replace.c.

31 {
34 }
#define TRUE
Definition: types.h:120
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
#define STRING_REPLACE_HELP3
Definition: resource.h:225
#define ConOutResPrintf(uID,...)
Definition: console.h:48
#define STRING_REPLACE_ERROR1
Definition: resource.h:60

Referenced by cmd_replace().

◆ recFindSubDirs()

INT recFindSubDirs ( DWORD  dwFlags,
TCHAR  szSrcPath[MAX_PATH],
TCHAR  szDestPath[MAX_PATH],
BOOL doMore 
)

Definition at line 315 of file replace.c.

319 {
320  HANDLE hFile;
321  WIN32_FIND_DATA findBuffer;
322  TCHAR tmpDestPath[MAX_PATH], tmpSrcPath[MAX_PATH];
323  INT filesReplaced = 0;
324  INT_PTR i;
325 
326  /*
327  * Add a wildcard to dest end so the it will be easy to iterate
328  * over all the files and directorys in the dest directory.
329  */
330  _tcscat(szDestPath, _T("*"));
331 
332  /* Get the first file in the directory */
333  hFile = FindFirstFile (szDestPath, &findBuffer);
334 
335  /* Remove the star added earlier to dest path */
336  for(i = (_tcslen(szDestPath) - 1); i > -1; i--)
337  {
338  if (szDestPath[i] != _T('\\'))
339  szDestPath[i] = _T('\0');
340  else
341  break;
342  }
343 
344  /* Iterate over all filed directories in the dest dir */
345  do
346  {
347  /* Save the source path so that it will not be wrecked */
348  _tcscpy(tmpSrcPath,szSrcPath);
349  /* Check for reading problems */
351  {
352  ConOutFormatMessage (GetLastError(), tmpSrcPath);
353  return filesReplaced;
354  }
355 
356  /*
357  * Check if the we should enter the dir or if it is a file
358  * or . or .. if so thake the next object to process.
359  */
360  if (!_tcscmp (findBuffer.cFileName, _T(".")) ||
361  !_tcscmp (findBuffer.cFileName, _T(".."))||
362  IsExistingFile(findBuffer.cFileName))
363  continue;
364  /* Add the destpath and the new dir path to tempDestPath */
365  _tcscpy(tmpDestPath,szDestPath);
366  _tcscat (tmpDestPath, findBuffer.cFileName);
367  /* Make sure that we have a directory */
368  if (IsExistingDirectory(tmpDestPath))
369  {
370  /* Add a \ to the end or the path */
371  if (szDestPath[_tcslen(tmpDestPath) - 1] != _T('\\'))
372  _tcscat(tmpDestPath, _T("\\"));
373  /* Call the function to replace files in the new directory */
374  filesReplaced += recReplace(dwFlags, tmpSrcPath, tmpDestPath, doMore);
375  /* If there were problems break e.g. read-only file */
376  if (!*doMore)
377  break;
378  _tcscpy(tmpSrcPath,szSrcPath);
379  /* Control the next level of subdirs */
380  filesReplaced += recFindSubDirs(dwFlags,tmpSrcPath,tmpDestPath, doMore);
381  if (!*doMore)
382  break;
383  }
384  /* Get the next handle */
385  } while(FindNextFile (hFile, &findBuffer));
386 
387  FindClose(hFile);
388 
389  return filesReplaced;
390 }
#define ConOutFormatMessage(MessageId,...)
Definition: console.h:54
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: misc.c:498
INT recReplace(DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
Definition: replace.c:215
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
int32_t INT_PTR
Definition: typedefs.h:64
int32_t INT
Definition: typedefs.h:58
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
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define FindFirstFile
Definition: winbase.h:3622
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define FindNextFile
Definition: winbase.h:3628
#define MAX_PATH
Definition: compat.h:34
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
_In_ HANDLE hFile
Definition: mswsock.h:90
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
INT recFindSubDirs(DWORD dwFlags, TCHAR szSrcPath[MAX_PATH], TCHAR szDestPath[MAX_PATH], BOOL *doMore)
Definition: replace.c:315
BOOL IsExistingDirectory(IN LPCTSTR pszPath)
Definition: misc.c:504
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502

Referenced by cmd_replace().

◆ recReplace()

INT recReplace ( DWORD  dwFlags,
TCHAR  szSrcPath[MAX_PATH],
TCHAR  szDestPath[MAX_PATH],
BOOL doMore 
)

Definition at line 215 of file replace.c.

219 {
220  TCHAR tmpDestPath[MAX_PATH], tmpSrcPath[MAX_PATH];
221  INT filesReplaced=0;
222  INT_PTR i;
223  DWORD dwAttrib = 0;
224  HANDLE hFile;
225  WIN32_FIND_DATA findBuffer;
226 
227  /* Get file handle to the sourcefile(s) */
228  hFile = FindFirstFile (szSrcPath, &findBuffer);
229 
230  /*
231  * Strip the paths back to the folder they are in, so that
232  * the different filenames can be added if more than one.
233  */
234  for(i = (_tcslen(szSrcPath) - 1); i > -1; i--)
235  {
236  if (szSrcPath[i] != _T('\\'))
237  szSrcPath[i] = _T('\0');
238  else
239  break;
240  }
241 
242  /* Go through all the sourcefiles and copy/replace them */
243  do
244  {
246  {
247  return filesReplaced;
248  }
249 
250  /* Problem with file handler */
252  return filesReplaced;
253 
254  /* We do not want to replace any .. . ocr directory */
255  if (!_tcscmp (findBuffer.cFileName, _T(".")) ||
256  !_tcscmp (findBuffer.cFileName, _T(".."))||
257  findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
258  continue;
259 
260  /* Add filename to destpath */
261  _tcscpy(tmpDestPath,szDestPath);
262  _tcscat (tmpDestPath, findBuffer.cFileName);
263 
264  dwAttrib = GetFileAttributes(tmpDestPath);
265  /* Check add flag */
266  if (dwFlags & REPLACE_ADD)
267  {
268  if (IsExistingFile(tmpDestPath))
269  continue;
270  else
271  dwAttrib = 0;
272  }
273  else
274  {
275  if (!IsExistingFile(tmpDestPath))
276  continue;
277  }
278 
279  /* Check if file is read only, if so check if that should be ignored */
280  if (dwAttrib & FILE_ATTRIBUTE_READONLY)
281  {
282  if (!(dwFlags & REPLACE_READ_ONLY))
283  {
285  *doMore = FALSE;
286  break;
287  }
288  }
289 
290  /* Add filename to sourcepath, insted of wildcards */
291  _tcscpy(tmpSrcPath,szSrcPath);
292  _tcscat (tmpSrcPath, findBuffer.cFileName);
293 
294  /* Make the replace */
295  if (replace(tmpSrcPath,tmpDestPath, dwFlags, doMore))
296  {
297  filesReplaced++;
298  }
299  else if (!*doMore)
300  {
301  /* The file to be replaced was the same as the source */
302  filesReplaced = -1;
303  break;
304  }
305 
306  /* Take next sourcefile if any */
307  } while(FindNextFile (hFile, &findBuffer));
308 
309  FindClose(hFile);
310 
311  return filesReplaced;
312 }
BOOL CheckCtrlBreak(INT)
Definition: misc.c:132
#define BREAK_INPUT
Definition: cmd.h:36
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: misc.c:498
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
int32_t INT_PTR
Definition: typedefs.h:64
int32_t INT
Definition: typedefs.h:58
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
#define FALSE
Definition: types.h:117
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define FindFirstFile
Definition: winbase.h:3622
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
INT replace(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], DWORD dwFlags, BOOL *doMore)
Definition: replace.c:47
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define FindNextFile
Definition: winbase.h:3628
#define MAX_PATH
Definition: compat.h:34
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
#define ConOutResPrintf(uID,...)
Definition: console.h:48
unsigned long DWORD
Definition: ntddk_ex.h:95
#define STRING_REPLACE_ERROR5
Definition: resource.h:64
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
_In_ HANDLE hFile
Definition: mswsock.h:90
#define GetFileAttributes
Definition: winbase.h:3655
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502

Referenced by cmd_replace(), and recFindSubDirs().

◆ replace()

INT replace ( TCHAR  source[MAX_PATH],
TCHAR  dest[MAX_PATH],
DWORD  dwFlags,
BOOL doMore 
)

Definition at line 47 of file replace.c.

48 {
49  TCHAR d[MAX_PATH];
50  TCHAR s[MAX_PATH];
51  HANDLE hFileSrc, hFileDest;
52  DWORD dwAttrib, dwRead, dwWritten;
53  LPBYTE buffer;
54  BOOL bEof = FALSE;
55  FILETIME srcCreationTime, destCreationTime, srcLastAccessTime, destLastAccessTime;
56  FILETIME srcLastWriteTime, destLastWriteTime;
58  GetPathCase(dest, d);
59  s[0] = _totupper(s[0]);
60  d[0] = _totupper(d[0]);
61  // ConOutPrintf(_T("old-src: %s\n"), s);
62  // ConOutPrintf(_T("old-dest: %s\n"), d);
63  // ConOutPrintf(_T("src: %s\n"), source);
64  // ConOutPrintf(_T("dest: %s\n"), dest);
65 
66  /* Open up the sourcefile */
68  if (hFileSrc == INVALID_HANDLE_VALUE)
69  {
71  return 0;
72  }
73 
74  /*
75  * Get the time from source file to be used in the comparison
76  * with dest time if update switch is set.
77  */
78  GetFileTime (hFileSrc, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime);
79 
80  /*
81  * Retrieve the source attributes so that they later on
82  * can be inserted in to the destination.
83  */
84  dwAttrib = GetFileAttributes (source);
85 
86  if (IsExistingFile (dest))
87  {
88  /*
89  * Resets the attributes to avoid problems with read only files,
90  * checks for read only has been made earlier.
91  */
93  /*
94  * Is the update flas set? The time has to be controled so that
95  * only older files are replaced.
96  */
97  if (dwFlags & REPLACE_UPDATE)
98  {
99  /* Read destination time */
101  0, NULL);
102 
103  if (hFileDest == INVALID_HANDLE_VALUE)
104  {
106  CloseHandle (hFileSrc);
107  return 0;
108  }
109 
110  /* Compare time */
111  GetFileTime (hFileDest, &destCreationTime, &destLastAccessTime, &destLastWriteTime);
112  if (!((srcLastWriteTime.dwHighDateTime > destLastWriteTime.dwHighDateTime) ||
113  (srcLastWriteTime.dwHighDateTime == destLastWriteTime.dwHighDateTime &&
114  srcLastWriteTime.dwLowDateTime > destLastWriteTime.dwLowDateTime)))
115  {
116  CloseHandle (hFileSrc);
117  CloseHandle (hFileDest);
118  return 0;
119  }
120  CloseHandle (hFileDest);
121  }
122  /* Delete the old file */
123  DeleteFile (dest);
124  }
125 
126  /* Check confirm flag, and take appropriate action */
127  if (dwFlags & REPLACE_CONFIRM)
128  {
129  /* Output depending on add flag */
130  if (dwFlags & REPLACE_ADD)
132  else
134  if ( !FilePromptYNA (0))
135  {
136  CloseHandle (hFileSrc);
137  return 0;
138  }
139  }
140 
141  /* Output depending on add flag */
142  if (dwFlags & REPLACE_ADD)
144  else
146 
147  /* Make sure source and destination is not the same */
148  if (!_tcscmp(s, d))
149  {
151  CloseHandle (hFileSrc);
152  *doMore = FALSE;
153  return 0;
154  }
155 
156  /* Open destination file to write to */
157  hFileDest = CreateFile (dest, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
158  if (hFileDest == INVALID_HANDLE_VALUE)
159  {
160  CloseHandle (hFileSrc);
162  *doMore = FALSE;
163  return 0;
164  }
165 
166  /* Get buffer for the copy process */
168  if (buffer == NULL)
169  {
170  CloseHandle (hFileDest);
171  CloseHandle (hFileSrc);
173  return 0;
174  }
175 
176  /* Put attribute and time to the new destination file */
177  SetFileAttributes (dest, dwAttrib);
178  SetFileTime (hFileDest, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime);
179  do
180  {
181  /* Read data from source */
182  ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL);
183 
184  /* Done? */
185  if (dwRead == 0)
186  break;
187 
188  /* Write to destination file */
189  WriteFile (hFileDest, buffer, dwRead, &dwWritten, NULL);
190 
191  /* Done! or ctrl break! */
192  if (dwWritten != dwRead || CheckCtrlBreak(BREAK_INPUT))
193  {
196  CloseHandle (hFileDest);
197  CloseHandle (hFileSrc);
198  nErrorLevel = 1;
199  return 0;
200  }
201  }
202  while (!bEof);
203 
204  /* Return memory and close files */
206  CloseHandle (hFileDest);
207  CloseHandle (hFileSrc);
208 
209  /* Return one file replaced */
210  return 1;
211 }
INT nErrorLevel
Definition: cmd.c:158
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 CloseHandle
Definition: compat.h:487
#define BREAK_INPUT
Definition: cmd.h:36
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
#define TRUE
Definition: types.h:120
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: misc.c:498
#define STRING_REPLACE_HELP5
Definition: resource.h:227
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
GLuint buffer
Definition: glext.h:5915
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
#define _totupper
Definition: tchar.h:1509
#define DeleteFile
Definition: winbase.h:3604
#define MEM_COMMIT
Definition: nt_native.h:1313
#define FILE_SHARE_READ
Definition: compat.h:136
#define STRING_REPLACE_HELP11
Definition: resource.h:233
BOOL WINAPI SetFileTime(IN HANDLE hFile, CONST FILETIME *lpCreationTime OPTIONAL, CONST FILETIME *lpLastAccessTime OPTIONAL, CONST FILETIME *lpLastWriteTime OPTIONAL)
Definition: fileinfo.c:1098
DWORD dwHighDateTime
Definition: mapidefs.h:66
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:53
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:523
#define STRING_ERROR_OUT_OF_MEMORY
Definition: resource.h:14
#define STRING_REPLACE_ERROR7
Definition: resource.h:66
#define STRING_COPY_ERROR1
Definition: resource.h:35
INT FilePromptYNA(UINT)
Definition: misc.c:623
char TCHAR
Definition: xmlstorage.h:189
#define d
Definition: ke_i.h:81
#define SetFileAttributes
Definition: winbase.h:3749
#define MAX_PATH
Definition: compat.h:34
VOID GetPathCase(TCHAR *, TCHAR *)
Definition: misc.c:86
#define ConOutResPrintf(uID,...)
Definition: console.h:48
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define STRING_REPLACE_HELP9
Definition: resource.h:231
GLdouble s
Definition: gl.h:2039
#define GENERIC_READ
Definition: compat.h:135
#define GetFileAttributes
Definition: winbase.h:3655
#define CREATE_ALWAYS
Definition: disk.h:72
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:913
#define ReadFile(a, b, c, d, e)
Definition: compat.h:490
#define ConOutResPuts(uID)
Definition: console.h:36
#define MEM_RELEASE
Definition: nt_native.h:1316
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 GetFileTime(IN HANDLE hFile, OUT LPFILETIME lpCreationTime OPTIONAL, OUT LPFILETIME lpLastAccessTime OPTIONAL, OUT LPFILETIME lpLastWriteTime OPTIONAL)
Definition: fileinfo.c:1046
#define STRING_REPLACE_HELP10
Definition: resource.h:232
DWORD dwLowDateTime
Definition: mapidefs.h:65
#define BUFF_SIZE
Definition: cmd.h:47
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by AddMRUData(), cache_container_create_object_name(), dialog_find(), EmptyStore_add(), epm_register(), ept_insert(), ext4_fs_set_xattr(), format_replace(), HLPFILE_RtfAddText(), HTMLDocument_open(), HTMLWindow2_open(), IEHTMLWindow2_open(), MAIN_ReplaceString(), msi_set_sourcedir_props(), ParaNdis_DebugInitialize(), recReplace(), RegExp_Replace(), basic_string< char, char_traits< char >, allocator< char > >::replace(), RmCpTest::replace0(), and RmCpTest::replace1().