Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

  1. #include "windows.h"
  2. #include <stdio.h>
  3. BOOL
  4. ForceCopy(
  5. LPCSTR lpszSourceFileName,
  6. LPCSTR lpszDestFileName
  7. )
  8. /*++
  9. Params: lpszSourceFileName Pointer to the source file.
  10. lpzDestFileName Pointer to the destination file.
  11. Return: TRUE on success, FALSE otherwise.
  12. Desc: Attempts to copy a file. If it's in use, move it and replace on reboot.
  13. --*/
  14. {
  15. char szTempPath[MAX_PATH];
  16. char szDelFileName[MAX_PATH];
  17. if (!CopyFileA(lpszSourceFileName, lpszDestFileName, FALSE)) {
  18. if (GetTempPathA(MAX_PATH, szTempPath) == 0) {
  19. printf("GetTempPath failed with 0x%x\n", GetLastError());
  20. return FALSE;
  21. }
  22. if (GetTempFileNameA(szTempPath, "DEL", 0, szDelFileName) == 0) {
  23. printf("GetTempFileName failed with 0x%x\n", GetLastError());
  24. return FALSE;
  25. }
  26. if (!MoveFileExA(lpszDestFileName, szDelFileName, MOVEFILE_REPLACE_EXISTING)) {
  27. printf("MoveFileEx failed with 0x%x\n", GetLastError());
  28. return FALSE;
  29. }
  30. if (!MoveFileExA(szDelFileName, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) {
  31. printf("MoveFileEx failed with 0x%x\n", GetLastError());
  32. return FALSE;
  33. }
  34. if (!CopyFileA(lpszSourceFileName, lpszDestFileName, FALSE)) {
  35. printf("CopyFile failed with 0x%x\n", GetLastError());
  36. return FALSE;
  37. }
  38. }
  39. return TRUE;
  40. }
  41. int __cdecl
  42. main(
  43. int argc,
  44. CHAR* argv[]
  45. )
  46. {
  47. if (argc != 3) {
  48. printf("Usage: forcecopy SourceFile DestFile\n");
  49. return 0;
  50. }
  51. return ForceCopy(argv[1], argv[2]);
  52. }