dos.pp 29 KB

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