dos.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Dos unit for BP7 compatible RTL
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$inline on}
  12. unit dos;
  13. interface
  14. Type
  15. searchrec = packed record
  16. fill : array[1..21] of byte;
  17. attr : byte;
  18. time : longint;
  19. { reserved : word; not in DJGPP V2 }
  20. size : longint;
  21. name : string[255]; { LFN Name, DJGPP uses only [12] but more can't hurt (PFV) }
  22. end;
  23. {$DEFINE HAS_REGISTERS}
  24. {$I registers.inc}
  25. {$i dosh.inc}
  26. {$IfDef SYSTEM_DEBUG_STARTUP}
  27. {$DEFINE FORCE_PROXY}
  28. {$endif SYSTEM_DEBUG_STARTUP}
  29. Const
  30. { This variable can be set to true
  31. to force use of !proxy command lines even for short
  32. strings, for debugging purposes mainly, as
  33. this might have negative impact if trying to
  34. call non-go32v2 programs }
  35. force_go32v2_proxy : boolean =
  36. {$ifdef FORCE_PROXY}
  37. true;
  38. {$DEFINE DEBUG_PROXY}
  39. {$else not FORCE_PROXY}
  40. false;
  41. {$endif not FORCE_PROXY}
  42. { This variable allows to use !proxy if command line is
  43. longer than 126 characters.
  44. This will only work if the called program knows how to handle
  45. those command lines.
  46. Luckily this is the case for Free Pascal compiled
  47. programs (even old versions)
  48. and go32v2 DJGPP programs.
  49. You can set this to false to get a warning to stderr
  50. if command line is too long. }
  51. Use_go32v2_proxy : boolean = true;
  52. { Added to interface so that there is no need to implement it
  53. both in dos and sysutils units }
  54. procedure exec_ansistring(path : string;comline : ansistring);
  55. procedure Intr(IntNo: Byte; var Regs: Registers); external name 'FPC_INTR';
  56. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  57. implementation
  58. uses
  59. strings;
  60. type
  61. PFarByte = ^Byte;far;
  62. PFarChar = ^Char;far;
  63. PFarWord = ^Word;far;
  64. {$DEFINE HAS_GETMSCOUNT}
  65. {$DEFINE HAS_INTR}
  66. {$DEFINE HAS_SETCBREAK}
  67. {$DEFINE HAS_GETCBREAK}
  68. {$DEFINE HAS_SETVERIFY}
  69. {$DEFINE HAS_GETVERIFY}
  70. {$DEFINE HAS_SWAPVECTORS}
  71. {$DEFINE HAS_GETSHORTNAME}
  72. {$DEFINE HAS_GETLONGNAME}
  73. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  74. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  75. {$I dos.inc}
  76. {******************************************************************************
  77. --- Dos Interrupt ---
  78. ******************************************************************************}
  79. var
  80. dosregs : registers;
  81. procedure LoadDosError;
  82. var
  83. r : registers;
  84. SimpleDosError : word;
  85. begin
  86. if (dosregs.flags and fcarry) <> 0 then
  87. begin
  88. { I got a extended error = 0
  89. while CarryFlag was set from Exec function }
  90. SimpleDosError:=dosregs.ax;
  91. r.ax:=$5900;
  92. r.bx:=$0;
  93. intr($21,r);
  94. { conversion from word to integer !!
  95. gave a Bound check error if ax is $FFFF !! PM }
  96. doserror:=integer(r.ax);
  97. case doserror of
  98. 0 : DosError:=integer(SimpleDosError);
  99. 19 : DosError:=150;
  100. 21 : DosError:=152;
  101. end;
  102. end
  103. else
  104. doserror:=0;
  105. end;
  106. {******************************************************************************
  107. --- Info / Date / Time ---
  108. ******************************************************************************}
  109. function dosversion : word;
  110. begin
  111. dosregs.ax:=$3000;
  112. msdos(dosregs);
  113. dosversion:=dosregs.ax;
  114. end;
  115. procedure getdate(var year,month,mday,wday : word);
  116. begin
  117. dosregs.ax:=$2a00;
  118. msdos(dosregs);
  119. wday:=dosregs.al;
  120. year:=dosregs.cx;
  121. month:=dosregs.dh;
  122. mday:=dosregs.dl;
  123. end;
  124. procedure setdate(year,month,day : word);
  125. begin
  126. dosregs.cx:=year;
  127. dosregs.dh:=month;
  128. dosregs.dl:=day;
  129. dosregs.ah:=$2b;
  130. msdos(dosregs);
  131. end;
  132. procedure gettime(var hour,minute,second,sec100 : word);
  133. begin
  134. dosregs.ah:=$2c;
  135. msdos(dosregs);
  136. hour:=dosregs.ch;
  137. minute:=dosregs.cl;
  138. second:=dosregs.dh;
  139. sec100:=dosregs.dl;
  140. end;
  141. procedure settime(hour,minute,second,sec100 : word);
  142. begin
  143. dosregs.ch:=hour;
  144. dosregs.cl:=minute;
  145. dosregs.dh:=second;
  146. dosregs.dl:=sec100;
  147. dosregs.ah:=$2d;
  148. msdos(dosregs);
  149. end;
  150. function GetMsCount: int64;
  151. begin
  152. GetMsCount := int64 (MemL [$40:$6c]) * 55;
  153. end;
  154. {******************************************************************************
  155. --- Exec ---
  156. ******************************************************************************}
  157. const
  158. DOS_MAX_COMMAND_LINE_LENGTH = 126;
  159. procedure exec_ansistring(path : string;comline : ansistring);
  160. begin
  161. {TODO: implement}
  162. runerror(304);
  163. end;
  164. procedure exec(const path : pathstr;const comline : comstr);
  165. begin
  166. exec_ansistring(path, comline);
  167. end;
  168. procedure getcbreak(var breakvalue : boolean);
  169. begin
  170. dosregs.ax:=$3300;
  171. msdos(dosregs);
  172. breakvalue:=dosregs.dl<>0;
  173. end;
  174. procedure setcbreak(breakvalue : boolean);
  175. begin
  176. dosregs.ax:=$3301;
  177. dosregs.dl:=ord(breakvalue);
  178. msdos(dosregs);
  179. end;
  180. procedure getverify(var verify : boolean);
  181. begin
  182. dosregs.ah:=$54;
  183. msdos(dosregs);
  184. verify:=dosregs.al<>0;
  185. end;
  186. procedure setverify(verify : boolean);
  187. begin
  188. dosregs.ah:=$2e;
  189. dosregs.al:=ord(verify);
  190. msdos(dosregs);
  191. end;
  192. {******************************************************************************
  193. --- Disk ---
  194. ******************************************************************************}
  195. type
  196. ExtendedFat32FreeSpaceRec = packed record
  197. RetSize : word; { $00 }
  198. Strucversion : word; { $02 }
  199. SecPerClus, { $04 }
  200. BytePerSec, { $08 }
  201. AvailClusters, { $0C }
  202. TotalClusters, { $10 }
  203. AvailPhysSect, { $14 }
  204. TotalPhysSect, { $18 }
  205. AvailAllocUnits, { $1C }
  206. TotalAllocUnits : longword; { $20 }
  207. Dummy, { $24 }
  208. Dummy2 : longword; { $28 }
  209. end; { $2C }
  210. const
  211. IOCTL_INPUT = 3; //For request header command field
  212. CDFUNC_SECTSIZE = 7; //For cdrom control block func field
  213. CDFUNC_VOLSIZE = 8; //For cdrom control block func field
  214. type
  215. TRequestHeader = packed record
  216. length : byte; { $00 }
  217. subunit : byte; { $01 }
  218. command : byte; { $02 }
  219. status : word; { $03 }
  220. reserved1 : longword; { $05 }
  221. reserved2 : longword; { $09 }
  222. media_desc : byte; { $0D }
  223. transf_ofs : word; { $0E }
  224. transf_seg : word; { $10 }
  225. numbytes : word; { $12 }
  226. end; { $14 }
  227. TCDSectSizeReq = packed record
  228. func : byte; { $00 }
  229. mode : byte; { $01 }
  230. secsize : word; { $02 }
  231. end; { $04 }
  232. TCDVolSizeReq = packed record
  233. func : byte; { $00 }
  234. size : longword; { $01 }
  235. end; { $05 }
  236. function do_diskdata(drive : byte; Free : boolean) : Int64;
  237. begin
  238. {TODO: implement}
  239. runerror(304);
  240. end;
  241. function diskfree(drive : byte) : int64;
  242. begin
  243. diskfree:=Do_DiskData(drive,TRUE);
  244. end;
  245. function disksize(drive : byte) : int64;
  246. begin
  247. disksize:=Do_DiskData(drive,false);
  248. end;
  249. {******************************************************************************
  250. --- LFNFindfirst LFNFindNext ---
  251. ******************************************************************************}
  252. type
  253. LFNSearchRec=packed record
  254. attr,
  255. crtime,
  256. crtimehi,
  257. actime,
  258. actimehi,
  259. lmtime,
  260. lmtimehi,
  261. sizehi,
  262. size : longint;
  263. reserved : array[0..7] of byte;
  264. name : array[0..259] of byte;
  265. shortname : array[0..13] of byte;
  266. end;
  267. procedure LFNSearchRec2Dos(const w:LFNSearchRec;hdl:longint;var d:Searchrec;from_findfirst : boolean);
  268. var
  269. Len : longint;
  270. begin
  271. With w do
  272. begin
  273. FillChar(d,sizeof(SearchRec),0);
  274. if DosError=0 then
  275. len:=StrLen(@Name)
  276. else
  277. len:=0;
  278. d.Name[0]:=chr(len);
  279. Move(Name[0],d.Name[1],Len);
  280. d.Time:=lmTime;
  281. d.Size:=Size;
  282. d.Attr:=Attr and $FF;
  283. if (DosError<>0) and from_findfirst then
  284. hdl:=-1;
  285. Move(hdl,d.Fill,4);
  286. end;
  287. end;
  288. {$ifdef DEBUG_LFN}
  289. const
  290. LFNFileName : string = 'LFN.log';
  291. LFNOpenNb : longint = 0;
  292. LogLFN : boolean = false;
  293. var
  294. lfnfile : text;
  295. {$endif DEBUG_LFN}
  296. procedure LFNFindFirst(path:pchar;attr:longint;var s:searchrec);
  297. begin
  298. {TODO: implement}
  299. runerror(304);
  300. end;
  301. procedure LFNFindNext(var s:searchrec);
  302. begin
  303. {TODO: implement}
  304. runerror(304);
  305. end;
  306. procedure LFNFindClose(var s:searchrec);
  307. begin
  308. {TODO: implement}
  309. runerror(304);
  310. end;
  311. {******************************************************************************
  312. --- DosFindfirst DosFindNext ---
  313. ******************************************************************************}
  314. procedure dossearchrec2searchrec(var f : searchrec);
  315. var
  316. len : longint;
  317. begin
  318. { Check is necessary!! OS/2's VDM doesn't clear the name with #0 if the }
  319. { file doesn't exist! (JM) }
  320. if dosError = 0 then
  321. len:=StrLen(@f.Name)
  322. else len := 0;
  323. Move(f.Name[0],f.Name[1],Len);
  324. f.Name[0]:=chr(len);
  325. end;
  326. procedure DosFindfirst(path : pchar;attr : word;var f : searchrec);
  327. begin
  328. {TODO: implement}
  329. runerror(304);
  330. end;
  331. procedure Dosfindnext(var f : searchrec);
  332. begin
  333. {TODO: implement}
  334. runerror(304);
  335. end;
  336. {******************************************************************************
  337. --- Findfirst FindNext ---
  338. ******************************************************************************}
  339. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  340. var
  341. path0 : array[0..255] of char;
  342. begin
  343. doserror:=0;
  344. strpcopy(path0,path);
  345. if LFNSupport then
  346. LFNFindFirst(path0,attr,f)
  347. else
  348. Dosfindfirst(path0,attr,f);
  349. end;
  350. procedure findnext(var f : searchRec);
  351. begin
  352. doserror:=0;
  353. if LFNSupport then
  354. LFNFindnext(f)
  355. else
  356. Dosfindnext(f);
  357. end;
  358. Procedure FindClose(Var f: SearchRec);
  359. begin
  360. DosError:=0;
  361. if LFNSupport then
  362. LFNFindClose(f);
  363. end;
  364. type swap_proc = procedure;
  365. var
  366. _swap_in : swap_proc;external name '_swap_in';
  367. _swap_out : swap_proc;external name '_swap_out';
  368. _exception_exit : pointer;external name '_exception_exit';
  369. _v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  370. procedure swapvectors;
  371. begin
  372. if _exception_exit<>nil then
  373. if _v2prt0_exceptions_on then
  374. _swap_out()
  375. else
  376. _swap_in();
  377. end;
  378. {******************************************************************************
  379. --- File ---
  380. ******************************************************************************}
  381. Function FSearch(path: pathstr; dirlist: string): pathstr;
  382. var
  383. i,p1 : longint;
  384. s : searchrec;
  385. newdir : pathstr;
  386. begin
  387. { check if the file specified exists }
  388. findfirst(path,anyfile and not(directory),s);
  389. if doserror=0 then
  390. begin
  391. findclose(s);
  392. fsearch:=path;
  393. exit;
  394. end;
  395. { No wildcards allowed in these things }
  396. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  397. fsearch:=''
  398. else
  399. begin
  400. { allow slash as backslash }
  401. DoDirSeparators(dirlist);
  402. repeat
  403. p1:=pos(';',dirlist);
  404. if p1<>0 then
  405. begin
  406. newdir:=copy(dirlist,1,p1-1);
  407. delete(dirlist,1,p1);
  408. end
  409. else
  410. begin
  411. newdir:=dirlist;
  412. dirlist:='';
  413. end;
  414. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  415. newdir:=newdir+'\';
  416. findfirst(newdir+path,anyfile and not(directory),s);
  417. if doserror=0 then
  418. newdir:=newdir+path
  419. else
  420. newdir:='';
  421. until (dirlist='') or (newdir<>'');
  422. fsearch:=newdir;
  423. end;
  424. findclose(s);
  425. end;
  426. { change to short filename if successful DOS call PM }
  427. function GetShortName(var p : String) : boolean;
  428. begin
  429. {TODO: implement}
  430. runerror(304);
  431. end;
  432. { change to long filename if successful DOS call PM }
  433. function GetLongName(var p : String) : boolean;
  434. begin
  435. {TODO: implement}
  436. runerror(304);
  437. end;
  438. {******************************************************************************
  439. --- Get/Set File Time,Attr ---
  440. ******************************************************************************}
  441. procedure getftime(var f;var time : longint);
  442. begin
  443. dosregs.bx:=textrec(f).handle;
  444. dosregs.ax:=$5700;
  445. msdos(dosregs);
  446. loaddoserror;
  447. time:=(dosregs.dx shl 16)+dosregs.cx;
  448. end;
  449. procedure setftime(var f;time : longint);
  450. begin
  451. dosregs.bx:=textrec(f).handle;
  452. dosregs.cx:=time and $ffff;
  453. dosregs.dx:=time shr 16;
  454. dosregs.ax:=$5701;
  455. msdos(dosregs);
  456. loaddoserror;
  457. end;
  458. procedure getfattr(var f;var attr : word);
  459. begin
  460. {TODO: implement}
  461. runerror(304);
  462. end;
  463. procedure setfattr(var f;attr : word);
  464. begin
  465. {TODO: implement}
  466. runerror(304);
  467. end;
  468. {******************************************************************************
  469. --- Environment ---
  470. ******************************************************************************}
  471. function GetEnvStr(EnvNo: Integer; var OutEnvStr: string): integer;
  472. var
  473. dos_env_seg: Word;
  474. ofs: Word;
  475. Ch, Ch2: Char;
  476. begin
  477. dos_env_seg := PFarWord(Ptr(dos_psp, $2C))^;
  478. GetEnvStr := 1;
  479. OutEnvStr := '';
  480. ofs := 0;
  481. repeat
  482. Ch := PFarChar(Ptr(dos_env_seg,ofs))^;
  483. Ch2 := PFarChar(Ptr(dos_env_seg,ofs + 1))^;
  484. if (Ch = #0) and (Ch2 = #0) then
  485. exit;
  486. if Ch = #0 then
  487. Inc(GetEnvStr);
  488. if (Ch <> #0) and (GetEnvStr = EnvNo) then
  489. OutEnvStr := OutEnvStr + Ch;
  490. Inc(ofs);
  491. if ofs = 0 then
  492. exit;
  493. until false;
  494. end;
  495. function envcount : longint;
  496. var
  497. tmpstr: string;
  498. begin
  499. envcount := GetEnvStr(-1, tmpstr);
  500. end;
  501. function envstr (Index: longint): string;
  502. begin
  503. GetEnvStr(Index, envstr);
  504. end;
  505. Function GetEnv(envvar: string): string;
  506. var
  507. hs : string;
  508. eqpos : longint;
  509. I : integer;
  510. begin
  511. envvar:=upcase(envvar);
  512. getenv:='';
  513. for I := 1 to envcount do
  514. begin
  515. hs:=envstr(I);
  516. eqpos:=pos('=',hs);
  517. if upcase(copy(hs,1,eqpos-1))=envvar then
  518. begin
  519. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  520. break;
  521. end;
  522. end;
  523. end;
  524. {$ifdef DEBUG_LFN}
  525. begin
  526. LogLFN:=(GetEnv('LOGLFN')<>'');
  527. assign(lfnfile,LFNFileName);
  528. {$I-}
  529. Reset(lfnfile);
  530. if IOResult<>0 then
  531. begin
  532. Rewrite(lfnfile);
  533. Writeln(lfnfile,'New lfn.log');
  534. end;
  535. close(lfnfile);
  536. {$endif DEBUG_LFN}
  537. end.