sysutils.pp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Sysutils unit for linux
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit sysutils;
  14. interface
  15. {$MODE objfpc}
  16. { force ansistrings }
  17. {$H+}
  18. {$DEFINE HAS_SLEEP}
  19. {$DEFINE HAS_OSERROR}
  20. {$DEFINE HAS_OSCONFIG}
  21. {$DEFINE HAS_TEMPDIR}
  22. {$DEFINE HASUNIX}
  23. uses
  24. Unix,errors,sysconst,Unixtype;
  25. { Include platform independent interface part }
  26. {$i sysutilh.inc}
  27. Procedure AddDisk(const path:string);
  28. var
  29. Tzseconds : Longint = 0;
  30. implementation
  31. Uses Syscall,Baseunix;
  32. {$Define OS_FILEISREADONLY} // Specific implementation for Unix.
  33. Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
  34. Type
  35. ComStr = String[255];
  36. PathStr = String[255];
  37. DirStr = String[255];
  38. NameStr = String[255];
  39. ExtStr = String[255];
  40. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  41. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  42. {$I fexpand.inc}
  43. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  44. {$UNDEF FPC_FEXPAND_TILDE}
  45. { Include platform independent implementation part }
  46. {$i sysutils.inc}
  47. Const
  48. {Date Translation}
  49. C1970=2440588;
  50. D0 = 1461;
  51. D1 = 146097;
  52. D2 =1721119;
  53. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  54. Var
  55. YYear,XYear,Temp,TempMonth : LongInt;
  56. Begin
  57. Temp:=((JulianDN-D2) shl 2)-1;
  58. JulianDN:=Temp Div D1;
  59. XYear:=(Temp Mod D1) or 3;
  60. YYear:=(XYear Div D0);
  61. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  62. Day:=((Temp Mod 153)+5) Div 5;
  63. TempMonth:=Temp Div 153;
  64. If TempMonth>=10 Then
  65. Begin
  66. inc(YYear);
  67. dec(TempMonth,12);
  68. End;
  69. inc(TempMonth,3);
  70. Month := TempMonth;
  71. Year:=YYear+(JulianDN*100);
  72. end;
  73. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  74. {
  75. Transforms Epoch time into local time (hour, minute,seconds)
  76. }
  77. Var
  78. DateNum: LongInt;
  79. Begin
  80. inc(Epoch,TZSeconds);
  81. Datenum:=(Epoch Div 86400) + c1970;
  82. JulianToGregorian(DateNum,Year,Month,day);
  83. Epoch:=Abs(Epoch Mod 86400);
  84. Hour:=Epoch Div 3600;
  85. Epoch:=Epoch Mod 3600;
  86. Minute:=Epoch Div 60;
  87. Second:=Epoch Mod 60;
  88. End;
  89. {****************************************************************************
  90. File Functions
  91. ****************************************************************************}
  92. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  93. Var
  94. DotPos,SlashPos,i : longint;
  95. Begin
  96. SlashPos:=0;
  97. DotPos:=256;
  98. i:=Length(Path);
  99. While (i>0) and (SlashPos=0) Do
  100. Begin
  101. If (DotPos=256) and (Path[i]='.') Then
  102. begin
  103. DotPos:=i;
  104. end;
  105. If (Path[i]='/') Then
  106. SlashPos:=i;
  107. Dec(i);
  108. End;
  109. Ext:=Copy(Path,DotPos,255);
  110. Dir:=Copy(Path,1,SlashPos);
  111. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  112. End;
  113. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  114. Var LinuxFlags : longint;
  115. BEGIN
  116. LinuxFlags:=0;
  117. Case (Mode and 3) of
  118. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  119. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  120. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  121. end;
  122. FileOpen:=fpOpen (FileName,LinuxFlags);
  123. //!! We need to set locking based on Mode !!
  124. end;
  125. Function FileCreate (Const FileName : String) : Longint;
  126. begin
  127. FileCreate:=fpOpen(FileName,O_RdWr or O_Creat or O_Trunc);
  128. end;
  129. Function FileCreate (Const FileName : String;Mode : Longint) : Longint;
  130. Var LinuxFlags : longint;
  131. BEGIN
  132. LinuxFlags:=0;
  133. Case (Mode and 3) of
  134. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  135. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  136. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  137. end;
  138. FileCreate:=fpOpen(FileName,LinuxFlags or O_Creat or O_Trunc);
  139. end;
  140. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  141. begin
  142. FileRead:=fpRead (Handle,Buffer,Count);
  143. end;
  144. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  145. begin
  146. FileWrite:=fpWrite (Handle,Buffer,Count);
  147. end;
  148. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  149. begin
  150. FileSeek:=fplSeek (Handle,FOffset,Origin);
  151. end;
  152. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  153. begin
  154. {$warning need to add 64bit call }
  155. FileSeek:=fplSeek (Handle,FOffset,Origin);
  156. end;
  157. Procedure FileClose (Handle : Longint);
  158. begin
  159. fpclose(Handle);
  160. end;
  161. Function FileTruncate (Handle,Size: Longint) : boolean;
  162. begin
  163. FileTruncate:=fpftruncate(Handle,Size)>=0;
  164. end;
  165. Function UnixToWinAge(UnixAge : time_t): Longint;
  166. Var
  167. Y,M,D,hh,mm,ss : word;
  168. begin
  169. EpochToLocal(UnixAge,y,m,d,hh,mm,ss);
  170. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  171. end;
  172. Function FileAge (Const FileName : String): Longint;
  173. Var Info : Stat;
  174. Y,M,D,hh,mm,ss : word;
  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. end;
  436. end;
  437. P^.Next:=Nil;
  438. GlobFree(P);
  439. end;
  440. end;
  441. Function DoFind(Var Rslt : TSearchRec) : Longint;
  442. Var
  443. GlobSearchRec : PGlobSearchRec;
  444. begin
  445. Result:=-1;
  446. GlobSearchRec:=Rslt.FindHandle;
  447. If (GlobSearchRec^.GlobHandle<>Nil) then
  448. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  449. If GlobToTSearchRec(Rslt) Then Result:=0;
  450. end;
  451. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  452. Var
  453. GlobSearchRec : PGlobSearchRec;
  454. begin
  455. New(GlobSearchRec);
  456. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  457. GlobSearchRec^.GlobHandle:=Glob(Path);
  458. Rslt.ExcludeAttr:=Not Attr and (faHidden or faSysFile or faVolumeID or faDirectory); //!! Not correct !!
  459. Rslt.FindHandle:=GlobSearchRec;
  460. Result:=DoFind (Rslt);
  461. end;
  462. Function FindNext (Var Rslt : TSearchRec) : Longint;
  463. begin
  464. Result:=DoFind (Rslt);
  465. end;
  466. Procedure FindClose (Var F : TSearchrec);
  467. Var
  468. GlobSearchRec : PGlobSearchRec;
  469. begin
  470. GlobSearchRec:=F.FindHandle;
  471. GlobFree (GlobSearchRec^.GlobHandle);
  472. Dispose(GlobSearchRec);
  473. end;
  474. Function FileGetDate (Handle : Longint) : Longint;
  475. Var Info : Stat;
  476. begin
  477. If (fpFStat(Handle,Info))<0 then
  478. Result:=-1
  479. else
  480. Result:=Info.st_Mtime;
  481. end;
  482. Function FileSetDate (Handle,Age : Longint) : Longint;
  483. begin
  484. // Impossible under Linux from FileHandle !!
  485. FileSetDate:=-1;
  486. end;
  487. Function FileGetAttr (Const FileName : String) : Longint;
  488. Var Info : Stat;
  489. begin
  490. If FpStat (FileName,Info)<0 then
  491. Result:=-1
  492. Else
  493. Result:=LinuxToWinAttr(Pchar(FileName),Info);
  494. end;
  495. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  496. begin
  497. Result:=-1;
  498. end;
  499. Function DeleteFile (Const FileName : String) : Boolean;
  500. begin
  501. Result:=fpUnLink (FileName)>=0;
  502. end;
  503. Function RenameFile (Const OldName, NewName : String) : Boolean;
  504. begin
  505. RenameFile:=BaseUnix.FpRename(OldNAme,NewName)>=0;
  506. end;
  507. Function FileIsReadOnly(const FileName: String): Boolean;
  508. begin
  509. Result := fpAccess(PChar(FileName),W_OK)<>0;
  510. end;
  511. {****************************************************************************
  512. Disk Functions
  513. ****************************************************************************}
  514. {
  515. The Diskfree and Disksize functions need a file on the specified drive, since this
  516. is required for the statfs system call.
  517. These filenames are set in drivestr[0..26], and have been preset to :
  518. 0 - '.' (default drive - hence current dir is ok.)
  519. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  520. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  521. 3 - '/' (C: equivalent of dos is the root partition)
  522. 4..26 (can be set by you're own applications)
  523. ! Use AddDisk() to Add new drives !
  524. They both return -1 when a failure occurs.
  525. }
  526. Const
  527. FixDriveStr : array[0..3] of pchar=(
  528. '.',
  529. '/fd0/.',
  530. '/fd1/.',
  531. '/.'
  532. );
  533. var
  534. Drives : byte;
  535. DriveStr : array[4..26] of pchar;
  536. Procedure AddDisk(const path:string);
  537. begin
  538. if not (DriveStr[Drives]=nil) then
  539. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  540. GetMem(DriveStr[Drives],length(Path)+1);
  541. StrPCopy(DriveStr[Drives],path);
  542. inc(Drives);
  543. if Drives>26 then
  544. Drives:=4;
  545. end;
  546. Function DiskFree(Drive: Byte): int64;
  547. var
  548. fs : tstatfs;
  549. Begin
  550. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  551. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  552. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  553. else
  554. Diskfree:=-1;
  555. End;
  556. Function DiskSize(Drive: Byte): int64;
  557. var
  558. fs : tstatfs;
  559. Begin
  560. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  561. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  562. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  563. else
  564. DiskSize:=-1;
  565. End;
  566. Function GetCurrentDir : String;
  567. begin
  568. GetDir (0,Result);
  569. end;
  570. Function SetCurrentDir (Const NewDir : String) : Boolean;
  571. begin
  572. {$I-}
  573. ChDir(NewDir);
  574. {$I+}
  575. result := (IOResult = 0);
  576. end;
  577. Function CreateDir (Const NewDir : String) : Boolean;
  578. begin
  579. {$I-}
  580. MkDir(NewDir);
  581. {$I+}
  582. result := (IOResult = 0);
  583. end;
  584. Function RemoveDir (Const Dir : String) : Boolean;
  585. begin
  586. {$I-}
  587. RmDir(Dir);
  588. {$I+}
  589. result := (IOResult = 0);
  590. end;
  591. {****************************************************************************
  592. Misc Functions
  593. ****************************************************************************}
  594. procedure Beep;
  595. begin
  596. end;
  597. {****************************************************************************
  598. Locale Functions
  599. ****************************************************************************}
  600. Function GetEpochTime: cint;
  601. {
  602. Get the number of seconds since 00:00, January 1 1970, GMT
  603. the time NOT corrected any way
  604. }
  605. begin
  606. GetEpochTime:=fptime;
  607. end;
  608. procedure GetTime(var hour,min,sec,msec,usec:word);
  609. {
  610. Gets the current time, adjusted to local time
  611. }
  612. var
  613. year,day,month:Word;
  614. tz:timeval;
  615. begin
  616. fpgettimeofday(@tz,nil);
  617. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  618. msec:=tz.tv_usec div 1000;
  619. usec:=tz.tv_usec mod 1000;
  620. end;
  621. procedure GetTime(var hour,min,sec,sec100:word);
  622. {
  623. Gets the current time, adjusted to local time
  624. }
  625. var
  626. usec : word;
  627. begin
  628. gettime(hour,min,sec,sec100,usec);
  629. sec100:=sec100 div 10;
  630. end;
  631. Procedure GetTime(Var Hour,Min,Sec:Word);
  632. {
  633. Gets the current time, adjusted to local time
  634. }
  635. var
  636. msec,usec : Word;
  637. Begin
  638. gettime(hour,min,sec,msec,usec);
  639. End;
  640. Procedure GetDate(Var Year,Month,Day:Word);
  641. {
  642. Gets the current date, adjusted to local time
  643. }
  644. var
  645. hour,minute,second : word;
  646. Begin
  647. EpochToLocal(fptime,year,month,day,hour,minute,second);
  648. End;
  649. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  650. {
  651. Gets the current date, adjusted to local time
  652. }
  653. Begin
  654. EpochToLocal(fptime,year,month,day,hour,minute,second);
  655. End;
  656. { Include timezone handling routines which use /usr/share/timezone info }
  657. {$i timezone.inc}
  658. Procedure GetLocalTime(var SystemTime: TSystemTime);
  659. begin
  660. GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  661. GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  662. SystemTime.MilliSecond := 0;
  663. end ;
  664. Procedure InitAnsi;
  665. Var
  666. i : longint;
  667. begin
  668. { Fill table entries 0 to 127 }
  669. for i := 0 to 96 do
  670. UpperCaseTable[i] := chr(i);
  671. for i := 97 to 122 do
  672. UpperCaseTable[i] := chr(i - 32);
  673. for i := 123 to 191 do
  674. UpperCaseTable[i] := chr(i);
  675. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  676. for i := 0 to 64 do
  677. LowerCaseTable[i] := chr(i);
  678. for i := 65 to 90 do
  679. LowerCaseTable[i] := chr(i + 32);
  680. for i := 91 to 191 do
  681. LowerCaseTable[i] := chr(i);
  682. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  683. end;
  684. Procedure InitInternational;
  685. begin
  686. InitAnsi;
  687. end;
  688. function SysErrorMessage(ErrorCode: Integer): String;
  689. begin
  690. Result:=StrError(ErrorCode);
  691. end;
  692. {****************************************************************************
  693. OS utility functions
  694. ****************************************************************************}
  695. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  696. begin
  697. Result:=StrPas(BaseUnix.FPGetenv(PChar(EnvVar)));
  698. end;
  699. {$define FPC_USE_FPEXEC} // leave the old code under IFDEF for a while.
  700. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
  701. var
  702. pid : longint;
  703. err : longint;
  704. e : EOSError;
  705. CommandLine: AnsiString;
  706. cmdline2 : ppchar;
  707. Begin
  708. { always surround the name of the application by quotes
  709. so that long filenames will always be accepted. But don't
  710. do it if there are already double quotes!
  711. }
  712. {$ifdef FPC_USE_FPEXEC} // Only place we still parse
  713. cmdline2:=nil;
  714. if Comline<>'' Then
  715. begin
  716. CommandLine:=ComLine;
  717. cmdline2:=StringtoPPChar(CommandLine,1);
  718. cmdline2^:=pchar(Path);
  719. end
  720. else
  721. begin
  722. getmem(cmdline2,2*sizeof(pchar));
  723. cmdline2^:=pchar(Path);
  724. cmdline2[1]:=nil;
  725. end;
  726. {$else}
  727. if Pos ('"', Path) = 0 then
  728. CommandLine := '"' + Path + '"'
  729. else
  730. CommandLine := Path;
  731. if ComLine <> '' then
  732. CommandLine := Commandline + ' ' + ComLine;
  733. {$endif}
  734. pid:=fpFork;
  735. if pid=0 then
  736. begin
  737. {The child does the actual exec, and then exits}
  738. {$ifdef FPC_USE_FPEXEC}
  739. fpexecv(pchar(Path),Cmdline2);
  740. {$else}
  741. Execl(CommandLine);
  742. {$endif}
  743. { If the execve fails, we return an exitvalue of 127, to let it be known}
  744. fpExit(127);
  745. end
  746. else
  747. if pid=-1 then {Fork failed}
  748. begin
  749. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  750. e.ErrorCode:=-1;
  751. raise e;
  752. end;
  753. { We're in the parent, let's wait. }
  754. result:=WaitProcess(pid); // WaitPid and result-convert
  755. if (result<0) or (result=127) then
  756. begin
  757. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  758. E.ErrorCode:=result;
  759. Raise E;
  760. end;
  761. End;
  762. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array Of AnsiString):integer;
  763. var
  764. pid : longint;
  765. err : longint;
  766. e : EOSError;
  767. Begin
  768. { always surround the name of the application by quotes
  769. so that long filenames will always be accepted. But don't
  770. do it if there are already double quotes!
  771. }
  772. pid:=fpFork;
  773. if pid=0 then
  774. begin
  775. {The child does the actual exec, and then exits}
  776. fpexecl(Path,Comline);
  777. { If the execve fails, we return an exitvalue of 127, to let it be known}
  778. fpExit(127);
  779. end
  780. else
  781. if pid=-1 then {Fork failed}
  782. begin
  783. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  784. e.ErrorCode:=-1;
  785. raise e;
  786. end;
  787. { We're in the parent, let's wait. }
  788. result:=WaitProcess(pid); // WaitPid and result-convert
  789. if (result<0) or (result=127) then
  790. begin
  791. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  792. E.ErrorCode:=result;
  793. raise E;
  794. end;
  795. End;
  796. procedure Sleep(milliseconds: Cardinal);
  797. Var
  798. fd : Integer;
  799. fds : TfdSet;
  800. timeout : TimeVal;
  801. begin
  802. fd:=FileOpen('/dev/null',fmOpenRead);
  803. If Not(Fd<0) then
  804. try
  805. fpfd_zero(fds);
  806. fpfd_set(0,fds);
  807. timeout.tv_sec:=Milliseconds div 1000;
  808. timeout.tv_usec:=(Milliseconds mod 1000) * 1000;
  809. fpSelect(1,Nil,Nil,@fds,@timeout);
  810. finally
  811. FileClose(fd);
  812. end;
  813. end;
  814. Function GetLastOSError : Integer;
  815. begin
  816. Result:=fpgetErrNo;
  817. end;
  818. { ---------------------------------------------------------------------
  819. Application config files
  820. ---------------------------------------------------------------------}
  821. Function GetHomeDir : String;
  822. begin
  823. Result:=GetEnvironmentVariable('HOME');
  824. If (Result<>'') then
  825. Result:=IncludeTrailingPathDelimiter(Result);
  826. end;
  827. Function GetAppConfigDir(Global : Boolean) : String;
  828. begin
  829. If Global then
  830. Result:=SysConfigDir
  831. else
  832. Result:=GetHomeDir+ApplicationName;
  833. end;
  834. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  835. begin
  836. if Global then
  837. begin
  838. Result:=IncludeTrailingPathDelimiter(SysConfigDir);
  839. if SubDir then
  840. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  841. Result:=Result+ApplicationName+ConfigExtension;
  842. end
  843. else
  844. begin
  845. if SubDir then
  846. begin
  847. Result:=IncludeTrailingPathDelimiter(GetAppConfigDir(False));
  848. Result:=Result+ApplicationName+ConfigExtension;
  849. end
  850. else
  851. begin
  852. Result:=GetHomeDir;
  853. Result:=Result+'.'+ApplicationName;
  854. end;
  855. end;
  856. end;
  857. {****************************************************************************
  858. Initialization code
  859. ****************************************************************************}
  860. Function GetTempDir(Global : Boolean) : String;
  861. begin
  862. If Assigned(OnGetTempDir) then
  863. Result:=OnGetTempDir(Global)
  864. else
  865. begin
  866. Result:=GetEnvironmentVariable('TEMP');
  867. If (Result='') Then
  868. Result:=GetEnvironmentVariable('TMP');
  869. if (Result='') then
  870. Result:='/tmp/' // fallback.
  871. end;
  872. if (Result<>'') then
  873. Result:=IncludeTrailingPathDelimiter(Result);
  874. end;
  875. {****************************************************************************
  876. Initialization code
  877. ****************************************************************************}
  878. Initialization
  879. InitExceptions; { Initialize exceptions. OS independent }
  880. InitInternational; { Initialize internationalization settings }
  881. SysConfigDir:='/etc'; { Initialize system config dir }
  882. Finalization
  883. DoneExceptions;
  884. end.
  885. {
  886. $Log$
  887. Revision 1.49 2004-10-30 20:55:54 marco
  888. * unix interface cleanup
  889. Revision 1.48 2004/10/12 15:22:23 michael
  890. + Fixed sleep: file needs to be closed again
  891. Revision 1.47 2004/10/10 10:28:34 michael
  892. + Implementation of GetTempDir and GetTempFileName
  893. Revision 1.46 2004/08/30 11:20:39 michael
  894. + Give path, not comline in ExecuteProcess
  895. Revision 1.45 2004/08/30 11:13:20 michael
  896. + Fixed ExecuteProcess. Now returns the exit code or raises an exception on failure
  897. Revision 1.44 2004/08/05 07:32:51 michael
  898. Added getappconfig calls
  899. Revision 1.43 2004/07/03 21:50:31 daniel
  900. * Modified bootstrap code so separate prt0.as/prt0_10.as files are no
  901. longer necessary
  902. Revision 1.42 2004/06/15 07:36:03 michael
  903. + Fixed Globtosearchrec to use unixtowinage
  904. Revision 1.41 2004/05/22 14:25:03 michael
  905. + Fixed FindFirst/FindNext so it treats the attributes correctly
  906. Revision 1.40 2004/04/28 20:48:20 peter
  907. * ordinal-pointer conversions fixed
  908. Revision 1.39 2004/04/26 14:50:19 peter
  909. * FileIsReadOnly fixed
  910. Revision 1.38 2004/04/20 18:24:32 marco
  911. * small fix for NIL arg ptr in first executeprocess
  912. Revision 1.37 2004/03/04 22:15:16 marco
  913. * UnixType changes. Please report problems to me.
  914. Revision 1.36 2004/02/13 10:50:23 marco
  915. * Hopefully last large changes to fpexec and friends.
  916. - naming conventions changes from Michael.
  917. - shell functions get alternative under ifdef.
  918. - arraystring function moves to unixutil
  919. - unixutil now regards quotes in stringtoppchar.
  920. - sysutils/unix get executeprocess(ansi,array of ansi), and
  921. both executeprocess functions are fixed
  922. - Sysutils/win32 get executeprocess(ansi,array of ansi)
  923. Revision 1.35 2004/02/12 15:31:06 marco
  924. * First version of fpexec change. Still under ifdef or silently overloaded
  925. Revision 1.34 2004/02/09 17:11:17 marco
  926. * fixed for 1.0 errno->fpgeterrno
  927. Revision 1.33 2004/02/08 14:50:51 michael
  928. + Added fileIsReadOnly
  929. Revision 1.32 2004/02/08 11:01:17 michael
  930. + Implemented getlastoserror
  931. Revision 1.31 2004/01/20 23:13:53 hajny
  932. * ExecuteProcess fixes, ProcessID and ThreadID added
  933. Revision 1.30 2004/01/10 17:34:36 michael
  934. + Implemented sleep() on Unix.
  935. Revision 1.29 2004/01/05 22:42:35 florian
  936. * compilation error fixed
  937. Revision 1.28 2004/01/05 22:37:15 florian
  938. * changed sysutils.exec to ExecuteProcess
  939. Revision 1.27 2004/01/03 09:09:11 marco
  940. * Unix exec(ansistring)
  941. Revision 1.26 2003/11/26 20:35:14 michael
  942. + Some fixes to have everything compile again
  943. Revision 1.25 2003/11/17 10:05:51 marco
  944. * threads for FreeBSD. Not working tho
  945. Revision 1.24 2003/10/25 23:43:59 hajny
  946. * THandle in sysutils common using System.THandle
  947. Revision 1.23 2003/10/07 08:28:49 marco
  948. * fix from Vincent to casetables
  949. Revision 1.22 2003/09/27 12:51:33 peter
  950. * fpISxxx macros renamed to C compliant fpS_ISxxx
  951. Revision 1.21 2003/09/17 19:07:44 marco
  952. * more fixes for Unix<->unixutil
  953. Revision 1.20 2003/09/17 12:41:31 marco
  954. * Uses more baseunix, less unix now
  955. Revision 1.19 2003/09/14 20:15:01 marco
  956. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  957. Revision 1.18 2003/04/01 15:57:41 peter
  958. * made THandle platform dependent and unique type
  959. Revision 1.17 2003/03/30 10:38:00 armin
  960. * corrected typo in DirectoryExists
  961. Revision 1.16 2003/03/29 18:21:42 hajny
  962. * DirectoryExists declaration changed to that one from fixes branch
  963. Revision 1.15 2003/03/28 19:06:59 peter
  964. * directoryexists added
  965. Revision 1.14 2003/01/03 20:41:04 peter
  966. * FileCreate(string,mode) overload added
  967. Revision 1.13 2002/09/07 16:01:28 peter
  968. * old logs removed and tabs fixed
  969. Revision 1.12 2002/01/25 16:23:03 peter
  970. * merged filesearch() fix
  971. }