2
0

sysutils.pp 26 KB

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