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.

554 lines
15 KiB

  1. TITLE doslib - DOS access library routines
  2. ; Windows Write, Copyright 1985-1992 Microsoft Corporation
  3. ; =====================================================================
  4. ; This file contains DOS access routines.
  5. ; These routines are general, simple calls to DOS and
  6. ; are likely to be generally useful. They assume /PLM calling
  7. ; is used.
  8. ; FAR calls are used throughout
  9. ;
  10. ;
  11. ; NOTE: DOSLIB.H CONTAINS A C HEADER DEFINING THE FUNCTIONS IN THIS
  12. ; MODULE. IT MUST BE UPDATED WHENEVER A FUNCTION IS ADDED OR AN INTERFACE
  13. ; CHANGES
  14. ; =====================================================================
  15. ; =====================================================================
  16. ; cmacros2.inc is a special version of cmacros.inc in which the only
  17. ; difference is that it defines the segment for assembly code to
  18. ; be FILE_TEXT instead of _TEXT.
  19. ; This is done so that functions in FILE.C will
  20. ; not call intermodule and subject themselves to possible file closure
  21. ; when calling DOS functions.
  22. ; =====================================================================
  23. include cmacros2.inc
  24. sBegin DATA
  25. ifdef DEBUG
  26. EXTRN vfCommDebug:WORD
  27. endif
  28. sEnd DATA
  29. cchMaxFile EQU 128
  30. EXTRN ANSITOOEM:FAR
  31. sBegin CODE
  32. assumes CS,CODE
  33. ; ---------------------------------------------------------------------
  34. ; int FAR CchCurSzPath( szPath, bDrive )
  35. ; PSTR szPath;
  36. ; int bDrive;
  37. ;
  38. ; Copy the current path name for the current drive into szPath
  39. ; szPath must have 67 bytes of storage
  40. ;
  41. ; bDrive is 0=default, 1=A, 2=B,...
  42. ;
  43. ; Returned cch includes the terminating '\0'
  44. ; Form of returned string is e.g. "C:\WINDOWS\BIN\" (0-terminated)
  45. ; String is guaranteed to: (1) Include the drive letter, colon, and leading "\"
  46. ; (2) End with a backslash
  47. ;
  48. ; 0 = an error occurred, nonzero = success
  49. ; the path string will be NULL if an error occurred
  50. ; An error should really not be possible, since the default drive ought to be
  51. ; valid
  52. ; ---------------------------------------------------------------------
  53. cProc CchCurSzPath, <FAR, PUBLIC>, <SI>
  54. parmDP <szPath>
  55. parmB <bDrive>
  56. cBegin CchCurSzPath
  57. mov al,bDrive
  58. mov dl,al
  59. cmp al,0
  60. jz cspDFLTDRV ; default drive
  61. dec al
  62. jmp cspGOTDRV ; not default drive
  63. cspDFLTDRV:
  64. mov ah,19h ; Get current drive
  65. int 21h
  66. ; now we have al: 0=A, 1=B, ....
  67. ; dl: 0=default, 1=A, 2=B
  68. cspGOTDRV: ; Put "X:\" at front of szPath
  69. add al,'A'
  70. mov si,szPath
  71. mov [si],al
  72. mov BYTE PTR [si+1],':'
  73. mov BYTE PTR [si+2],'\' ; Leave si pointing at szPath
  74. add si,3 ; Rest of path goes at SzPath+3
  75. mov ah,47h
  76. int 21h
  77. mov si,szPath
  78. jc cspERR ; error -- return negative of err code in AX
  79. dec si ; Path was OK - find null terminator
  80. cspLOOP: inc si
  81. cmp al,[si]
  82. jnz cspLOOP
  83. cmp BYTE PTR [si-1],'\' ; Append backslash if needed
  84. jz cspSTROK ; not needed, string is already OK
  85. mov BYTE PTR [si],'\'
  86. inc si
  87. mov BYTE PTR [si],0
  88. cspSTROK: ; now we are guaranteed a good string
  89. mov ax,si ; determine string length
  90. sub ax,szPath
  91. inc ax
  92. jmp cspRET
  93. cspERR: mov BYTE PTR [si],0 ; error -- NULL path string
  94. neg ax
  95. cspRET:
  96. cEnd CchCurSzPath
  97. ifdef ENABLE
  98. ;-----------------------------------------------------------------------------
  99. ; DOSHND FAR WCreateNewSzFfname( szFfname, attrib )
  100. ;
  101. ; Create specified file, leave open for read/write, return handle
  102. ; filename is an ffname, with drive and path. Uses the NEW
  103. ; DOS 3.0 CREATE call which fails if the file exists. Caller has
  104. ; responsibility for assuring DOS version number sufficiently high
  105. ;
  106. ; returned handle is negative if there was an error
  107. ; the value will be the negative of the error code returned in AX
  108. ;-----------------------------------------------------------------------------
  109. cProc WCreateNewSzFfname, <FAR, PUBLIC>
  110. parmDP <szFfname>
  111. parmW <attrib>
  112. cBegin WCreateNewSzFfname
  113. mov dx,szFfname
  114. mov cx,attrib
  115. mov ah,5bh
  116. int 21h
  117. jnc cnsfdone
  118. neg ax ; error - return the negative of the error code
  119. cnsfdone:
  120. cEnd WCreateNewSzFfname
  121. ;-----------------------------------------------------------------------------
  122. ; DOSHND FAR WCreateSzFfname( szFfname, attrib )
  123. ;
  124. ; Create specified file, leave open for read/write, return handle
  125. ; filename is an ffname, with drive and path
  126. ;
  127. ; returned handle is negative if there was an error
  128. ; the value will be the negative of the error code returned in AX
  129. ;-----------------------------------------------------------------------------
  130. cProc WCreateSzFfname, <FAR, PUBLIC>
  131. parmDP <szFfname>
  132. parmW <attrib>
  133. cBegin WCreateSzFfname
  134. mov dx,szFfname
  135. mov cx,attrib
  136. mov ah,3ch
  137. int 21h
  138. jnc csfdone
  139. neg ax ; error - return the negative of the error code
  140. csfdone:
  141. cEnd WCreateSzFfname
  142. endif
  143. ;-----------------------------------------------------------------------------
  144. ; int DosxError()
  145. ;
  146. ; Return a DOS extended error code
  147. ;-----------------------------------------------------------------------------
  148. cProc DosxError, <FAR, PUBLIC>
  149. cBegin DosxError
  150. mov ah,59h
  151. mov bx,0 ; bug fix, 10/2/86, BryanL
  152. int 21h
  153. cEnd DosxError
  154. ;-----------------------------------------------------------------------------
  155. ; WORD WDosVersion()
  156. ;
  157. ; Return a word indicating DOS version, major in low 8 bits, minor in high 8
  158. ;-----------------------------------------------------------------------------
  159. cProc WDosVersion, <FAR, PUBLIC>
  160. cBegin WDosVersion
  161. mov ah,30h
  162. int 21h
  163. cEnd WDosVersion
  164. ;-----------------------------------------------------------------------------
  165. ; WORD DaGetFileModeSz(sz)
  166. ;
  167. ; Return a word indicating attributes of file sz (EXPECTED IN ANSI);
  168. ; 0xFFFF if it fails.
  169. ;-----------------------------------------------------------------------------
  170. cProc DaGetFileModeSz, <FAR, PUBLIC>
  171. parmDP <sz>
  172. localV <szOem>,cchMaxFile
  173. cBegin DaGetFileModeSz
  174. ; Convert filename from ANSI set to OEM Set
  175. push ds
  176. push sz
  177. push ds
  178. lea ax,szOem
  179. push ax
  180. call ANSITOOEM
  181. lea dx,szOem
  182. mov ax,4300h
  183. int 21h
  184. mov ax, cx
  185. jnc daNoErr
  186. mov ax, 0ffffh ; error -- return 0xFFFF
  187. daNoErr:
  188. cEnd DaGetFileModeSz
  189. ; WRITE uses OpenFile instead
  190. ifdef ENABLE
  191. ;-----------------------------------------------------------------------------
  192. ; DOSHND FAR WOpenSzFfname( szFfname, openmode )
  193. ;
  194. ; Open specified file in specified mode, return a handle or
  195. ; the negative of an error code if the open failed
  196. ;-----------------------------------------------------------------------------
  197. cProc WOpenSzFfname, <FAR, PUBLIC>
  198. parmDP <szFfname>
  199. parmB <openmode>
  200. cBegin WOpenSzFfname
  201. mov dx,szFfname
  202. mov al,openmode
  203. mov ah,3dh
  204. int 21h
  205. jnc osfdone
  206. neg ax ; error - return the negative of the error code
  207. osfdone:
  208. cEnd WOpenSzFfname
  209. endif
  210. ;-----------------------------------------------------------------------------
  211. ; int FAR FCloseDoshnd( doshnd )
  212. ;
  213. ; Close file given DOS handle, return 0 = error, nonzero = no error
  214. ;-----------------------------------------------------------------------------
  215. cProc FCloseDoshnd, <FAR, PUBLIC>
  216. parmW <doshnd>
  217. cBegin FCloseDoshnd
  218. mov bx,doshnd
  219. mov ah,3eh
  220. int 21h
  221. mov ax,0000
  222. jc cdhskip ; error, leave a zero in ax
  223. inc ax
  224. cdhskip:
  225. cEnd FCloseDoshnd
  226. ;-----------------------------------------------------------------------------
  227. ; int FAR FpeDeleteSzFfname( szFfname )
  228. ;
  229. ; Delete specified file, return < 0=failure, 0=success
  230. ;-----------------------------------------------------------------------------
  231. cProc FpeDeleteSzFfname, <FAR, PUBLIC>
  232. parmDP <szFfname>
  233. localV <szOem>,cchMaxFile
  234. cBegin FpeDeleteSzFfname
  235. ; Convert filename from ANSI set to OEM Set
  236. push ds
  237. push szFfname
  238. push ds
  239. lea ax,szOem
  240. push ax
  241. call ANSITOOEM
  242. lea dx,szOem
  243. mov ah,41h
  244. int 21h
  245. jc dsfskip ; error - return the negative of the error code
  246. mov ax,0ffffh
  247. dsfskip:
  248. neg ax
  249. cEnd FpeDeleteSzFfname
  250. ;-----------------------------------------------------------------------------
  251. ; int FAR FpeRenameSzFfname( szCur, szNew )
  252. ;
  253. ; Rename file szCur to szNew, return < 0=failure, 0=success
  254. ;-----------------------------------------------------------------------------
  255. cProc FpeRenameSzFfname, <FAR, PUBLIC>, <ES,DI>
  256. parmDP <szCur>
  257. parmDP <szNew>
  258. localV <szCurOem>,cchMaxFile
  259. localV <szNewOem>,cchMaxFile
  260. cBegin FpeRenameSzFfname
  261. ; Convert filenames to Oem char set
  262. push ds
  263. push szCur
  264. push ds
  265. lea ax,szCurOem
  266. push ax
  267. call ANSITOOEM
  268. push ds
  269. push szNew
  270. push ds
  271. lea ax, szNewOem
  272. push ax
  273. call ANSITOOEM
  274. lea dx,szCurOem ; old filename in ds:dx
  275. push ds ; new filename in es:di
  276. pop es
  277. lea di,szNewOem
  278. mov ah,56h
  279. int 21h
  280. jc rnfskip ; error - return the negative of the error code
  281. mov ax,0ffffh
  282. rnfskip:
  283. neg ax
  284. cEnd FpeRenameSzFfname
  285. ;-----------------------------------------------------------------------------
  286. ; int CchReadDoshnd ( doshnd, lpchBuffer, bytes )
  287. ;
  288. ; Read bytes from an open file, place into buffer
  289. ; Returns # of bytes read (should be == bytes unless EOF or error)
  290. ; If an error occurs, returns the negative of the error code
  291. ;-----------------------------------------------------------------------------
  292. cProc CchReadDoshnd, <FAR, PUBLIC>, <DS>
  293. parmW <doshnd>
  294. parmD <lpchBuffer>
  295. parmW <bytes>
  296. cBegin CchReadDoshnd
  297. mov bx,doshnd
  298. lds dx,lpchBuffer
  299. mov cx,bytes
  300. mov ah,3fh
  301. int 21h
  302. jnc crdone
  303. neg ax ; error - return value is the negative of the error code
  304. crdone:
  305. cEnd CchReadDoshnd
  306. ;-----------------------------------------------------------------------------
  307. ; int CchWriteDoshnd ( doshnd, lpchBuffer, bytes )
  308. ;
  309. ; Write bytes from an open file, place into buffer
  310. ; Returns # of bytes read (should be == bytes unless EOF or error)
  311. ; If an error occurs, returns the negative of the error code
  312. ; Disk full is not an "error"; detect it by return code != bytes
  313. ;-----------------------------------------------------------------------------
  314. cProc CchWriteDoshnd, <FAR, PUBLIC>,<DS>
  315. parmW <doshnd>
  316. parmD <lpchBuffer>
  317. parmW <bytes>
  318. cBegin CchWriteDoshnd
  319. mov bx,doshnd
  320. lds dx,lpchBuffer
  321. mov cx,bytes
  322. mov ah,40h
  323. int 21h
  324. jnc cwdone
  325. neg ax ; error: return the negative of the error code
  326. cwdone:
  327. cEnd CchWriteDoshnd
  328. ;-----------------------------------------------------------------------------
  329. ; long DwSeekDw ( doshnd, dwSeekpos, bSeekfrom )
  330. ;
  331. ; Seek to requested position in file
  332. ; bSeekfrom is: 0 = seek relative to beginning of file
  333. ; 1 = seek relative to current pointer location
  334. ; 2 = seek relative to end of file
  335. ;
  336. ; Returns the new location of the read/write pointer (a long)
  337. ; If an error occurs, returns the negative of the error code (long)
  338. ;-----------------------------------------------------------------------------
  339. cProc DwSeekDw, <FAR, PUBLIC>
  340. parmW <doshnd>
  341. parmD <dwSeekpos>
  342. parmB <bSeekfrom>
  343. cBegin DwSeekDw
  344. mov bx,doshnd
  345. mov cx,SEG_dwSeekpos
  346. mov dx,OFF_dwSeekpos
  347. mov al,bSeekfrom
  348. mov ah,42h
  349. int 21h
  350. jnc seekdone
  351. neg ax ; Error: return the negative of the error code
  352. mov dx,0ffffH
  353. seekdone:
  354. cEnd DwSeekDw
  355. ; WRITE does not use these currently
  356. ifdef ENABLE
  357. ;-----------------------------------------------------------------------------
  358. ; int FAR FFirst(pb, szFileSpec, attrib)
  359. ; Get first directory entry, place in buffer at pb. (buffer must contain
  360. ; 43 bytes of storage)
  361. ; attrib specifies attribute per MSDOS spec.
  362. ; szFileSpec is filename specification
  363. ; Returns 0=no error, nonzero = error
  364. ;-----------------------------------------------------------------------------
  365. cProc FFirst, <FAR, PUBLIC>, <SI, DI>
  366. parmDP <pb, szFileSpec>
  367. parmW <attrib>
  368. cBegin FFirst
  369. mov dx,pb ; set dta to pb
  370. mov ah,1ah
  371. int 21h
  372. mov cx,attrib ; get first directory record, place in *pb
  373. mov dx,szFileSpec
  374. mov ah,4eh
  375. int 21h
  376. jc ffdone
  377. xor ax,ax
  378. ffdone:
  379. cEnd FFirst
  380. ;-----------------------------------------------------------------------------
  381. ; int FAR FNext(pb)
  382. ; Get next directory entry, place in buffer at pb.
  383. ; Return 0= found match OK, nonzero = error or no more matches
  384. ;-----------------------------------------------------------------------------
  385. cProc FNext, <FAR, PUBLIC>, <SI, DI>
  386. parmDP <pb>
  387. cBegin FFirst
  388. mov dx,pb ; set dta to pb
  389. mov ah,1ah
  390. int 21h
  391. mov ah,4fh
  392. int 21h
  393. jc fndone
  394. xor ax,ax
  395. fndone:
  396. cEnd FNext
  397. endif
  398. ifdef OLDDEBUG
  399. /* This method isn't quite working under Win 3.0 ..pault */
  400. ;-----------------------------------------------------------------------------
  401. ; void CommSz( sz ) - put out string to AUX device
  402. ;
  403. ; For debugging
  404. ;-----------------------------------------------------------------------------
  405. cProc CommSz, <FAR, PUBLIC>
  406. parmDP <sz>
  407. cBegin CommSz
  408. CommSz1:
  409. mov bx, sz ; if ((dl = *(sz++)) == 0) goto CommSz2
  410. inc sz
  411. mov dl, [bx]
  412. cmp dl, 0
  413. jz CommSz2
  414. call DebugOutput ; Output dl to AUX or LPT device
  415. jmp CommSz1
  416. CommSz2:
  417. cEnd CommSz
  418. ;-----------------------------------------------------------------------------
  419. ; static void DebugOutput - put character to AUX or LPT device
  420. ; if (vfCommDebug)
  421. ; output to AUX port
  422. ; else output to LPT port
  423. ; input: dl = character
  424. ; output: none. Uses ah
  425. ;
  426. ;-----------------------------------------------------------------------------
  427. assumes ds,DATA
  428. DebugOutput PROC NEAR
  429. mov ah,4 ; Assume AUX device
  430. test vfCommDebug,0ffh
  431. jnz DebugOut1
  432. inc ah ; Change to LPT device
  433. DebugOut1:
  434. int 21h ; Output character
  435. ret
  436. DebugOutput ENDP
  437. endif ;DEBUG
  438. sEnd CODE
  439. END
  440.