sysutils.pp 29 KB

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