dos.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. unit dos;
  12. interface
  13. Uses
  14. Go32;
  15. Type
  16. searchrec = packed record
  17. fill : array[1..21] of byte;
  18. attr : byte;
  19. time : longint;
  20. { reserved : word; not in DJGPP V2 }
  21. size : longint;
  22. name : string[255]; { LFN Name, DJGPP uses only [12] but more can't hurt (PFV) }
  23. end;
  24. {$DEFINE HAS_REGISTERS}
  25. Registers = Go32.Registers;
  26. {$i dosh.inc}
  27. implementation
  28. uses
  29. strings;
  30. {$DEFINE HAS_GETMSCOUNT}
  31. {$DEFINE HAS_INTR}
  32. {$DEFINE HAS_SETCBREAK}
  33. {$DEFINE HAS_GETCBREAK}
  34. {$DEFINE HAS_SETVERIFY}
  35. {$DEFINE HAS_GETVERIFY}
  36. {$DEFINE HAS_SWAPVECTORS}
  37. {$DEFINE HAS_GETSHORTNAME}
  38. {$DEFINE HAS_GETLONGNAME}
  39. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  40. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  41. {$I dos.inc}
  42. {******************************************************************************
  43. --- Dos Interrupt ---
  44. ******************************************************************************}
  45. var
  46. dosregs : registers;
  47. procedure LoadDosError;
  48. var
  49. r : registers;
  50. SimpleDosError : word;
  51. begin
  52. if (dosregs.flags and fcarry) <> 0 then
  53. begin
  54. { I got a extended error = 0
  55. while CarryFlag was set from Exec function }
  56. SimpleDosError:=dosregs.ax;
  57. r.eax:=$5900;
  58. r.ebx:=$0;
  59. realintr($21,r);
  60. { conversion from word to integer !!
  61. gave a Bound check error if ax is $FFFF !! PM }
  62. doserror:=integer(r.ax);
  63. case doserror of
  64. 0 : DosError:=integer(SimpleDosError);
  65. 19 : DosError:=150;
  66. 21 : DosError:=152;
  67. end;
  68. end
  69. else
  70. doserror:=0;
  71. end;
  72. procedure intr(intno : byte;var regs : registers);
  73. begin
  74. realintr(intno,regs);
  75. end;
  76. {******************************************************************************
  77. --- Info / Date / Time ---
  78. ******************************************************************************}
  79. function dosversion : word;
  80. begin
  81. dosregs.ax:=$3000;
  82. msdos(dosregs);
  83. dosversion:=dosregs.ax;
  84. end;
  85. procedure getdate(var year,month,mday,wday : word);
  86. begin
  87. dosregs.ax:=$2a00;
  88. msdos(dosregs);
  89. wday:=dosregs.al;
  90. year:=dosregs.cx;
  91. month:=dosregs.dh;
  92. mday:=dosregs.dl;
  93. end;
  94. procedure setdate(year,month,day : word);
  95. begin
  96. dosregs.cx:=year;
  97. dosregs.dh:=month;
  98. dosregs.dl:=day;
  99. dosregs.ah:=$2b;
  100. msdos(dosregs);
  101. end;
  102. procedure gettime(var hour,minute,second,sec100 : word);
  103. begin
  104. dosregs.ah:=$2c;
  105. msdos(dosregs);
  106. hour:=dosregs.ch;
  107. minute:=dosregs.cl;
  108. second:=dosregs.dh;
  109. sec100:=dosregs.dl;
  110. end;
  111. procedure settime(hour,minute,second,sec100 : word);
  112. begin
  113. dosregs.ch:=hour;
  114. dosregs.cl:=minute;
  115. dosregs.dh:=second;
  116. dosregs.dl:=sec100;
  117. dosregs.ah:=$2d;
  118. msdos(dosregs);
  119. end;
  120. function GetMsCount: int64;
  121. begin
  122. GetMsCount := int64 (MemL [$40:$6c]) * 55;
  123. end;
  124. {******************************************************************************
  125. --- Exec ---
  126. ******************************************************************************}
  127. procedure exec(const path : pathstr;const comline : comstr);
  128. type
  129. realptr = packed record
  130. ofs,seg : word;
  131. end;
  132. texecblock = packed record
  133. envseg : word;
  134. comtail : realptr;
  135. firstFCB : realptr;
  136. secondFCB : realptr;
  137. { iniStack : realptr;
  138. iniCSIP : realptr;}
  139. end;
  140. var
  141. current_dos_buffer_pos,
  142. arg_ofs,
  143. i,la_env,
  144. la_p,la_c,la_e,
  145. fcb1_la,fcb2_la : longint;
  146. execblock : texecblock;
  147. c,p : string;
  148. function paste_to_dos(src : string;cr : boolean; n : longint) : boolean;
  149. {Changed by Laaca - added parameter N}
  150. var
  151. c : pchar;
  152. CLen : cardinal;
  153. ls : longint;
  154. begin
  155. paste_to_dos:=false;
  156. ls:=Length(src)-n;
  157. if current_dos_buffer_pos+ls+3>transfer_buffer+tb_size then
  158. RunError(217);
  159. getmem(c,ls+3);
  160. move(src[n],c^,ls+1);
  161. if cr then
  162. begin
  163. c[ls+1]:=#13;
  164. c[ls+2]:=#0;
  165. end
  166. else
  167. c[ls+1]:=#0;
  168. CLen := StrLen (C) + 1;
  169. seg_move(get_ds,longint(c),dosmemselector,current_dos_buffer_pos,CLen);
  170. current_dos_buffer_pos:=current_dos_buffer_pos+CLen;
  171. freemem(c,ls+3);
  172. paste_to_dos:=true;
  173. end;
  174. begin
  175. { create command line }
  176. c:=comline;
  177. { create path }
  178. p:=path;
  179. { allow slash as backslash }
  180. for i:=1 to length(p) do
  181. if p[i]='/' then
  182. p[i]:='\';
  183. if LFNSupport then
  184. GetShortName(p);
  185. { create buffer }
  186. la_env:=transfer_buffer;
  187. while (la_env and 15)<>0 do
  188. inc(la_env);
  189. current_dos_buffer_pos:=la_env;
  190. { copy environment }
  191. for i:=1 to envcount do
  192. paste_to_dos(envstr(i),false,1);
  193. {the behaviour is still suboptimal because variable COMMAND is stripped out}
  194. paste_to_dos(chr(0),false,1); { adds a double zero at the end }
  195. { allow slash as backslash }
  196. la_p:=current_dos_buffer_pos;
  197. paste_to_dos(p,false,0);
  198. la_c:=current_dos_buffer_pos;
  199. paste_to_dos(c,true,0);
  200. la_e:=current_dos_buffer_pos;
  201. fcb1_la:=la_e;
  202. la_e:=la_e+16;
  203. fcb2_la:=la_e;
  204. la_e:=la_e+16;
  205. { allocate FCB see dosexec code }
  206. arg_ofs:=1;
  207. while (c[arg_ofs] in [' ',#9]) and
  208. (arg_ofs<length(c)) do
  209. inc(arg_ofs);
  210. dosregs.ax:=$2901;
  211. dosregs.ds:=(la_c+arg_ofs) shr 4;
  212. dosregs.esi:=(la_c+arg_ofs) and 15;
  213. dosregs.es:=fcb1_la shr 4;
  214. dosregs.edi:=fcb1_la and 15;
  215. msdos(dosregs);
  216. { allocate second FCB see dosexec code }
  217. dosregs.ax:=$2901;
  218. dosregs.ds:=(la_c+arg_ofs) shr 4;
  219. dosregs.esi:=(la_c+arg_ofs) and 15;
  220. dosregs.es:=fcb2_la shr 4;
  221. dosregs.edi:=fcb2_la and 15;
  222. msdos(dosregs);
  223. with execblock do
  224. begin
  225. envseg:=la_env shr 4;
  226. comtail.seg:=la_c shr 4;
  227. comtail.ofs:=la_c and 15;
  228. firstFCB.seg:=fcb1_la shr 4;
  229. firstFCB.ofs:=fcb1_la and 15;
  230. secondFCB.seg:=fcb2_la shr 4;
  231. secondFCB.ofs:=fcb2_la and 15;
  232. end;
  233. seg_move(get_ds,longint(@execblock),dosmemselector,la_e,sizeof(texecblock));
  234. dosregs.edx:=la_p and 15+1;
  235. dosregs.ds:=la_p shr 4;
  236. dosregs.ebx:=la_p and 15+la_e-la_p;
  237. dosregs.es:=la_p shr 4;
  238. dosregs.ax:=$4b00;
  239. msdos(dosregs);
  240. LoadDosError;
  241. if DosError<>0 then
  242. begin
  243. dosregs.ax:=$4d00;
  244. msdos(dosregs);
  245. LastDosExitCode:=DosRegs.al
  246. end
  247. else
  248. LastDosExitCode:=0;
  249. end;
  250. procedure getcbreak(var breakvalue : boolean);
  251. begin
  252. dosregs.ax:=$3300;
  253. msdos(dosregs);
  254. breakvalue:=dosregs.dl<>0;
  255. end;
  256. procedure setcbreak(breakvalue : boolean);
  257. begin
  258. dosregs.ax:=$3301;
  259. dosregs.dl:=ord(breakvalue);
  260. msdos(dosregs);
  261. end;
  262. procedure getverify(var verify : boolean);
  263. begin
  264. dosregs.ah:=$54;
  265. msdos(dosregs);
  266. verify:=dosregs.al<>0;
  267. end;
  268. procedure setverify(verify : boolean);
  269. begin
  270. dosregs.ah:=$2e;
  271. dosregs.al:=ord(verify);
  272. msdos(dosregs);
  273. end;
  274. {******************************************************************************
  275. --- Disk ---
  276. ******************************************************************************}
  277. TYPE ExtendedFat32FreeSpaceRec=packed Record
  278. RetSize : WORD; { (ret) size of returned structure}
  279. Strucversion : WORD; {(call) structure version (0000h)
  280. (ret) actual structure version (0000h)}
  281. SecPerClus, {number of sectors per cluster}
  282. BytePerSec, {number of bytes per sector}
  283. AvailClusters, {number of available clusters}
  284. TotalClusters, {total number of clusters on the drive}
  285. AvailPhysSect, {physical sectors available on the drive}
  286. TotalPhysSect, {total physical sectors on the drive}
  287. AvailAllocUnits, {Available allocation units}
  288. TotalAllocUnits : DWORD; {Total allocation units}
  289. Dummy,Dummy2 : DWORD; {8 bytes reserved}
  290. END;
  291. function do_diskdata(drive : byte; Free : BOOLEAN) : Int64;
  292. VAR
  293. S : String;
  294. Rec : ExtendedFat32FreeSpaceRec;
  295. BEGIN
  296. if {(swap(dosversion)>=$070A) AND} LFNSupport then
  297. begin
  298. S:='C:\'#0;
  299. if Drive=0 then
  300. begin
  301. GetDir(Drive,S);
  302. Setlength(S,4);
  303. S[4]:=#0;
  304. end
  305. else
  306. S[1]:=chr(Drive+64);
  307. Rec.Strucversion:=0;
  308. dosmemput(tb_segment,tb_offset,Rec,SIZEOF(ExtendedFat32FreeSpaceRec));
  309. dosmemput(tb_segment,tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1,S[1],4);
  310. dosregs.dx:=tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1;
  311. dosregs.ds:=tb_segment;
  312. dosregs.di:=tb_offset;
  313. dosregs.es:=tb_segment;
  314. dosregs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  315. dosregs.ax:=$7303;
  316. msdos(dosregs);
  317. if (dosregs.flags and fcarry) = 0 then {No error clausule in int except cf}
  318. begin
  319. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  320. if Free then
  321. Do_DiskData:=int64(rec.AvailAllocUnits)*rec.SecPerClus*rec.BytePerSec
  322. else
  323. Do_DiskData:=int64(rec.TotalAllocUnits)*rec.SecPerClus*rec.BytePerSec;
  324. end
  325. else
  326. Do_DiskData:=-1;
  327. end
  328. else
  329. begin
  330. dosregs.dl:=drive;
  331. dosregs.ah:=$36;
  332. msdos(dosregs);
  333. if dosregs.ax<>$FFFF then
  334. begin
  335. if Free then
  336. Do_DiskData:=int64(dosregs.ax)*dosregs.bx*dosregs.cx
  337. else
  338. Do_DiskData:=int64(dosregs.ax)*dosregs.cx*dosregs.dx;
  339. end
  340. else
  341. do_diskdata:=-1;
  342. end;
  343. end;
  344. function diskfree(drive : byte) : int64;
  345. begin
  346. diskfree:=Do_DiskData(drive,TRUE);
  347. end;
  348. function disksize(drive : byte) : int64;
  349. begin
  350. disksize:=Do_DiskData(drive,false);
  351. end;
  352. {******************************************************************************
  353. --- LFNFindfirst LFNFindNext ---
  354. ******************************************************************************}
  355. type
  356. LFNSearchRec=packed record
  357. attr,
  358. crtime,
  359. crtimehi,
  360. actime,
  361. actimehi,
  362. lmtime,
  363. lmtimehi,
  364. sizehi,
  365. size : longint;
  366. reserved : array[0..7] of byte;
  367. name : array[0..259] of byte;
  368. shortname : array[0..13] of byte;
  369. end;
  370. procedure LFNSearchRec2Dos(const w:LFNSearchRec;hdl:longint;var d:Searchrec;from_findfirst : boolean);
  371. var
  372. Len : longint;
  373. begin
  374. With w do
  375. begin
  376. FillChar(d,sizeof(SearchRec),0);
  377. if DosError=0 then
  378. len:=StrLen(@Name)
  379. else
  380. len:=0;
  381. d.Name[0]:=chr(len);
  382. Move(Name[0],d.Name[1],Len);
  383. d.Time:=lmTime;
  384. d.Size:=Size;
  385. d.Attr:=Attr and $FF;
  386. if (DosError<>0) and from_findfirst then
  387. hdl:=-1;
  388. Move(hdl,d.Fill,4);
  389. end;
  390. end;
  391. procedure LFNFindFirst(path:pchar;attr:longint;var s:searchrec);
  392. var
  393. i : longint;
  394. w : LFNSearchRec;
  395. begin
  396. { allow slash as backslash }
  397. for i:=0 to strlen(path) do
  398. if path[i]='/' then path[i]:='\';
  399. dosregs.si:=1; { use ms-dos time }
  400. { don't include the label if not asked for it, needed for network drives }
  401. if attr=$8 then
  402. dosregs.ecx:=8
  403. else
  404. dosregs.ecx:=attr and (not 8);
  405. dosregs.edx:=tb_offset+Sizeof(LFNSearchrec)+1;
  406. dosmemput(tb_segment,tb_offset+Sizeof(LFNSearchrec)+1,path^,strlen(path)+1);
  407. dosregs.ds:=tb_segment;
  408. dosregs.edi:=tb_offset;
  409. dosregs.es:=tb_segment;
  410. dosregs.ax:=$714e;
  411. msdos(dosregs);
  412. LoadDosError;
  413. copyfromdos(w,sizeof(LFNSearchRec));
  414. LFNSearchRec2Dos(w,dosregs.ax,s,true);
  415. end;
  416. procedure LFNFindNext(var s:searchrec);
  417. var
  418. hdl : longint;
  419. w : LFNSearchRec;
  420. begin
  421. Move(s.Fill,hdl,4);
  422. dosregs.si:=1; { use ms-dos time }
  423. dosregs.edi:=tb_offset;
  424. dosregs.es:=tb_segment;
  425. dosregs.ebx:=hdl;
  426. dosregs.ax:=$714f;
  427. msdos(dosregs);
  428. LoadDosError;
  429. copyfromdos(w,sizeof(LFNSearchRec));
  430. LFNSearchRec2Dos(w,hdl,s,false);
  431. end;
  432. procedure LFNFindClose(var s:searchrec);
  433. var
  434. hdl : longint;
  435. begin
  436. Move(s.Fill,hdl,4);
  437. { Do not call MsDos if FindFirst returned with an error }
  438. if hdl=-1 then
  439. begin
  440. DosError:=0;
  441. exit;
  442. end;
  443. dosregs.ebx:=hdl;
  444. dosregs.ax:=$71a1;
  445. msdos(dosregs);
  446. LoadDosError;
  447. end;
  448. {******************************************************************************
  449. --- DosFindfirst DosFindNext ---
  450. ******************************************************************************}
  451. procedure dossearchrec2searchrec(var f : searchrec);
  452. var
  453. len : longint;
  454. begin
  455. { Check is necessary!! OS/2's VDM doesn't clear the name with #0 if the }
  456. { file doesn't exist! (JM) }
  457. if dosError = 0 then
  458. len:=StrLen(@f.Name)
  459. else len := 0;
  460. Move(f.Name[0],f.Name[1],Len);
  461. f.Name[0]:=chr(len);
  462. end;
  463. procedure DosFindfirst(path : pchar;attr : word;var f : searchrec);
  464. var
  465. i : longint;
  466. begin
  467. { allow slash as backslash }
  468. for i:=0 to strlen(path) do
  469. if path[i]='/' then path[i]:='\';
  470. copytodos(f,sizeof(searchrec));
  471. dosregs.edx:=tb_offset;
  472. dosregs.ds:=tb_segment;
  473. dosregs.ah:=$1a;
  474. msdos(dosregs);
  475. dosregs.ecx:=attr;
  476. dosregs.edx:=tb_offset+Sizeof(searchrec)+1;
  477. dosmemput(tb_segment,tb_offset+Sizeof(searchrec)+1,path^,strlen(path)+1);
  478. dosregs.ds:=tb_segment;
  479. dosregs.ah:=$4e;
  480. msdos(dosregs);
  481. copyfromdos(f,sizeof(searchrec));
  482. LoadDosError;
  483. dossearchrec2searchrec(f);
  484. end;
  485. procedure Dosfindnext(var f : searchrec);
  486. begin
  487. copytodos(f,sizeof(searchrec));
  488. dosregs.edx:=tb_offset;
  489. dosregs.ds:=tb_segment;
  490. dosregs.ah:=$1a;
  491. msdos(dosregs);
  492. dosregs.ah:=$4f;
  493. msdos(dosregs);
  494. copyfromdos(f,sizeof(searchrec));
  495. LoadDosError;
  496. dossearchrec2searchrec(f);
  497. end;
  498. {******************************************************************************
  499. --- Findfirst FindNext ---
  500. ******************************************************************************}
  501. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  502. var
  503. path0 : array[0..255] of char;
  504. begin
  505. doserror:=0;
  506. strpcopy(path0,path);
  507. if LFNSupport then
  508. LFNFindFirst(path0,attr,f)
  509. else
  510. Dosfindfirst(path0,attr,f);
  511. end;
  512. procedure findnext(var f : searchRec);
  513. begin
  514. doserror:=0;
  515. if LFNSupport then
  516. LFNFindnext(f)
  517. else
  518. Dosfindnext(f);
  519. end;
  520. Procedure FindClose(Var f: SearchRec);
  521. begin
  522. DosError:=0;
  523. if LFNSupport then
  524. LFNFindClose(f);
  525. end;
  526. type swap_proc = procedure;
  527. var
  528. _swap_in : swap_proc;external name '_swap_in';
  529. _swap_out : swap_proc;external name '_swap_out';
  530. _exception_exit : pointer;external name '_exception_exit';
  531. _v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  532. procedure swapvectors;
  533. begin
  534. if _exception_exit<>nil then
  535. if _v2prt0_exceptions_on then
  536. _swap_out()
  537. else
  538. _swap_in();
  539. end;
  540. {******************************************************************************
  541. --- File ---
  542. ******************************************************************************}
  543. Function FSearch(path: pathstr; dirlist: string): pathstr;
  544. var
  545. i,p1 : longint;
  546. s : searchrec;
  547. newdir : pathstr;
  548. begin
  549. { check if the file specified exists }
  550. findfirst(path,anyfile and not(directory),s);
  551. if doserror=0 then
  552. begin
  553. findclose(s);
  554. fsearch:=path;
  555. exit;
  556. end;
  557. { No wildcards allowed in these things }
  558. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  559. fsearch:=''
  560. else
  561. begin
  562. { allow slash as backslash }
  563. for i:=1 to length(dirlist) do
  564. if dirlist[i]='/' then dirlist[i]:='\';
  565. repeat
  566. p1:=pos(';',dirlist);
  567. if p1<>0 then
  568. begin
  569. newdir:=copy(dirlist,1,p1-1);
  570. delete(dirlist,1,p1);
  571. end
  572. else
  573. begin
  574. newdir:=dirlist;
  575. dirlist:='';
  576. end;
  577. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  578. newdir:=newdir+'\';
  579. findfirst(newdir+path,anyfile and not(directory),s);
  580. if doserror=0 then
  581. newdir:=newdir+path
  582. else
  583. newdir:='';
  584. until (dirlist='') or (newdir<>'');
  585. fsearch:=newdir;
  586. end;
  587. findclose(s);
  588. end;
  589. { change to short filename if successful DOS call PM }
  590. function GetShortName(var p : String) : boolean;
  591. var
  592. c : array[0..255] of char;
  593. begin
  594. move(p[1],c[0],length(p));
  595. c[length(p)]:=#0;
  596. copytodos(c,length(p)+1);
  597. dosregs.ax:=$7160;
  598. dosregs.cx:=1;
  599. dosregs.ds:=tb_segment;
  600. dosregs.si:=tb_offset;
  601. dosregs.es:=tb_segment;
  602. dosregs.di:=tb_offset;
  603. msdos(dosregs);
  604. LoadDosError;
  605. if DosError=0 then
  606. begin
  607. copyfromdos(c,256);
  608. move(c[0],p[1],strlen(c));
  609. p[0]:=char(strlen(c));
  610. GetShortName:=true;
  611. end
  612. else
  613. GetShortName:=false;
  614. end;
  615. { change to long filename if successful DOS call PM }
  616. function GetLongName(var p : String) : boolean;
  617. var
  618. c : array[0..255] of char;
  619. begin
  620. move(p[1],c[0],length(p));
  621. c[length(p)]:=#0;
  622. copytodos(c,length(p)+1);
  623. dosregs.ax:=$7160;
  624. dosregs.cx:=2;
  625. dosregs.ds:=tb_segment;
  626. dosregs.si:=tb_offset;
  627. dosregs.es:=tb_segment;
  628. dosregs.di:=tb_offset;
  629. msdos(dosregs);
  630. LoadDosError;
  631. if DosError=0 then
  632. begin
  633. copyfromdos(c,256);
  634. move(c[0],p[1],strlen(c));
  635. p[0]:=char(strlen(c));
  636. GetLongName:=true;
  637. end
  638. else
  639. GetLongName:=false;
  640. end;
  641. {******************************************************************************
  642. --- Get/Set File Time,Attr ---
  643. ******************************************************************************}
  644. procedure getftime(var f;var time : longint);
  645. begin
  646. dosregs.bx:=textrec(f).handle;
  647. dosregs.ax:=$5700;
  648. msdos(dosregs);
  649. loaddoserror;
  650. time:=(dosregs.dx shl 16)+dosregs.cx;
  651. end;
  652. procedure setftime(var f;time : longint);
  653. begin
  654. dosregs.bx:=textrec(f).handle;
  655. dosregs.cx:=time and $ffff;
  656. dosregs.dx:=time shr 16;
  657. dosregs.ax:=$5701;
  658. msdos(dosregs);
  659. loaddoserror;
  660. end;
  661. procedure getfattr(var f;var attr : word);
  662. begin
  663. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  664. dosregs.edx:=tb_offset;
  665. dosregs.ds:=tb_segment;
  666. if LFNSupport then
  667. begin
  668. dosregs.ax:=$7143;
  669. dosregs.bx:=0;
  670. end
  671. else
  672. dosregs.ax:=$4300;
  673. msdos(dosregs);
  674. LoadDosError;
  675. Attr:=dosregs.cx;
  676. end;
  677. procedure setfattr(var f;attr : word);
  678. begin
  679. copytodos(filerec(f).name,strlen(filerec(f).name)+1);
  680. dosregs.edx:=tb_offset;
  681. dosregs.ds:=tb_segment;
  682. if LFNSupport then
  683. begin
  684. dosregs.ax:=$7143;
  685. dosregs.bx:=1;
  686. end
  687. else
  688. dosregs.ax:=$4301;
  689. dosregs.cx:=attr;
  690. msdos(dosregs);
  691. LoadDosError;
  692. end;
  693. {******************************************************************************
  694. --- Environment ---
  695. ******************************************************************************}
  696. function envcount : longint;
  697. var
  698. hp : ppchar;
  699. begin
  700. hp:=envp;
  701. envcount:=0;
  702. while assigned(hp^) do
  703. begin
  704. inc(envcount);
  705. inc(hp);
  706. end;
  707. end;
  708. function envstr (Index: longint): string;
  709. begin
  710. if (index<=0) or (index>envcount) then
  711. envstr:=''
  712. else
  713. envstr:=strpas(ppchar(pointer(envp)+SizeOf(PChar)*(index-1))^);
  714. end;
  715. Function GetEnv(envvar: string): string;
  716. var
  717. hp : ppchar;
  718. hs : string;
  719. eqpos : longint;
  720. begin
  721. envvar:=upcase(envvar);
  722. hp:=envp;
  723. getenv:='';
  724. while assigned(hp^) do
  725. begin
  726. hs:=strpas(hp^);
  727. eqpos:=pos('=',hs);
  728. if upcase(copy(hs,1,eqpos-1))=envvar then
  729. begin
  730. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  731. break;
  732. end;
  733. inc(hp);
  734. end;
  735. end;
  736. end.