sysutils.pp 24 KB

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