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.

375 lines
9.8 KiB

  1. /*
  2. * pmdos.c
  3. *
  4. * Copyright (c) 1991, Microsoft Corporation
  5. *
  6. * DESCRIPTION
  7. *
  8. * This file is for support of program manager under NT Windows.
  9. * This file is/was ported from pmdos.asm (program manager).
  10. * It was in x86 asm code, and now is in ansi C.
  11. * Some functions will be removed, due to they are only needed
  12. * by DOS Windows.
  13. *
  14. * MODIFICATION HISTORY
  15. * Initial Version: x/x/90 Author Unknown, since he didn't feel
  16. * like commenting the code...
  17. *
  18. * NT 32b Version: 1/9/91 Jeff Pack
  19. * Intitial port to begin.
  20. *
  21. * WARNING: since this is NOT for DOS, I'm making it soley 32bit aware.
  22. * Following functions not ported
  23. * IsRemovable() is in pmcomman.c (already ifdef'd in asm code)
  24. * IsRemote() is in pmcomman.c (ditto!)
  25. *
  26. */
  27. #include <io.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <windows.h>
  31. #include <port1632.h>
  32. #if DBG
  33. #define KdPrint(_x_) OutputDebugStringA _x_;
  34. #else
  35. #define KdPrint(_x_)
  36. #endif
  37. #define LOCALBUFFERSIZE 128
  38. /*** FileTime -- Gets time of last modification.
  39. *
  40. *
  41. *
  42. * DWORD FileTime(HFILE hFile)
  43. *
  44. * ENTRY - int hFile - file handle to access
  45. *
  46. * EXIT - LPWORD - which is gotten from lpTimeStamp = 0 (ERROR).
  47. * or lpTimeStamp != 0 (value of timestamp)
  48. *
  49. * SYNOPSIS - calls GetFileTime() to get timestamp. If error, then
  50. * lpTimeStamp = 0, else contains TimeStamp for file.
  51. * WARNINGS -
  52. * EFFECTS -
  53. *
  54. */
  55. DWORD FileTime(
  56. HFILE hFile)
  57. {
  58. BOOL bReturnCode;
  59. FILETIME CreationTime;
  60. FILETIME LastAccessTime;
  61. FILETIME LastWriteTime;
  62. WORD FatTime;
  63. WORD FatDate;
  64. bReturnCode = GetFileTime((HANDLE)hFile, &CreationTime, &LastAccessTime,
  65. &LastWriteTime);
  66. /*
  67. * Test return code
  68. */
  69. if (bReturnCode == FALSE) {
  70. return 0; /*set to zero, for error*/
  71. }
  72. /*
  73. * Now convert 64bit time to DOS 16bit time
  74. */
  75. FileTimeToDosDateTime( &LastWriteTime, &FatDate, &FatTime);
  76. return FatTime;
  77. }
  78. /*** IsReadOnly -- determines if file is readonly or not.
  79. *
  80. *
  81. *
  82. * BOOL IsReadOnly(LPSTR lpszFileString)
  83. *
  84. * ENTRY - LPSTR lpszFileString - file name to use
  85. *
  86. * EXIT - BOOL xxx - returns (0) = not readonly (1) = read only
  87. * or lpTimeStamp != 0 (value of timestamp)
  88. *
  89. * SYNOPSIS - calls GetAttributes, then tests if file is read only.
  90. * WARNINGS -
  91. * EFFECTS -
  92. *
  93. */
  94. BOOL IsReadOnly(LPTSTR lpszFileString)
  95. {
  96. DWORD dwReturnedAttributes;
  97. LPTSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  98. DWORD nBufferLength;
  99. nBufferLength = lstrlen(lpszFileString) + 1;
  100. /*alloc local, non-moveable, zero filled buffer*/
  101. lpszLocalBuffer = (LPTSTR)LocalAlloc(0, sizeof(TCHAR)*nBufferLength);
  102. if(lpszLocalBuffer == NULL){
  103. KdPrint(("<IsReadOnly> LocalAlloc FAILed\n"));
  104. }
  105. lstrcpy(lpszLocalBuffer,lpszFileString);
  106. /*get attributes of filestring*/
  107. dwReturnedAttributes = GetFileAttributes(lpszLocalBuffer);
  108. if(dwReturnedAttributes == -1){
  109. KdPrint(("<IsReadOnly> - GetFileAttributes() FAILed!\n"));
  110. LocalFree(lpszLocalBuffer);
  111. return FALSE;
  112. } else {
  113. /*AND with read_only attribute*/
  114. dwReturnedAttributes = dwReturnedAttributes & FILE_ATTRIBUTE_READONLY;
  115. switch(dwReturnedAttributes){
  116. case FILE_ATTRIBUTE_READONLY:
  117. LocalFree(lpszLocalBuffer);
  118. return TRUE;
  119. break;
  120. default:
  121. LocalFree(lpszLocalBuffer);
  122. return FALSE;
  123. }
  124. }
  125. }
  126. /*** GetDOSErrorCode -- returns extended error code
  127. *
  128. *
  129. *
  130. * DWORD GetDOSErrorCode(VOID)
  131. *
  132. * ENTRY - VOID
  133. *
  134. * EXIT - DWORD - returned extended code.
  135. *
  136. * SYNOPSIS - calls GetLastError() to get error code from OS
  137. * WARNINGS -
  138. * EFFECTS -
  139. *
  140. */
  141. DWORD GetDOSErrorCode(VOID)
  142. {
  143. return( (int) GetLastError());
  144. /*BUG BUG, pmgseg.c uses this from _lcreat() to determine if returned
  145. 5 (access denied) or 13 (invalid_data). So this need be tested
  146. to see if win32 returns these.*/
  147. }
  148. /*** DosDelete -- Delete named file.
  149. *
  150. * int DosDelete(LPSTR lpszFileToDelete)
  151. *
  152. * ENTRY - LPSTR lpszFileToDelete - filename to delete.
  153. *
  154. * EXIT - int xxx - returns (0) if success
  155. *
  156. * SYNOPSIS - calls win32 DeleteFile.
  157. * WARNINGS -
  158. * EFFECTS -
  159. *
  160. */
  161. int DosDelete(LPTSTR lpszFileToDelete)
  162. {
  163. BOOL bReturnCode;
  164. LPTSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  165. DWORD nBufferLength;
  166. nBufferLength = lstrlen(lpszFileToDelete) + 1;
  167. /*alloc local, non-moveable, zero filled buffer*/
  168. lpszLocalBuffer = (LPTSTR)LocalAlloc(0, sizeof(TCHAR)*nBufferLength);
  169. if(lpszLocalBuffer == NULL){
  170. KdPrint(("<DosDelete> LocalAlloc FAILed\n"));
  171. }
  172. lstrcpy(lpszLocalBuffer,lpszFileToDelete);
  173. bReturnCode = DeleteFile(lpszLocalBuffer);
  174. LocalFree(lpszLocalBuffer);
  175. if(bReturnCode){
  176. return(0);
  177. }
  178. else{
  179. return(1);
  180. }
  181. }
  182. /*** DosRename -- Rename file.
  183. *
  184. * int DosRename(LPSTR lpszOrgFileName, LPSTR lpszNewFileName)
  185. *
  186. * ENTRY - LPSTR lpszOrgFileName - origianl filename.
  187. * LPSTR lpszNewFileName - New filename.
  188. *
  189. * EXIT - int xxx - returns (0) if success
  190. *
  191. * SYNOPSIS - calls win32 MoveFile.
  192. * WARNINGS -
  193. * EFFECTS -
  194. *
  195. */
  196. int DosRename(LPTSTR lpszOrgFileName, LPTSTR lpszNewFileName)
  197. {
  198. BOOL bReturnCode;
  199. LPTSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  200. LPTSTR lpszLocalBuffer1; /*local buffer for AnsiToOem()*/
  201. DWORD nBufferLength;
  202. DWORD nBufferLength1;
  203. nBufferLength = lstrlen(lpszOrgFileName) + 1;
  204. nBufferLength1 = lstrlen(lpszNewFileName) + 1;
  205. /*alloc local, non-moveable, zero filled buffer*/
  206. lpszLocalBuffer = (LPTSTR)LocalAlloc(0, sizeof(TCHAR)*nBufferLength);
  207. if(lpszLocalBuffer == NULL){
  208. KdPrint(("<DosRename> LocalAlloc FAILed\n"));
  209. }
  210. lpszLocalBuffer1 = (LPTSTR)LocalAlloc(0, sizeof(TCHAR)*nBufferLength1);
  211. if(lpszLocalBuffer1 == NULL){
  212. KdPrint(("<DosRename> LocalAlloc FAILed\n"));
  213. }
  214. lstrcpy(lpszLocalBuffer,lpszOrgFileName);
  215. lstrcpy(lpszLocalBuffer1,lpszNewFileName);
  216. /*rename file*/
  217. bReturnCode = MoveFile(lpszLocalBuffer, lpszLocalBuffer1);
  218. LocalFree(lpszLocalBuffer);
  219. LocalFree(lpszLocalBuffer1);
  220. if(bReturnCode){
  221. return(0);
  222. }
  223. else{
  224. return(1);
  225. }
  226. }
  227. #if 0
  228. #ifdef i386
  229. //
  230. // Returns true if the application exe type is a DOS binary.
  231. //
  232. BOOL IsDOSApplication(LPTSTR lpPath)
  233. {
  234. DWORD dwBinaryType;
  235. BOOL bRet;
  236. bRet = GetBinaryType(lpPath, &dwBinaryType);
  237. if (bRet) {
  238. if (dwBinaryType != SCS_DOS_BINARY) {
  239. bRet = FALSE;
  240. }
  241. }
  242. return(bRet);
  243. }
  244. //
  245. // this routine translates path characters into _ characters because
  246. // the NT registry apis do not allow the creation of keys with
  247. // names that contain path characters. it allocates a buffer that
  248. // must be freed.
  249. //
  250. LPTSTR TranslateConsoleTitle(LPTSTR ConsoleTitle)
  251. {
  252. int ConsoleTitleLength,i;
  253. LPTSTR TranslatedConsoleTitle,Tmp;
  254. ConsoleTitleLength = lstrlen(ConsoleTitle) + 1;
  255. Tmp = TranslatedConsoleTitle = (LPTSTR)LocalAlloc(LMEM_FIXED,ConsoleTitleLength * sizeof(TCHAR));
  256. if (TranslatedConsoleTitle == NULL) {
  257. return NULL;
  258. }
  259. for (i=0;i<ConsoleTitleLength;i++) {
  260. if (*ConsoleTitle == TEXT('\\')) {
  261. *TranslatedConsoleTitle++ = (TCHAR)TEXT('_');
  262. ConsoleTitle++;
  263. } else {
  264. *TranslatedConsoleTitle++ = *ConsoleTitle++;
  265. }
  266. }
  267. return Tmp;
  268. }
  269. // DOS apps are no longer set to fullscreen by default in progman
  270. // 5-3-93 johannec (bug 8343)
  271. #define CONSOLE_REGISTRY_STRING TEXT("Console")
  272. #define CONSOLE_REGISTRY_FULLSCR TEXT("FullScreen")
  273. BOOL SetDOSApplicationToFullScreen(LPTSTR lpTitle)
  274. {
  275. HKEY hkeyConsole,hkeyTitle;
  276. LPTSTR lpTranslatedTitle;
  277. DWORD Error;
  278. DWORD dwFullScreen = 1;
  279. Error = RegOpenKeyEx(HKEY_CURRENT_USER,
  280. CONSOLE_REGISTRY_STRING,
  281. 0,
  282. KEY_READ | KEY_WRITE,
  283. &hkeyConsole);
  284. if (Error) {
  285. return FALSE;
  286. }
  287. lpTranslatedTitle = TranslateConsoleTitle(lpTitle);
  288. if (lpTranslatedTitle == NULL) {
  289. return FALSE;
  290. }
  291. Error = RegCreateKey(hkeyConsole,
  292. lpTranslatedTitle,
  293. &hkeyTitle);
  294. LocalFree(lpTranslatedTitle);
  295. if (Error) {
  296. RegCloseKey(hkeyConsole);
  297. return FALSE;
  298. }
  299. Error = RegSetValueEx(hkeyTitle,
  300. CONSOLE_REGISTRY_FULLSCR,
  301. 0,
  302. REG_DWORD,
  303. (LPBYTE)&dwFullScreen,
  304. sizeof(dwFullScreen));
  305. RegCloseKey(hkeyTitle);
  306. RegCloseKey(hkeyConsole);
  307. return(!Error);
  308. }
  309. #endif
  310. #endif