Leaked source code of windows server 2003
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.

140 lines
4.2 KiB

  1. /* move from one file to another */
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <process.h>
  7. #include <windows.h>
  8. #include <tools.h>
  9. __cdecl main (c, v)
  10. int c;
  11. char *v[];
  12. {
  13. struct findType fbuf;
  14. char src[MAX_PATH], dst[MAX_PATH], name[MAX_PATH];
  15. char *s;
  16. int i, erc;
  17. char *y;
  18. BOOL fExpunge, fDelayUntilReboot;
  19. DWORD dwMoveFileFlags;
  20. ConvertAppToOem( c, v );
  21. SHIFT (c,v);
  22. if (c < 2) {
  23. ShowUsage:
  24. printf ("Usage: mv [/x [/d]] file1 [ file2 ...] target\n");
  25. printf (" /x dont save deleted files in deleted subdirectory\n");
  26. printf (" /d specifies to delay the rename until the next reboot.\n");
  27. exit (1);
  28. }
  29. dwMoveFileFlags = MOVEFILE_REPLACE_EXISTING |
  30. MOVEFILE_COPY_ALLOWED;
  31. fExpunge = FALSE;
  32. fDelayUntilReboot = FALSE;
  33. for (i=0; i<c; i++) {
  34. nextArg:
  35. s = v[i];
  36. if (*s == '/' || *s == '-') {
  37. SHIFT (c,v);
  38. while (*++s) {
  39. switch (tolower(*s)) {
  40. case 'x': fExpunge = TRUE; break;
  41. case 'd': if (fExpunge) {
  42. dwMoveFileFlags |= MOVEFILE_DELAY_UNTIL_REBOOT;
  43. dwMoveFileFlags &= ~MOVEFILE_COPY_ALLOWED;
  44. break;
  45. }
  46. default: goto ShowUsage;
  47. }
  48. goto nextArg;
  49. }
  50. } else {
  51. findpath (v[i], src, FALSE);
  52. pname (src);
  53. v[i] = _strdup (src);
  54. }
  55. }
  56. if (rootpath (v[c-1], dst) == -1) {
  57. printf ("Cannot move to %s - %s\n", v[c-1], error ());
  58. exit (1);
  59. } else {
  60. if ( dst[0] == '\\' && dst[1] == '\\' ) {
  61. y = strbscan (&dst[3], "/\\");
  62. if ( *y != '\0' ) {
  63. y = strbscan( y+1, "/\\");
  64. if ( *y == '\0' ) {
  65. strcat(dst, "\\" );
  66. }
  67. }
  68. }
  69. }
  70. if (fPathChr (dst[strlen(dst)-1])) {
  71. SETFLAG (fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  72. }
  73. else if (ffirst (dst, FILE_ATTRIBUTE_DIRECTORY, &fbuf)) {
  74. findclose( &fbuf ); /* Let next ffirst work */
  75. RSETFLAG (fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY);
  76. }
  77. else if (TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  78. strcat (dst, "\\");
  79. }
  80. /* if more than 1 source and dest is a file */
  81. if (c != 2 && !TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  82. printf ("Cannot move > 1 file to another file\n");
  83. exit (1);
  84. }
  85. erc = 0;
  86. for (i=0; i < c-1; i++) {
  87. if (rootpath (v[i], src) == -1) {
  88. printf ("Cannot move %s - %s\n", v[i], error ());
  89. erc++;
  90. continue;
  91. }
  92. strcpy (name, dst);
  93. if (TESTFLAG(fbuf.fbuf.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) {
  94. if (!fPathChr (name[strlen(name)-1])) {
  95. strcat (name, "\\");
  96. }
  97. upd (src, name, name);
  98. }
  99. if (strcmp (src, name)) {
  100. printf ("%s => %s ", src, name);
  101. fflush (stdout);
  102. if (fExpunge) {
  103. if (MoveFileEx( src, dst, dwMoveFileFlags )) {
  104. if (dwMoveFileFlags & MOVEFILE_DELAY_UNTIL_REBOOT)
  105. printf ("[ok, will happen next reboot]\n");
  106. else
  107. printf ("[ok]\n");
  108. }
  109. else {
  110. printf( "failed - Error Code == %u\n", GetLastError() );
  111. }
  112. }
  113. else {
  114. s = fmove( src, name );
  115. if (s) {
  116. erc++;
  117. printf ("[%s]\n", s);
  118. }
  119. else
  120. printf ("[ok]\n");
  121. }
  122. }
  123. else
  124. printf ("Source and destination the same, %s\n", src);
  125. }
  126. return(erc != 0);
  127. }