dos.pp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Dos unit for BP7 compatible RTL
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit dos;
  13. interface
  14. Uses
  15. Go32;
  16. Const
  17. {Bitmasks for CPU Flags}
  18. fcarry = $0001;
  19. fparity = $0004;
  20. fauxiliary = $0010;
  21. fzero = $0040;
  22. fsign = $0080;
  23. foverflow = $0800;
  24. {Bitmasks for file attribute}
  25. readonly = $01;
  26. hidden = $02;
  27. sysfile = $04;
  28. volumeid = $08;
  29. directory = $10;
  30. archive = $20;
  31. anyfile = $3F;
  32. {File Status}
  33. fmclosed = $D7B0;
  34. fminput = $D7B1;
  35. fmoutput = $D7B2;
  36. fminout = $D7B3;
  37. Type
  38. { Needed for LFN Support }
  39. ComStr = String[255];
  40. PathStr = String[255];
  41. DirStr = String[255];
  42. NameStr = String[255];
  43. ExtStr = String[255];
  44. {
  45. filerec.inc contains the definition of the filerec.
  46. textrec.inc contains the definition of the textrec.
  47. It is in a separate file to make it available in other units without
  48. having to use the DOS unit for it.
  49. }
  50. {$i filerec.inc}
  51. {$i textrec.inc}
  52. DateTime = packed record
  53. Year,
  54. Month,
  55. Day,
  56. Hour,
  57. Min,
  58. Sec : word;
  59. End;
  60. searchrec = packed record
  61. fill : array[1..21] of byte;
  62. attr : byte;
  63. time : longint;
  64. { reserved : word; not in DJGPP V2 }
  65. size : longint;
  66. name : string[255]; { LFN Name, DJGPP uses only [12] but more can't hurt (PFV) }
  67. end;
  68. Registers = Go32.Registers;
  69. Var
  70. DosError : integer;
  71. {Interrupt}
  72. Procedure Intr(intno: byte; var regs: registers);
  73. Procedure MSDos(var regs: registers);
  74. {Info/Date/Time}
  75. Function DosVersion: Word;
  76. Procedure GetDate(var year, month, mday, wday: word);
  77. Procedure GetTime(var hour, minute, second, sec100: word);
  78. procedure SetDate(year,month,day: word);
  79. Procedure SetTime(hour,minute,second,sec100: word);
  80. Procedure UnpackTime(p: longint; var t: datetime);
  81. Procedure PackTime(var t: datetime; var p: longint);
  82. {Exec}
  83. Procedure Exec(const path: pathstr; const comline: comstr);
  84. Function DosExitCode: word;
  85. {Disk}
  86. Function DiskFree(drive: byte) : int64;
  87. Function DiskSize(drive: byte) : int64;
  88. Procedure FindFirst(const path: pathstr; attr: word; var f: searchRec);
  89. Procedure FindNext(var f: searchRec);
  90. Procedure FindClose(Var f: SearchRec);
  91. {File}
  92. Procedure GetFAttr(var f; var attr: word);
  93. Procedure GetFTime(var f; var time: longint);
  94. Function FSearch(path: pathstr; dirlist: string): pathstr;
  95. Function FExpand(const path: pathstr): pathstr;
  96. Procedure FSplit(path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
  97. function GetShortName(var p : String) : boolean;
  98. function GetLongName(var p : String) : boolean;
  99. {Environment}
  100. Function EnvCount: longint;
  101. Function EnvStr(index: integer): string;
  102. Function GetEnv(envvar: string): string;
  103. {Misc}
  104. Procedure SetFAttr(var f; attr: word);
  105. Procedure SetFTime(var f; time: longint);
  106. Procedure GetCBreak(var breakvalue: boolean);
  107. Procedure SetCBreak(breakvalue: boolean);
  108. Procedure GetVerify(var verify: boolean);
  109. Procedure SetVerify(verify: boolean);
  110. {Do Nothing Functions}
  111. Procedure SwapVectors;
  112. Procedure GetIntVec(intno: byte; var vector: pointer);
  113. Procedure SetIntVec(intno: byte; vector: pointer);
  114. Procedure Keep(exitcode: word);
  115. implementation
  116. uses
  117. strings;
  118. {$ASMMODE ATT}
  119. {******************************************************************************
  120. --- Dos Interrupt ---
  121. ******************************************************************************}
  122. var
  123. dosregs : registers;
  124. procedure LoadDosError;
  125. var
  126. r : registers;
  127. SimpleDosError : word;
  128. begin
  129. if (dosregs.flags and carryflag) <> 0 then
  130. begin
  131. { I got a extended error = 0
  132. while CarryFlag was set from Exec function }
  133. SimpleDosError:=dosregs.ax;
  134. r.eax:=$5900;
  135. r.ebx:=$0;
  136. realintr($21,r);
  137. { conversion from word to integer !!
  138. gave a Bound check error if ax is $FFFF !! PM }
  139. doserror:=integer(r.ax);
  140. case doserror of
  141. 0 : DosError:=integer(SimpleDosError);
  142. 19 : DosError:=150;
  143. 21 : DosError:=152;
  144. end;
  145. end
  146. else
  147. doserror:=0;
  148. end;
  149. procedure intr(intno : byte;var regs : registers);
  150. begin
  151. realintr(intno,regs);
  152. end;
  153. procedure msdos(var regs : registers);
  154. begin
  155. intr($21,regs);
  156. end;
  157. {******************************************************************************
  158. --- Info / Date / Time ---
  159. ******************************************************************************}
  160. function dosversion : word;
  161. begin
  162. dosregs.ax:=$3000;
  163. msdos(dosregs);
  164. dosversion:=dosregs.ax;
  165. end;
  166. procedure getdate(var year,month,mday,wday : word);
  167. begin
  168. dosregs.ax:=$2a00;
  169. msdos(dosregs);
  170. wday:=dosregs.al;
  171. year:=dosregs.cx;
  172. month:=dosregs.dh;
  173. mday:=dosregs.dl;
  174. end;
  175. procedure setdate(year,month,day : word);
  176. begin
  177. dosregs.cx:=year;
  178. dosregs.dh:=month;
  179. dosregs.dl:=day;
  180. dosregs.ah:=$2b;
  181. msdos(dosregs);
  182. end;
  183. procedure gettime(var hour,minute,second,sec100 : word);
  184. begin
  185. dosregs.ah:=$2c;
  186. msdos(dosregs);
  187. hour:=dosregs.ch;
  188. minute:=dosregs.cl;
  189. second:=dosregs.dh;
  190. sec100:=dosregs.dl;
  191. end;
  192. procedure settime(hour,minute,second,sec100 : word);
  193. begin
  194. dosregs.ch:=hour;
  195. dosregs.cl:=minute;
  196. dosregs.dh:=second;
  197. dosregs.dl:=sec100;
  198. dosregs.ah:=$2d;
  199. msdos(dosregs);
  200. end;
  201. Procedure packtime(var t : datetime;var p : longint);
  202. Begin
  203. p:=(t.sec shr 1)+(t.min shl 5)+(t.hour shl 11)+(t.day shl 16)+(t.month shl 21)+((t.year-1980) shl 25);
  204. End;
  205. Procedure unpacktime(p : longint;var t : datetime);
  206. Begin
  207. with t do
  208. begin
  209. sec:=(p and 31) shl 1;
  210. min:=(p shr 5) and 63;
  211. hour:=(p shr 11) and 31;
  212. day:=(p shr 16) and 31;
  213. month:=(p shr 21) and 15;
  214. year:=(p shr 25)+1980;
  215. end;
  216. End;
  217. {******************************************************************************
  218. --- Exec ---
  219. ******************************************************************************}
  220. var
  221. lastdosexitcode : word;
  222. procedure exec(const path : pathstr;const comline : comstr);
  223. type
  224. realptr = packed record
  225. ofs,seg : word;
  226. end;
  227. texecblock = packed record
  228. envseg : word;
  229. comtail : realptr;
  230. firstFCB : realptr;
  231. secondFCB : realptr;
  232. iniStack : realptr;
  233. iniCSIP : realptr;
  234. end;
  235. var
  236. current_dos_buffer_pos,
  237. arg_ofs,
  238. i,la_env,
  239. la_p,la_c,la_e,
  240. fcb1_la,fcb2_la : longint;
  241. execblock : texecblock;
  242. c,p : string;
  243. function paste_to_dos(src : string) : boolean;
  244. var
  245. c : array[0..255] of char;
  246. begin
  247. paste_to_dos:=false;
  248. if current_dos_buffer_pos+length(src)+1>transfer_buffer+tb_size then
  249. RunError(217);
  250. move(src[1],c[0],length(src));
  251. c[length(src)]:=#0;
  252. seg_move(get_ds,longint(@c),dosmemselector,current_dos_buffer_pos,length(src)+1);
  253. current_dos_buffer_pos:=current_dos_buffer_pos+length(src)+1;
  254. paste_to_dos:=true;
  255. end;
  256. begin
  257. { create command line }
  258. move(comline[0],c[1],length(comline)+1);
  259. c[length(comline)+2]:=#13;
  260. c[0]:=char(length(comline)+2);
  261. { create path }
  262. p:=path;
  263. for i:=1 to length(p) do
  264. if p[i]='/' then
  265. p[i]:='\';
  266. if LFNSupport then
  267. GetShortName(p);
  268. { create buffer }
  269. la_env:=transfer_buffer;
  270. while (la_env and 15)<>0 do
  271. inc(la_env);
  272. current_dos_buffer_pos:=la_env;
  273. { copy environment }
  274. for i:=1 to envcount do
  275. paste_to_dos(envstr(i));
  276. paste_to_dos(''); { adds a double zero at the end }
  277. { allow slash as backslash }
  278. la_p:=current_dos_buffer_pos;
  279. paste_to_dos(p);
  280. la_c:=current_dos_buffer_pos;
  281. paste_to_dos(c);
  282. la_e:=current_dos_buffer_pos;
  283. fcb1_la:=la_e;
  284. la_e:=la_e+16;
  285. fcb2_la:=la_e;
  286. la_e:=la_e+16;
  287. { allocate FCB see dosexec code }
  288. arg_ofs:=1;
  289. while (c[arg_ofs] in [' ',#9]) do
  290. inc(arg_ofs);
  291. dosregs.ax:=$2901;
  292. dosregs.ds:=(la_c+arg_ofs) shr 4;
  293. dosregs.esi:=(la_c+arg_ofs) and 15;
  294. dosregs.es:=fcb1_la shr 4;
  295. dosregs.edi:=fcb1_la and 15;
  296. msdos(dosregs);
  297. { allocate second FCB see dosexec code }
  298. repeat
  299. inc(arg_ofs);
  300. until (c[arg_ofs] in [' ',#9,#13]);
  301. if c[arg_ofs]<>#13 then
  302. begin
  303. repeat
  304. inc(arg_ofs);
  305. until not (c[arg_ofs] in [' ',#9]);
  306. end;
  307. dosregs.ax:=$2901;
  308. dosregs.ds:=(la_c+arg_ofs) shr 4;
  309. dosregs.si:=(la_c+arg_ofs) and 15;
  310. dosregs.es:=fcb2_la shr 4;
  311. dosregs.di:=fcb2_la and 15;
  312. msdos(dosregs);
  313. with execblock do
  314. begin
  315. envseg:=la_env shr 4;
  316. comtail.seg:=la_c shr 4;
  317. comtail.ofs:=la_c and 15;
  318. firstFCB.seg:=fcb1_la shr 4;
  319. firstFCB.ofs:=fcb1_la and 15;
  320. secondFCB.seg:=fcb2_la shr 4;
  321. secondFCB.ofs:=fcb2_la and 15;
  322. end;
  323. seg_move(get_ds,longint(@execblock),dosmemselector,la_e,sizeof(texecblock));
  324. dosregs.edx:=la_p and 15;
  325. dosregs.ds:=la_p shr 4;
  326. dosregs.ebx:=la_e and 15;
  327. dosregs.es:=la_e shr 4;
  328. dosregs.ax:=$4b00;
  329. msdos(dosregs);
  330. LoadDosError;
  331. if DosError=0 then
  332. begin
  333. dosregs.ax:=$4d00;
  334. msdos(dosregs);
  335. LastDosExitCode:=DosRegs.al
  336. end
  337. else
  338. LastDosExitCode:=0;
  339. end;
  340. function dosexitcode : word;
  341. begin
  342. dosexitcode:=lastdosexitcode;
  343. end;
  344. procedure getcbreak(var breakvalue : boolean);
  345. begin
  346. DosError:=0;
  347. dosregs.ax:=$3300;
  348. msdos(dosregs);
  349. breakvalue:=dosregs.dl<>0;
  350. end;
  351. procedure setcbreak(breakvalue : boolean);
  352. begin
  353. DosError:=0;
  354. dosregs.ax:=$3301;
  355. dosregs.dl:=ord(breakvalue);
  356. msdos(dosregs);
  357. end;
  358. procedure getverify(var verify : boolean);
  359. begin
  360. DosError:=0;
  361. dosregs.ah:=$54;
  362. msdos(dosregs);
  363. verify:=dosregs.al<>0;
  364. end;
  365. procedure setverify(verify : boolean);
  366. begin
  367. DosError:=0;
  368. dosregs.ah:=$2e;
  369. dosregs.al:=ord(verify);
  370. msdos(dosregs);
  371. end;
  372. {******************************************************************************
  373. --- Disk ---
  374. ******************************************************************************}
  375. TYPE ExtendedFat32FreeSpaceRec=packed Record
  376. RetSize : WORD; { (ret) size of returned structure}
  377. Strucversion : WORD; {(call) structure version (0000h)
  378. (ret) actual structure version (0000h)}
  379. SecPerClus, {number of sectors per cluster}
  380. BytePerSec, {number of bytes per sector}
  381. AvailClusters, {number of available clusters}
  382. TotalClusters, {total number of clusters on the drive}
  383. AvailPhysSect, {physical sectors available on the drive}
  384. TotalPhysSect, {total physical sectors on the drive}
  385. AvailAllocUnits, {Available allocation units}
  386. TotalAllocUnits : DWORD; {Total allocation units}
  387. Dummy,Dummy2 : DWORD; {8 bytes reserved}
  388. END;
  389. function do_diskdata(drive : byte; Free : BOOLEAN) : Int64;
  390. VAR
  391. S : String;
  392. Rec : ExtendedFat32FreeSpaceRec;
  393. BEGIN
  394. if (swap(dosversion)>=$070A) AND LFNSupport then
  395. begin
  396. S:='C:\'#0;
  397. if Drive=0 then
  398. begin
  399. GetDir(Drive,S);
  400. Setlength(S,4);
  401. S[4]:=#0;
  402. end
  403. else
  404. S[1]:=chr(Drive+64);
  405. Rec.Strucversion:=0;
  406. dosmemput(tb_segment,tb_offset,Rec,SIZEOF(ExtendedFat32FreeSpaceRec));
  407. dosmemput(tb_segment,tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1,S[1],4);
  408. dosregs.dx:=tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1;
  409. dosregs.ds:=tb_segment;
  410. dosregs.di:=tb_offset;
  411. dosregs.es:=tb_segment;
  412. dosregs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  413. dosregs.ax:=$7303;
  414. msdos(dosregs);
  415. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  416. if dosregs.eax<>-1 THEN {No error clausule in int except cf}
  417. begin
  418. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  419. if Free then
  420. Do_DiskData:=int64(rec.AvailAllocUnits)*rec.SecPerClus*rec.BytePerSec
  421. else
  422. Do_DiskData:=int64(rec.TotalAllocUnits)*rec.SecPerClus*rec.BytePerSec;
  423. end
  424. else
  425. Do_DiskData:=-1;
  426. end
  427. else
  428. begin
  429. dosregs.dl:=drive;
  430. dosregs.ah:=$36;
  431. msdos(dosregs);
  432. if dosregs.ax<>$FFFF then
  433. begin
  434. if Free then
  435. Do_DiskData:=int64(dosregs.ax)*dosregs.bx*dosregs.cx
  436. else
  437. Do_DiskData:=int64(dosregs.ax)*dosregs.cx*dosregs.dx;
  438. end
  439. else
  440. do_diskdata:=-1;
  441. end;
  442. end;
  443. function diskfree(drive : byte) : int64;
  444. begin
  445. diskfree:=Do_DiskData(drive,TRUE);
  446. end;
  447. function disksize(drive : byte) : int64;
  448. begin
  449. disksize:=Do_DiskData(drive,false);
  450. end;
  451. {******************************************************************************
  452. --- LFNFindfirst LFNFindNext ---
  453. ******************************************************************************}
  454. type
  455. LFNSearchRec=packed record
  456. attr,
  457. crtime,
  458. crtimehi,
  459. actime,
  460. actimehi,
  461. lmtime,
  462. lmtimehi,
  463. sizehi,
  464. size : longint;
  465. reserved : array[0..7] of byte;
  466. name : array[0..259] of byte;
  467. shortname : array[0..13] of byte;
  468. end;
  469. procedure LFNSearchRec2Dos(const w:LFNSearchRec;hdl:longint;var d:Searchrec);
  470. var
  471. Len : longint;
  472. begin
  473. With w do
  474. begin
  475. FillChar(d,sizeof(SearchRec),0);
  476. if DosError=0 then
  477. len:=StrLen(@Name)
  478. else
  479. len:=0;
  480. d.Name[0]:=chr(len);
  481. Move(Name[0],d.Name[1],Len);
  482. d.Time:=lmTime;
  483. d.Size:=Size;
  484. d.Attr:=Attr and $FF;
  485. Move(hdl,d.Fill,4);
  486. end;
  487. end;
  488. procedure LFNFindFirst(path:pchar;attr:longint;var s:searchrec);
  489. var
  490. i : longint;
  491. w : LFNSearchRec;
  492. begin
  493. { allow slash as backslash }
  494. for i:=0 to strlen(path) do
  495. if path[i]='/' then path[i]:='\';
  496. dosregs.si:=1; { use ms-dos time }
  497. { don't include the label if not asked for it, needed for network drives }
  498. if attr=$8 then
  499. dosregs.ecx:=8
  500. else
  501. dosregs.ecx:=attr and (not 8);
  502. dosregs.edx:=tb_offset+Sizeof(LFNSearchrec)+1;
  503. dosmemput(tb_segment,tb_offset+Sizeof(LFNSearchrec)+1,path^,strlen(path)+1);
  504. dosregs.ds:=tb_segment;
  505. dosregs.edi:=tb_offset;
  506. dosregs.es:=tb_segment;
  507. dosregs.ax:=$714e;
  508. msdos(dosregs);
  509. LoadDosError;
  510. copyfromdos(w,sizeof(LFNSearchRec));
  511. LFNSearchRec2Dos(w,dosregs.ax,s);
  512. end;
  513. procedure LFNFindNext(var s:searchrec);
  514. var
  515. hdl : longint;
  516. w : LFNSearchRec;
  517. begin
  518. Move(s.Fill,hdl,4);
  519. dosregs.si:=1; { use ms-dos time }
  520. dosregs.edi:=tb_offset;
  521. dosregs.es:=tb_segment;
  522. dosregs.ebx:=hdl;
  523. dosregs.ax:=$714f;
  524. msdos(dosregs);
  525. LoadDosError;
  526. copyfromdos(w,sizeof(LFNSearchRec));
  527. LFNSearchRec2Dos(w,hdl,s);
  528. end;
  529. procedure LFNFindClose(var s:searchrec);
  530. var
  531. hdl : longint;
  532. begin
  533. Move(s.Fill,hdl,4);
  534. dosregs.ebx:=hdl;
  535. dosregs.ax:=$71a1;
  536. msdos(dosregs);
  537. LoadDosError;
  538. end;
  539. {******************************************************************************
  540. --- DosFindfirst DosFindNext ---
  541. ******************************************************************************}
  542. procedure dossearchrec2searchrec(var f : searchrec);
  543. var
  544. len : longint;
  545. begin
  546. { Check is necessary!! OS/2's VDM doesn't clear the name with #0 if the }
  547. { file doesn't exist! (JM) }
  548. if dosError = 0 then
  549. len:=StrLen(@f.Name)
  550. else len := 0;
  551. Move(f.Name[0],f.Name[1],Len);
  552. f.Name[0]:=chr(len);
  553. end;
  554. procedure DosFindfirst(path : pchar;attr : word;var f : searchrec);
  555. var
  556. i : longint;
  557. begin
  558. { allow slash as backslash }
  559. for i:=0 to strlen(path) do
  560. if path[i]='/' then path[i]:='\';
  561. copytodos(f,sizeof(searchrec));
  562. dosregs.edx:=tb_offset;
  563. dosregs.ds:=tb_segment;
  564. dosregs.ah:=$1a;
  565. msdos(dosregs);
  566. dosregs.ecx:=attr;
  567. dosregs.edx:=tb_offset+Sizeof(searchrec)+1;
  568. dosmemput(tb_segment,tb_offset+Sizeof(searchrec)+1,path^,strlen(path)+1);
  569. dosregs.ds:=tb_segment;
  570. dosregs.ah:=$4e;
  571. msdos(dosregs);
  572. copyfromdos(f,sizeof(searchrec));
  573. LoadDosError;
  574. dossearchrec2searchrec(f);
  575. end;
  576. procedure Dosfindnext(var f : searchrec);
  577. begin
  578. copytodos(f,sizeof(searchrec));
  579. dosregs.edx:=tb_offset;
  580. dosregs.ds:=tb_segment;
  581. dosregs.ah:=$1a;
  582. msdos(dosregs);
  583. dosregs.ah:=$4f;
  584. msdos(dosregs);
  585. copyfromdos(f,sizeof(searchrec));
  586. LoadDosError;
  587. dossearchrec2searchrec(f);
  588. end;
  589. {******************************************************************************
  590. --- Findfirst FindNext ---
  591. ******************************************************************************}
  592. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  593. var
  594. path0 : array[0..256] of char;
  595. begin
  596. doserror:=0;
  597. strpcopy(path0,path);
  598. if LFNSupport then
  599. LFNFindFirst(path0,attr,f)
  600. else
  601. Dosfindfirst(path0,attr,f);
  602. end;
  603. procedure findnext(var f : searchRec);
  604. begin
  605. doserror:=0;
  606. if LFNSupport then
  607. LFNFindnext(f)
  608. else
  609. Dosfindnext(f);
  610. end;
  611. Procedure FindClose(Var f: SearchRec);
  612. begin
  613. DosError:=0;
  614. if LFNSupport then
  615. LFNFindClose(f);
  616. end;
  617. type swap_proc = procedure;
  618. var
  619. _swap_in : swap_proc;external name '_swap_in';
  620. _swap_out : swap_proc;external name '_swap_out';
  621. _exception_exit : pointer;external name '_exception_exit';
  622. _v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  623. procedure swapvectors;
  624. begin
  625. if _exception_exit<>nil then
  626. if _v2prt0_exceptions_on then
  627. _swap_in()
  628. else
  629. _swap_out();
  630. end;
  631. {******************************************************************************
  632. --- File ---
  633. ******************************************************************************}
  634. procedure fsplit(path : pathstr;var dir : dirstr;var name : namestr;var ext : extstr);
  635. var
  636. dotpos,p1,i : longint;
  637. begin
  638. { allow slash as backslash }
  639. for i:=1 to length(path) do
  640. if path[i]='/' then path[i]:='\';
  641. { get drive name }
  642. p1:=pos(':',path);
  643. if p1>0 then
  644. begin
  645. dir:=path[1]+':';
  646. delete(path,1,p1);
  647. end
  648. else
  649. dir:='';
  650. { split the path and the name, there are no more path informtions }
  651. { if path contains no backslashes }
  652. while true do
  653. begin
  654. p1:=pos('\',path);
  655. if p1=0 then
  656. break;
  657. dir:=dir+copy(path,1,p1);
  658. delete(path,1,p1);
  659. end;
  660. { try to find out a extension }
  661. if LFNSupport then
  662. begin
  663. Ext:='';
  664. i:=Length(Path);
  665. DotPos:=256;
  666. While (i>0) Do
  667. Begin
  668. If (Path[i]='.') Then
  669. begin
  670. DotPos:=i;
  671. break;
  672. end;
  673. Dec(i);
  674. end;
  675. Ext:=Copy(Path,DotPos,255);
  676. Name:=Copy(Path,1,DotPos - 1);
  677. end
  678. else
  679. begin
  680. p1:=pos('.',path);
  681. if p1>0 then
  682. begin
  683. ext:=copy(path,p1,4);
  684. delete(path,p1,length(path)-p1+1);
  685. end
  686. else
  687. ext:='';
  688. name:=path;
  689. end;
  690. end;
  691. function fexpand(const path : pathstr) : pathstr;
  692. var
  693. s,pa : pathstr;
  694. i,j : longint;
  695. begin
  696. getdir(0,s);
  697. i:=ioresult;
  698. if LFNSupport then
  699. begin
  700. pa:=path;
  701. end
  702. else
  703. if FileNameCaseSensitive then
  704. pa:=path
  705. else
  706. pa:=upcase(path);
  707. { allow slash as backslash }
  708. for i:=1 to length(pa) do
  709. if pa[i]='/' then
  710. pa[i]:='\';
  711. if (length(pa)>1) and (pa[2]=':') and (pa[1] in ['A'..'Z','a'..'z']) then
  712. begin
  713. { Always uppercase driveletter }
  714. if (pa[1] in ['a'..'z']) then
  715. pa[1]:=Chr(Ord(Pa[1])-32);
  716. { we must get the right directory }
  717. getdir(ord(pa[1])-ord('A')+1,s);
  718. i:=ioresult;
  719. if (ord(pa[0])>2) and (pa[3]<>'\') then
  720. if pa[1]=s[1] then
  721. begin
  722. { remove ending slash if it already exists }
  723. if s[length(s)]='\' then
  724. dec(s[0]);
  725. pa:=s+'\'+copy (pa,3,length(pa));
  726. end
  727. else
  728. pa:=pa[1]+':\'+copy (pa,3,length(pa))
  729. end
  730. else
  731. if pa[1]='\' then
  732. begin
  733. { Do not touch Network drive names if LFNSupport is true }
  734. if not ((Length(pa)>1) and (pa[2]='\') and LFNSupport) then
  735. pa:=s[1]+':'+pa;
  736. end
  737. else if s[0]=#3 then
  738. pa:=s+pa
  739. else
  740. pa:=s+'\'+pa;
  741. { Turbo Pascal gives current dir on drive if only drive given as parameter! }
  742. if length(pa) = 2 then
  743. begin
  744. getdir(byte(pa[1])-64,s);
  745. pa := s;
  746. end;
  747. {First remove all references to '\.\'}
  748. while pos ('\.\',pa)<>0 do
  749. delete (pa,pos('\.\',pa),2);
  750. {Now remove also all references to '\..\' + of course previous dirs..}
  751. repeat
  752. i:=pos('\..\',pa);
  753. if i<>0 then
  754. begin
  755. j:=i-1;
  756. while (j>1) and (pa[j]<>'\') do
  757. dec (j);
  758. if pa[j+1] = ':' then j := 3;
  759. delete (pa,j,i-j+3);
  760. end;
  761. until i=0;
  762. { Turbo Pascal gets rid of a \.. at the end of the path }
  763. { Now remove also any reference to '\..' at end of line
  764. + of course previous dir.. }
  765. i:=pos('\..',pa);
  766. if i<>0 then
  767. begin
  768. if i = length(pa) - 2 then
  769. begin
  770. j:=i-1;
  771. while (j>1) and (pa[j]<>'\') do
  772. dec (j);
  773. delete (pa,j,i-j+3);
  774. end;
  775. pa := pa + '\';
  776. end;
  777. { Remove End . and \}
  778. if (length(pa)>0) and (pa[length(pa)]='.') then
  779. dec(byte(pa[0]));
  780. { if only the drive + a '\' is left then the '\' should be left to prevtn the program
  781. accessing the current directory on the drive rather than the root!}
  782. { if the last char of path = '\' then leave it in as this is what TP does! }
  783. if ((length(pa)>3) and (pa[length(pa)]='\')) and (path[length(path)] <> '\') then
  784. dec(byte(pa[0]));
  785. { if only a drive is given in path then there should be a '\' at the
  786. end of the string given back }
  787. if length(pa) = 2 then pa := pa + '\';
  788. fexpand:=pa;
  789. end;
  790. Function FSearch(path: pathstr; dirlist: string): pathstr;
  791. var
  792. i,p1 : longint;
  793. s : searchrec;
  794. newdir : pathstr;
  795. begin
  796. { check if the file specified exists }
  797. findfirst(path,anyfile,s);
  798. if doserror=0 then
  799. begin
  800. findclose(s);
  801. fsearch:=path;
  802. exit;
  803. end;
  804. { No wildcards allowed in these things }
  805. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  806. fsearch:=''
  807. else
  808. begin
  809. { allow slash as backslash }
  810. for i:=1 to length(dirlist) do
  811. if dirlist[i]='/' then dirlist[i]:='\';
  812. repeat
  813. p1:=pos(';',dirlist);
  814. if p1<>0 then
  815. begin
  816. newdir:=copy(dirlist,1,p1-1);
  817. delete(dirlist,1,p1);
  818. end
  819. else
  820. begin
  821. newdir:=dirlist;
  822. dirlist:='';
  823. end;
  824. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  825. newdir:=newdir+'\';
  826. findfirst(newdir+path,anyfile,s);
  827. if doserror=0 then
  828. newdir:=newdir+path
  829. else
  830. newdir:='';
  831. until (dirlist='') or (newdir<>'');
  832. fsearch:=newdir;
  833. end;
  834. findclose(s);
  835. end;
  836. { change to short filename if successful DOS call PM }
  837. function GetShortName(var p : String) : boolean;
  838. var
  839. c : array[0..255] of char;
  840. begin
  841. move(p[1],c[0],length(p));
  842. c[length(p)]:=#0;
  843. copytodos(c,length(p)+1);
  844. dosregs.ax:=$7160;
  845. dosregs.cx:=1;
  846. dosregs.ds:=tb_segment;
  847. dosregs.si:=tb_offset;
  848. dosregs.es:=tb_segment;
  849. dosregs.di:=tb_offset;
  850. msdos(dosregs);
  851. LoadDosError;
  852. if DosError=0 then
  853. begin
  854. copyfromdos(c,255);
  855. move(c[0],p[1],strlen(c));
  856. p[0]:=char(strlen(c));
  857. GetShortName:=true;
  858. end
  859. else
  860. GetShortName:=false;
  861. end;
  862. { change to long filename if successful DOS call PM }
  863. function GetLongName(var p : String) : boolean;
  864. var
  865. c : array[0..255] of char;
  866. begin
  867. move(p[1],c[0],length(p));
  868. c[length(p)]:=#0;
  869. copytodos(c,length(p)+1);
  870. dosregs.ax:=$7160;
  871. dosregs.cx:=2;
  872. dosregs.ds:=tb_segment;
  873. dosregs.si:=tb_offset;
  874. dosregs.es:=tb_segment;
  875. dosregs.di:=tb_offset;
  876. msdos(dosregs);
  877. LoadDosError;
  878. if DosError=0 then
  879. begin
  880. copyfromdos(c,255);
  881. move(c[0],p[1],strlen(c));
  882. p[0]:=char(strlen(c));
  883. GetLongName:=true;
  884. end
  885. else
  886. GetLongName:=false;
  887. end;
  888. {******************************************************************************
  889. --- Get/Set File Time,Attr ---
  890. ******************************************************************************}
  891. procedure getftime(var f;var time : longint);
  892. begin
  893. dosregs.bx:=textrec(f).handle;
  894. dosregs.ax:=$5700;
  895. msdos(dosregs);
  896. loaddoserror;
  897. time:=(dosregs.dx shl 16)+dosregs.cx;
  898. end;
  899. procedure setftime(var f;time : longint);
  900. begin
  901. dosregs.bx:=textrec(f).handle;
  902. dosregs.cx:=time and $ffff;
  903. dosregs.dx:=time shr 16;
  904. dosregs.ax:=$5701;
  905. msdos(dosregs);
  906. loaddoserror;
  907. end;
  908. procedure getfattr(var f;var attr : word);
  909. begin
  910. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  911. dosregs.edx:=tb_offset;
  912. dosregs.ds:=tb_segment;
  913. if LFNSupport then
  914. begin
  915. dosregs.ax:=$7143;
  916. dosregs.bx:=0;
  917. end
  918. else
  919. dosregs.ax:=$4300;
  920. msdos(dosregs);
  921. LoadDosError;
  922. Attr:=dosregs.cx;
  923. end;
  924. procedure setfattr(var f;attr : word);
  925. begin
  926. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  927. dosregs.edx:=tb_offset;
  928. dosregs.ds:=tb_segment;
  929. if LFNSupport then
  930. begin
  931. dosregs.ax:=$7143;
  932. dosregs.bx:=1;
  933. end
  934. else
  935. dosregs.ax:=$4301;
  936. dosregs.cx:=attr;
  937. msdos(dosregs);
  938. LoadDosError;
  939. end;
  940. {******************************************************************************
  941. --- Environment ---
  942. ******************************************************************************}
  943. function envcount : longint;
  944. var
  945. hp : ppchar;
  946. begin
  947. hp:=envp;
  948. envcount:=0;
  949. while assigned(hp^) do
  950. begin
  951. inc(envcount);
  952. inc(hp);
  953. end;
  954. end;
  955. function envstr(index : integer) : string;
  956. begin
  957. if (index<=0) or (index>envcount) then
  958. begin
  959. envstr:='';
  960. exit;
  961. end;
  962. envstr:=strpas(ppchar(pointer(envp)+4*(index-1))^);
  963. end;
  964. Function GetEnv(envvar: string): string;
  965. var
  966. hp : ppchar;
  967. hs : string;
  968. eqpos : longint;
  969. begin
  970. envvar:=upcase(envvar);
  971. hp:=envp;
  972. getenv:='';
  973. while assigned(hp^) do
  974. begin
  975. hs:=strpas(hp^);
  976. eqpos:=pos('=',hs);
  977. if upcase(copy(hs,1,eqpos-1))=envvar then
  978. begin
  979. getenv:=copy(hs,eqpos+1,255);
  980. exit;
  981. end;
  982. inc(hp);
  983. end;
  984. end;
  985. {******************************************************************************
  986. --- Not Supported ---
  987. ******************************************************************************}
  988. Procedure keep(exitcode : word);
  989. Begin
  990. End;
  991. Procedure getintvec(intno : byte;var vector : pointer);
  992. Begin
  993. End;
  994. Procedure setintvec(intno : byte;vector : pointer);
  995. Begin
  996. End;
  997. end.
  998. {
  999. $Log$
  1000. Revision 1.10 2000-10-11 15:38:03 peter
  1001. * diskfree doserror fix (merged)
  1002. Revision 1.9 2000/09/06 20:47:34 peter
  1003. * removed previous fsplit() patch as it's not the correct behaviour for
  1004. LFNs. The code showing the bug could easily be adapted (merged)
  1005. Revision 1.8 2000/09/04 20:17:53 peter
  1006. * fixed previous commit (merged)
  1007. Revision 1.7 2000/09/04 19:38:12 peter
  1008. * fsplit with .. fix from Thomas (merged)
  1009. Revision 1.6 2000/08/04 21:45:39 peter
  1010. * getenv case insensitive (merged)
  1011. Revision 1.4 2000/07/22 12:24:55 jonas
  1012. * merged dossearchrec2searchrec() fix from fixes branch
  1013. Revision 1.3 2000/07/14 10:33:09 michael
  1014. + Conditionals fixed
  1015. Revision 1.2 2000/07/13 11:33:39 michael
  1016. + removed logs
  1017. }