dos.pp 25 KB

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