dos.pp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. Uses
  15. Go32;
  16. Type
  17. searchrec = packed record
  18. fill : array[1..21] of byte;
  19. attr : byte;
  20. time : longint;
  21. { reserved : word; not in DJGPP V2 }
  22. size : longint;
  23. name : string[255]; { LFN Name, DJGPP uses only [12] but more can't hurt (PFV) }
  24. end;
  25. {$DEFINE HAS_REGISTERS}
  26. Registers = Go32.Registers;
  27. {$i dosh.inc}
  28. {$IfDef SYSTEM_DEBUG_STARTUP}
  29. {$DEFINE FORCE_PROXY}
  30. {$endif SYSTEM_DEBUG_STARTUP}
  31. Const
  32. { This variable can be set to true
  33. to force use of !proxy command lines even for short
  34. strings, for debugging purposes mainly, as
  35. this might have negative impact if trying to
  36. call non-go32v2 programs }
  37. force_go32v2_proxy : boolean =
  38. {$ifdef FORCE_PROXY}
  39. true;
  40. {$DEFINE DEBUG_PROXY}
  41. {$else not FORCE_PROXY}
  42. false;
  43. {$endif not FORCE_PROXY}
  44. { This variable allows to use !proxy if command line is
  45. longer than 126 characters.
  46. This will only work if the called program knows how to handle
  47. those command lines.
  48. Luckily this is the case for Free Pascal compiled
  49. programs (even old versions)
  50. and go32v2 DJGPP programs.
  51. You can set this to false to get a warning to stderr
  52. if command line is too long. }
  53. Use_go32v2_proxy : boolean = true;
  54. { Added to interface so that there is no need to implement it
  55. both in dos and sysutils units }
  56. procedure exec_ansistring(path : string;comline : ansistring);
  57. implementation
  58. uses
  59. strings;
  60. {$DEFINE HAS_GETMSCOUNT}
  61. {$DEFINE HAS_INTR}
  62. {$DEFINE HAS_SETCBREAK}
  63. {$DEFINE HAS_GETCBREAK}
  64. {$DEFINE HAS_SETVERIFY}
  65. {$DEFINE HAS_GETVERIFY}
  66. {$DEFINE HAS_SWAPVECTORS}
  67. {$DEFINE HAS_GETSHORTNAME}
  68. {$DEFINE HAS_GETLONGNAME}
  69. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  70. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  71. {$I dos.inc}
  72. {******************************************************************************
  73. --- Dos Interrupt ---
  74. ******************************************************************************}
  75. var
  76. dosregs : registers;
  77. procedure LoadDosError;
  78. var
  79. r : registers;
  80. SimpleDosError : word;
  81. begin
  82. if (dosregs.flags and fcarry) <> 0 then
  83. begin
  84. { I got a extended error = 0
  85. while CarryFlag was set from Exec function }
  86. SimpleDosError:=dosregs.ax;
  87. r.eax:=$5900;
  88. r.ebx:=$0;
  89. realintr($21,r);
  90. { conversion from word to integer !!
  91. gave a Bound check error if ax is $FFFF !! PM }
  92. doserror:=integer(r.ax);
  93. case doserror of
  94. 0 : DosError:=integer(SimpleDosError);
  95. 19 : DosError:=150;
  96. 21 : DosError:=152;
  97. end;
  98. end
  99. else
  100. doserror:=0;
  101. end;
  102. procedure intr(intno : byte;var regs : registers);
  103. begin
  104. realintr(intno,regs);
  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. type
  161. realptr = packed record
  162. ofs,seg : word;
  163. end;
  164. texecblock = packed record
  165. envseg : word;
  166. comtail : realptr;
  167. firstFCB : realptr;
  168. secondFCB : realptr;
  169. { iniStack : realptr;
  170. iniCSIP : realptr;}
  171. end;
  172. var
  173. current_dos_buffer_pos,
  174. arg_ofs,
  175. i,la_env,
  176. la_p,la_c,la_e,
  177. fcb1_la,fcb2_la : longint;
  178. use_proxy : boolean;
  179. proxy_argc : longint;
  180. execblock : texecblock;
  181. c : ansistring;
  182. p : string;
  183. function paste_to_dos(src : string;add_cr_at_end, include_string_length : boolean) : boolean;
  184. {Changed by Laaca - added parameter N}
  185. var
  186. c : pchar;
  187. CLen : cardinal;
  188. start_pos,ls : longint;
  189. begin
  190. paste_to_dos:=false;
  191. if include_string_length then
  192. start_pos:=0
  193. else
  194. start_pos:=1;
  195. ls:=Length(src)-start_pos;
  196. if current_dos_buffer_pos+ls+3>transfer_buffer+tb_size then
  197. RunError(217);
  198. getmem(c,ls+3);
  199. move(src[start_pos],c^,ls+1);
  200. if add_cr_at_end then
  201. begin
  202. c[ls+1]:=#13;
  203. c[ls+2]:=#0;
  204. end
  205. else
  206. c[ls+1]:=#0;
  207. CLen := StrLen (C) + 1;
  208. seg_move(get_ds,longint(c),dosmemselector,current_dos_buffer_pos,CLen);
  209. current_dos_buffer_pos:=current_dos_buffer_pos+CLen;
  210. freemem(c,ls+3);
  211. paste_to_dos:=true;
  212. end;
  213. procedure setup_proxy_cmdline;
  214. const
  215. MAX_ARGS = 128;
  216. var
  217. i : longint;
  218. quote : char;
  219. end_of_arg, skip_char : boolean;
  220. la_proxy_seg : word;
  221. la_proxy_ofs : longint;
  222. current_arg : string;
  223. la_argv_ofs : array [0..MAX_ARGS] of word;
  224. begin
  225. quote:=#0;
  226. current_arg:='';
  227. proxy_argc:=0;
  228. end_of_arg:=false;
  229. while current_dos_buffer_pos mod 16 <> 0 do
  230. inc(current_dos_buffer_pos);
  231. la_proxy_seg:=current_dos_buffer_pos shr 4;
  232. { Also copy parameter 0 }
  233. la_argv_ofs[0]:=current_dos_buffer_pos-la_proxy_seg*16;
  234. { Note that this should be done before
  235. alteriing p value }
  236. paste_to_dos(p,false,false);
  237. inc(proxy_argc);
  238. for i:=1 to length(c) do
  239. begin
  240. skip_char:=false;
  241. case c[i] of
  242. #1..#32:
  243. begin
  244. if quote=#0 then
  245. end_of_arg:=true;
  246. end;
  247. '"' :
  248. begin
  249. if quote=#0 then
  250. begin
  251. quote:='"';
  252. skip_char:=true;
  253. end
  254. else if quote='"' then
  255. end_of_arg:=true;
  256. end;
  257. '''' :
  258. begin
  259. if quote=#0 then
  260. begin
  261. quote:='''';
  262. skip_char:=true;
  263. end
  264. else if quote='''' then
  265. end_of_arg:=true;
  266. end;
  267. end;
  268. if not end_of_arg and not skip_char then
  269. current_arg:=current_arg+c[i];
  270. if i=length(c) then
  271. end_of_arg:=true;
  272. if end_of_arg and (current_arg<>'') then
  273. begin
  274. if proxy_argc>MAX_ARGS then
  275. begin
  276. writeln(stderr,'Too many arguments in Dos.exec');
  277. RunError(217);
  278. end;
  279. la_argv_ofs[proxy_argc]:=current_dos_buffer_pos-la_proxy_seg*16;
  280. {$ifdef DEBUG_PROXY}
  281. writeln(stderr,'arg ',proxy_argc,'="',current_arg,'"');
  282. {$endif DEBUG_PROXY}
  283. paste_to_dos(current_arg,false,false);
  284. inc(proxy_argc);
  285. quote:=#0;
  286. current_arg:='';
  287. end_of_arg:=false;
  288. end;
  289. end;
  290. la_proxy_ofs:=current_dos_buffer_pos - la_proxy_seg*16;
  291. seg_move(get_ds,longint(@la_argv_ofs),dosmemselector,
  292. current_dos_buffer_pos,proxy_argc*sizeof(word));
  293. current_dos_buffer_pos:=current_dos_buffer_pos + proxy_argc*sizeof(word);
  294. c:='!proxy '+hexstr(proxy_argc,4)+' '+hexstr(la_proxy_seg,4)
  295. +' '+hexstr(la_proxy_ofs,4);
  296. {$ifdef DEBUG_PROXY}
  297. writeln(stderr,'Using comline "',c,'"');
  298. {$endif DEBUG_PROXY}
  299. end;
  300. begin
  301. { create command line }
  302. c:=comline;
  303. if force_go32v2_proxy then
  304. Use_proxy:=true
  305. else if length(c)>DOS_MAX_COMMAND_LINE_LENGTH then
  306. begin
  307. if Use_go32v2_proxy then
  308. begin
  309. Use_Proxy:=true;
  310. end
  311. else
  312. begin
  313. writeln(stderr,'Dos.exec command line truncated to ',
  314. DOS_MAX_COMMAND_LINE_LENGTH,' chars');
  315. writeln(stderr,'Before: "',c,'"');
  316. setlength(c, DOS_MAX_COMMAND_LINE_LENGTH);
  317. writeln(stderr,'After: "',c,'"');
  318. end;
  319. end;
  320. { create path }
  321. {$ifdef DEBUG_PROXY}
  322. writeln(stderr,'Dos.exec path="',path,'"');
  323. {$endif DEBUG_PROXY}
  324. p:=path;
  325. { create buffer }
  326. la_env:=transfer_buffer;
  327. while (la_env and 15)<>0 do
  328. inc(la_env);
  329. current_dos_buffer_pos:=la_env;
  330. { copy environment }
  331. for i:=1 to envcount do
  332. paste_to_dos(envstr(i),false,false);
  333. {the behaviour is still suboptimal because variable COMMAND is stripped out}
  334. paste_to_dos(chr(0),false,false); { adds a double zero at the end }
  335. if use_proxy then
  336. setup_proxy_cmdline;
  337. { allow slash as backslash }
  338. DoDirSeparators(p);
  339. if LFNSupport then
  340. GetShortName(p);
  341. { Add program to DosBuffer with
  342. length at start }
  343. la_p:=current_dos_buffer_pos;
  344. paste_to_dos(p,false,true);
  345. { Add command line args to DosBuffer with
  346. length at start and Carriage Return at end }
  347. la_c:=current_dos_buffer_pos;
  348. paste_to_dos(c,true,true);
  349. la_e:=current_dos_buffer_pos;
  350. fcb1_la:=la_e;
  351. la_e:=la_e+16;
  352. fcb2_la:=la_e;
  353. la_e:=la_e+16;
  354. { allocate FCB see dosexec code }
  355. arg_ofs:=1;
  356. while (c[arg_ofs] in [' ',#9]) and
  357. (arg_ofs<length(c)) do
  358. inc(arg_ofs);
  359. dosregs.ax:=$2901;
  360. dosregs.ds:=(la_c+arg_ofs) shr 4;
  361. dosregs.esi:=(la_c+arg_ofs) and 15;
  362. dosregs.es:=fcb1_la shr 4;
  363. dosregs.edi:=fcb1_la and 15;
  364. msdos(dosregs);
  365. { allocate second FCB see dosexec code }
  366. dosregs.ax:=$2901;
  367. dosregs.ds:=(la_c+arg_ofs) shr 4;
  368. dosregs.esi:=(la_c+arg_ofs) and 15;
  369. dosregs.es:=fcb2_la shr 4;
  370. dosregs.edi:=fcb2_la and 15;
  371. {$ifdef DEBUG_PROXY}
  372. flush(stderr);
  373. {$endif DEBUG_PROXY}
  374. msdos(dosregs);
  375. with execblock do
  376. begin
  377. envseg:=la_env shr 4;
  378. comtail.seg:=la_c shr 4;
  379. comtail.ofs:=la_c and 15;
  380. firstFCB.seg:=fcb1_la shr 4;
  381. firstFCB.ofs:=fcb1_la and 15;
  382. secondFCB.seg:=fcb2_la shr 4;
  383. secondFCB.ofs:=fcb2_la and 15;
  384. end;
  385. seg_move(get_ds,longint(@execblock),dosmemselector,la_e,sizeof(texecblock));
  386. dosregs.edx:=la_p and 15+1;
  387. dosregs.ds:=la_p shr 4;
  388. dosregs.ebx:=la_p and 15+la_e-la_p;
  389. dosregs.es:=la_p shr 4;
  390. dosregs.ax:=$4b00;
  391. msdos(dosregs);
  392. LoadDosError;
  393. if DosError=0 then
  394. begin
  395. dosregs.ax:=$4d00;
  396. msdos(dosregs);
  397. LastDosExitCode:=DosRegs.al
  398. end
  399. else
  400. LastDosExitCode:=0;
  401. end;
  402. procedure exec(const path : pathstr;const comline : comstr);
  403. begin
  404. exec_ansistring(path, comline);
  405. end;
  406. procedure getcbreak(var breakvalue : boolean);
  407. begin
  408. dosregs.ax:=$3300;
  409. msdos(dosregs);
  410. breakvalue:=dosregs.dl<>0;
  411. end;
  412. procedure setcbreak(breakvalue : boolean);
  413. begin
  414. dosregs.ax:=$3301;
  415. dosregs.dl:=ord(breakvalue);
  416. msdos(dosregs);
  417. end;
  418. procedure getverify(var verify : boolean);
  419. begin
  420. dosregs.ah:=$54;
  421. msdos(dosregs);
  422. verify:=dosregs.al<>0;
  423. end;
  424. procedure setverify(verify : boolean);
  425. begin
  426. dosregs.ah:=$2e;
  427. dosregs.al:=ord(verify);
  428. msdos(dosregs);
  429. end;
  430. {******************************************************************************
  431. --- Disk ---
  432. ******************************************************************************}
  433. type
  434. ExtendedFat32FreeSpaceRec = packed record
  435. RetSize : word; { $00 }
  436. Strucversion : word; { $02 }
  437. SecPerClus, { $04 }
  438. BytePerSec, { $08 }
  439. AvailClusters, { $0C }
  440. TotalClusters, { $10 }
  441. AvailPhysSect, { $14 }
  442. TotalPhysSect, { $18 }
  443. AvailAllocUnits, { $1C }
  444. TotalAllocUnits : longword; { $20 }
  445. Dummy, { $24 }
  446. Dummy2 : longword; { $28 }
  447. end; { $2C }
  448. const
  449. IOCTL_INPUT = 3; //For request header command field
  450. CDFUNC_SECTSIZE = 7; //For cdrom control block func field
  451. CDFUNC_VOLSIZE = 8; //For cdrom control block func field
  452. type
  453. TRequestHeader = packed record
  454. length : byte; { $00 }
  455. subunit : byte; { $01 }
  456. command : byte; { $02 }
  457. status : word; { $03 }
  458. reserved1 : longword; { $05 }
  459. reserved2 : longword; { $09 }
  460. media_desc : byte; { $0D }
  461. transf_ofs : word; { $0E }
  462. transf_seg : word; { $10 }
  463. numbytes : word; { $12 }
  464. end; { $14 }
  465. TCDSectSizeReq = packed record
  466. func : byte; { $00 }
  467. mode : byte; { $01 }
  468. secsize : word; { $02 }
  469. end; { $04 }
  470. TCDVolSizeReq = packed record
  471. func : byte; { $00 }
  472. size : longword; { $01 }
  473. end; { $05 }
  474. function do_diskdata(drive : byte; Free : boolean) : Int64;
  475. var
  476. blocksize, freeblocks, totblocks : longword;
  477. { Get disk data via old int21/36 (GET FREE DISK SPACE). It's always supported
  478. even if it returns wrong values for volumes > 2GB and for cdrom drives when
  479. in pure DOS. Note that it's also the only way to get some data on WinNTs. }
  480. function DiskData_36 : boolean;
  481. begin
  482. DiskData_36:=false;
  483. dosregs.dl:=drive;
  484. dosregs.ah:=$36;
  485. msdos(dosregs);
  486. if dosregs.ax=$FFFF then exit;
  487. blocksize:=dosregs.ax*dosregs.cx;
  488. freeblocks:=dosregs.bx;
  489. totblocks:=dosregs.dx;
  490. Diskdata_36:=true;
  491. end;
  492. { Get disk data via int21/7303 (FAT32 - GET EXTENDED FREE SPACE ON DRIVE).
  493. It is supported by win9x even in pure DOS }
  494. function DiskData_7303 : boolean;
  495. var
  496. s : shortstring;
  497. rec : ExtendedFat32FreeSpaceRec;
  498. begin
  499. DiskData_7303:=false;
  500. s:=chr(drive+$40)+':\'+#0;
  501. rec.Strucversion:=0;
  502. rec.RetSize := 0;
  503. dosmemput(tb_segment,tb_offset,Rec,sizeof(ExtendedFat32FreeSpaceRec));
  504. dosmemput(tb_segment,tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1,s[1],4);
  505. dosregs.dx:=tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1;
  506. dosregs.ds:=tb_segment;
  507. dosregs.di:=tb_offset;
  508. dosregs.es:=tb_segment;
  509. dosregs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  510. dosregs.ax:=$7303;
  511. msdos(dosregs);
  512. if (dosregs.flags and fcarry) <> 0 then
  513. exit;
  514. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  515. if Rec.RetSize = 0 then
  516. exit;
  517. blocksize:=rec.SecPerClus*rec.BytePerSec;
  518. freeblocks:=rec.AvailAllocUnits;
  519. totblocks:=rec.TotalAllocUnits;
  520. DiskData_7303:=true;
  521. end;
  522. { Get disk data asking to MSCDEX. Pure DOS returns wrong values with
  523. int21/7303 or int21/36 if the drive is a CDROM drive }
  524. function DiskData_CDROM : boolean;
  525. var req : TRequestHeader;
  526. sectreq : TCDSectSizeReq;
  527. sizereq : TCDVolSizeReq;
  528. i : integer;
  529. status,byteswritten : word;
  530. drnum : byte;
  531. begin
  532. DiskData_CDROM:=false;
  533. drnum:=drive-1; //for MSCDEX, 0 = a, 1 = b etc, unlike int21/36
  534. { Is this a CDROM drive? }
  535. dosregs.ax:=$150b;
  536. dosregs.cx:=drnum;
  537. realintr($2f,dosregs);
  538. if (dosregs.bx<>$ADAD) or (dosregs.ax=0) then
  539. exit; // no, it isn't
  540. { Prepare the request header to send to the cdrom driver }
  541. FillByte(req,sizeof(req),0);
  542. req.length:=sizeof(req);
  543. req.command:=IOCTL_INPUT;
  544. req.transf_ofs:=tb_offset+sizeof(req); //CDROM control block will follow
  545. req.transf_seg:=tb_segment; //the request header
  546. req.numbytes:=sizeof(sectreq);
  547. { We're asking the sector size }
  548. sectreq.func:=CDFUNC_SECTSIZE;
  549. sectreq.mode:=0; //cooked
  550. sectreq.secsize:=0;
  551. for i:=1 to 2 do
  552. begin
  553. { Send the request to the cdrom driver }
  554. dosmemput(tb_segment,tb_offset,req,sizeof(req));
  555. dosmemput(tb_segment,tb_offset+sizeof(req),sectreq,sizeof(sectreq));
  556. dosregs.ax:=$1510;
  557. dosregs.cx:=drnum;
  558. dosregs.es:=tb_segment;
  559. dosregs.bx:=tb_offset;
  560. realintr($2f,dosregs);
  561. dosmemget(tb_segment,tb_offset+3,status,2);
  562. { status = $800F means "disk changed". Try once more. }
  563. if (status and $800F) <> $800F then break;
  564. end;
  565. dosmemget(tb_segment,tb_offset+$12,byteswritten,2);
  566. if (status<>$0100) or (byteswritten<>sizeof(sectreq)) then
  567. exit; //An error occurred
  568. dosmemget(tb_segment,tb_offset+sizeof(req),sectreq,sizeof(sectreq));
  569. { Update the request header for the next request }
  570. req.numbytes:=sizeof(sizereq);
  571. { We're asking the volume size (in blocks) }
  572. sizereq.func:=CDFUNC_VOLSIZE;
  573. sizereq.size:=0;
  574. { Send the request to the cdrom driver }
  575. dosmemput(tb_segment,tb_offset,req,sizeof(req));
  576. dosmemput(tb_segment,tb_offset+sizeof(req),sizereq,sizeof(sizereq));
  577. dosregs.ax:=$1510;
  578. dosregs.cx:=drnum;
  579. dosregs.es:=tb_segment;
  580. dosregs.bx:=tb_offset;
  581. realintr($2f,dosregs);
  582. dosmemget(tb_segment,tb_offset,req,sizeof(req));
  583. if (req.status<>$0100) or (req.numbytes<>sizeof(sizereq)) then
  584. exit; //An error occurred
  585. dosmemget(tb_segment,tb_offset+sizeof(req)+1,sizereq.size,4);
  586. blocksize:=sectreq.secsize;
  587. freeblocks:=0; //always 0 for a cdrom
  588. totblocks:=sizereq.size;
  589. DiskData_CDROM:=true;
  590. end;
  591. begin
  592. if drive=0 then
  593. begin
  594. dosregs.ax:=$1900; //get current default drive
  595. msdos(dosregs);
  596. drive:=dosregs.al+1;
  597. end;
  598. if not DiskData_CDROM then
  599. if not DiskData_7303 then
  600. if not DiskData_36 then
  601. begin
  602. do_diskdata:=-1;
  603. exit;
  604. end;
  605. do_diskdata:=blocksize;
  606. if free then
  607. do_diskdata:=do_diskdata*freeblocks
  608. else
  609. do_diskdata:=do_diskdata*totblocks;
  610. end;
  611. function diskfree(drive : byte) : int64;
  612. begin
  613. diskfree:=Do_DiskData(drive,TRUE);
  614. end;
  615. function disksize(drive : byte) : int64;
  616. begin
  617. disksize:=Do_DiskData(drive,false);
  618. end;
  619. {******************************************************************************
  620. --- LFNFindfirst LFNFindNext ---
  621. ******************************************************************************}
  622. type
  623. LFNSearchRec=packed record
  624. attr,
  625. crtime,
  626. crtimehi,
  627. actime,
  628. actimehi,
  629. lmtime,
  630. lmtimehi,
  631. sizehi,
  632. size : longint;
  633. reserved : array[0..7] of byte;
  634. name : array[0..259] of byte;
  635. shortname : array[0..13] of byte;
  636. end;
  637. procedure LFNSearchRec2Dos(const w:LFNSearchRec;hdl:longint;var d:Searchrec;from_findfirst : boolean);
  638. var
  639. Len : longint;
  640. begin
  641. With w do
  642. begin
  643. FillChar(d,sizeof(SearchRec),0);
  644. if DosError=0 then
  645. len:=StrLen(@Name)
  646. else
  647. len:=0;
  648. d.Name[0]:=chr(len);
  649. Move(Name[0],d.Name[1],Len);
  650. d.Time:=lmTime;
  651. d.Size:=Size;
  652. d.Attr:=Attr and $FF;
  653. if (DosError<>0) and from_findfirst then
  654. hdl:=-1;
  655. Move(hdl,d.Fill,4);
  656. end;
  657. end;
  658. {$ifdef DEBUG_LFN}
  659. const
  660. LFNFileName : string = 'LFN.log';
  661. LFNOpenNb : longint = 0;
  662. LogLFN : boolean = false;
  663. var
  664. lfnfile : text;
  665. {$endif DEBUG_LFN}
  666. procedure LFNFindFirst(path:pchar;attr:longint;var s:searchrec);
  667. var
  668. i : longint;
  669. w : LFNSearchRec;
  670. begin
  671. { allow slash as backslash }
  672. DoDirSeparators(path);
  673. dosregs.si:=1; { use ms-dos time }
  674. { don't include the label if not asked for it, needed for network drives }
  675. if attr=$8 then
  676. dosregs.ecx:=8
  677. else
  678. dosregs.ecx:=attr and (not 8);
  679. dosregs.edx:=tb_offset+Sizeof(LFNSearchrec)+1;
  680. dosmemput(tb_segment,tb_offset+Sizeof(LFNSearchrec)+1,path^,strlen(path)+1);
  681. dosregs.ds:=tb_segment;
  682. dosregs.edi:=tb_offset;
  683. dosregs.es:=tb_segment;
  684. dosregs.ax:=$714e;
  685. msdos(dosregs);
  686. LoadDosError;
  687. if DosError=2 then
  688. DosError:=18;
  689. {$ifdef DEBUG_LFN}
  690. if (DosError=0) and LogLFN then
  691. begin
  692. Append(lfnfile);
  693. inc(LFNOpenNb);
  694. Writeln(lfnfile,LFNOpenNb,' LFNFindFirst called ',path);
  695. close(lfnfile);
  696. end;
  697. {$endif DEBUG_LFN}
  698. copyfromdos(w,sizeof(LFNSearchRec));
  699. LFNSearchRec2Dos(w,dosregs.ax,s,true);
  700. end;
  701. procedure LFNFindNext(var s:searchrec);
  702. var
  703. hdl : longint;
  704. w : LFNSearchRec;
  705. begin
  706. Move(s.Fill,hdl,4);
  707. dosregs.si:=1; { use ms-dos time }
  708. dosregs.edi:=tb_offset;
  709. dosregs.es:=tb_segment;
  710. dosregs.ebx:=hdl;
  711. dosregs.ax:=$714f;
  712. msdos(dosregs);
  713. LoadDosError;
  714. copyfromdos(w,sizeof(LFNSearchRec));
  715. LFNSearchRec2Dos(w,hdl,s,false);
  716. end;
  717. procedure LFNFindClose(var s:searchrec);
  718. var
  719. hdl : longint;
  720. begin
  721. Move(s.Fill,hdl,4);
  722. { Do not call MsDos if FindFirst returned with an error }
  723. if hdl=-1 then
  724. begin
  725. DosError:=0;
  726. exit;
  727. end;
  728. dosregs.ebx:=hdl;
  729. dosregs.ax:=$71a1;
  730. msdos(dosregs);
  731. LoadDosError;
  732. {$ifdef DEBUG_LFN}
  733. if (DosError=0) and LogLFN then
  734. begin
  735. Append(lfnfile);
  736. Writeln(lfnfile,LFNOpenNb,' LFNFindClose called ');
  737. close(lfnfile);
  738. if LFNOpenNb>0 then
  739. dec(LFNOpenNb);
  740. end;
  741. {$endif DEBUG_LFN}
  742. end;
  743. {******************************************************************************
  744. --- DosFindfirst DosFindNext ---
  745. ******************************************************************************}
  746. procedure dossearchrec2searchrec(var f : searchrec);
  747. var
  748. len : longint;
  749. begin
  750. { Check is necessary!! OS/2's VDM doesn't clear the name with #0 if the }
  751. { file doesn't exist! (JM) }
  752. if dosError = 0 then
  753. len:=StrLen(@f.Name)
  754. else len := 0;
  755. Move(f.Name[0],f.Name[1],Len);
  756. f.Name[0]:=chr(len);
  757. end;
  758. procedure DosFindfirst(path : pchar;attr : word;var f : searchrec);
  759. var
  760. i : longint;
  761. begin
  762. { allow slash as backslash }
  763. DoDirSeparators(path);
  764. copytodos(f,sizeof(searchrec));
  765. dosregs.edx:=tb_offset;
  766. dosregs.ds:=tb_segment;
  767. dosregs.ah:=$1a;
  768. msdos(dosregs);
  769. dosregs.ecx:=attr;
  770. dosregs.edx:=tb_offset+Sizeof(searchrec)+1;
  771. dosmemput(tb_segment,tb_offset+Sizeof(searchrec)+1,path^,strlen(path)+1);
  772. dosregs.ds:=tb_segment;
  773. dosregs.ah:=$4e;
  774. msdos(dosregs);
  775. copyfromdos(f,sizeof(searchrec));
  776. LoadDosError;
  777. dossearchrec2searchrec(f);
  778. end;
  779. procedure Dosfindnext(var f : searchrec);
  780. begin
  781. copytodos(f,sizeof(searchrec));
  782. dosregs.edx:=tb_offset;
  783. dosregs.ds:=tb_segment;
  784. dosregs.ah:=$1a;
  785. msdos(dosregs);
  786. dosregs.ah:=$4f;
  787. msdos(dosregs);
  788. copyfromdos(f,sizeof(searchrec));
  789. LoadDosError;
  790. dossearchrec2searchrec(f);
  791. end;
  792. {******************************************************************************
  793. --- Findfirst FindNext ---
  794. ******************************************************************************}
  795. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  796. var
  797. path0 : array[0..255] of char;
  798. begin
  799. doserror:=0;
  800. strpcopy(path0,path);
  801. if LFNSupport then
  802. LFNFindFirst(path0,attr,f)
  803. else
  804. Dosfindfirst(path0,attr,f);
  805. end;
  806. procedure findnext(var f : searchRec);
  807. begin
  808. doserror:=0;
  809. if LFNSupport then
  810. LFNFindnext(f)
  811. else
  812. Dosfindnext(f);
  813. end;
  814. Procedure FindClose(Var f: SearchRec);
  815. begin
  816. DosError:=0;
  817. if LFNSupport then
  818. LFNFindClose(f);
  819. end;
  820. type swap_proc = procedure;
  821. var
  822. _swap_in : swap_proc;external name '_swap_in';
  823. _swap_out : swap_proc;external name '_swap_out';
  824. _exception_exit : pointer;external name '_exception_exit';
  825. _v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  826. procedure swapvectors;
  827. begin
  828. if _exception_exit<>nil then
  829. if _v2prt0_exceptions_on then
  830. _swap_out()
  831. else
  832. _swap_in();
  833. end;
  834. {******************************************************************************
  835. --- File ---
  836. ******************************************************************************}
  837. Function FSearch(path: pathstr; dirlist: string): pathstr;
  838. var
  839. i,p1 : longint;
  840. s : searchrec;
  841. newdir : pathstr;
  842. begin
  843. { check if the file specified exists }
  844. findfirst(path,anyfile and not(directory),s);
  845. if doserror=0 then
  846. begin
  847. findclose(s);
  848. fsearch:=path;
  849. exit;
  850. end;
  851. { No wildcards allowed in these things }
  852. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  853. fsearch:=''
  854. else
  855. begin
  856. { allow slash as backslash }
  857. DoDirSeparators(dirlist);
  858. repeat
  859. p1:=pos(';',dirlist);
  860. if p1<>0 then
  861. begin
  862. newdir:=copy(dirlist,1,p1-1);
  863. delete(dirlist,1,p1);
  864. end
  865. else
  866. begin
  867. newdir:=dirlist;
  868. dirlist:='';
  869. end;
  870. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  871. newdir:=newdir+'\';
  872. findfirst(newdir+path,anyfile and not(directory),s);
  873. if doserror=0 then
  874. newdir:=newdir+path
  875. else
  876. newdir:='';
  877. until (dirlist='') or (newdir<>'');
  878. fsearch:=newdir;
  879. end;
  880. findclose(s);
  881. end;
  882. { change to short filename if successful DOS call PM }
  883. function GetShortName(var p : String) : boolean;
  884. var
  885. c : array[0..255] of char;
  886. begin
  887. move(p[1],c[0],length(p));
  888. c[length(p)]:=#0;
  889. copytodos(c,length(p)+1);
  890. dosregs.ax:=$7160;
  891. dosregs.cx:=1;
  892. dosregs.ds:=tb_segment;
  893. dosregs.si:=tb_offset;
  894. dosregs.es:=tb_segment;
  895. dosregs.di:=tb_offset;
  896. msdos(dosregs);
  897. LoadDosError;
  898. if DosError=0 then
  899. begin
  900. copyfromdos(c,256);
  901. move(c[0],p[1],strlen(c));
  902. p[0]:=char(strlen(c));
  903. GetShortName:=true;
  904. end
  905. else
  906. GetShortName:=false;
  907. end;
  908. { change to long filename if successful DOS call PM }
  909. function GetLongName(var p : String) : boolean;
  910. var
  911. c : array[0..255] of char;
  912. begin
  913. move(p[1],c[0],length(p));
  914. c[length(p)]:=#0;
  915. copytodos(c,length(p)+1);
  916. dosregs.ax:=$7160;
  917. dosregs.cx:=2;
  918. dosregs.ds:=tb_segment;
  919. dosregs.si:=tb_offset;
  920. dosregs.es:=tb_segment;
  921. dosregs.di:=tb_offset;
  922. msdos(dosregs);
  923. LoadDosError;
  924. if DosError=0 then
  925. begin
  926. copyfromdos(c,256);
  927. move(c[0],p[1],strlen(c));
  928. p[0]:=char(strlen(c));
  929. GetLongName:=true;
  930. end
  931. else
  932. GetLongName:=false;
  933. end;
  934. {******************************************************************************
  935. --- Get/Set File Time,Attr ---
  936. ******************************************************************************}
  937. procedure getftime(var f;var time : longint);
  938. begin
  939. dosregs.bx:=textrec(f).handle;
  940. dosregs.ax:=$5700;
  941. msdos(dosregs);
  942. loaddoserror;
  943. time:=(dosregs.dx shl 16)+dosregs.cx;
  944. end;
  945. procedure setftime(var f;time : longint);
  946. begin
  947. dosregs.bx:=textrec(f).handle;
  948. dosregs.cx:=time and $ffff;
  949. dosregs.dx:=time shr 16;
  950. dosregs.ax:=$5701;
  951. msdos(dosregs);
  952. loaddoserror;
  953. end;
  954. procedure getfattr(var f;var attr : word);
  955. begin
  956. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  957. dosregs.edx:=tb_offset;
  958. dosregs.ds:=tb_segment;
  959. if LFNSupport then
  960. begin
  961. dosregs.ax:=$7143;
  962. dosregs.bx:=0;
  963. end
  964. else
  965. dosregs.ax:=$4300;
  966. msdos(dosregs);
  967. LoadDosError;
  968. Attr:=dosregs.cx;
  969. end;
  970. procedure setfattr(var f;attr : word);
  971. begin
  972. { Fail for setting VolumeId. }
  973. if ((attr and VolumeID)<>0) then
  974. begin
  975. doserror:=5;
  976. exit;
  977. end;
  978. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  979. dosregs.edx:=tb_offset;
  980. dosregs.ds:=tb_segment;
  981. if LFNSupport then
  982. begin
  983. dosregs.ax:=$7143;
  984. dosregs.bx:=1;
  985. end
  986. else
  987. dosregs.ax:=$4301;
  988. dosregs.cx:=attr;
  989. msdos(dosregs);
  990. LoadDosError;
  991. end;
  992. {******************************************************************************
  993. --- Environment ---
  994. ******************************************************************************}
  995. function envcount : longint;
  996. var
  997. hp : ppchar;
  998. begin
  999. hp:=envp;
  1000. envcount:=0;
  1001. while assigned(hp^) do
  1002. begin
  1003. inc(envcount);
  1004. inc(hp);
  1005. end;
  1006. end;
  1007. function envstr (Index: longint): string;
  1008. begin
  1009. if (index<=0) or (index>envcount) then
  1010. envstr:=''
  1011. else
  1012. envstr:=strpas(ppchar(pointer(envp)+SizeOf(PChar)*(index-1))^);
  1013. end;
  1014. Function GetEnv(envvar: string): string;
  1015. var
  1016. hp : ppchar;
  1017. hs : string;
  1018. eqpos : longint;
  1019. begin
  1020. envvar:=upcase(envvar);
  1021. hp:=envp;
  1022. getenv:='';
  1023. while assigned(hp^) do
  1024. begin
  1025. hs:=strpas(hp^);
  1026. eqpos:=pos('=',hs);
  1027. if upcase(copy(hs,1,eqpos-1))=envvar then
  1028. begin
  1029. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  1030. break;
  1031. end;
  1032. inc(hp);
  1033. end;
  1034. end;
  1035. {$ifdef DEBUG_LFN}
  1036. begin
  1037. LogLFN:=(GetEnv('LOGLFN')<>'');
  1038. assign(lfnfile,LFNFileName);
  1039. {$I-}
  1040. Reset(lfnfile);
  1041. if IOResult<>0 then
  1042. begin
  1043. Rewrite(lfnfile);
  1044. Writeln(lfnfile,'New lfn.log');
  1045. end;
  1046. close(lfnfile);
  1047. {$endif DEBUG_LFN}
  1048. end.