dmisc.pas 21 KB

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