dmisc.pas 20 KB

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