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.

219 lines
5.2 KiB

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <windows.h>
  4. #include <compdir.h>
  5. BOOL BinaryCompare( char *file1, char *file2)
  6. {
  7. register int char1, char2;
  8. FILE *filehandle1, *filehandle2;
  9. if ( (filehandle1 = fopen( file1, "rb")) == NULL)
  10. {
  11. fprintf( stderr, "cannot open %s\n", file1);
  12. return( FALSE);
  13. }
  14. if ( (filehandle2 = fopen( file2, "rb")) == NULL)
  15. {
  16. fprintf( stderr, "cannot open %s\n", file2);
  17. fclose( filehandle1);
  18. return( FALSE);
  19. }
  20. while ( TRUE)
  21. {
  22. if ( (char1 = getc( filehandle1)) != EOF)
  23. {
  24. if ( (char2 = getc( filehandle2)) != EOF)
  25. {
  26. if ( char1 != char2)
  27. {
  28. fclose( filehandle1);
  29. fclose( filehandle2);
  30. return( FALSE);
  31. }
  32. } else
  33. {
  34. fclose( filehandle1);
  35. fclose( filehandle2);
  36. return( FALSE);
  37. }
  38. } else
  39. {
  40. if ( (char2 = getc( filehandle2)) == EOF)
  41. {
  42. fclose( filehandle1);
  43. fclose( filehandle2);
  44. return( TRUE);
  45. } else
  46. {
  47. fclose( filehandle1);
  48. fclose( filehandle2);
  49. return( FALSE);
  50. }
  51. }
  52. }
  53. }
  54. /* Copies one file to another (both specified by path). Dynamically
  55. * allocates memory for the file buffer. Returns TRUE if successful,
  56. * or FALSE if unsuccessful. This function uses _dos_ functions only;
  57. * standard C functions are not used.
  58. */
  59. BOOL fastcopy( HANDLE hfSrcParm, HANDLE hfDstParm )
  60. {
  61. char _far *buf = NULL;
  62. unsigned segbuf, count;
  63. /* Attempt to dynamically allocate all of memory (0xffff paragraphs).
  64. * This will fail, but will return the amount actually available
  65. * in segbuf. Then allocate this amount.
  66. */
  67. if ( _dos_allocmem( 0xffff, &segbuf ) )
  68. {
  69. count = segbuf;
  70. if ( _dos_allocmem( count, &segbuf ) )
  71. {
  72. return FALSE;
  73. }
  74. }
  75. FP_SEG( buf ) = segbuf;
  76. /* Read and write until there is nothing left. */
  77. while ( count )
  78. {
  79. /* Read and write input. */
  80. if ( ( _dos_read( hfSrcParm, buf, count, &count )) )
  81. {
  82. _dos_freemem( segbuf );
  83. return FALSE;
  84. }
  85. if ( (_dos_write( hfDstParm, buf, count, &count )) )
  86. {
  87. _dos_freemem( segbuf );
  88. return FALSE;
  89. }
  90. }
  91. /* Free memory. */
  92. _dos_freemem( segbuf );
  93. return TRUE;
  94. }
  95. BOOL FCopy( char *src, char *dst, BOOL Output)
  96. {
  97. HANDLE srcfh, dstfh;
  98. BOOL result;
  99. ATTRIBUTE_TYPE Attributes;
  100. unsigned filedate, filetime;
  101. GET_ATTRIBUTES( src, Attributes);
  102. if ( Attributes == FILE_ATTRIBUTE_DIRECTORY)
  103. {
  104. if ( Output )
  105. {
  106. fprintf( stderr, "\nUnable to open source");
  107. }
  108. return FALSE;
  109. }
  110. if ( _dos_creatnew( src, _A_RDONLY, &srcfh) != 0)
  111. {
  112. if ( _dos_open( src, O_RDONLY, &srcfh) != 0)
  113. {
  114. if ( Output )
  115. {
  116. fprintf( stderr, "\nUnable to open source, error code %d", GetLastError() );
  117. }
  118. if ( srcfh != INVALID_HANDLE_VALUE)
  119. {
  120. CloseHandle( srcfh );
  121. }
  122. return FALSE;
  123. }
  124. }
  125. if ( _dos_getftime( srcfh, &filedate, &filetime) != 0)
  126. {
  127. if ( Output )
  128. {
  129. fprintf( stderr, "\nUnable to get time of source");
  130. }
  131. if ( srcfh != INVALID_HANDLE_VALUE)
  132. {
  133. CloseHandle( srcfh );
  134. }
  135. return FALSE;
  136. }
  137. if ( _dos_creatnew( dst, _A_NORMAL, &dstfh) != 0)
  138. {
  139. if ( _dos_open( dst, O_RDWR, &dstfh) != 0)
  140. {
  141. if ( Output )
  142. {
  143. fprintf( stderr, "\nUnable to create destination, error code %d", GetLastError() );
  144. }
  145. if ( srcfh != INVALID_HANDLE_VALUE)
  146. {
  147. CloseHandle( srcfh );
  148. }
  149. if ( dstfh != INVALID_HANDLE_VALUE)
  150. {
  151. CloseHandle( dstfh );
  152. }
  153. return FALSE;
  154. }
  155. }
  156. result = fastcopy( srcfh, dstfh );
  157. if ( !result)
  158. {
  159. if ( dstfh != INVALID_HANDLE_VALUE)
  160. {
  161. CloseHandle( dstfh );
  162. dstfh = INVALID_HANDLE_VALUE;
  163. }
  164. DeleteFile( dst );
  165. if ( srcfh != INVALID_HANDLE_VALUE)
  166. {
  167. CloseHandle( srcfh );
  168. }
  169. if ( Output )
  170. {
  171. fprintf( stderr, "\nUnable to copy file");
  172. }
  173. return FALSE;
  174. }
  175. if ( _dos_setftime( dstfh, filedate, filetime != 0))
  176. {
  177. if ( Output )
  178. {
  179. fprintf( stderr, "\nUnable to set time of destination");
  180. }
  181. if ( srcfh != INVALID_HANDLE_VALUE)
  182. {
  183. CloseHandle( srcfh );
  184. }
  185. if ( dstfh != INVALID_HANDLE_VALUE)
  186. {
  187. CloseHandle( dstfh );
  188. }
  189. return FALSE;
  190. }
  191. if ( srcfh != INVALID_HANDLE_VALUE)
  192. {
  193. CloseHandle( srcfh );
  194. }
  195. if ( dstfh != INVALID_HANDLE_VALUE)
  196. {
  197. CloseHandle( dstfh );
  198. }
  199. return TRUE;
  200. } // FCopy