sysutils.pp 29 KB

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