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.

281 lines
6.8 KiB

  1. /* (C) Microsoft Corp., 1991. All rights reserved. */
  2. /* mapcopy.c - duplicates the MIDIMAP.CFG file because of poor design
  3. * !!!!!!!!!!!! THIS IS NEEDLESS GRATUITIOUS CODE !!!!!!!!!!!!!!!!!!!
  4. * The MIDIMAP and applet were badly implemented and because of design
  5. * flaws needed this gratuitous file copy to back up the configuration
  6. * file.
  7. */
  8. #include<windows.h>
  9. #include <mmsystem.h>
  10. #if defined(WIN32)
  11. #include <port1632.h>
  12. #endif
  13. #include "hack.h"
  14. #include"midimap.h"
  15. #include "extern.h"
  16. #include "midi.h"
  17. #define MAXBUF (10 * 1024) // size of default copy buffer
  18. #define FOPEN(sz) (_lopen(sz,OF_READ|OF_SHARE_DENY_NONE))
  19. #define FCREATE(sz) (_lcreat(sz,0))
  20. #define FCLOSE(fh) (_lclose(fh))
  21. #define FREAD(fh,buf,len) (_lread(fh,buf,len))
  22. #define FWRITE(fh,buf,len) (_lwrite(fh,buf,len))
  23. #define FALLOC(n) (LPVOID)GlobalLock(GlobalAlloc(GMEM_MOVEABLE, (DWORD)(n)))
  24. #define FFREE(n) (GlobalUnlock((HGLOBAL)HIWORD((DWORD)(n))),GlobalFree((HGLOBAL)HIWORD((DWORD)(n))))
  25. #define SLASH(c) ((c) == '/' || (c) == '\\')
  26. static int FileCopy(LPSTR szSrc, LPSTR szDst);
  27. #if defined(WIN16)
  28. static BOOL FileCopy(LPSTR,LPSTR);
  29. static UINT GetFileAttributes(LPSTR);
  30. static DWORD DosDiskFree ( BYTE bDrive );
  31. static int DosCWDrive (VOID);
  32. /*
  33. * Get DOS file attributes
  34. */
  35. static UINT GetFileAttributes(LPSTR lpszPath)
  36. {
  37. _asm {
  38. push ds ; Preserve DS
  39. lds dx,lpszPath ; DS:DI = lpszPath
  40. mov ax,4300h ; Get File Attributes
  41. int 21h
  42. jc GFAErr
  43. mov ax,cx ; AX = attribute
  44. jmp GFAExit ; Return attribute
  45. GFAErr: mov ah,80h ; Return negative error code
  46. GFAExit:
  47. pop ds ; Restore DS
  48. }
  49. }
  50. int FAR PASCAL DosDelete(LPSTR szFile)
  51. {
  52. _asm {
  53. push ds ; Preserve DS
  54. lds dx,szFile
  55. mov ah,41h
  56. int 21h
  57. jc dexit
  58. xor ax,ax
  59. dexit:
  60. pop ds ; Restore DS
  61. }
  62. }
  63. static DWORD DosDiskFree ( BYTE bDrive )
  64. {
  65. WORD wSectors,wBytes,wClusters;
  66. _asm {
  67. mov dl,bDrive
  68. mov ah,36h
  69. int 21h
  70. mov wSectors,ax
  71. mov wClusters,bx
  72. mov wBytes,cx
  73. }
  74. if (wSectors == 0xffff)
  75. return -1;
  76. else
  77. return ((DWORD)wClusters*(DWORD)wSectors*(DWORD)wBytes);
  78. }
  79. static int DosCWDrive ()
  80. {
  81. _asm {
  82. mov ah,19h ; Get Current Drive
  83. int 21h
  84. sub ah,ah ; Zero out AH
  85. }
  86. }
  87. #endif //WIN16
  88. /*
  89. * Create a duplicate map file for editing
  90. */
  91. BOOL FAR PASCAL DupMapCfg(LPSTR lpstrCfgPath, LPSTR lpstrBakPath)
  92. {
  93. extern BOOL fReadOnly;
  94. UINT uAttr;
  95. OFSTRUCT of;
  96. int err;
  97. char szFunc[50],szMessage[256];
  98. if (OpenFile(lpstrCfgPath,&of,OF_EXIST|OF_SHARE_DENY_NONE) == HFILE_ERROR)
  99. {
  100. // We need to let the mapper create a new one.
  101. return FALSE;
  102. }
  103. uAttr = GetFileAttributes(lpstrCfgPath);
  104. if (uAttr != (UINT)(-1))
  105. {
  106. if (uAttr & FILE_ATTRIBUTE_READONLY)
  107. {
  108. LoadString(hLibInst, IDS_FCERR_WARN, szFunc, sizeof(szFunc));
  109. LoadString(hLibInst, IDS_FCERR_READONLY, szMessage, sizeof(szMessage));
  110. MessageBox(NULL, szMessage, szFunc,
  111. MB_ICONEXCLAMATION | MB_OK);
  112. fReadOnly = TRUE; // Stay Read Only for the remainder
  113. return FALSE;
  114. }
  115. }
  116. else
  117. return FALSE;
  118. err = FileCopy(lpstrCfgPath,lpstrBakPath);
  119. if (err != IDS_FCERR_SUCCESS)
  120. {
  121. LoadString(hLibInst, IDS_FCERR_ERROR, szFunc, sizeof(szFunc));
  122. LoadString(hLibInst, err, szMessage, sizeof(szMessage));
  123. MessageBox(NULL, szMessage, szFunc, MB_ICONHAND | MB_TASKMODAL
  124. | MB_OK);
  125. fReadOnly = TRUE; // Read Only if there is some kind of disk problem
  126. return FALSE;
  127. }
  128. return TRUE;
  129. }
  130. /*
  131. * Copy the duplicate map file over the original map file
  132. */
  133. BOOL FAR PASCAL UpdateMapCfg(LPSTR lpstrCfgPath, LPSTR lpstrBakPath)
  134. {
  135. int err;
  136. char szFunc[50],szMessage[256];
  137. if ((err = FileCopy(lpstrBakPath,lpstrCfgPath)) != IDS_FCERR_SUCCESS)
  138. {
  139. LoadString(hLibInst, IDS_FCERR_ERROR, szFunc, sizeof(szFunc));
  140. LoadString(hLibInst, err, szMessage, sizeof(szMessage));
  141. MessageBox(NULL, szMessage, szFunc, MB_ICONHAND | MB_TASKMODAL
  142. | MB_OK);
  143. }
  144. return err;
  145. }
  146. #if defined(WIN16)
  147. /* So who was too lazy to write a comment? */
  148. static int FileCopy(LPSTR szSrc,LPSTR szDst)
  149. {
  150. HFILE fhSrc,fhDst;
  151. WORD size;
  152. BOOL fComplete = TRUE;
  153. OFSTRUCT of;
  154. LPSTR lpbBuf;
  155. DWORD dwSrcSize,dwDestFree;
  156. BYTE bDrive;
  157. int err;
  158. if (OpenFile(szSrc,&of,OF_EXIST|OF_SHARE_DENY_NONE) == HFILE_ERROR)
  159. {
  160. err = IDS_FCERR_NOSRC;
  161. goto exit; //ERROR:File Not Found
  162. }
  163. fhSrc = FOPEN(szSrc);
  164. if (fhSrc == HFILE_ERROR)
  165. {
  166. err = IDS_FCERR_NOSRC;
  167. return FALSE; //ERROR:File inaccessible
  168. }
  169. dwSrcSize = _llseek(fhSrc,0,2); //file size
  170. _llseek(fhSrc,0,0);
  171. if (*(szDst+1) == ':')
  172. {
  173. bDrive = (BYTE)(*(szDst) - 'A' + 1);
  174. }
  175. else
  176. bDrive = (BYTE)(DosCWDrive() + 1);
  177. if ((LONG)(dwDestFree = DosDiskFree(bDrive)) != -1)
  178. {
  179. if (dwSrcSize > dwDestFree)
  180. {
  181. err = IDS_FCERR_DISKFULL;
  182. goto errclose1;
  183. }
  184. }
  185. /* This failed on a wierd 386 machine for no good reason, so removed.
  186. MM Bug 6277.
  187. else
  188. {
  189. err = IDS_FCERR_DISK;
  190. goto errclose1;
  191. }
  192. */
  193. lpbBuf = FALLOC(MAXBUF);
  194. if (lpbBuf == NULL)
  195. {
  196. err = IDS_FCERR_LOMEM;
  197. goto errclose1; //ERROR:Low Memory
  198. }
  199. fhDst = FCREATE(szDst);
  200. if (fhDst == HFILE_ERROR)
  201. {
  202. err = IDS_FCERR_NODEST;
  203. goto errfree; //ERROR:Couldn't create Destination
  204. }
  205. while (size = FREAD(fhSrc,lpbBuf,MAXBUF))
  206. {
  207. if (FWRITE(fhDst,lpbBuf,size) != size)
  208. {
  209. err = IDS_FCERR_WRITE;
  210. goto errclose; //ERROR:Write Error
  211. }
  212. }
  213. err = IDS_FCERR_SUCCESS;
  214. errclose: // Close the Destination File
  215. FCLOSE(fhDst);
  216. errfree: // Free the buffer
  217. FFREE(lpbBuf);
  218. errclose1: // Close the Source File
  219. FCLOSE(fhSrc);
  220. exit:
  221. return err;
  222. }
  223. #else
  224. /* Copy the file szSrc to the file szDst, overwriting it if it already exists */
  225. static int FileCopy(LPSTR szSrc, LPSTR szDst)
  226. {
  227. if (CopyFile(szSrc, szDst, FALSE)) {
  228. return IDS_FCERR_SUCCESS;
  229. } else {
  230. switch (GetLastError()) {
  231. // ??? This is throwing away valuable error information!
  232. default:
  233. return IDS_FCERR_NOSRC;
  234. }
  235. }
  236. } /* FileCopy */
  237. #endif //WIN16