sysutils.pp 25 KB

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