dos.pp 26 KB

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