sysutils.pp 26 KB

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