sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. uses
  19. Unix,errors,sysconst;
  20. { Include platform independent interface part }
  21. {$i sysutilh.inc}
  22. implementation
  23. Uses UnixUtil,Baseunix;
  24. { Include platform independent implementation part }
  25. {$i sysutils.inc}
  26. {****************************************************************************
  27. File Functions
  28. ****************************************************************************}
  29. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  30. Var LinuxFlags : longint;
  31. BEGIN
  32. LinuxFlags:=0;
  33. Case (Mode and 3) of
  34. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  35. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  36. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  37. end;
  38. FileOpen:=fpOpen (FileName,LinuxFlags);
  39. //!! We need to set locking based on Mode !!
  40. end;
  41. Function FileCreate (Const FileName : String) : Longint;
  42. begin
  43. FileCreate:=fpOpen(FileName,O_RdWr or O_Creat or O_Trunc);
  44. end;
  45. Function FileCreate (Const FileName : String;Mode : Longint) : Longint;
  46. Var LinuxFlags : longint;
  47. BEGIN
  48. LinuxFlags:=0;
  49. Case (Mode and 3) of
  50. 0 : LinuxFlags:=LinuxFlags or O_RdOnly;
  51. 1 : LinuxFlags:=LinuxFlags or O_WrOnly;
  52. 2 : LinuxFlags:=LinuxFlags or O_RdWr;
  53. end;
  54. FileCreate:=fpOpen(FileName,LinuxFlags or O_Creat or O_Trunc);
  55. end;
  56. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  57. begin
  58. FileRead:=fpRead (Handle,Buffer,Count);
  59. end;
  60. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  61. begin
  62. FileWrite:=fpWrite (Handle,Buffer,Count);
  63. end;
  64. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  65. begin
  66. FileSeek:=fplSeek (Handle,FOffset,Origin);
  67. end;
  68. Function FileSeek (Handle : Longint; FOffset,Origin : Int64) : Int64;
  69. begin
  70. {$warning need to add 64bit call }
  71. FileSeek:=fplSeek (Handle,FOffset,Origin);
  72. end;
  73. Procedure FileClose (Handle : Longint);
  74. begin
  75. fpclose(Handle);
  76. end;
  77. Function FileTruncate (Handle,Size: Longint) : boolean;
  78. begin
  79. FileTruncate:=fpftruncate(Handle,Size)>=0;
  80. end;
  81. Function FileAge (Const FileName : String): Longint;
  82. Var Info : Stat;
  83. Y,M,D,hh,mm,ss : word;
  84. begin
  85. If fpstat (FileName,Info)<0 then
  86. exit(-1)
  87. else
  88. begin
  89. EpochToLocal(info.st_mtime,y,m,d,hh,mm,ss);
  90. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  91. end;
  92. end;
  93. Function FileExists (Const FileName : String) : Boolean;
  94. Var Info : Stat;
  95. begin
  96. FileExists:=fpstat(filename,Info)>=0;
  97. end;
  98. Function DirectoryExists (Const Directory : String) : Boolean;
  99. Var Info : Stat;
  100. begin
  101. DirectoryExists:=(fpstat(Directory,Info)>=0) and fpS_ISDIR(Info.st_mode);
  102. end;
  103. Function LinuxToWinAttr (FN : Pchar; Const Info : Stat) : Longint;
  104. begin
  105. Result:=faArchive;
  106. If fpS_ISDIR(Info.st_mode) then
  107. Result:=Result or faDirectory;
  108. If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
  109. Result:=Result or faHidden;
  110. If (Info.st_Mode and S_IWUSR)=0 Then
  111. Result:=Result or faReadOnly;
  112. 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
  113. Result:=Result or faSysFile;
  114. end;
  115. {
  116. GlobToSearch takes a glob entry, stats the file.
  117. The glob entry is removed.
  118. If FileAttributes match, the entry is reused
  119. }
  120. Type
  121. TGlobSearchRec = Record
  122. Path : String;
  123. GlobHandle : PGlob;
  124. end;
  125. PGlobSearchRec = ^TGlobSearchRec;
  126. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  127. Var SInfo : Stat;
  128. p : Pglob;
  129. GlobSearchRec : PGlobSearchrec;
  130. begin
  131. GlobSearchRec:=PGlobSearchrec(Info.FindHandle);
  132. P:=GlobSearchRec^.GlobHandle;
  133. Result:=P<>Nil;
  134. If Result then
  135. begin
  136. GlobSearchRec^.GlobHandle:=P^.Next;
  137. Result:=Fpstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo)>=0;
  138. If Result then
  139. begin
  140. Info.Attr:=LinuxToWinAttr(p^.name,SInfo);
  141. Result:=(Info.ExcludeAttr and Info.Attr)=0;
  142. If Result Then
  143. With Info do
  144. begin
  145. Attr:=Info.Attr;
  146. If P^.Name<>Nil then
  147. Name:=strpas(p^.name);
  148. Time:=Sinfo.st_mtime;
  149. Size:=Sinfo.st_Size;
  150. end;
  151. end;
  152. P^.Next:=Nil;
  153. GlobFree(P);
  154. end;
  155. end;
  156. Function DoFind(Var Rslt : TSearchRec) : Longint;
  157. Var
  158. GlobSearchRec : PGlobSearchRec;
  159. begin
  160. Result:=-1;
  161. GlobSearchRec:=PGlobSearchRec(Rslt.FindHandle);
  162. If (GlobSearchRec^.GlobHandle<>Nil) then
  163. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  164. If GlobToTSearchRec(Rslt) Then Result:=0;
  165. end;
  166. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  167. Var
  168. GlobSearchRec : PGlobSearchRec;
  169. begin
  170. New(GlobSearchRec);
  171. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  172. GlobSearchRec^.GlobHandle:=Glob(Path);
  173. Rslt.ExcludeAttr:=Not Attr; //!! Not correct !!
  174. Rslt.FindHandle:=Longint(GlobSearchRec);
  175. Result:=DoFind (Rslt);
  176. end;
  177. Function FindNext (Var Rslt : TSearchRec) : Longint;
  178. begin
  179. Result:=DoFind (Rslt);
  180. end;
  181. Procedure FindClose (Var F : TSearchrec);
  182. Var
  183. GlobSearchRec : PGlobSearchRec;
  184. begin
  185. GlobSearchRec:=PGlobSearchRec(F.FindHandle);
  186. GlobFree (GlobSearchRec^.GlobHandle);
  187. Dispose(GlobSearchRec);
  188. end;
  189. Function FileGetDate (Handle : Longint) : Longint;
  190. Var Info : Stat;
  191. begin
  192. If (fpFStat(Handle,Info))<0 then
  193. Result:=-1
  194. else
  195. Result:=Info.st_Mtime;
  196. end;
  197. Function FileSetDate (Handle,Age : Longint) : Longint;
  198. begin
  199. // Impossible under Linux from FileHandle !!
  200. FileSetDate:=-1;
  201. end;
  202. Function FileGetAttr (Const FileName : String) : Longint;
  203. Var Info : Stat;
  204. begin
  205. If FpStat (FileName,Info)<0 then
  206. Result:=-1
  207. Else
  208. Result:=LinuxToWinAttr(Pchar(FileName),Info);
  209. end;
  210. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  211. begin
  212. Result:=-1;
  213. end;
  214. Function DeleteFile (Const FileName : String) : Boolean;
  215. begin
  216. Result:=fpUnLink (FileName)>=0;
  217. end;
  218. Function RenameFile (Const OldName, NewName : String) : Boolean;
  219. begin
  220. RenameFile:=BaseUnix.FpRename(OldNAme,NewName)>=0;
  221. end;
  222. {****************************************************************************
  223. Disk Functions
  224. ****************************************************************************}
  225. {
  226. The Diskfree and Disksize functions need a file on the specified drive, since this
  227. is required for the statfs system call.
  228. These filenames are set in drivestr[0..26], and have been preset to :
  229. 0 - '.' (default drive - hence current dir is ok.)
  230. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  231. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  232. 3 - '/' (C: equivalent of dos is the root partition)
  233. 4..26 (can be set by you're own applications)
  234. ! Use AddDisk() to Add new drives !
  235. They both return -1 when a failure occurs.
  236. }
  237. Const
  238. FixDriveStr : array[0..3] of pchar=(
  239. '.',
  240. '/fd0/.',
  241. '/fd1/.',
  242. '/.'
  243. );
  244. var
  245. Drives : byte;
  246. DriveStr : array[4..26] of pchar;
  247. Procedure AddDisk(const path:string);
  248. begin
  249. if not (DriveStr[Drives]=nil) then
  250. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  251. GetMem(DriveStr[Drives],length(Path)+1);
  252. StrPCopy(DriveStr[Drives],path);
  253. inc(Drives);
  254. if Drives>26 then
  255. Drives:=4;
  256. end;
  257. Function DiskFree(Drive: Byte): int64;
  258. var
  259. fs : tstatfs;
  260. Begin
  261. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  262. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  263. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  264. else
  265. Diskfree:=-1;
  266. End;
  267. Function DiskSize(Drive: Byte): int64;
  268. var
  269. fs : tstatfs;
  270. Begin
  271. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  272. ((not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  273. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  274. else
  275. DiskSize:=-1;
  276. End;
  277. Function GetCurrentDir : String;
  278. begin
  279. GetDir (0,Result);
  280. end;
  281. Function SetCurrentDir (Const NewDir : String) : Boolean;
  282. begin
  283. {$I-}
  284. ChDir(NewDir);
  285. {$I+}
  286. result := (IOResult = 0);
  287. end;
  288. Function CreateDir (Const NewDir : String) : Boolean;
  289. begin
  290. {$I-}
  291. MkDir(NewDir);
  292. {$I+}
  293. result := (IOResult = 0);
  294. end;
  295. Function RemoveDir (Const Dir : String) : Boolean;
  296. begin
  297. {$I-}
  298. RmDir(Dir);
  299. {$I+}
  300. result := (IOResult = 0);
  301. end;
  302. {****************************************************************************
  303. Misc Functions
  304. ****************************************************************************}
  305. procedure Beep;
  306. begin
  307. end;
  308. {****************************************************************************
  309. Locale Functions
  310. ****************************************************************************}
  311. Procedure GetLocalTime(var SystemTime: TSystemTime);
  312. begin
  313. Unix.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  314. Unix.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  315. SystemTime.MilliSecond := 0;
  316. end ;
  317. Procedure InitAnsi;
  318. Var
  319. i : longint;
  320. begin
  321. { Fill table entries 0 to 127 }
  322. for i := 0 to 96 do
  323. UpperCaseTable[i] := chr(i);
  324. for i := 97 to 122 do
  325. UpperCaseTable[i] := chr(i - 32);
  326. for i := 123 to 191 do
  327. UpperCaseTable[i] := chr(i);
  328. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  329. for i := 0 to 64 do
  330. LowerCaseTable[i] := chr(i);
  331. for i := 65 to 90 do
  332. LowerCaseTable[i] := chr(i + 32);
  333. for i := 91 to 191 do
  334. LowerCaseTable[i] := chr(i);
  335. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  336. end;
  337. Procedure InitInternational;
  338. begin
  339. InitAnsi;
  340. end;
  341. function SysErrorMessage(ErrorCode: Integer): String;
  342. begin
  343. Result:=StrError(ErrorCode);
  344. end;
  345. {****************************************************************************
  346. OS utility functions
  347. ****************************************************************************}
  348. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  349. begin
  350. Result:=StrPas(BaseUnix.FPGetenv(PChar(EnvVar)));
  351. end;
  352. {****************************************************************************
  353. Initialization code
  354. ****************************************************************************}
  355. Initialization
  356. InitExceptions; { Initialize exceptions. OS independent }
  357. InitInternational; { Initialize internationalization settings }
  358. Finalization
  359. DoneExceptions;
  360. end.
  361. {
  362. $Log$
  363. Revision 1.26 2003-11-26 20:35:14 michael
  364. + Some fixes to have everything compile again
  365. Revision 1.25 2003/11/17 10:05:51 marco
  366. * threads for FreeBSD. Not working tho
  367. Revision 1.24 2003/10/25 23:43:59 hajny
  368. * THandle in sysutils common using System.THandle
  369. Revision 1.23 2003/10/07 08:28:49 marco
  370. * fix from Vincent to casetables
  371. Revision 1.22 2003/09/27 12:51:33 peter
  372. * fpISxxx macros renamed to C compliant fpS_ISxxx
  373. Revision 1.21 2003/09/17 19:07:44 marco
  374. * more fixes for Unix<->unixutil
  375. Revision 1.20 2003/09/17 12:41:31 marco
  376. * Uses more baseunix, less unix now
  377. Revision 1.19 2003/09/14 20:15:01 marco
  378. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  379. Revision 1.18 2003/04/01 15:57:41 peter
  380. * made THandle platform dependent and unique type
  381. Revision 1.17 2003/03/30 10:38:00 armin
  382. * corrected typo in DirectoryExists
  383. Revision 1.16 2003/03/29 18:21:42 hajny
  384. * DirectoryExists declaration changed to that one from fixes branch
  385. Revision 1.15 2003/03/28 19:06:59 peter
  386. * directoryexists added
  387. Revision 1.14 2003/01/03 20:41:04 peter
  388. * FileCreate(string,mode) overload added
  389. Revision 1.13 2002/09/07 16:01:28 peter
  390. * old logs removed and tabs fixed
  391. Revision 1.12 2002/01/25 16:23:03 peter
  392. * merged filesearch() fix
  393. }