sysutils.pp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for linux
  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 sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. {$if defined(BSD) and defined(FPC_USE_LIBC)}
  18. {$define USE_VFORK}
  19. {$endif}
  20. {$DEFINE OS_FILESETDATEBYNAME}
  21. {$DEFINE HAS_SLEEP}
  22. {$DEFINE HAS_OSERROR}
  23. {$DEFINE HAS_OSCONFIG}
  24. {$DEFINE HAS_TEMPDIR}
  25. {$DEFINE HASUNIX}
  26. {$DEFINE HASCREATEGUID}
  27. uses
  28. Unix,errors,sysconst,Unixtype;
  29. { Include platform independent interface part }
  30. {$i sysutilh.inc}
  31. Function AddDisk(const path:string) : Byte;
  32. { the following is Kylix compatibility stuff, it should be moved to a
  33. special compatibilty unit (FK) }
  34. const
  35. RTL_SIGINT = 0;
  36. RTL_SIGFPE = 1;
  37. RTL_SIGSEGV = 2;
  38. RTL_SIGILL = 3;
  39. RTL_SIGBUS = 4;
  40. RTL_SIGQUIT = 5;
  41. RTL_SIGLAST = RTL_SIGQUIT;
  42. RTL_SIGDEFAULT = -1;
  43. type
  44. TSignalState = (ssNotHooked, ssHooked, ssOverridden);
  45. function InquireSignal(RtlSigNum: Integer): TSignalState;
  46. procedure AbandonSignalHandler(RtlSigNum: Integer);
  47. procedure HookSignal(RtlSigNum: Integer);
  48. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  49. implementation
  50. Uses
  51. {$ifdef FPC_USE_LIBC}initc{$ELSE}Syscall{$ENDIF}, Baseunix, unixutil;
  52. function InquireSignal(RtlSigNum: Integer): TSignalState;
  53. begin
  54. end;
  55. procedure AbandonSignalHandler(RtlSigNum: Integer);
  56. begin
  57. end;
  58. procedure HookSignal(RtlSigNum: Integer);
  59. begin
  60. end;
  61. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  62. begin
  63. end;
  64. {$Define OS_FILEISREADONLY} // Specific implementation for Unix.
  65. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  66. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  67. { Include platform independent implementation part }
  68. {$i sysutils.inc}
  69. { Include SysCreateGUID function }
  70. {$i suuid.inc}
  71. Const
  72. {Date Translation}
  73. C1970=2440588;
  74. D0 = 1461;
  75. D1 = 146097;
  76. D2 =1721119;
  77. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  78. Var
  79. YYear,XYear,Temp,TempMonth : LongInt;
  80. Begin
  81. Temp:=((JulianDN-D2) shl 2)-1;
  82. JulianDN:=Temp Div D1;
  83. XYear:=(Temp Mod D1) or 3;
  84. YYear:=(XYear Div D0);
  85. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  86. Day:=((Temp Mod 153)+5) Div 5;
  87. TempMonth:=Temp Div 153;
  88. If TempMonth>=10 Then
  89. Begin
  90. inc(YYear);
  91. dec(TempMonth,12);
  92. End;
  93. inc(TempMonth,3);
  94. Month := TempMonth;
  95. Year:=YYear+(JulianDN*100);
  96. end;
  97. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  98. {
  99. Transforms Epoch time into local time (hour, minute,seconds)
  100. }
  101. Var
  102. DateNum: LongInt;
  103. Begin
  104. inc(Epoch,TZSeconds);
  105. Datenum:=(Epoch Div 86400) + c1970;
  106. JulianToGregorian(DateNum,Year,Month,day);
  107. Epoch:=Abs(Epoch Mod 86400);
  108. Hour:=Epoch Div 3600;
  109. Epoch:=Epoch Mod 3600;
  110. Minute:=Epoch Div 60;
  111. Second:=Epoch Mod 60;
  112. End;
  113. {****************************************************************************
  114. File Functions
  115. ****************************************************************************}
  116. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  117. Var
  118. DotPos,SlashPos,i : longint;
  119. Begin
  120. SlashPos:=0;
  121. DotPos:=256;
  122. i:=Length(Path);
  123. While (i>0) and (SlashPos=0) Do
  124. Begin
  125. If (DotPos=256) and (Path[i]='.') Then
  126. begin
  127. DotPos:=i;
  128. end;
  129. If (Path[i]='/') Then
  130. SlashPos:=i;
  131. Dec(i);
  132. End;
  133. Ext:=Copy(Path,DotPos,255);
  134. Dir:=Copy(Path,1,SlashPos);
  135. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  136. End;
  137. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  138. Var LinuxFlags : longint;
  139. BEGIN
  140. LinuxFlags:=0;
  141. Case (Mode and 3) of
  142. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  143. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  144. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  145. end;
  146. FileOpen:=fpOpen (FileName,LinuxFlags);
  147. //!! We need to set locking based on Mode !!
  148. end;
  149. Function FileCreate (Const FileName : String) : Longint;
  150. begin
  151. FileCreate:=fpOpen(FileName,O_RdWr or O_Creat or O_Trunc);
  152. end;
  153. Function FileCreate (Const FileName : String;Mode : Longint) : Longint;
  154. Var LinuxFlags : longint;
  155. BEGIN
  156. LinuxFlags:=0;
  157. Case (Mode and 3) of
  158. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  159. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  160. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  161. end;
  162. FileCreate:=fpOpen(FileName,LinuxFlags or O_Creat or O_Trunc);
  163. end;
  164. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  165. begin
  166. FileRead:=fpRead (Handle,Buffer,Count);
  167. end;
  168. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  169. begin
  170. FileWrite:=fpWrite (Handle,Buffer,Count);
  171. end;
  172. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  173. begin
  174. FileSeek:=fplSeek (Handle,FOffset,Origin);
  175. end;
  176. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  177. begin
  178. {$warning need to add 64bit call }
  179. FileSeek:=fplSeek (Handle,FOffset,Origin);
  180. end;
  181. Procedure FileClose (Handle : Longint);
  182. begin
  183. fpclose(Handle);
  184. end;
  185. Function FileTruncate (Handle,Size: Longint) : boolean;
  186. begin
  187. FileTruncate:=fpftruncate(Handle,Size)>=0;
  188. end;
  189. Function UnixToWinAge(UnixAge : time_t): Longint;
  190. Var
  191. Y,M,D,hh,mm,ss : word;
  192. begin
  193. EpochToLocal(UnixAge,y,m,d,hh,mm,ss);
  194. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  195. end;
  196. Function FileAge (Const FileName : String): Longint;
  197. Var Info : Stat;
  198. begin
  199. If fpstat (FileName,Info)<0 then
  200. exit(-1)
  201. else
  202. Result:=UnixToWinAge(info.st_mtime);
  203. end;
  204. Function FileExists (Const FileName : String) : Boolean;
  205. Var Info : Stat;
  206. begin
  207. FileExists:=fpstat(filename,Info)>=0;
  208. end;
  209. Function DirectoryExists (Const Directory : String) : Boolean;
  210. Var Info : Stat;
  211. begin
  212. DirectoryExists:=(fpstat(Directory,Info)>=0) and fpS_ISDIR(Info.st_mode);
  213. end;
  214. Function LinuxToWinAttr (FN : Pchar; Const Info : Stat) : Longint;
  215. begin
  216. Result:=faArchive;
  217. If fpS_ISDIR(Info.st_mode) then
  218. Result:=Result or faDirectory;
  219. If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
  220. Result:=Result or faHidden;
  221. If (Info.st_Mode and S_IWUSR)=0 Then
  222. Result:=Result or faReadOnly;
  223. If fpS_ISSOCK(Info.st_mode) or fpS_ISBLK(Info.st_mode) or fpS_ISCHR(Info.st_mode) or fpS_ISFIFO(Info.st_mode) Then
  224. Result:=Result or faSysFile;
  225. If fpS_ISLNK(Info.st_mode) Then
  226. Result:=Result or faSymLink;
  227. end;
  228. type
  229. pglob = ^tglob;
  230. tglob = record
  231. name : pchar;
  232. next : pglob;
  233. end;
  234. Function Dirname(Const path:pathstr):pathstr;
  235. {
  236. This function returns the directory part of a complete path.
  237. Unless the directory is root '/', The last character is not
  238. a slash.
  239. }
  240. var
  241. Dir : DirStr;
  242. Name : NameStr;
  243. Ext : ExtStr;
  244. begin
  245. FSplit(Path,Dir,Name,Ext);
  246. if length(Dir)>1 then
  247. Delete(Dir,length(Dir),1);
  248. DirName:=Dir;
  249. end;
  250. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  251. {
  252. This function returns the filename part of a complete path. If suf is
  253. supplied, it is cut off the filename.
  254. }
  255. var
  256. Dir : DirStr;
  257. Name : NameStr;
  258. Ext : ExtStr;
  259. begin
  260. FSplit(Path,Dir,Name,Ext);
  261. if Suf<>Ext then
  262. Name:=Name+Ext;
  263. BaseName:=Name;
  264. end;
  265. Function FNMatch(const Pattern,Name:shortstring):Boolean;
  266. Var
  267. LenPat,LenName : longint;
  268. Function DoFNMatch(i,j:longint):Boolean;
  269. Var
  270. Found : boolean;
  271. Begin
  272. Found:=true;
  273. While Found and (i<=LenPat) Do
  274. Begin
  275. Case Pattern[i] of
  276. '?' : Found:=(j<=LenName);
  277. '*' : Begin
  278. {find the next character in pattern, different of ? and *}
  279. while Found do
  280. begin
  281. inc(i);
  282. if i>LenPat then Break;
  283. case Pattern[i] of
  284. '*' : ;
  285. '?' : begin
  286. if j>LenName then begin DoFNMatch:=false; Exit; end;
  287. inc(j);
  288. end;
  289. else
  290. Found:=false;
  291. end;
  292. end;
  293. Assert((i>LenPat) or ( (Pattern[i]<>'*') and (Pattern[i]<>'?') ));
  294. {Now, find in name the character which i points to, if the * or ?
  295. wasn't the last character in the pattern, else, use up all the
  296. chars in name}
  297. Found:=false;
  298. if (i<=LenPat) then
  299. begin
  300. repeat
  301. {find a letter (not only first !) which maches pattern[i]}
  302. while (j<=LenName) and (name[j]<>pattern[i]) do
  303. inc (j);
  304. if (j<LenName) then
  305. begin
  306. if DoFnMatch(i+1,j+1) then
  307. begin
  308. i:=LenPat;
  309. j:=LenName;{we can stop}
  310. Found:=true;
  311. Break;
  312. end else
  313. inc(j);{We didn't find one, need to look further}
  314. end else
  315. if j=LenName then
  316. begin
  317. Found:=true;
  318. Break;
  319. end;
  320. { This 'until' condition must be j>LenName, not j>=LenName.
  321. That's because when we 'need to look further' and
  322. j = LenName then loop must not terminate. }
  323. until (j>LenName);
  324. end else
  325. begin
  326. j:=LenName;{we can stop}
  327. Found:=true;
  328. end;
  329. end;
  330. else {not a wildcard character in pattern}
  331. Found:=(j<=LenName) and (pattern[i]=name[j]);
  332. end;
  333. inc(i);
  334. inc(j);
  335. end;
  336. DoFnMatch:=Found and (j>LenName);
  337. end;
  338. Begin {start FNMatch}
  339. LenPat:=Length(Pattern);
  340. LenName:=Length(Name);
  341. FNMatch:=DoFNMatch(1,1);
  342. End;
  343. Procedure Globfree(var p : pglob);
  344. {
  345. Release memory occupied by pglob structure, and names in it.
  346. sets p to nil.
  347. }
  348. var
  349. temp : pglob;
  350. begin
  351. while assigned(p) do
  352. begin
  353. temp:=p^.next;
  354. if assigned(p^.name) then
  355. freemem(p^.name);
  356. dispose(p);
  357. p:=temp;
  358. end;
  359. end;
  360. Function Glob(Const path:pathstr):pglob;
  361. {
  362. Fills a tglob structure with entries matching path,
  363. and returns a pointer to it. Returns nil on error,
  364. linuxerror is set accordingly.
  365. }
  366. var
  367. temp,
  368. temp2 : string[255];
  369. thedir : pdir;
  370. buffer : pdirent;
  371. root,
  372. current : pglob;
  373. begin
  374. { Get directory }
  375. temp:=dirname(path);
  376. if temp='' then
  377. temp:='.';
  378. temp:=temp+#0;
  379. thedir:=fpopendir(@temp[1]);
  380. if thedir=nil then
  381. exit(nil);
  382. temp:=basename(path,''); { get the pattern }
  383. if thedir^.dd_fd<0 then
  384. exit(nil);
  385. {get the entries}
  386. root:=nil;
  387. current:=nil;
  388. repeat
  389. buffer:=fpreaddir(thedir^);
  390. if buffer=nil then
  391. break;
  392. temp2:=strpas(@(buffer^.d_name[0]));
  393. if fnmatch(temp,temp2) then
  394. begin
  395. if root=nil then
  396. begin
  397. new(root);
  398. current:=root;
  399. end
  400. else
  401. begin
  402. new(current^.next);
  403. current:=current^.next;
  404. end;
  405. if current=nil then
  406. begin
  407. fpseterrno(ESysENOMEM);
  408. globfree(root);
  409. break;
  410. end;
  411. current^.next:=nil;
  412. getmem(current^.name,length(temp2)+1);
  413. if current^.name=nil then
  414. begin
  415. fpseterrno(ESysENOMEM);
  416. globfree(root);
  417. break;
  418. end;
  419. move(buffer^.d_name[0],current^.name^,length(temp2)+1);
  420. end;
  421. until false;
  422. fpclosedir(thedir^);
  423. glob:=root;
  424. end;
  425. {
  426. GlobToSearch takes a glob entry, stats the file.
  427. The glob entry is removed.
  428. If FileAttributes match, the entry is reused
  429. }
  430. Type
  431. TGlobSearchRec = Record
  432. Path : shortString;
  433. GlobHandle : PGlob;
  434. end;
  435. PGlobSearchRec = ^TGlobSearchRec;
  436. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  437. Var SInfo : Stat;
  438. p : Pglob;
  439. GlobSearchRec : PGlobSearchrec;
  440. begin
  441. GlobSearchRec:=Info.FindHandle;
  442. P:=GlobSearchRec^.GlobHandle;
  443. Result:=P<>Nil;
  444. If Result then
  445. begin
  446. GlobSearchRec^.GlobHandle:=P^.Next;
  447. Result:=Fpstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo)>=0;
  448. Info.PathOnly:=GlobSearchRec^.Path;
  449. If Result then
  450. begin
  451. Info.Attr:=LinuxToWinAttr(p^.name,SInfo);
  452. Result:=(Info.ExcludeAttr and Info.Attr)=0;
  453. If Result Then
  454. With Info do
  455. begin
  456. Attr:=Info.Attr;
  457. If P^.Name<>Nil then
  458. Name:=strpas(p^.name);
  459. Time:=UnixToWinAge(Sinfo.st_mtime);
  460. Size:=Sinfo.st_Size;
  461. Mode:=Sinfo.st_mode;
  462. end;
  463. end;
  464. P^.Next:=Nil;
  465. GlobFree(P);
  466. end;
  467. end;
  468. Function DoFind(Var Rslt : TSearchRec) : Longint;
  469. Var
  470. GlobSearchRec : PGlobSearchRec;
  471. begin
  472. Result:=-1;
  473. GlobSearchRec:=Rslt.FindHandle;
  474. If (GlobSearchRec^.GlobHandle<>Nil) then
  475. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  476. If GlobToTSearchRec(Rslt) Then Result:=0;
  477. end;
  478. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  479. Var
  480. GlobSearchRec : PGlobSearchRec;
  481. begin
  482. New(GlobSearchRec);
  483. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  484. GlobSearchRec^.GlobHandle:=Glob(Path);
  485. Rslt.ExcludeAttr:=Not Attr and (faHidden or faSysFile or faVolumeID or faDirectory); //!! Not correct !!
  486. Rslt.FindHandle:=GlobSearchRec;
  487. Result:=DoFind (Rslt);
  488. end;
  489. Function FindNext (Var Rslt : TSearchRec) : Longint;
  490. begin
  491. Result:=DoFind (Rslt);
  492. end;
  493. Procedure FindClose (Var F : TSearchrec);
  494. Var
  495. GlobSearchRec : PGlobSearchRec;
  496. begin
  497. GlobSearchRec:=F.FindHandle;
  498. GlobFree (GlobSearchRec^.GlobHandle);
  499. Dispose(GlobSearchRec);
  500. end;
  501. Function FileGetDate (Handle : Longint) : Longint;
  502. Var Info : Stat;
  503. begin
  504. If (fpFStat(Handle,Info))<0 then
  505. Result:=-1
  506. else
  507. Result:=Info.st_Mtime;
  508. end;
  509. Function FileSetDate (Handle,Age : Longint) : Longint;
  510. begin
  511. // Impossible under Linux from FileHandle !!
  512. FileSetDate:=-1;
  513. end;
  514. Function FileGetAttr (Const FileName : String) : Longint;
  515. Var Info : Stat;
  516. begin
  517. If FpStat (FileName,Info)<0 then
  518. Result:=-1
  519. Else
  520. Result:=LinuxToWinAttr(Pchar(ExtractFileName(FileName)),Info);
  521. end;
  522. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  523. begin
  524. Result:=-1;
  525. end;
  526. Function DeleteFile (Const FileName : String) : Boolean;
  527. begin
  528. Result:=fpUnLink (FileName)>=0;
  529. end;
  530. Function RenameFile (Const OldName, NewName : String) : Boolean;
  531. begin
  532. RenameFile:=BaseUnix.FpRename(OldNAme,NewName)>=0;
  533. end;
  534. Function FileIsReadOnly(const FileName: String): Boolean;
  535. begin
  536. Result := fpAccess(PChar(FileName),W_OK)<>0;
  537. end;
  538. Function FileSetDate (Const FileName : String;Age : Longint) : Longint;
  539. var
  540. t: TUTimBuf;
  541. begin
  542. Result := 0;
  543. t.actime := Age;
  544. t.modtime := Age;
  545. if fputime(PChar(FileName), @t) = -1 then
  546. Result := fpgeterrno;
  547. end;
  548. {****************************************************************************
  549. Disk Functions
  550. ****************************************************************************}
  551. {
  552. The Diskfree and Disksize functions need a file on the specified drive, since this
  553. is required for the statfs system call.
  554. These filenames are set in drivestr[0..26], and have been preset to :
  555. 0 - '.' (default drive - hence current dir is ok.)
  556. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  557. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  558. 3 - '/' (C: equivalent of dos is the root partition)
  559. 4..26 (can be set by you're own applications)
  560. ! Use AddDisk() to Add new drives !
  561. They both return -1 when a failure occurs.
  562. }
  563. Const
  564. FixDriveStr : array[0..3] of pchar=(
  565. '.',
  566. '/fd0/.',
  567. '/fd1/.',
  568. '/.'
  569. );
  570. var
  571. Drives : byte;
  572. DriveStr : array[4..26] of pchar;
  573. Function AddDisk(const path:string) : Byte;
  574. begin
  575. if not (DriveStr[Drives]=nil) then
  576. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  577. GetMem(DriveStr[Drives],length(Path)+1);
  578. StrPCopy(DriveStr[Drives],path);
  579. inc(Drives);
  580. if Drives>26 then
  581. Drives:=4;
  582. Result:=Drives;
  583. end;
  584. Function DiskFree(Drive: Byte): int64;
  585. var
  586. fs : tstatfs;
  587. Begin
  588. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  589. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  590. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  591. else
  592. Diskfree:=-1;
  593. End;
  594. Function DiskSize(Drive: Byte): int64;
  595. var
  596. fs : tstatfs;
  597. Begin
  598. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  599. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  600. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  601. else
  602. DiskSize:=-1;
  603. End;
  604. Function GetCurrentDir : String;
  605. begin
  606. GetDir (0,Result);
  607. end;
  608. Function SetCurrentDir (Const NewDir : String) : Boolean;
  609. begin
  610. {$I-}
  611. ChDir(NewDir);
  612. {$I+}
  613. result := (IOResult = 0);
  614. end;
  615. Function CreateDir (Const NewDir : String) : Boolean;
  616. begin
  617. {$I-}
  618. MkDir(NewDir);
  619. {$I+}
  620. result := (IOResult = 0);
  621. end;
  622. Function RemoveDir (Const Dir : String) : Boolean;
  623. begin
  624. {$I-}
  625. RmDir(Dir);
  626. {$I+}
  627. result := (IOResult = 0);
  628. end;
  629. {****************************************************************************
  630. Misc Functions
  631. ****************************************************************************}
  632. procedure Beep;
  633. begin
  634. end;
  635. {****************************************************************************
  636. Locale Functions
  637. ****************************************************************************}
  638. Function GetEpochTime: cint;
  639. {
  640. Get the number of seconds since 00:00, January 1 1970, GMT
  641. the time NOT corrected any way
  642. }
  643. begin
  644. GetEpochTime:=fptime;
  645. end;
  646. procedure GetTime(var hour,min,sec,msec,usec:word);
  647. {
  648. Gets the current time, adjusted to local time
  649. }
  650. var
  651. year,day,month:Word;
  652. tz:timeval;
  653. begin
  654. fpgettimeofday(@tz,nil);
  655. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  656. msec:=tz.tv_usec div 1000;
  657. usec:=tz.tv_usec mod 1000;
  658. end;
  659. procedure GetTime(var hour,min,sec,sec100:word);
  660. {
  661. Gets the current time, adjusted to local time
  662. }
  663. var
  664. usec : word;
  665. begin
  666. gettime(hour,min,sec,sec100,usec);
  667. sec100:=sec100 div 10;
  668. end;
  669. Procedure GetTime(Var Hour,Min,Sec:Word);
  670. {
  671. Gets the current time, adjusted to local time
  672. }
  673. var
  674. msec,usec : Word;
  675. Begin
  676. gettime(hour,min,sec,msec,usec);
  677. End;
  678. Procedure GetDate(Var Year,Month,Day:Word);
  679. {
  680. Gets the current date, adjusted to local time
  681. }
  682. var
  683. hour,minute,second : word;
  684. Begin
  685. EpochToLocal(fptime,year,month,day,hour,minute,second);
  686. End;
  687. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  688. {
  689. Gets the current date, adjusted to local time
  690. }
  691. Begin
  692. EpochToLocal(fptime,year,month,day,hour,minute,second);
  693. End;
  694. Procedure GetLocalTime(var SystemTime: TSystemTime);
  695. var
  696. usecs : Word;
  697. begin
  698. GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  699. GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  700. // SystemTime.MilliSecond := 0;
  701. end ;
  702. Procedure InitAnsi;
  703. Var
  704. i : longint;
  705. begin
  706. { Fill table entries 0 to 127 }
  707. for i := 0 to 96 do
  708. UpperCaseTable[i] := chr(i);
  709. for i := 97 to 122 do
  710. UpperCaseTable[i] := chr(i - 32);
  711. for i := 123 to 191 do
  712. UpperCaseTable[i] := chr(i);
  713. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  714. for i := 0 to 64 do
  715. LowerCaseTable[i] := chr(i);
  716. for i := 65 to 90 do
  717. LowerCaseTable[i] := chr(i + 32);
  718. for i := 91 to 191 do
  719. LowerCaseTable[i] := chr(i);
  720. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  721. end;
  722. Procedure InitInternational;
  723. begin
  724. InitInternationalGeneric;
  725. InitAnsi;
  726. end;
  727. function SysErrorMessage(ErrorCode: Integer): String;
  728. begin
  729. Result:=StrError(ErrorCode);
  730. end;
  731. {****************************************************************************
  732. OS utility functions
  733. ****************************************************************************}
  734. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  735. begin
  736. Result:=StrPas(BaseUnix.FPGetenv(PChar(EnvVar)));
  737. end;
  738. Function GetEnvironmentVariableCount : Integer;
  739. begin
  740. Result:=FPCCountEnvVar(EnvP);
  741. end;
  742. Function GetEnvironmentString(Index : Integer) : String;
  743. begin
  744. Result:=FPCGetEnvStrFromP(Envp,Index);
  745. end;
  746. {$define FPC_USE_FPEXEC} // leave the old code under IFDEF for a while.
  747. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  748. var
  749. pid : longint;
  750. e : EOSError;
  751. CommandLine: AnsiString;
  752. cmdline2 : ppchar;
  753. Begin
  754. { always surround the name of the application by quotes
  755. so that long filenames will always be accepted. But don't
  756. do it if there are already double quotes!
  757. }
  758. {$ifdef FPC_USE_FPEXEC} // Only place we still parse
  759. cmdline2:=nil;
  760. if Comline<>'' Then
  761. begin
  762. CommandLine:=ComLine;
  763. { Make an unique copy because stringtoppchar modifies the
  764. string }
  765. UniqueString(CommandLine);
  766. cmdline2:=StringtoPPChar(CommandLine,1);
  767. cmdline2^:=pchar(Path);
  768. end
  769. else
  770. begin
  771. getmem(cmdline2,2*sizeof(pchar));
  772. cmdline2^:=pchar(Path);
  773. cmdline2[1]:=nil;
  774. end;
  775. {$else}
  776. if Pos ('"', Path) = 0 then
  777. CommandLine := '"' + Path + '"'
  778. else
  779. CommandLine := Path;
  780. if ComLine <> '' then
  781. CommandLine := Commandline + ' ' + ComLine;
  782. {$endif}
  783. {$ifdef USE_VFORK}
  784. pid:=fpvFork;
  785. {$else USE_VFORK}
  786. pid:=fpFork;
  787. {$endif USE_VFORK}
  788. if pid=0 then
  789. begin
  790. {The child does the actual exec, and then exits}
  791. {$ifdef FPC_USE_FPEXEC}
  792. fpexecv(pchar(Path),Cmdline2);
  793. {$else}
  794. Execl(CommandLine);
  795. {$endif}
  796. { If the execve fails, we return an exitvalue of 127, to let it be known}
  797. fpExit(127);
  798. end
  799. else
  800. if pid=-1 then {Fork failed}
  801. begin
  802. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  803. e.ErrorCode:=-1;
  804. raise e;
  805. end;
  806. { We're in the parent, let's wait. }
  807. result:=WaitProcess(pid); // WaitPid and result-convert
  808. if (result<0) or (result=127) then
  809. begin
  810. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  811. E.ErrorCode:=result;
  812. Raise E;
  813. end;
  814. End;
  815. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array Of AnsiString):integer;
  816. var
  817. pid : longint;
  818. e : EOSError;
  819. Begin
  820. pid:=fpFork;
  821. if pid=0 then
  822. begin
  823. {The child does the actual exec, and then exits}
  824. fpexecl(Path,Comline);
  825. { If the execve fails, we return an exitvalue of 127, to let it be known}
  826. fpExit(127);
  827. end
  828. else
  829. if pid=-1 then {Fork failed}
  830. begin
  831. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  832. e.ErrorCode:=-1;
  833. raise e;
  834. end;
  835. { We're in the parent, let's wait. }
  836. result:=WaitProcess(pid); // WaitPid and result-convert
  837. if (result<0) or (result=127) then
  838. begin
  839. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  840. E.ErrorCode:=result;
  841. raise E;
  842. end;
  843. End;
  844. procedure Sleep(milliseconds: Cardinal);
  845. Var
  846. timeout,timeoutresult : TTimespec;
  847. begin
  848. timeout.tv_sec:=milliseconds div 1000;
  849. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  850. fpnanosleep(@timeout,@timeoutresult);
  851. end;
  852. Function GetLastOSError : Integer;
  853. begin
  854. Result:=fpgetErrNo;
  855. end;
  856. { ---------------------------------------------------------------------
  857. Application config files
  858. ---------------------------------------------------------------------}
  859. Function GetHomeDir : String;
  860. begin
  861. Result:=GetEnvironmentVariable('HOME');
  862. If (Result<>'') then
  863. Result:=IncludeTrailingPathDelimiter(Result);
  864. end;
  865. { Follows base-dir spec,
  866. see [http://freedesktop.org/Standards/basedir-spec].
  867. Always ends with PathDelim. }
  868. Function XdgConfigHome : String;
  869. begin
  870. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  871. if (Result='') then
  872. Result:=GetHomeDir + '.config/'
  873. else
  874. Result:=IncludeTrailingPathDelimiter(Result);
  875. end;
  876. Function GetAppConfigDir(Global : Boolean) : String;
  877. begin
  878. If Global then
  879. Result:=SysConfigDir
  880. else
  881. Result:=XdgConfigHome + ApplicationName;
  882. end;
  883. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  884. begin
  885. if Global then
  886. begin
  887. Result:=IncludeTrailingPathDelimiter(SysConfigDir);
  888. if SubDir then
  889. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  890. Result:=Result+ApplicationName+ConfigExtension;
  891. end
  892. else
  893. begin
  894. if SubDir then
  895. begin
  896. Result:=IncludeTrailingPathDelimiter(GetAppConfigDir(False));
  897. Result:=Result+ApplicationName+ConfigExtension;
  898. end
  899. else
  900. begin
  901. Result:=XdgConfigHome + ApplicationName + ConfigExtension;
  902. end;
  903. end;
  904. end;
  905. {****************************************************************************
  906. Initialization code
  907. ****************************************************************************}
  908. Function GetTempDir(Global : Boolean) : String;
  909. begin
  910. If Assigned(OnGetTempDir) then
  911. Result:=OnGetTempDir(Global)
  912. else
  913. begin
  914. Result:=GetEnvironmentVariable('TEMP');
  915. If (Result='') Then
  916. Result:=GetEnvironmentVariable('TMP');
  917. if (Result='') then
  918. Result:='/tmp/' // fallback.
  919. end;
  920. if (Result<>'') then
  921. Result:=IncludeTrailingPathDelimiter(Result);
  922. end;
  923. {****************************************************************************
  924. Initialization code
  925. ****************************************************************************}
  926. Initialization
  927. InitExceptions; { Initialize exceptions. OS independent }
  928. InitInternational; { Initialize internationalization settings }
  929. SysConfigDir:='/etc'; { Initialize system config dir }
  930. Finalization
  931. DoneExceptions;
  932. end.