2
0

dos.pp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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 (arg_ofs<length(c)) and (c[arg_ofs] in [' ',#9]) do
  201. inc(arg_ofs);
  202. dosregs.ax:=$2901;
  203. dosregs.ds:=Seg(c[arg_ofs]);
  204. dosregs.si:=Ofs(c[arg_ofs]);
  205. dosregs.es:=Seg(fcb1);
  206. dosregs.di:=Ofs(fcb1);
  207. msdos(dosregs);
  208. { allocate second FCB see dosexec code }
  209. dosregs.ax:=$2901;
  210. dosregs.ds:=Seg(c[arg_ofs]);
  211. dosregs.si:=Ofs(c[arg_ofs]);
  212. dosregs.es:=Seg(fcb2);
  213. dosregs.di:=Ofs(fcb2);
  214. msdos(dosregs);
  215. c := Chr(Length(c)) + c + #13 + #0;
  216. with execblock do
  217. begin
  218. envseg:={la_env shr 4}0;
  219. comtail.seg:=Seg(c[1]);
  220. comtail.ofs:=Ofs(c[1]);
  221. firstFCB.seg:=Seg(fcb1);
  222. firstFCB.ofs:=Ofs(fcb1);
  223. secondFCB.seg:=Seg(fcb2);
  224. secondFCB.ofs:=Ofs(fcb2);
  225. end;
  226. p := p + #0;
  227. dosregs.dx:=Ofs(p[1]);
  228. dosregs.ds:=Seg(p[1]);
  229. dosregs.bx:=Ofs(execblock);
  230. dosregs.es:=Seg(execblock);
  231. dosregs.ax:=$4b00;
  232. msdos(dosregs);
  233. LoadDosError;
  234. if DosError=0 then
  235. begin
  236. dosregs.ax:=$4d00;
  237. msdos(dosregs);
  238. LastDosExitCode:=DosRegs.al
  239. end
  240. else
  241. LastDosExitCode:=0;
  242. end;
  243. procedure exec(const path : pathstr;const comline : comstr);
  244. begin
  245. exec_ansistring(path, comline);
  246. end;
  247. procedure getcbreak(var breakvalue : boolean);
  248. begin
  249. dosregs.ax:=$3300;
  250. msdos(dosregs);
  251. breakvalue:=dosregs.dl<>0;
  252. end;
  253. procedure setcbreak(breakvalue : boolean);
  254. begin
  255. dosregs.ax:=$3301;
  256. dosregs.dl:=ord(breakvalue);
  257. msdos(dosregs);
  258. end;
  259. procedure getverify(var verify : boolean);
  260. begin
  261. dosregs.ah:=$54;
  262. msdos(dosregs);
  263. verify:=dosregs.al<>0;
  264. end;
  265. procedure setverify(verify : boolean);
  266. begin
  267. dosregs.ah:=$2e;
  268. dosregs.dl:=0;
  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) and $FF; { No required attributes }
  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. SwapIntVec($10, SaveInt10);
  655. SwapIntVec($75, SaveInt75);
  656. end;
  657. {******************************************************************************
  658. --- File ---
  659. ******************************************************************************}
  660. Function FSearch(path: pathstr; dirlist: string): pathstr;
  661. var
  662. p1 : longint;
  663. s : searchrec;
  664. newdir : pathstr;
  665. begin
  666. { No wildcards allowed in these things }
  667. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  668. begin
  669. fsearch:='';
  670. exit;
  671. end;
  672. { check if the file specified exists }
  673. findfirst(path,anyfile and not(directory),s);
  674. if doserror=0 then
  675. begin
  676. findclose(s);
  677. fsearch:=path;
  678. exit;
  679. end;
  680. findclose(s);
  681. { allow slash as backslash }
  682. DoDirSeparators(dirlist);
  683. repeat
  684. p1:=pos(';',dirlist);
  685. if p1<>0 then
  686. begin
  687. newdir:=copy(dirlist,1,p1-1);
  688. delete(dirlist,1,p1);
  689. end
  690. else
  691. begin
  692. newdir:=dirlist;
  693. dirlist:='';
  694. end;
  695. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  696. newdir:=newdir+'\';
  697. findfirst(newdir+path,anyfile and not(directory),s);
  698. if doserror=0 then
  699. newdir:=newdir+path
  700. else
  701. newdir:='';
  702. findclose(s);
  703. until (dirlist='') or (newdir<>'');
  704. fsearch:=newdir;
  705. end;
  706. { change to short filename if successful DOS call PM }
  707. function GetShortName(var p : String) : boolean;
  708. var
  709. c : array[0..255] of char;
  710. begin
  711. move(p[1],c[0],length(p));
  712. c[length(p)]:=#0;
  713. dosregs.ax:=$7160;
  714. dosregs.cx:=1;
  715. dosregs.ds:=Seg(c);
  716. dosregs.si:=Ofs(c);
  717. dosregs.es:=Seg(c);
  718. dosregs.di:=Ofs(c);
  719. msdos(dosregs);
  720. LoadDosError;
  721. if DosError=0 then
  722. begin
  723. move(c[0],p[1],strlen(c));
  724. p[0]:=char(strlen(c));
  725. GetShortName:=true;
  726. end
  727. else
  728. GetShortName:=false;
  729. end;
  730. { change to long filename if successful DOS call PM }
  731. function GetLongName(var p : String) : boolean;
  732. var
  733. c : array[0..260] of char;
  734. begin
  735. move(p[1],c[0],length(p));
  736. c[length(p)]:=#0;
  737. dosregs.ax:=$7160;
  738. dosregs.cx:=2;
  739. dosregs.ds:=Seg(c);
  740. dosregs.si:=Ofs(c);
  741. dosregs.es:=Seg(c);
  742. dosregs.di:=Ofs(c);
  743. msdos(dosregs);
  744. LoadDosError;
  745. if DosError=0 then
  746. begin
  747. c[255]:=#0;
  748. move(c[0],p[1],strlen(c));
  749. p[0]:=char(strlen(c));
  750. GetLongName:=true;
  751. end
  752. else
  753. GetLongName:=false;
  754. end;
  755. {******************************************************************************
  756. --- Get/Set File Time,Attr ---
  757. ******************************************************************************}
  758. procedure getftime(var f;var time : longint);
  759. begin
  760. dosregs.bx:=textrec(f).handle;
  761. dosregs.ax:=$5700;
  762. msdos(dosregs);
  763. loaddoserror;
  764. time:=(longint(dosregs.dx) shl 16)+dosregs.cx;
  765. end;
  766. procedure setftime(var f;time : longint);
  767. begin
  768. dosregs.bx:=textrec(f).handle;
  769. dosregs.cx:=time and $ffff;
  770. dosregs.dx:=time shr 16;
  771. dosregs.ax:=$5701;
  772. msdos(dosregs);
  773. loaddoserror;
  774. end;
  775. procedure getfattr(var f;var attr : word);
  776. var
  777. path: pchar;
  778. {$ifndef FPC_ANSI_TEXTFILEREC}
  779. r: rawbytestring;
  780. {$endif not FPC_ANSI_TEXTFILEREC}
  781. begin
  782. {$ifdef FPC_ANSI_TEXTFILEREC}
  783. path:=@filerec(f).Name;
  784. {$else}
  785. r:=ToSingleByteFileSystemEncodedFileName(filerec(f).Name);
  786. path:=pchar(r);
  787. {$endif}
  788. dosregs.dx:=Ofs(path^);
  789. dosregs.ds:=Seg(path^);
  790. if LFNSupport then
  791. begin
  792. dosregs.ax:=$7143;
  793. dosregs.bx:=0;
  794. end
  795. else
  796. dosregs.ax:=$4300;
  797. msdos(dosregs);
  798. LoadDosError;
  799. Attr:=dosregs.cx;
  800. end;
  801. procedure setfattr(var f;attr : word);
  802. var
  803. path: pchar;
  804. {$ifndef FPC_ANSI_TEXTFILEREC}
  805. r: rawbytestring;
  806. {$endif not FPC_ANSI_TEXTFILEREC}
  807. begin
  808. { Fail for setting VolumeId. }
  809. if ((attr and VolumeID)<>0) then
  810. begin
  811. doserror:=5;
  812. exit;
  813. end;
  814. {$ifdef FPC_ANSI_TEXTFILEREC}
  815. path:=@filerec(f).Name;
  816. {$else}
  817. r:=ToSingleByteFileSystemEncodedFileName(filerec(f).Name);
  818. path:=pchar(r);
  819. {$endif}
  820. dosregs.dx:=Ofs(path^);
  821. dosregs.ds:=Seg(path^);
  822. if LFNSupport then
  823. begin
  824. dosregs.ax:=$7143;
  825. dosregs.bx:=1;
  826. end
  827. else
  828. dosregs.ax:=$4301;
  829. dosregs.cx:=attr;
  830. msdos(dosregs);
  831. LoadDosError;
  832. end;
  833. {******************************************************************************
  834. --- Environment ---
  835. ******************************************************************************}
  836. function GetEnvStr(EnvNo: Integer; var OutEnvStr: string): integer;
  837. var
  838. dos_env_seg: Word;
  839. ofs: Word;
  840. Ch, Ch2: Char;
  841. begin
  842. dos_env_seg := PFarWord(Ptr(PrefixSeg, $2C))^;
  843. GetEnvStr := 1;
  844. OutEnvStr := '';
  845. ofs := 0;
  846. repeat
  847. Ch := PFarChar(Ptr(dos_env_seg,ofs))^;
  848. Ch2 := PFarChar(Ptr(dos_env_seg,ofs + 1))^;
  849. if (Ch = #0) and (Ch2 = #0) then
  850. exit;
  851. if Ch = #0 then
  852. Inc(GetEnvStr);
  853. if (Ch <> #0) and (GetEnvStr = EnvNo) then
  854. OutEnvStr := OutEnvStr + Ch;
  855. Inc(ofs);
  856. if ofs = 0 then
  857. exit;
  858. until false;
  859. end;
  860. function envcount : longint;
  861. var
  862. tmpstr: string;
  863. begin
  864. envcount := GetEnvStr(-1, tmpstr);
  865. end;
  866. function envstr (Index: longint): string;
  867. begin
  868. GetEnvStr(Index, envstr);
  869. end;
  870. Function GetEnv(envvar: string): string;
  871. var
  872. hs : string;
  873. eqpos : integer;
  874. I : integer;
  875. begin
  876. envvar:=upcase(envvar);
  877. getenv:='';
  878. for I := 1 to envcount do
  879. begin
  880. hs:=envstr(I);
  881. eqpos:=pos('=',hs);
  882. if upcase(copy(hs,1,eqpos-1))=envvar then
  883. begin
  884. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  885. break;
  886. end;
  887. end;
  888. end;
  889. {******************************************************************************
  890. --- Get/SetIntVec ---
  891. ******************************************************************************}
  892. procedure GetIntVec(intno: Byte; var vector: farpointer); assembler;
  893. asm
  894. mov al, intno
  895. mov ah, 35h
  896. int 21h
  897. xchg ax, bx
  898. {$if defined(FPC_MM_TINY) or defined(FPC_MM_SMALL) or defined(FPC_MM_MEDIUM)}
  899. mov bx, vector
  900. mov [bx], ax
  901. mov ax, es
  902. mov [bx + 2], ax
  903. {$else}
  904. {$ifdef FPC_ENABLED_CLD}
  905. cld
  906. {$endif FPC_ENABLED_CLD}
  907. push es
  908. pop bx
  909. les di, vector
  910. stosw
  911. xchg ax, bx
  912. stosw
  913. {$endif}
  914. end;
  915. procedure SetIntVec(intno: Byte; vector: farpointer); assembler;
  916. asm
  917. push ds
  918. mov al, intno
  919. mov ah, 25h
  920. lds dx, word [vector]
  921. int 21h
  922. pop ds
  923. end;
  924. {******************************************************************************
  925. --- Keep ---
  926. ******************************************************************************}
  927. Procedure Keep(exitcode: word); assembler;
  928. asm
  929. mov bx, PrefixSeg
  930. dec bx
  931. mov es, bx
  932. mov dx, es:[3]
  933. mov ax, exitcode
  934. mov ah, 31h
  935. int 21h
  936. end;
  937. {$ifdef DEBUG_LFN}
  938. begin
  939. LogLFN:=(GetEnv('LOGLFN')<>'');
  940. assign(lfnfile,LFNFileName);
  941. {$I-}
  942. Reset(lfnfile);
  943. if IOResult<>0 then
  944. begin
  945. Rewrite(lfnfile);
  946. Writeln(lfnfile,'New lfn.log');
  947. end;
  948. close(lfnfile);
  949. {$endif DEBUG_LFN}
  950. end.