sysutils.pp 25 KB

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