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.

659 lines
21 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. #ifndef ORGCODE
  28. #include <io.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #endif
  32. #include "windows.h"
  33. #include <port1632.h>
  34. BOOL PathType(LPSTR);
  35. DWORD FileTime(HFILE);
  36. DWORD GetDOSErrorCode(VOID);
  37. int GetCurrentDrive(VOID);
  38. int w_GetCurrentDirectory(int, LPSTR);
  39. int w_SetCurrentDirectory(LPSTR);
  40. int DosDelete(LPSTR);
  41. LPSTR lmemmove(LPSTR, LPSTR, WORD);
  42. BOOL FAR PASCAL IsRemoteDrive(int);
  43. BOOL FAR PASCAL IsRemovableDrive(int);
  44. #define LOCALBUFFERSIZE 128
  45. /*** PathType -- Determines if string denotes a directory or not.
  46. *
  47. *
  48. *
  49. * BOOL PathType(LPSTR pszFileString)
  50. *
  51. * ENTRY - LPSTR pszFileString - pointer to string to use to determine if directory
  52. * or not.
  53. * window, with focus.
  54. * EXIT - int iReturnValue - 2 = is directory 1 = Is Not directory
  55. *
  56. * SYNOPSIS - This function takes a pointer to a string, calls OS to determine
  57. * if string is, or is not a directory.
  58. * WARNINGS - Cna't even see where this is called!
  59. * EFFECTS -
  60. *
  61. */
  62. BOOL PathType(LPSTR lpszFileString)
  63. {
  64. LPSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  65. DWORD dwReturnedAttributes;
  66. DWORD nBufferLength;
  67. nBufferLength = strlen(lpszFileString) + 1;
  68. /*alloc local, non-moveable, zero filled buffer*/
  69. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  70. if(lpszLocalBuffer == NULL){
  71. #if DBG
  72. OutputDebugString("<PathType> LocalAlloc FAILed\n");
  73. #endif /* DBG */
  74. return 0;
  75. }
  76. AnsiToOem(lpszFileString, lpszLocalBuffer);
  77. /*get attributes of filestring*/
  78. dwReturnedAttributes = GetFileAttributes(lpszLocalBuffer);
  79. if(dwReturnedAttributes == -1){
  80. #if DBG
  81. OutputDebugString("<PathType> - GetFileAttributes() FAILed!\n");
  82. #endif /* DBG */
  83. LocalFree(lpszLocalBuffer);
  84. return(0);
  85. }
  86. else{
  87. /*and with directory attribute*/
  88. dwReturnedAttributes = dwReturnedAttributes & FILE_ATTRIBUTE_DIRECTORY;
  89. switch(dwReturnedAttributes){
  90. case FILE_ATTRIBUTE_DIRECTORY:
  91. LocalFree(lpszLocalBuffer);
  92. return(2);
  93. break;
  94. default:
  95. LocalFree(lpszLocalBuffer);
  96. return(1);
  97. }
  98. }
  99. }
  100. /*** FileTime -- Gets time of last modification.
  101. *
  102. *
  103. *
  104. * DWORD FileTime(HFILE hFile)
  105. *
  106. * ENTRY - int hFile - file handle to access
  107. *
  108. * EXIT - LPWORD - which is gotten from lpTimeStamp = 0 (ERROR).
  109. * or lpTimeStamp != 0 (value of timestamp)
  110. *
  111. * SYNOPSIS - calls GetFileTime() to get timestamp. If error, then
  112. * lpTimeStamp = 0, else contains TimeStamp for file.
  113. * WARNINGS -
  114. * EFFECTS -
  115. *
  116. */
  117. DWORD FileTime(
  118. HFILE hFile)
  119. {
  120. BOOL bReturnCode;
  121. FILETIME CreationTime;
  122. FILETIME LastAccessTime;
  123. FILETIME LastWriteTime;
  124. WORD FatTime = 0;
  125. WORD FatDate;
  126. bReturnCode = GetFileTime(LongToHandle(hFile), &CreationTime, &LastAccessTime,
  127. &LastWriteTime);
  128. /*
  129. * Test return code
  130. */
  131. if (bReturnCode == FALSE) {
  132. return 0; /*set to zero, for error*/
  133. }
  134. /*
  135. * Now convert 64bit time to DOS 16bit time
  136. */
  137. FileTimeToDosDateTime( &LastWriteTime, &FatDate, &FatTime);
  138. return FatTime;
  139. }
  140. /*** IsReadOnly -- determines if file is readonly or not.
  141. *
  142. *
  143. *
  144. * BOOL IsReadOnly(LPSTR lpszFileString)
  145. *
  146. * ENTRY - LPSTR lpszFileString - file name to use
  147. *
  148. * EXIT - BOOL xxx - returns (0) = not readonly (1) = read only
  149. * or lpTimeStamp != 0 (value of timestamp)
  150. *
  151. * SYNOPSIS - calls GetAttributes, then tests if file is read only.
  152. * WARNINGS -
  153. * EFFECTS -
  154. *
  155. */
  156. BOOL IsReadOnly(LPSTR lpszFileString)
  157. {
  158. DWORD dwReturnedAttributes;
  159. LPSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  160. DWORD nBufferLength;
  161. nBufferLength = strlen(lpszFileString) + 1;
  162. /*alloc local, non-moveable, zero filled buffer*/
  163. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  164. if(lpszLocalBuffer == NULL){
  165. #if DBG
  166. OutputDebugString("<IsReadOnly> LocalAlloc FAILed\n");
  167. #endif /* DBG */
  168. return 0;
  169. }
  170. AnsiToOem(lpszFileString, lpszLocalBuffer);
  171. /*get attributes of filestring*/
  172. dwReturnedAttributes = GetFileAttributes(lpszLocalBuffer);
  173. if(dwReturnedAttributes == -1){
  174. #if DBG
  175. OutputDebugString("<IsReadOnly> - GetFileAttributes() FAILed!\n");
  176. #endif /* DBG */
  177. LocalFree(lpszLocalBuffer);
  178. return FALSE;
  179. } else {
  180. /*AND with read_only attribute*/
  181. dwReturnedAttributes = dwReturnedAttributes & FILE_ATTRIBUTE_READONLY;
  182. switch(dwReturnedAttributes){
  183. case FILE_ATTRIBUTE_READONLY:
  184. LocalFree(lpszLocalBuffer);
  185. return TRUE;
  186. break;
  187. default:
  188. LocalFree(lpszLocalBuffer);
  189. return FALSE;
  190. }
  191. }
  192. }
  193. /*** GetDOSErrorCode -- returns extended error code
  194. *
  195. *
  196. *
  197. * DWORD GetDOSErrorCode(VOID)
  198. *
  199. * ENTRY - VOID
  200. *
  201. * EXIT - DWORD - returned extended code.
  202. *
  203. * SYNOPSIS - calls GetLastError() to get error code from OS
  204. * WARNINGS -
  205. * EFFECTS -
  206. *
  207. */
  208. DWORD GetDOSErrorCode(VOID)
  209. {
  210. return( (int) GetLastError());
  211. /*BUG BUG, pmgseg.c uses this from _lcreat() to determine if returned
  212. 5 (access denied) or 13 (invalid_data). So this need be tested
  213. to see if win32 returns these.*/
  214. }
  215. /*** GetCurrentDrive -- get current drive number.
  216. *
  217. *
  218. *
  219. * int GetCurrentDrive(VOID)
  220. *
  221. * ENTRY - VOID
  222. *
  223. * EXIT - int CurrentDrive - drive number of current drive (0=a, etc).
  224. *
  225. * SYNOPSIS - calls GetCurrentDirectory, must parse returned string
  226. * for either drive letter, or UNC path. If UNC I gotta
  227. * somehow, covert UNC path to drive letter to drive number.
  228. * WARNINGS -
  229. * EFFECTS -
  230. *
  231. */
  232. int GetCurrentDrive(VOID)
  233. {
  234. /*BUG BUG, not DBCS aware!*/
  235. DWORD nBufferLength = LOCALBUFFERSIZE;
  236. DWORD dwReturnCode;
  237. LPSTR lpszLocalBuffer;
  238. int iDriveNumber;
  239. /*alloc local, non-moveable, zero filled buffer*/
  240. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  241. if(lpszLocalBuffer == NULL){
  242. #if DBG
  243. OutputDebugString("<GetCurrentDrive> LocalAlloc FAILed\n");
  244. #endif /* DBG */
  245. return 0;
  246. }
  247. GetCurDrive1:
  248. dwReturnCode = GetCurrentDirectory(nBufferLength, lpszLocalBuffer);
  249. /*failed for reason other than bufferlength too small*/
  250. if(dwReturnCode == 0){
  251. #if DBG
  252. OutputDebugString("<GetCurrentDrive> GetCurrentDirectory() FAILed\n");
  253. #endif /* DBG */
  254. LocalFree(lpszLocalBuffer);
  255. return 0;
  256. }
  257. /*test for success, if dwReturnCode is > buffer, then need increase buffer*/
  258. if(dwReturnCode > nBufferLength){
  259. PVOID pv = LocalReAlloc(lpszLocalBuffer, nBufferLength + LOCALBUFFERSIZE, LMEM_ZEROINIT | LMEM_MOVEABLE);
  260. if (!pv) {
  261. #if DBG
  262. OutputDebugString("<GetCurrentDrive> LocalAlloc FAILed\n");
  263. #endif /* DBG */
  264. LocalFree(lpszLocalBuffer);
  265. return 0;
  266. } else {
  267. lpszLocalBuffer = pv;
  268. nBufferLength += LOCALBUFFERSIZE;
  269. }
  270. goto GetCurDrive1;
  271. }
  272. /*finally lpszLocalBuffer has string containing current directory*/
  273. /* now must parse string for ":" or "\\" for drive letter or UNC*/
  274. /*if : then get drive letter, and convert to number a=0, b=1, etc.*/
  275. /*if \\ then gotta enumerate net drives, to learn what drive letter*/
  276. /*corresponds to that UNC path*/
  277. /*check for drive letter*/
  278. if(lpszLocalBuffer[1] == ':'){
  279. /*is drive letter, proceed*/
  280. if(isupper(lpszLocalBuffer[0])){
  281. iDriveNumber = lpszLocalBuffer[0] - 'A'; /*convert letter > number*/
  282. }
  283. else{
  284. iDriveNumber = lpszLocalBuffer[0] - 'a'; /*convert letter > number*/
  285. }
  286. }
  287. else{
  288. /*must be UNC path*/
  289. /*BUG BUG need write code to convert UNC path */
  290. #if DBG
  291. OutputDebugString("<GetCurrentDrive> Got UNC path, didnt expect, and no code!\n");
  292. #endif /* DBG */
  293. }
  294. LocalFree(lpszLocalBuffer);
  295. return(iDriveNumber);
  296. }
  297. /*** SetCurrentDrive -- set current drive.
  298. *
  299. *
  300. *
  301. * int SetCurrentDrive(int iDrive)
  302. *
  303. * ENTRY - int iDrive - drive number to set as current drive
  304. *
  305. * EXIT - int xxx - under DOS would have returned # of logical drives.
  306. * I can do this, but it's not used, if fact, no error
  307. * checks are done on this return value.
  308. *
  309. * SYNOPSIS - calls SetCurrentDirectory to set current drive.
  310. * WARNINGS - ALWAYS sets to root directory, since can't get cur dir
  311. * on other than current working drive.
  312. * EFFECTS -
  313. *
  314. */
  315. int SetCurrentDrive(int iDrive)
  316. {
  317. char cLocalBuffer[LOCALBUFFERSIZE] = "C:\\";
  318. char cDriveLetter;
  319. /*convert drive number (zero based) to letter*/
  320. cDriveLetter = (char) iDrive + (char)'A';
  321. cLocalBuffer[0] = cDriveLetter; /*set new drive in string*/
  322. if(!SetCurrentDirectory(cLocalBuffer)){
  323. /*call failed*/
  324. #if DBG
  325. OutputDebugString("<SetCurrentDrive> SetCurrentDirectory FAILed!\n");
  326. #endif /* DBG */
  327. return 0;
  328. }
  329. return(0);
  330. }
  331. /*** w_GetCurrentDirectory -- GetCurrent Working Directory
  332. *
  333. *
  334. *
  335. * int w_GetCurrentDirectory(int iDrive, LPSTR lpszCurrentDirectory)
  336. *
  337. * ENTRY - int iDrive - drive number to use as current drive.
  338. * LPSTR lpszCurrentDirectory - pointer to return data to.
  339. *
  340. * EXIT - int iReturnCode - returns (0) if success
  341. * LPSTR lpszCurrentDirectory - has curretn directory.
  342. *
  343. * SYNOPSIS - calls GetCurrentDirectory to get current directory.
  344. * the original asm code, checked idrive for zero, if so
  345. * then calls GetCurrentDrive. Under win32, is not neccessary,
  346. * since GetCUrrentDirectory() returns current drive.
  347. * Since it checks this, it means then that other than current
  348. * drive can be checked, yet win32 doesnt allow this, so I have to
  349. * code in a debug check, if iDrive != current drive.
  350. * WARNINGS - win32 doesn't allow multiple cur dirs across drives.
  351. * EFFECTS -
  352. *
  353. */
  354. int w_GetCurrentDirectory(int iDrive, LPSTR lpszCurrentDirectory)
  355. {
  356. /*first see if iDrive == 0, if so then only need call GetCurrentDirectory*/
  357. /*if non-zero, then could be current drive, OR another drive.*/
  358. /*THIS IS NOT ALLOWED!*/
  359. /*BUG BUG, not DBCS aware!*/
  360. DWORD nBufferLength = LOCALBUFFERSIZE;
  361. DWORD dwReturnCode;
  362. LPSTR lpszLocalBuffer;
  363. int iDriveNumber;
  364. /*alloc local, non-moveable, zero filled buffer*/
  365. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  366. if(lpszLocalBuffer == NULL){
  367. #if DBG
  368. OutputDebugString("<w_GetCurrentDirectory> LocalAlloc FAILed\n");
  369. #endif /* DBG */
  370. return(1);
  371. }
  372. GetCurDir1:
  373. dwReturnCode = GetCurrentDirectory(nBufferLength, lpszLocalBuffer);
  374. /*failed for reason other than bufferlength too small*/
  375. if(dwReturnCode == 0){
  376. #if DBG
  377. OutputDebugString("<w_GetCurrentDirectory> GetCurrentDirectory() FAILed\n");
  378. #endif /* DBG */
  379. LocalFree(lpszLocalBuffer);
  380. return(1);
  381. }
  382. /*test for success, if dwReturnCode is > buffer, then need increase buffer*/
  383. if(dwReturnCode > nBufferLength){
  384. PVOID pv = LocalReAlloc(lpszLocalBuffer, nBufferLength + LOCALBUFFERSIZE, LMEM_ZEROINIT | LMEM_MOVEABLE);
  385. if (!pv) {
  386. #if DBG
  387. OutputDebugString("<w_GetCurrentDirectory> LocalAlloc FAILed\n");
  388. #endif /* DBG */
  389. LocalFree(lpszLocalBuffer);
  390. return(1);
  391. } else {
  392. lpszLocalBuffer = pv;
  393. nBufferLength += LOCALBUFFERSIZE;
  394. }
  395. goto GetCurDir1;
  396. }
  397. /*now I have string that contains EITHER current drive in a drive letter*/
  398. /*or current drive by a UNC name*/
  399. /*BUG BUG UNC name check uncoded, since I have to go from UNC name to drive letter*/
  400. /*debug code, to make sure iDrive == current drive*/
  401. /*see if drive letter based string*/
  402. if(lpszLocalBuffer[1] == ':'){
  403. /*is Drive letter based!*/
  404. /*never know case of returned string from kernel*/
  405. if(isupper(lpszLocalBuffer[0])){
  406. iDriveNumber = lpszLocalBuffer[0] - 'A';
  407. }
  408. else{
  409. iDriveNumber = lpszLocalBuffer[0] - 'a';
  410. }
  411. /*DEBUG make sure that we are indeed setting a new drive */
  412. /* remember that iDrive == 0 means use current drive!*/
  413. if(iDrive == iDriveNumber || iDrive == 0){
  414. /*is current drive and drive letter based, set to after "x:\"*/
  415. strcpy(lpszCurrentDirectory, lpszLocalBuffer); /*copy directory to pointer*/
  416. }
  417. else{ /* is different drive, or not using current drive (== 0)*/
  418. SetCurrentDrive(iDriveNumber); /*set new drive "<iDrive>:\" */
  419. /*now that new drive/dir is set, return current dir*/
  420. /* BUG BUG, because setting drive, overides cur dir, I return*/
  421. /* "<newdrive>:\" */
  422. strcpy(lpszCurrentDirectory, "c:\\");
  423. lpszCurrentDirectory[0] = (char) (iDriveNumber + 'a'); /*set new drive*/
  424. }
  425. }
  426. else{
  427. /*is NOT drive letter based*/
  428. /* BUG BUG need write code to parse UNC, and return only the path*/
  429. /* BUG BUGalso need check to see if iDrive == UNC drive, so I gotta*/
  430. /* convert UNC path to drive, and compare*/
  431. #if DBG
  432. OutputDebugString("<w_GetCurrentDirectory> Took path for UNC, and no code!\n");
  433. #endif /* DBG */
  434. LocalFree(lpszLocalBuffer);
  435. return(1);
  436. }
  437. LocalFree(lpszLocalBuffer);
  438. return(0); /*success*/
  439. }
  440. /*** w_SetCurrentDirectory -- SetCurrent Working Directory and drive
  441. *
  442. * int w_SetCurrentDirectory(LPSTR lpszCurrentDirectory)
  443. *
  444. * ENTRY - LPSTR lpszCurrentDirectory - string to set current drive/dir to
  445. *
  446. * EXIT - int iReturnCode - returns (0) if success
  447. *
  448. * SYNOPSIS - calls SetCurrentDirectory to set current directory and drive.
  449. * WARNINGS -
  450. * EFFECTS -
  451. *
  452. */
  453. int w_SetCurrentDirectory(LPSTR lpszCurrentDirectory)
  454. {
  455. DWORD dwReturnCode;
  456. dwReturnCode = SetCurrentDirectory(lpszCurrentDirectory);
  457. if(dwReturnCode == 0){
  458. #if DBG
  459. OutputDebugString("<w_SetCurrentDirectory> SetCurrentDirectory FAILed!\n");
  460. #endif /* DBG */
  461. return(1);
  462. }
  463. return(0); /*success*/
  464. }
  465. /*** DosDelete -- Delete named file.
  466. *
  467. * int DosDelete(LPSTR lpszFileToDelete)
  468. *
  469. * ENTRY - LPSTR lpszFileToDelete - filename to delete.
  470. *
  471. * EXIT - int xxx - returns (0) if success
  472. *
  473. * SYNOPSIS - calls win32 DeleteFile.
  474. * WARNINGS -
  475. * EFFECTS -
  476. *
  477. */
  478. int DosDelete(LPSTR lpszFileToDelete)
  479. {
  480. BOOL bReturnCode;
  481. LPSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  482. DWORD nBufferLength;
  483. nBufferLength = strlen(lpszFileToDelete) + 1;
  484. /*alloc local, non-moveable, zero filled buffer*/
  485. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  486. if(lpszLocalBuffer == NULL){
  487. #if DBG
  488. OutputDebugString("<DosDelete> LocalAlloc FAILed\n");
  489. #endif /* DBG */
  490. return 1;
  491. }
  492. AnsiToOem(lpszFileToDelete, lpszLocalBuffer);
  493. bReturnCode = DeleteFile(lpszLocalBuffer);
  494. LocalFree(lpszLocalBuffer);
  495. if(bReturnCode){
  496. return(0);
  497. }
  498. else{
  499. return(1);
  500. }
  501. }
  502. /*** DosRename -- Rename file.
  503. *
  504. * int DosRename(LPSTR lpszOrgFileName, LPSTR lpszNewFileName)
  505. *
  506. * ENTRY - LPSTR lpszOrgFileName - origianl filename.
  507. * LPSTR lpszNewFileName - New filename.
  508. *
  509. * EXIT - int xxx - returns (0) if success
  510. *
  511. * SYNOPSIS - calls win32 MoveFile.
  512. * WARNINGS -
  513. * EFFECTS -
  514. *
  515. */
  516. int DosRename(LPSTR lpszOrgFileName, LPSTR lpszNewFileName)
  517. {
  518. BOOL bReturnCode;
  519. LPSTR lpszLocalBuffer; /*local buffer for AnsiToOem()*/
  520. LPSTR lpszLocalBuffer1; /*local buffer for AnsiToOem()*/
  521. DWORD nBufferLength;
  522. DWORD nBufferLength1;
  523. nBufferLength = strlen(lpszOrgFileName) + 1;
  524. nBufferLength1 = strlen(lpszNewFileName) + 1;
  525. /*alloc local, non-moveable, zero filled buffer*/
  526. lpszLocalBuffer = LocalAlloc(LMEM_ZEROINIT, nBufferLength);
  527. if(lpszLocalBuffer == NULL){
  528. #if DBG
  529. OutputDebugString("<DosRename> LocalAlloc FAILed\n");
  530. #endif /* DBG */
  531. return 1;
  532. }
  533. lpszLocalBuffer1 = LocalAlloc(LMEM_ZEROINIT, nBufferLength1);
  534. if(lpszLocalBuffer1 == NULL){
  535. OutputDebugString("<DosRename> LocalAlloc FAILed\n");
  536. LocalFree(lpszLocalBuffer);
  537. return 1;
  538. }
  539. AnsiToOem(lpszOrgFileName, lpszLocalBuffer);
  540. AnsiToOem(lpszNewFileName, lpszLocalBuffer1);
  541. /*rename file*/
  542. bReturnCode = MoveFile(lpszLocalBuffer, lpszLocalBuffer1);
  543. LocalFree(lpszLocalBuffer);
  544. LocalFree(lpszLocalBuffer1);
  545. if(bReturnCode){
  546. return(0);
  547. }
  548. else{
  549. return(1);
  550. }
  551. }
  552. /*** lmemmove -- move memory.
  553. *
  554. * LPSTR lmemmove(LPSTR lpszDst, LPSTR lpszSrc, WORD wCount)
  555. *
  556. * ENTRY - LPSTR lpszDst - destination
  557. * LPSTR lpszSrc - source
  558. * WORD wCount - number of chars to move.
  559. *
  560. * EXIT - LPSTR lpszDst - returns lpszDst.
  561. *
  562. * SYNOPSIS - calls c runtime. Done cause they hacked lmemove to asm.
  563. * WARNINGS -
  564. * EFFECTS -
  565. *
  566. */
  567. LPSTR lmemmove(LPSTR lpszDst, LPSTR lpszSrc, WORD wCount)
  568. {
  569. return(memmove(lpszDst, lpszSrc, wCount));
  570. }