sysutils.pp 11 KB

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