dmisc.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Dos unit for BP7 compatible RTL for Delphi
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit dmisc;
  19. {$i fpcdefs.inc}
  20. interface
  21. {$ifndef linux}
  22. {$define MSWindows}
  23. {$endif}
  24. uses
  25. {$ifdef linux}
  26. Libc,
  27. {$else}
  28. windows,
  29. {$endif}
  30. sysutils;
  31. {$ifdef VER100}
  32. type int64 = longint;
  33. {$endif}
  34. Const
  35. Max_Path = 255;
  36. {Bitmasks for CPU Flags}
  37. fcarry = $0001;
  38. fparity = $0004;
  39. fauxiliary = $0010;
  40. fzero = $0040;
  41. fsign = $0080;
  42. foverflow = $0800;
  43. {Bitmasks for file attribute}
  44. readonly = $01;
  45. hidden = $02;
  46. sysfile = $04;
  47. volumeid = $08;
  48. directory = $10;
  49. archive = $20;
  50. anyfile = $3F;
  51. {File Status}
  52. fmclosed = $D7B0;
  53. fminput = $D7B1;
  54. fmoutput = $D7B2;
  55. fminout = $D7B3;
  56. Type
  57. DWord = Cardinal;
  58. { Needed for Win95 LFN Support }
  59. ComStr = String[255];
  60. PathStr = String[255];
  61. DirStr = String[255];
  62. NameStr = String[255];
  63. ExtStr = String[255];
  64. FileRec = TFileRec;
  65. DateTime = packed record
  66. Year,
  67. Month,
  68. Day,
  69. Hour,
  70. Min,
  71. Sec : word;
  72. End;
  73. SearchRec = Sysutils.TSearchRec;
  74. registers = packed record
  75. case i : integer of
  76. 0 : (ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,f8,flags,fs,gs : word);
  77. 1 : (al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh : byte);
  78. 2 : (eax, ebx, ecx, edx, ebp, esi, edi : longint);
  79. end;
  80. Var
  81. DosError : integer;
  82. {Interrupt}
  83. Procedure Intr(intno: byte; var regs: registers);
  84. Procedure MSDos(var regs: registers);
  85. {Info/Date/Time}
  86. Function DosVersion: Word;
  87. Procedure GetDate(var year, month, mday, wday: word);
  88. Procedure GetTime(var hour, minute, second, sec100: word);
  89. Procedure UnpackTime(p: longint; var t: datetime);
  90. Procedure PackTime(var t: datetime; var p: longint);
  91. {Exec}
  92. Procedure Exec(const path: pathstr; const comline: comstr);
  93. Function DosExitCode: word;
  94. {Disk}
  95. Function DiskFree(drive: byte) : int64;
  96. Function DiskSize(drive: byte) : int64;
  97. Procedure FindFirst(const path: pathstr; attr: word; var f: searchRec);
  98. Procedure FindNext(var f: searchRec);
  99. Procedure FindClose(Var f: SearchRec);
  100. {File}
  101. Procedure GetFAttr(var f; var attr: word);
  102. Procedure GetFTime(var f; var tim: longint);
  103. Function FSearch(path: pathstr; dirlist: string): pathstr;
  104. Function FExpand(const path: pathstr): pathstr;
  105. Procedure FSplit(path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
  106. {Environment}
  107. Function EnvCount: longint;
  108. Function EnvStr(index: integer): string;
  109. Function GetEnv(envvar: string): string;
  110. {Misc}
  111. Procedure SetFAttr(var f; attr: word);
  112. Procedure SetFTime(var f; time: longint);
  113. Procedure GetCBreak(var breakvalue: boolean);
  114. Procedure SetCBreak(breakvalue: boolean);
  115. Procedure GetVerify(var verify: boolean);
  116. Procedure SetVerify(verify: boolean);
  117. {Do Nothing Functions}
  118. Procedure SwapVectors;
  119. Procedure GetIntVec(intno: byte; var vector: pointer);
  120. Procedure SetIntVec(intno: byte; vector: pointer);
  121. Procedure Keep(exitcode: word);
  122. implementation
  123. function upper(const s : string) : string;
  124. {
  125. return uppercased string of s
  126. }
  127. var
  128. i : longint;
  129. begin
  130. for i:=1 to length(s) do
  131. if s[i] in ['a'..'z'] then
  132. upper[i]:=char(byte(s[i])-32)
  133. else
  134. upper[i]:=s[i];
  135. upper[0]:=s[0];
  136. end;
  137. {******************************************************************************
  138. --- Conversion ---
  139. ******************************************************************************}
  140. {$ifdef MSWindows}
  141. function GetLastError : DWORD;stdcall;
  142. external 'Kernel32.dll' name 'GetLastError';
  143. function FileTimeToDosDateTime(const ft :TFileTime;var data,time : word) : boolean;stdcall;
  144. external 'Kernel32.dll' name 'FileTimeToDosDateTime';
  145. function DosDateTimeToFileTime(date,time : word;var ft :TFileTime) : boolean;stdcall;
  146. external 'Kernel32.dll' name 'DosDateTimeToFileTime';
  147. function FileTimeToLocalFileTime(const ft : TFileTime;var lft : TFileTime) : boolean;stdcall;
  148. external 'Kernel32.dll' name 'FileTimeToLocalFileTime';
  149. function LocalFileTimeToFileTime(const lft : TFileTime;var ft : TFileTime) : boolean;stdcall;
  150. external 'Kernel32.dll' name 'LocalFileTimeToFileTime';
  151. type
  152. Longrec=packed record
  153. lo,hi : word;
  154. end;
  155. function Last2DosError(d:dword):integer;
  156. begin
  157. Last2DosError:=d;
  158. end;
  159. Function DosToWinAttr (Const Attr : Longint) : longint;
  160. begin
  161. DosToWinAttr:=Attr;
  162. end;
  163. Function WinToDosAttr (Const Attr : Longint) : longint;
  164. begin
  165. WinToDosAttr:=Attr;
  166. end;
  167. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):boolean;
  168. var
  169. lft : TFileTime;
  170. begin
  171. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,lft) and
  172. LocalFileTimeToFileTime(lft,Wtime);
  173. end;
  174. Function WinToDosTime (Const Wtime : TFileTime;var DTime:longint):boolean;
  175. var
  176. lft : TFileTime;
  177. begin
  178. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  179. FileTimeToDosDateTime(lft,longrec(dtime).hi,longrec(dtime).lo);
  180. end;
  181. {$endif}
  182. {******************************************************************************
  183. --- Dos Interrupt ---
  184. ******************************************************************************}
  185. procedure intr(intno : byte;var regs : registers);
  186. begin
  187. { !!!!!!!! }
  188. end;
  189. procedure msdos(var regs : registers);
  190. begin
  191. { !!!!!!!! }
  192. end;
  193. {******************************************************************************
  194. --- Info / Date / Time ---
  195. ******************************************************************************}
  196. function dosversion : word;
  197. begin
  198. dosversion:=0;
  199. end;
  200. procedure getdate(var year,month,mday,wday : word);
  201. begin
  202. DecodeDate(Now,Year,Month,MDay);
  203. WDay:=0;
  204. // DecodeDateFully(Now,Year,Month,MDay,WDay);
  205. end;
  206. procedure gettime(var hour,minute,second,sec100 : word);
  207. begin
  208. DecodeTime(Now,Hour,Minute,Second,Sec100);
  209. Sec100:=Sec100 div 10;
  210. end;
  211. Procedure packtime(var t : datetime;var p : longint);
  212. Begin
  213. 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);
  214. End;
  215. Procedure unpacktime(p : longint;var t : datetime);
  216. Begin
  217. with t do
  218. begin
  219. sec:=(p and 31) shl 1;
  220. min:=(p shr 5) and 63;
  221. hour:=(p shr 11) and 31;
  222. day:=(p shr 16) and 31;
  223. month:=(p shr 21) and 15;
  224. year:=(p shr 25)+1980;
  225. end;
  226. End;
  227. {******************************************************************************
  228. --- Exec ---
  229. ******************************************************************************}
  230. var
  231. lastdosexitcode : word;
  232. {$ifdef MSWindows}
  233. procedure exec(const path : pathstr;const comline : comstr);
  234. var
  235. SI: TStartupInfo;
  236. PI: TProcessInformation;
  237. Proc : THandle;
  238. l : DWord;
  239. AppPath,
  240. AppParam : array[0..255] of char;
  241. begin
  242. FillChar(SI, SizeOf(SI), 0);
  243. SI.cb:=SizeOf(SI);
  244. SI.wShowWindow:=1;
  245. Move(Path[1],AppPath,length(Path));
  246. AppPath[Length(Path)]:=#0;
  247. AppParam[0]:='-';
  248. AppParam[1]:=' ';
  249. Move(ComLine[1],AppParam[2],length(Comline));
  250. AppParam[Length(ComLine)+2]:=#0;
  251. if not CreateProcess(PChar(@AppPath), PChar(@AppParam), Nil, Nil, False,$20, Nil, Nil, SI, PI) then
  252. begin
  253. DosError:=Last2DosError(GetLastError);
  254. exit;
  255. end
  256. else
  257. DosError:=0;
  258. Proc:=PI.hProcess;
  259. CloseHandle(PI.hThread);
  260. if WaitForSingleObject(Proc, Infinite) <> $ffffffff then
  261. GetExitCodeProcess(Proc,l)
  262. else
  263. l:=$ffffffff;
  264. CloseHandle(Proc);
  265. LastDosExitCode:=l;
  266. end;
  267. {$endif MSWindows}
  268. {$ifdef Linux}
  269. Procedure Exec (Const Path: PathStr; Const ComLine: ComStr);
  270. var
  271. pid,status : longint;
  272. Begin
  273. LastDosExitCode:=0;
  274. pid:=Fork;
  275. if pid=0 then
  276. begin
  277. {The child does the actual exec, and then exits}
  278. Execl(@Path[1],@ComLine[1]);
  279. {If the execve fails, we return an exitvalue of 127, to let it be known}
  280. __exit(127);
  281. end
  282. else
  283. if pid=-1 then {Fork failed}
  284. begin
  285. DosError:=8;
  286. exit
  287. end;
  288. {We're in the parent, let's wait.}
  289. WaitPid(Pid,@Status,0);
  290. LastDosExitCode:=Status; // WaitPid and result-convert
  291. if (LastDosExitCode>=0) and (LastDosExitCode<>127) then
  292. DosError:=0
  293. else
  294. DosError:=8; // perhaps one time give an better error
  295. End;
  296. {$endif Linux}
  297. function dosexitcode : word;
  298. begin
  299. dosexitcode:=lastdosexitcode;
  300. end;
  301. procedure swapvectors;
  302. begin
  303. end;
  304. procedure getcbreak(var breakvalue : boolean);
  305. begin
  306. { !! No Win32 Function !! }
  307. end;
  308. procedure setcbreak(breakvalue : boolean);
  309. begin
  310. { !! No Win32 Function !! }
  311. end;
  312. procedure getverify(var verify : boolean);
  313. begin
  314. { !! No Win32 Function !! }
  315. end;
  316. procedure setverify(verify : boolean);
  317. begin
  318. { !! No Win32 Function !! }
  319. end;
  320. {******************************************************************************
  321. --- Disk ---
  322. ******************************************************************************}
  323. {$ifdef Linux]
  324. {
  325. The Diskfree and Disksize functions need a file on the specified drive, since this
  326. is required for the statfs system call.
  327. These filenames are set in drivestr[0..26], and have been preset to :
  328. 0 - '.' (default drive - hence current dir is ok.)
  329. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  330. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  331. 3 - '/' (C: equivalent of dos is the root partition)
  332. 4..26 (can be set by you're own applications)
  333. ! Use AddDisk() to Add new drives !
  334. They both return -1 when a failure occurs.
  335. }
  336. Const
  337. FixDriveStr : array[0..3] of pchar=(
  338. '.',
  339. '/fd0/.',
  340. '/fd1/.',
  341. '/.'
  342. );
  343. var
  344. Drives : byte = 4;
  345. var
  346. DriveStr : array[4..26] of pchar;
  347. Procedure AddDisk(const path:string);
  348. begin
  349. if not (DriveStr[Drives]=nil) then
  350. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  351. GetMem(DriveStr[Drives],length(Path)+1);
  352. StrPCopy(DriveStr[Drives],path);
  353. inc(Drives);
  354. if Drives>26 then
  355. Drives:=4;
  356. end;
  357. Function DiskFree(Drive: Byte): int64;
  358. var
  359. fs : tstatfs;
  360. Begin
  361. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(fixdrivestr[drive],fs)=0)) or
  362. ((not (drivestr[Drive]=nil)) and (statfs(drivestr[drive],fs)=0)) then
  363. Diskfree:=int64(fs.f_bavail)*int64(fs.f_bsize)
  364. else
  365. Diskfree:=-1;
  366. End;
  367. Function DiskSize(Drive: Byte): int64;
  368. var
  369. fs : tstatfs;
  370. Begin
  371. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(fixdrivestr[drive],fs)=0)) or
  372. ((not (drivestr[Drive]=nil)) and (statfs(drivestr[drive],fs)=0)) then
  373. Disksize:=int64(fs.f_blocks)*int64(fs.f_bsize)
  374. else
  375. Disksize:=-1;
  376. End;
  377. {$else linux}
  378. function diskfree(drive : byte) : int64;
  379. begin
  380. DiskFree:=SysUtils.DiskFree(drive);
  381. end;
  382. function disksize(drive : byte) : int64;
  383. begin
  384. DiskSize:=SysUtils.DiskSize(drive);
  385. end;
  386. {$endif linux}
  387. {******************************************************************************
  388. --- Findfirst FindNext ---
  389. ******************************************************************************}
  390. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  391. begin
  392. DosError:=SysUtils.FindFirst(Path,Attr,f);
  393. end;
  394. procedure findnext(var f : searchRec);
  395. begin
  396. DosError:=Sysutils.FindNext(f);
  397. end;
  398. Procedure FindClose(Var f: SearchRec);
  399. begin
  400. Sysutils.FindClose(f);
  401. end;
  402. {******************************************************************************
  403. --- File ---
  404. ******************************************************************************}
  405. procedure fsplit(path : pathstr;var dir : dirstr;var name : namestr;var ext : extstr);
  406. var
  407. p1,i : longint;
  408. begin
  409. { allow slash as backslash }
  410. for i:=1 to length(path) do
  411. if path[i]='/' then path[i]:='\';
  412. { get drive name }
  413. p1:=pos(':',path);
  414. if p1>0 then
  415. begin
  416. dir:=path[1]+':';
  417. delete(path,1,p1);
  418. end
  419. else
  420. dir:='';
  421. { split the path and the name, there are no more path informtions }
  422. { if path contains no backslashes }
  423. while true do
  424. begin
  425. p1:=pos('\',path);
  426. if p1=0 then
  427. break;
  428. dir:=dir+copy(path,1,p1);
  429. delete(path,1,p1);
  430. end;
  431. { try to find out a extension }
  432. p1:=pos('.',path);
  433. if p1>0 then
  434. begin
  435. ext:=copy(path,p1,4);
  436. delete(path,p1,length(path)-p1+1);
  437. end
  438. else
  439. ext:='';
  440. name:=path;
  441. end;
  442. function fexpand(const path : pathstr) : pathstr;
  443. var
  444. s,pa : string[79];
  445. i,j : longint;
  446. begin
  447. getdir(0,s);
  448. pa:=upper(path);
  449. { allow slash as backslash }
  450. for i:=1 to length(pa) do
  451. if pa[i]='/' then
  452. pa[i]:='\';
  453. if (length(pa)>1) and (pa[1] in ['A'..'Z']) and (pa[2]=':') then
  454. begin
  455. { we must get the right directory }
  456. getdir(ord(pa[1])-ord('A')+1,s);
  457. if (ord(pa[0])>2) and (pa[3]<>'\') then
  458. if pa[1]=s[1] then
  459. pa:=s+'\'+copy (pa,3,length(pa))
  460. else
  461. pa:=pa[1]+':\'+copy (pa,3,length(pa))
  462. end
  463. else
  464. if pa[1]='\' then
  465. pa:=s[1]+':'+pa
  466. else if s[0]=#3 then
  467. pa:=s+pa
  468. else
  469. pa:=s+'\'+pa;
  470. { Turbo Pascal gives current dir on drive if only drive given as parameter! }
  471. if length(pa) = 2 then
  472. begin
  473. getdir(byte(pa[1])-64,s);
  474. pa := s;
  475. end;
  476. {First remove all references to '\.\'}
  477. while pos ('\.\',pa)<>0 do
  478. delete (pa,pos('\.\',pa),2);
  479. {Now remove also all references to '\..\' + of course previous dirs..}
  480. repeat
  481. i:=pos('\..\',pa);
  482. if i<>0 then
  483. begin
  484. j:=i-1;
  485. while (j>1) and (pa[j]<>'\') do
  486. dec (j);
  487. if pa[j+1] = ':' then j := 3;
  488. delete (pa,j,i-j+3);
  489. end;
  490. until i=0;
  491. { Turbo Pascal gets rid of a \.. at the end of the path }
  492. { Now remove also any reference to '\..' at end of line
  493. + of course previous dir.. }
  494. i:=pos('\..',pa);
  495. if i<>0 then
  496. begin
  497. if i = length(pa) - 2 then
  498. begin
  499. j:=i-1;
  500. while (j>1) and (pa[j]<>'\') do
  501. dec (j);
  502. delete (pa,j,i-j+3);
  503. end;
  504. pa := pa + '\';
  505. end;
  506. { Remove End . and \}
  507. if (length(pa)>0) and (pa[length(pa)]='.') then
  508. dec(byte(pa[0]));
  509. { if only the drive + a '\' is left then the '\' should be left to prevtn the program
  510. accessing the current directory on the drive rather than the root!}
  511. { if the last char of path = '\' then leave it in as this is what TP does! }
  512. if ((length(pa)>3) and (pa[length(pa)]='\')) and (path[length(path)] <> '\') then
  513. dec(byte(pa[0]));
  514. { if only a drive is given in path then there should be a '\' at the
  515. end of the string given back }
  516. if length(path) = 2 then pa := pa + '\';
  517. fexpand:=pa;
  518. end;
  519. Function FSearch(path: pathstr; dirlist: string): pathstr;
  520. var
  521. i,p1 : longint;
  522. s : searchrec;
  523. newdir : pathstr;
  524. begin
  525. { No wildcards allowed in these things }
  526. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  527. fsearch:=''
  528. else
  529. begin
  530. { allow slash as backslash }
  531. for i:=1 to length(dirlist) do
  532. if dirlist[i]='/' then dirlist[i]:='\';
  533. repeat
  534. p1:=pos(';',dirlist);
  535. if p1=0 then
  536. begin
  537. newdir:=copy(dirlist,1,p1-1);
  538. delete(dirlist,1,p1);
  539. end
  540. else
  541. begin
  542. newdir:=dirlist;
  543. dirlist:='';
  544. end;
  545. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  546. newdir:=newdir+'\';
  547. findfirst(newdir+path,anyfile,s);
  548. if doserror=0 then
  549. newdir:=newdir+path
  550. else
  551. newdir:='';
  552. until (dirlist='') or (newdir<>'');
  553. fsearch:=newdir;
  554. end;
  555. end;
  556. procedure getftime(var f;var tim : longint);
  557. begin
  558. tim:=FileGetDate(filerec(f).handle);
  559. end;
  560. procedure setftime(var f;time : longint);
  561. begin
  562. {$ifdef linux}
  563. FileSetDate(filerec(f).name,Time);
  564. {$else}
  565. FileSetDate(filerec(f).handle,Time);
  566. {$endif}
  567. end;
  568. {$ifdef linux}
  569. procedure getfattr(var f;var attr : word);
  570. Var
  571. info : tstatbuf;
  572. LinAttr : longint;
  573. Begin
  574. DosError:=0;
  575. if (FStat(filerec(f).handle,info)<>0) then
  576. begin
  577. Attr:=0;
  578. DosError:=3;
  579. exit;
  580. end
  581. else
  582. LinAttr:=Info.st_Mode;
  583. if S_ISDIR(LinAttr) then
  584. Attr:=$10
  585. else
  586. Attr:=$20;
  587. if Access(@filerec(f).name,W_OK)<>0 then
  588. Attr:=Attr or $1;
  589. if (not S_ISDIR(LinAttr)) and (filerec(f).name[0]='.') then
  590. Attr:=Attr or $2;
  591. end;
  592. {$else}
  593. procedure getfattr(var f;var attr : word);
  594. var
  595. l : longint;
  596. begin
  597. l:=FileGetAttr(filerec(f).name);
  598. attr:=l;
  599. end;
  600. {$endif}
  601. procedure setfattr(var f;attr : word);
  602. begin
  603. {$ifdef MSWindows}
  604. FileSetAttr(filerec(f).name,attr);
  605. {$endif}
  606. end;
  607. {******************************************************************************
  608. --- Environment ---
  609. ******************************************************************************}
  610. {
  611. The environment is a block of zero terminated strings
  612. terminated by a #0
  613. }
  614. {$ifdef MSWindows}
  615. function GetEnvironmentStrings : pchar;stdcall;
  616. external 'Kernel32.dll' name 'GetEnvironmentStringsA';
  617. function FreeEnvironmentStrings(p : pchar) : boolean;stdcall;
  618. external 'Kernel32.dll' name 'FreeEnvironmentStringsA';
  619. function envcount : longint;
  620. var
  621. hp,p : pchar;
  622. count : longint;
  623. begin
  624. p:=GetEnvironmentStrings;
  625. hp:=p;
  626. count:=0;
  627. while hp^<>#0 do
  628. begin
  629. { next string entry}
  630. hp:=hp+strlen(hp)+1;
  631. inc(count);
  632. end;
  633. FreeEnvironmentStrings(p);
  634. envcount:=count;
  635. end;
  636. Function EnvStr(index: integer): string;
  637. var
  638. hp,p : pchar;
  639. count,i : longint;
  640. begin
  641. { envcount takes some time in win32 }
  642. count:=envcount;
  643. { range checking }
  644. if (index<=0) or (index>count) then
  645. begin
  646. envstr:='';
  647. exit;
  648. end;
  649. p:=GetEnvironmentStrings;
  650. hp:=p;
  651. { retrive the string with the given index }
  652. for i:=2 to index do
  653. hp:=hp+strlen(hp)+1;
  654. envstr:=strpas(hp);
  655. FreeEnvironmentStrings(p);
  656. end;
  657. Function GetEnv(envvar: string): string;
  658. var
  659. s : string;
  660. i : longint;
  661. hp,p : pchar;
  662. begin
  663. getenv:='';
  664. p:=GetEnvironmentStrings;
  665. hp:=p;
  666. while hp^<>#0 do
  667. begin
  668. s:=strpas(hp);
  669. i:=pos('=',s);
  670. if copy(s,1,i-1)=envvar then
  671. begin
  672. getenv:=copy(s,i+1,length(s)-i);
  673. break;
  674. end;
  675. { next string entry}
  676. hp:=hp+strlen(hp)+1;
  677. end;
  678. FreeEnvironmentStrings(p);
  679. end;
  680. {$else}
  681. function envcount : longint;
  682. begin
  683. envcount:=0;
  684. end;
  685. Function EnvStr(index: integer): string;
  686. begin
  687. envstr:='';
  688. end;
  689. Function GetEnv(envvar: string): string;
  690. begin
  691. getenv:=GetEnvironmentVariable(envvar);
  692. end;
  693. {$endif}
  694. {******************************************************************************
  695. --- Not Supported ---
  696. ******************************************************************************}
  697. Procedure keep(exitcode : word);
  698. Begin
  699. End;
  700. Procedure getintvec(intno : byte;var vector : pointer);
  701. Begin
  702. End;
  703. Procedure setintvec(intno : byte;vector : pointer);
  704. Begin
  705. End;
  706. end.
  707. {
  708. $Log$
  709. Revision 1.10 2002-08-12 15:08:39 carl
  710. + stab register indexes for powerpc (moved from gdb to cpubase)
  711. + tprocessor enumeration moved to cpuinfo
  712. + linker in target_info is now a class
  713. * many many updates for m68k (will soon start to compile)
  714. - removed some ifdef or correct them for correct cpu
  715. Revision 1.9 2002/05/18 13:34:07 peter
  716. * readded missing revisions
  717. Revision 1.8 2002/05/16 19:46:36 carl
  718. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  719. + try to fix temp allocation (still in ifdef)
  720. + generic constructor calls
  721. + start of tassembler / tmodulebase class cleanup
  722. }