dos.pp 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Dos unit for BP7 compatible RTL
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$inline on}
  12. unit dos;
  13. interface
  14. Type
  15. searchrec = packed record
  16. fill : array[1..21] of byte;
  17. attr : byte;
  18. time : longint;
  19. { reserved : word; not in DJGPP V2 }
  20. size : longint;
  21. name : string[255]; { LFN Name, DJGPP uses only [12] but more can't hurt (PFV) }
  22. end;
  23. {$DEFINE HAS_REGISTERS}
  24. {$I registers.inc}
  25. {$i dosh.inc}
  26. {$IfDef SYSTEM_DEBUG_STARTUP}
  27. {$DEFINE FORCE_PROXY}
  28. {$endif SYSTEM_DEBUG_STARTUP}
  29. Const
  30. { This variable can be set to true
  31. to force use of !proxy command lines even for short
  32. strings, for debugging purposes mainly, as
  33. this might have negative impact if trying to
  34. call non-go32v2 programs }
  35. force_go32v2_proxy : boolean =
  36. {$ifdef FORCE_PROXY}
  37. true;
  38. {$DEFINE DEBUG_PROXY}
  39. {$else not FORCE_PROXY}
  40. false;
  41. {$endif not FORCE_PROXY}
  42. { This variable allows to use !proxy if command line is
  43. longer than 126 characters.
  44. This will only work if the called program knows how to handle
  45. those command lines.
  46. Luckily this is the case for Free Pascal compiled
  47. programs (even old versions)
  48. and go32v2 DJGPP programs.
  49. You can set this to false to get a warning to stderr
  50. if command line is too long. }
  51. Use_go32v2_proxy : boolean = true;
  52. { Added to interface so that there is no need to implement it
  53. both in dos and sysutils units }
  54. procedure exec_ansistring(path : string;comline : ansistring);
  55. procedure Intr(IntNo: Byte; var Regs: Registers); external name 'FPC_INTR';
  56. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  57. implementation
  58. uses
  59. strings;
  60. type
  61. PFarByte = ^Byte;far;
  62. PFarChar = ^Char;far;
  63. PFarWord = ^Word;far;
  64. {$DEFINE HAS_GETMSCOUNT}
  65. {$DEFINE HAS_INTR}
  66. {$DEFINE HAS_SETCBREAK}
  67. {$DEFINE HAS_GETCBREAK}
  68. {$DEFINE HAS_SETVERIFY}
  69. {$DEFINE HAS_GETVERIFY}
  70. {$DEFINE HAS_SWAPVECTORS}
  71. {$DEFINE HAS_GETINTVEC}
  72. {$DEFINE HAS_SETINTVEC}
  73. {$DEFINE HAS_KEEP}
  74. {$DEFINE HAS_GETSHORTNAME}
  75. {$DEFINE HAS_GETLONGNAME}
  76. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  77. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  78. {$I dos.inc}
  79. {******************************************************************************
  80. --- Dos Interrupt ---
  81. ******************************************************************************}
  82. var
  83. dosregs : registers;
  84. procedure LoadDosError;
  85. var
  86. r : registers;
  87. SimpleDosError : word;
  88. begin
  89. if (dosregs.flags and fcarry) <> 0 then
  90. begin
  91. { I got a extended error = 0
  92. while CarryFlag was set from Exec function }
  93. SimpleDosError:=dosregs.ax;
  94. r.ax:=$5900;
  95. r.bx:=$0;
  96. intr($21,r);
  97. { conversion from word to integer !!
  98. gave a Bound check error if ax is $FFFF !! PM }
  99. doserror:=integer(r.ax);
  100. case doserror of
  101. 0 : DosError:=integer(SimpleDosError);
  102. 19 : DosError:=150;
  103. 21 : DosError:=152;
  104. end;
  105. end
  106. else
  107. doserror:=0;
  108. end;
  109. {******************************************************************************
  110. --- Info / Date / Time ---
  111. ******************************************************************************}
  112. function dosversion : word;
  113. begin
  114. dosregs.ax:=$3000;
  115. msdos(dosregs);
  116. dosversion:=dosregs.ax;
  117. end;
  118. procedure getdate(var year,month,mday,wday : word);
  119. begin
  120. dosregs.ax:=$2a00;
  121. msdos(dosregs);
  122. wday:=dosregs.al;
  123. year:=dosregs.cx;
  124. month:=dosregs.dh;
  125. mday:=dosregs.dl;
  126. end;
  127. procedure setdate(year,month,day : word);
  128. begin
  129. dosregs.cx:=year;
  130. dosregs.dh:=month;
  131. dosregs.dl:=day;
  132. dosregs.ah:=$2b;
  133. msdos(dosregs);
  134. end;
  135. procedure gettime(var hour,minute,second,sec100 : word);
  136. begin
  137. dosregs.ah:=$2c;
  138. msdos(dosregs);
  139. hour:=dosregs.ch;
  140. minute:=dosregs.cl;
  141. second:=dosregs.dh;
  142. sec100:=dosregs.dl;
  143. end;
  144. procedure settime(hour,minute,second,sec100 : word);
  145. begin
  146. dosregs.ch:=hour;
  147. dosregs.cl:=minute;
  148. dosregs.dh:=second;
  149. dosregs.dl:=sec100;
  150. dosregs.ah:=$2d;
  151. msdos(dosregs);
  152. end;
  153. function GetMsCount: int64;
  154. begin
  155. GetMsCount := int64 (MemL [$40:$6c]) * 55;
  156. end;
  157. {******************************************************************************
  158. --- Exec ---
  159. ******************************************************************************}
  160. const
  161. DOS_MAX_COMMAND_LINE_LENGTH = 126;
  162. procedure exec_ansistring(path : string;comline : ansistring);
  163. type
  164. realptr = packed record
  165. ofs,seg : word;
  166. end;
  167. texecblock = packed record
  168. envseg : word;
  169. comtail : realptr;
  170. firstFCB : realptr;
  171. secondFCB : realptr;
  172. iniStack : realptr;
  173. iniCSIP : realptr;
  174. end;
  175. var
  176. execblock : texecblock;
  177. c : ansistring;
  178. p : string;
  179. arg_ofs : integer;
  180. fcb1 : array [0..15] of byte;
  181. fcb2 : array [0..15] of byte;
  182. begin
  183. { create command line }
  184. c:=comline;
  185. if length(c)>DOS_MAX_COMMAND_LINE_LENGTH then
  186. begin
  187. writeln(stderr,'Dos.exec command line truncated to ',
  188. DOS_MAX_COMMAND_LINE_LENGTH,' chars');
  189. writeln(stderr,'Before: "',c,'"');
  190. setlength(c, DOS_MAX_COMMAND_LINE_LENGTH);
  191. writeln(stderr,'After: "',c,'"');
  192. end;
  193. p:=path;
  194. { allow slash as backslash }
  195. DoDirSeparators(p);
  196. if LFNSupport then
  197. GetShortName(p);
  198. { allocate FCB see dosexec code }
  199. arg_ofs:=1;
  200. while (c[arg_ofs] in [' ',#9]) and
  201. (arg_ofs<length(c)) do
  202. inc(arg_ofs);
  203. dosregs.ax:=$2901;
  204. dosregs.ds:=Seg(c[arg_ofs]);
  205. dosregs.si:=Ofs(c[arg_ofs]);
  206. dosregs.es:=Seg(fcb1);
  207. dosregs.di:=Ofs(fcb1);
  208. msdos(dosregs);
  209. { allocate second FCB see dosexec code }
  210. dosregs.ax:=$2901;
  211. dosregs.ds:=Seg(c[arg_ofs]);
  212. dosregs.si:=Ofs(c[arg_ofs]);
  213. dosregs.es:=Seg(fcb2);
  214. dosregs.di:=Ofs(fcb2);
  215. msdos(dosregs);
  216. c := Chr(Length(c)) + c + #13 + #0;
  217. with execblock do
  218. begin
  219. envseg:={la_env shr 4}0;
  220. comtail.seg:=Seg(c[1]);
  221. comtail.ofs:=Ofs(c[1]);
  222. firstFCB.seg:=Seg(fcb1);
  223. firstFCB.ofs:=Ofs(fcb1);
  224. secondFCB.seg:=Seg(fcb2);
  225. secondFCB.ofs:=Ofs(fcb2);
  226. end;
  227. p := p + #0;
  228. dosregs.dx:=Ofs(p[1]);
  229. dosregs.ds:=Seg(p[1]);
  230. dosregs.bx:=Ofs(execblock);
  231. dosregs.es:=Seg(execblock);
  232. dosregs.ax:=$4b00;
  233. msdos(dosregs);
  234. LoadDosError;
  235. if DosError=0 then
  236. begin
  237. dosregs.ax:=$4d00;
  238. msdos(dosregs);
  239. LastDosExitCode:=DosRegs.al
  240. end
  241. else
  242. LastDosExitCode:=0;
  243. end;
  244. procedure exec(const path : pathstr;const comline : comstr);
  245. begin
  246. exec_ansistring(path, comline);
  247. end;
  248. procedure getcbreak(var breakvalue : boolean);
  249. begin
  250. dosregs.ax:=$3300;
  251. msdos(dosregs);
  252. breakvalue:=dosregs.dl<>0;
  253. end;
  254. procedure setcbreak(breakvalue : boolean);
  255. begin
  256. dosregs.ax:=$3301;
  257. dosregs.dl:=ord(breakvalue);
  258. msdos(dosregs);
  259. end;
  260. procedure getverify(var verify : boolean);
  261. begin
  262. dosregs.ah:=$54;
  263. msdos(dosregs);
  264. verify:=dosregs.al<>0;
  265. end;
  266. procedure setverify(verify : boolean);
  267. begin
  268. dosregs.ah:=$2e;
  269. dosregs.al:=ord(verify);
  270. msdos(dosregs);
  271. end;
  272. {******************************************************************************
  273. --- Disk ---
  274. ******************************************************************************}
  275. type
  276. ExtendedFat32FreeSpaceRec = packed record
  277. RetSize : word; { $00 }
  278. Strucversion : word; { $02 }
  279. SecPerClus, { $04 }
  280. BytePerSec, { $08 }
  281. AvailClusters, { $0C }
  282. TotalClusters, { $10 }
  283. AvailPhysSect, { $14 }
  284. TotalPhysSect, { $18 }
  285. AvailAllocUnits, { $1C }
  286. TotalAllocUnits : longword; { $20 }
  287. Dummy, { $24 }
  288. Dummy2 : longword; { $28 }
  289. end; { $2C }
  290. const
  291. IOCTL_INPUT = 3; //For request header command field
  292. CDFUNC_SECTSIZE = 7; //For cdrom control block func field
  293. CDFUNC_VOLSIZE = 8; //For cdrom control block func field
  294. type
  295. TRequestHeader = packed record
  296. length : byte; { $00 }
  297. subunit : byte; { $01 }
  298. command : byte; { $02 }
  299. status : word; { $03 }
  300. reserved1 : longword; { $05 }
  301. reserved2 : longword; { $09 }
  302. media_desc : byte; { $0D }
  303. transf_ofs : word; { $0E }
  304. transf_seg : word; { $10 }
  305. numbytes : word; { $12 }
  306. end; { $14 }
  307. TCDSectSizeReq = packed record
  308. func : byte; { $00 }
  309. mode : byte; { $01 }
  310. secsize : word; { $02 }
  311. end; { $04 }
  312. TCDVolSizeReq = packed record
  313. func : byte; { $00 }
  314. size : longword; { $01 }
  315. end; { $05 }
  316. function do_diskdata(drive : byte; Free : boolean) : Int64;
  317. var
  318. blocksize, freeblocks, totblocks : longword;
  319. { Get disk data via old int21/36 (GET FREE DISK SPACE). It's always supported
  320. even if it returns wrong values for volumes > 2GB and for cdrom drives when
  321. in pure DOS. Note that it's also the only way to get some data on WinNTs. }
  322. function DiskData_36 : boolean;
  323. begin
  324. DiskData_36:=false;
  325. dosregs.dl:=drive;
  326. dosregs.ah:=$36;
  327. msdos(dosregs);
  328. if dosregs.ax=$FFFF then exit;
  329. blocksize:=dosregs.ax*dosregs.cx;
  330. freeblocks:=dosregs.bx;
  331. totblocks:=dosregs.dx;
  332. Diskdata_36:=true;
  333. end;
  334. { Get disk data via int21/7303 (FAT32 - GET EXTENDED FREE SPACE ON DRIVE).
  335. It is supported by win9x even in pure DOS }
  336. function DiskData_7303 : boolean;
  337. var
  338. s : shortstring;
  339. rec : ExtendedFat32FreeSpaceRec;
  340. begin
  341. DiskData_7303:=false;
  342. s:=chr(drive+$40)+':\'+#0;
  343. rec.Strucversion:=0;
  344. rec.RetSize := 0;
  345. dosregs.dx:=Ofs(s[1]);
  346. dosregs.ds:=Seg(s[1]);
  347. dosregs.di:=Ofs(Rec);
  348. dosregs.es:=Seg(Rec);
  349. dosregs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  350. dosregs.ax:=$7303;
  351. msdos(dosregs);
  352. if (dosregs.flags and fcarry) <> 0 then
  353. exit;
  354. if Rec.RetSize = 0 then
  355. exit;
  356. blocksize:=rec.SecPerClus*rec.BytePerSec;
  357. freeblocks:=rec.AvailAllocUnits;
  358. totblocks:=rec.TotalAllocUnits;
  359. DiskData_7303:=true;
  360. end;
  361. { Get disk data asking to MSCDEX. Pure DOS returns wrong values with
  362. int21/7303 or int21/36 if the drive is a CDROM drive }
  363. function DiskData_CDROM : boolean;
  364. var req : TRequestHeader;
  365. sectreq : TCDSectSizeReq;
  366. sizereq : TCDVolSizeReq;
  367. i : integer;
  368. drnum : byte;
  369. begin
  370. DiskData_CDROM:=false;
  371. drnum:=drive-1; //for MSCDEX, 0 = a, 1 = b etc, unlike int21/36
  372. { Is this a CDROM drive? }
  373. dosregs.ax:=$150b;
  374. dosregs.cx:=drnum;
  375. intr($2f,dosregs);
  376. if (dosregs.bx<>$ADAD) or (dosregs.ax=0) then
  377. exit; // no, it isn't
  378. { Prepare the request header to send to the cdrom driver }
  379. FillByte(req,sizeof(req),0);
  380. req.length:=sizeof(req);
  381. req.command:=IOCTL_INPUT;
  382. req.transf_ofs:=Ofs(sectreq);
  383. req.transf_seg:=Seg(sectreq);
  384. req.numbytes:=sizeof(sectreq);
  385. { We're asking the sector size }
  386. sectreq.func:=CDFUNC_SECTSIZE;
  387. sectreq.mode:=0; //cooked
  388. sectreq.secsize:=0;
  389. for i:=1 to 2 do
  390. begin
  391. { Send the request to the cdrom driver }
  392. dosregs.ax:=$1510;
  393. dosregs.cx:=drnum;
  394. dosregs.es:=Seg(req);
  395. dosregs.bx:=Ofs(req);
  396. intr($2f,dosregs);
  397. { status = $800F means "disk changed". Try once more. }
  398. if (req.status and $800F) <> $800F then break;
  399. end;
  400. if (req.status<>$0100) or (req.numbytes<>sizeof(sectreq)) then
  401. exit; //An error occurred
  402. { Update the request header for the next request }
  403. FillByte(req,sizeof(req),0);
  404. req.length:=sizeof(req);
  405. req.command:=IOCTL_INPUT;
  406. req.transf_ofs:=Ofs(sizereq);
  407. req.transf_seg:=Seg(sizereq);
  408. req.numbytes:=sizeof(sizereq);
  409. { We're asking the volume size (in blocks) }
  410. sizereq.func:=CDFUNC_VOLSIZE;
  411. sizereq.size:=0;
  412. { Send the request to the cdrom driver }
  413. dosregs.ax:=$1510;
  414. dosregs.cx:=drnum;
  415. dosregs.es:=Seg(req);
  416. dosregs.bx:=Ofs(req);
  417. intr($2f,dosregs);
  418. if (req.status<>$0100) or (req.numbytes<>sizeof(sizereq)) then
  419. exit; //An error occurred
  420. blocksize:=sectreq.secsize;
  421. freeblocks:=0; //always 0 for a cdrom
  422. totblocks:=sizereq.size;
  423. DiskData_CDROM:=true;
  424. end;
  425. begin
  426. if drive=0 then
  427. begin
  428. dosregs.ax:=$1900; //get current default drive
  429. msdos(dosregs);
  430. drive:=dosregs.al+1;
  431. end;
  432. if not DiskData_CDROM then
  433. if not DiskData_7303 then
  434. if not DiskData_36 then
  435. begin
  436. do_diskdata:=-1;
  437. exit;
  438. end;
  439. do_diskdata:=blocksize;
  440. if free then
  441. do_diskdata:=do_diskdata*freeblocks
  442. else
  443. do_diskdata:=do_diskdata*totblocks;
  444. end;
  445. function diskfree(drive : byte) : int64;
  446. begin
  447. diskfree:=Do_DiskData(drive,TRUE);
  448. end;
  449. function disksize(drive : byte) : int64;
  450. begin
  451. disksize:=Do_DiskData(drive,false);
  452. end;
  453. {******************************************************************************
  454. --- LFNFindfirst LFNFindNext ---
  455. ******************************************************************************}
  456. type
  457. LFNSearchRec=packed record
  458. attr,
  459. crtime,
  460. crtimehi,
  461. actime,
  462. actimehi,
  463. lmtime,
  464. lmtimehi,
  465. sizehi,
  466. size : longint;
  467. reserved : array[0..7] of byte;
  468. name : array[0..259] of byte;
  469. shortname : array[0..13] of byte;
  470. end;
  471. procedure LFNSearchRec2Dos(const w:LFNSearchRec;hdl:longint;var d:Searchrec;from_findfirst : boolean);
  472. var
  473. Len : integer;
  474. begin
  475. With w do
  476. begin
  477. FillChar(d,sizeof(SearchRec),0);
  478. if DosError=0 then
  479. len:=StrLen(@Name)
  480. else
  481. len:=0;
  482. d.Name[0]:=chr(len);
  483. Move(Name[0],d.Name[1],Len);
  484. d.Time:=lmTime;
  485. d.Size:=Size;
  486. d.Attr:=Attr and $FF;
  487. if (DosError<>0) and from_findfirst then
  488. hdl:=-1;
  489. Move(hdl,d.Fill,4);
  490. end;
  491. end;
  492. {$ifdef DEBUG_LFN}
  493. const
  494. LFNFileName : string = 'LFN.log';
  495. LFNOpenNb : longint = 0;
  496. LogLFN : boolean = false;
  497. var
  498. lfnfile : text;
  499. {$endif DEBUG_LFN}
  500. procedure LFNFindFirst(path:pchar;attr:longint;var s:searchrec);
  501. var
  502. w : LFNSearchRec;
  503. begin
  504. { allow slash as backslash }
  505. DoDirSeparators(path);
  506. dosregs.si:=1; { use ms-dos time }
  507. { don't include the label if not asked for it, needed for network drives }
  508. if attr=$8 then
  509. dosregs.cx:=8
  510. else
  511. dosregs.cx:=attr and (not 8);
  512. dosregs.dx:=Ofs(path^);
  513. dosregs.ds:=Seg(path^);
  514. dosregs.di:=Ofs(w);
  515. dosregs.es:=Seg(w);
  516. dosregs.ax:=$714e;
  517. msdos(dosregs);
  518. LoadDosError;
  519. if DosError=2 then
  520. DosError:=18;
  521. {$ifdef DEBUG_LFN}
  522. if (DosError=0) and LogLFN then
  523. begin
  524. Append(lfnfile);
  525. inc(LFNOpenNb);
  526. Writeln(lfnfile,LFNOpenNb,' LFNFindFirst called ',path);
  527. close(lfnfile);
  528. end;
  529. {$endif DEBUG_LFN}
  530. LFNSearchRec2Dos(w,dosregs.ax,s,true);
  531. end;
  532. procedure LFNFindNext(var s:searchrec);
  533. var
  534. hdl : longint;
  535. w : LFNSearchRec;
  536. begin
  537. Move(s.Fill,hdl,4);
  538. dosregs.si:=1; { use ms-dos time }
  539. dosregs.di:=Ofs(w);
  540. dosregs.es:=Seg(w);
  541. dosregs.bx:=hdl;
  542. dosregs.ax:=$714f;
  543. msdos(dosregs);
  544. LoadDosError;
  545. LFNSearchRec2Dos(w,hdl,s,false);
  546. end;
  547. procedure LFNFindClose(var s:searchrec);
  548. var
  549. hdl : longint;
  550. begin
  551. Move(s.Fill,hdl,4);
  552. { Do not call MsDos if FindFirst returned with an error }
  553. if hdl=-1 then
  554. begin
  555. DosError:=0;
  556. exit;
  557. end;
  558. dosregs.bx:=hdl;
  559. dosregs.ax:=$71a1;
  560. msdos(dosregs);
  561. LoadDosError;
  562. {$ifdef DEBUG_LFN}
  563. if (DosError=0) and LogLFN then
  564. begin
  565. Append(lfnfile);
  566. Writeln(lfnfile,LFNOpenNb,' LFNFindClose called ');
  567. close(lfnfile);
  568. if LFNOpenNb>0 then
  569. dec(LFNOpenNb);
  570. end;
  571. {$endif DEBUG_LFN}
  572. end;
  573. {******************************************************************************
  574. --- DosFindfirst DosFindNext ---
  575. ******************************************************************************}
  576. procedure dossearchrec2searchrec(var f : searchrec);
  577. var
  578. len : integer;
  579. begin
  580. { Check is necessary!! OS/2's VDM doesn't clear the name with #0 if the }
  581. { file doesn't exist! (JM) }
  582. if dosError = 0 then
  583. len:=StrLen(@f.Name)
  584. else len := 0;
  585. Move(f.Name[0],f.Name[1],Len);
  586. f.Name[0]:=chr(len);
  587. end;
  588. procedure DosFindfirst(path : pchar;attr : word;var f : searchrec);
  589. begin
  590. { allow slash as backslash }
  591. DoDirSeparators(path);
  592. dosregs.dx:=Ofs(f);
  593. dosregs.ds:=Seg(f);
  594. dosregs.ah:=$1a;
  595. msdos(dosregs);
  596. dosregs.cx:=attr;
  597. dosregs.dx:=Ofs(path^);
  598. dosregs.ds:=Seg(path^);
  599. dosregs.ah:=$4e;
  600. msdos(dosregs);
  601. LoadDosError;
  602. dossearchrec2searchrec(f);
  603. end;
  604. procedure Dosfindnext(var f : searchrec);
  605. begin
  606. dosregs.dx:=Ofs(f);
  607. dosregs.ds:=Seg(f);
  608. dosregs.ah:=$1a;
  609. msdos(dosregs);
  610. dosregs.ah:=$4f;
  611. msdos(dosregs);
  612. LoadDosError;
  613. dossearchrec2searchrec(f);
  614. end;
  615. {******************************************************************************
  616. --- Findfirst FindNext ---
  617. ******************************************************************************}
  618. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  619. var
  620. path0 : array[0..255] of char;
  621. begin
  622. doserror:=0;
  623. strpcopy(path0,path);
  624. if LFNSupport then
  625. LFNFindFirst(path0,attr,f)
  626. else
  627. Dosfindfirst(path0,attr,f);
  628. end;
  629. procedure findnext(var f : searchRec);
  630. begin
  631. doserror:=0;
  632. if LFNSupport then
  633. LFNFindnext(f)
  634. else
  635. Dosfindnext(f);
  636. end;
  637. Procedure FindClose(Var f: SearchRec);
  638. begin
  639. DosError:=0;
  640. if LFNSupport then
  641. LFNFindClose(f);
  642. end;
  643. procedure SwapIntVec(IntNo: Byte; var Vector: FarPointer);
  644. var
  645. tmpvec: FarPointer;
  646. begin
  647. GetIntVec(IntNo, tmpvec);
  648. SetIntVec(IntNo, Vector);
  649. Vector := tmpvec;
  650. end;
  651. procedure SwapVectors;
  652. begin
  653. SwapIntVec(0, SaveInt00);
  654. end;
  655. {******************************************************************************
  656. --- File ---
  657. ******************************************************************************}
  658. Function FSearch(path: pathstr; dirlist: string): pathstr;
  659. var
  660. p1 : integer;
  661. s : searchrec;
  662. newdir : pathstr;
  663. begin
  664. { check if the file specified exists }
  665. findfirst(path,anyfile and not(directory),s);
  666. if doserror=0 then
  667. begin
  668. findclose(s);
  669. fsearch:=path;
  670. exit;
  671. end;
  672. { No wildcards allowed in these things }
  673. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  674. fsearch:=''
  675. else
  676. begin
  677. { allow slash as backslash }
  678. DoDirSeparators(dirlist);
  679. repeat
  680. p1:=pos(';',dirlist);
  681. if p1<>0 then
  682. begin
  683. newdir:=copy(dirlist,1,p1-1);
  684. delete(dirlist,1,p1);
  685. end
  686. else
  687. begin
  688. newdir:=dirlist;
  689. dirlist:='';
  690. end;
  691. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  692. newdir:=newdir+'\';
  693. findfirst(newdir+path,anyfile and not(directory),s);
  694. if doserror=0 then
  695. newdir:=newdir+path
  696. else
  697. newdir:='';
  698. until (dirlist='') or (newdir<>'');
  699. fsearch:=newdir;
  700. end;
  701. findclose(s);
  702. end;
  703. { change to short filename if successful DOS call PM }
  704. function GetShortName(var p : String) : boolean;
  705. var
  706. c : array[0..255] of char;
  707. begin
  708. move(p[1],c[0],length(p));
  709. c[length(p)]:=#0;
  710. dosregs.ax:=$7160;
  711. dosregs.cx:=1;
  712. dosregs.ds:=Seg(c);
  713. dosregs.si:=Ofs(c);
  714. dosregs.es:=Seg(c);
  715. dosregs.di:=Ofs(c);
  716. msdos(dosregs);
  717. LoadDosError;
  718. if DosError=0 then
  719. begin
  720. move(c[0],p[1],strlen(c));
  721. p[0]:=char(strlen(c));
  722. GetShortName:=true;
  723. end
  724. else
  725. GetShortName:=false;
  726. end;
  727. { change to long filename if successful DOS call PM }
  728. function GetLongName(var p : String) : boolean;
  729. var
  730. c : array[0..260] of char;
  731. begin
  732. move(p[1],c[0],length(p));
  733. c[length(p)]:=#0;
  734. dosregs.ax:=$7160;
  735. dosregs.cx:=2;
  736. dosregs.ds:=Seg(c);
  737. dosregs.si:=Ofs(c);
  738. dosregs.es:=Seg(c);
  739. dosregs.di:=Ofs(c);
  740. msdos(dosregs);
  741. LoadDosError;
  742. if DosError=0 then
  743. begin
  744. c[255]:=#0;
  745. move(c[0],p[1],strlen(c));
  746. p[0]:=char(strlen(c));
  747. GetLongName:=true;
  748. end
  749. else
  750. GetLongName:=false;
  751. end;
  752. {******************************************************************************
  753. --- Get/Set File Time,Attr ---
  754. ******************************************************************************}
  755. procedure getftime(var f;var time : longint);
  756. begin
  757. dosregs.bx:=textrec(f).handle;
  758. dosregs.ax:=$5700;
  759. msdos(dosregs);
  760. loaddoserror;
  761. time:=(dosregs.dx shl 16)+dosregs.cx;
  762. end;
  763. procedure setftime(var f;time : longint);
  764. begin
  765. dosregs.bx:=textrec(f).handle;
  766. dosregs.cx:=time and $ffff;
  767. dosregs.dx:=time shr 16;
  768. dosregs.ax:=$5701;
  769. msdos(dosregs);
  770. loaddoserror;
  771. end;
  772. procedure getfattr(var f;var attr : word);
  773. var
  774. path: pchar;
  775. {$ifndef FPC_ANSI_TEXTFILEREC}
  776. r: rawbytestring;
  777. {$endif not FPC_ANSI_TEXTFILEREC}
  778. begin
  779. {$ifdef FPC_ANSI_TEXTFILEREC}
  780. path:=@filerec(f).Name;
  781. {$else}
  782. r:=ToSingleByteFileSystemEncodedFileName(filerec(f).Name);
  783. path:=pchar(r);
  784. {$endif}
  785. dosregs.dx:=Ofs(path^);
  786. dosregs.ds:=Seg(path^);
  787. if LFNSupport then
  788. begin
  789. dosregs.ax:=$7143;
  790. dosregs.bx:=0;
  791. end
  792. else
  793. dosregs.ax:=$4300;
  794. msdos(dosregs);
  795. LoadDosError;
  796. Attr:=dosregs.cx;
  797. end;
  798. procedure setfattr(var f;attr : word);
  799. var
  800. path: pchar;
  801. {$ifndef FPC_ANSI_TEXTFILEREC}
  802. r: rawbytestring;
  803. {$endif not FPC_ANSI_TEXTFILEREC}
  804. begin
  805. { Fail for setting VolumeId. }
  806. if ((attr and VolumeID)<>0) then
  807. begin
  808. doserror:=5;
  809. exit;
  810. end;
  811. {$ifdef FPC_ANSI_TEXTFILEREC}
  812. path:=@filerec(f).Name;
  813. {$else}
  814. r:=ToSingleByteFileSystemEncodedFileName(filerec(f).Name);
  815. path:=pchar(r);
  816. {$endif}
  817. dosregs.dx:=Ofs(path);
  818. dosregs.ds:=Seg(path);
  819. if LFNSupport then
  820. begin
  821. dosregs.ax:=$7143;
  822. dosregs.bx:=1;
  823. end
  824. else
  825. dosregs.ax:=$4301;
  826. dosregs.cx:=attr;
  827. msdos(dosregs);
  828. LoadDosError;
  829. end;
  830. {******************************************************************************
  831. --- Environment ---
  832. ******************************************************************************}
  833. function GetEnvStr(EnvNo: Integer; var OutEnvStr: string): integer;
  834. var
  835. dos_env_seg: Word;
  836. ofs: Word;
  837. Ch, Ch2: Char;
  838. begin
  839. dos_env_seg := PFarWord(Ptr(dos_psp, $2C))^;
  840. GetEnvStr := 1;
  841. OutEnvStr := '';
  842. ofs := 0;
  843. repeat
  844. Ch := PFarChar(Ptr(dos_env_seg,ofs))^;
  845. Ch2 := PFarChar(Ptr(dos_env_seg,ofs + 1))^;
  846. if (Ch = #0) and (Ch2 = #0) then
  847. exit;
  848. if Ch = #0 then
  849. Inc(GetEnvStr);
  850. if (Ch <> #0) and (GetEnvStr = EnvNo) then
  851. OutEnvStr := OutEnvStr + Ch;
  852. Inc(ofs);
  853. if ofs = 0 then
  854. exit;
  855. until false;
  856. end;
  857. function envcount : longint;
  858. var
  859. tmpstr: string;
  860. begin
  861. envcount := GetEnvStr(-1, tmpstr);
  862. end;
  863. function envstr (Index: longint): string;
  864. begin
  865. GetEnvStr(Index, envstr);
  866. end;
  867. Function GetEnv(envvar: string): string;
  868. var
  869. hs : string;
  870. eqpos : integer;
  871. I : integer;
  872. begin
  873. envvar:=upcase(envvar);
  874. getenv:='';
  875. for I := 1 to envcount do
  876. begin
  877. hs:=envstr(I);
  878. eqpos:=pos('=',hs);
  879. if upcase(copy(hs,1,eqpos-1))=envvar then
  880. begin
  881. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  882. break;
  883. end;
  884. end;
  885. end;
  886. {******************************************************************************
  887. --- Get/SetIntVec ---
  888. ******************************************************************************}
  889. procedure GetIntVec(intno: Byte; var vector: farpointer); assembler;
  890. asm
  891. mov al, intno
  892. mov ah, 35h
  893. int 21h
  894. xchg ax, bx
  895. mov bx, vector
  896. mov [bx], ax
  897. mov ax, es
  898. mov [bx + 2], ax
  899. end;
  900. procedure SetIntVec(intno: Byte; vector: farpointer); assembler;
  901. asm
  902. push ds
  903. mov al, intno
  904. mov ah, 25h
  905. lds dx, word [vector]
  906. int 21h
  907. pop ds
  908. end;
  909. {******************************************************************************
  910. --- Keep ---
  911. ******************************************************************************}
  912. Procedure Keep(exitcode: word); assembler;
  913. asm
  914. mov bx, dos_psp
  915. dec bx
  916. mov es, bx
  917. mov dx, es:[3]
  918. mov ax, exitcode
  919. mov ah, 31h
  920. int 21h
  921. end;
  922. {$ifdef DEBUG_LFN}
  923. begin
  924. LogLFN:=(GetEnv('LOGLFN')<>'');
  925. assign(lfnfile,LFNFileName);
  926. {$I-}
  927. Reset(lfnfile);
  928. if IOResult<>0 then
  929. begin
  930. Rewrite(lfnfile);
  931. Writeln(lfnfile,'New lfn.log');
  932. end;
  933. close(lfnfile);
  934. {$endif DEBUG_LFN}
  935. end.