2
0

sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. linux,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 FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  45. begin
  46. FileRead:=fdRead (Handle,Buffer,Count);
  47. end;
  48. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  49. begin
  50. FileWrite:=fdWrite (Handle,Buffer,Count);
  51. end;
  52. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  53. begin
  54. FileSeek:=fdSeek (Handle,FOffset,Origin);
  55. end;
  56. Procedure FileClose (Handle : Longint);
  57. begin
  58. fdclose(Handle);
  59. end;
  60. Function FileTruncate (Handle,Size: Longint) : boolean;
  61. begin
  62. FileTruncate:=fdtruncate(Handle,Size);
  63. end;
  64. Function FileAge (Const FileName : String): Longint;
  65. Var Info : Stat;
  66. Y,M,D,hh,mm,ss : word;
  67. begin
  68. If not fstat (FileName,Info) then
  69. exit(-1)
  70. else
  71. begin
  72. EpochToLocal(info.mtime,y,m,d,hh,mm,ss);
  73. Result:=DateTimeToFileDate(EncodeDate(y,m,d)+EncodeTime(hh,mm,ss,0));
  74. end;
  75. end;
  76. Function FileExists (Const FileName : String) : Boolean;
  77. Var Info : Stat;
  78. begin
  79. FileExists:=fstat(filename,Info);
  80. end;
  81. Function LinuxToWinAttr (FN : Char; Const Info : Stat) : Longint;
  82. begin
  83. Result:=faArchive;
  84. If FN='.' then
  85. Result:=Result or faHidden;
  86. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  87. Result:=Result or faDirectory;
  88. If (Info.Mode and STAT_IWUSR)=0 Then
  89. Result:=Result or faReadOnly;
  90. If (Info.Mode and
  91. (STAT_IFSOCK or STAT_IFBLK or STAT_IFCHR or STAT_IFIFO))<>0 then
  92. Result:=Result or faSysFile;
  93. end;
  94. {
  95. GlobToSearch takes a glob entry, stats the file.
  96. The glob entry is removed.
  97. If FileAttributes match, the entry is reused
  98. }
  99. Type
  100. TGlobSearchRec = Record
  101. Path : String;
  102. GlobHandle : PGlob;
  103. end;
  104. PGlobSearchRec = ^TGlobSearchRec;
  105. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  106. Var SInfo : Stat;
  107. p : Pglob;
  108. TAttr : Longint;
  109. GlobSearchRec : PGlobSearchrec;
  110. begin
  111. TAttr:=longint($ffffffff);
  112. GlobSearchRec:=PGlobSearchrec(Info.FindHandle);
  113. P:=GlobSearchRec^.GlobHandle;
  114. Result:=P<>Nil;
  115. If Result then
  116. begin
  117. GlobSearchRec^.GlobHandle:=P^.Next;
  118. Result:=Fstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo);
  119. If Result then
  120. begin
  121. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  122. Result:=(Info.ExcludeAttr and TAttr)<>0;
  123. If Result Then
  124. With Info do
  125. begin
  126. Attr:=Info.Attr;
  127. If P^.Name<>Nil then
  128. Name:=strpas(p^.name);
  129. Time:=Sinfo.mtime;
  130. Size:=Sinfo.Size;
  131. end;
  132. end;
  133. P^.Next:=Nil;
  134. GlobFree(P);
  135. end;
  136. end;
  137. Function DoFind(Var Rslt : TSearchRec) : Longint;
  138. Var
  139. GlobSearchRec : PGlobSearchRec;
  140. begin
  141. Result:=-1;
  142. GlobSearchRec:=PGlobSearchRec(Rslt.FindHandle);
  143. If (GlobSearchRec^.GlobHandle<>Nil) then
  144. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  145. If GlobToTSearchRec(Rslt) Then Result:=0;
  146. end;
  147. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  148. Var
  149. GlobSearchRec : PGlobSearchRec;
  150. begin
  151. New(GlobSearchRec);
  152. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  153. GlobSearchRec^.GlobHandle:=Glob(Path);
  154. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  155. Rslt.FindHandle:=Longint(GlobSearchRec);
  156. Result:=DoFind (Rslt);
  157. end;
  158. Function FindNext (Var Rslt : TSearchRec) : Longint;
  159. begin
  160. Result:=DoFind (Rslt);
  161. end;
  162. Procedure FindClose (Var F : TSearchrec);
  163. Var
  164. GlobSearchRec : PGlobSearchRec;
  165. begin
  166. GlobSearchRec:=PGlobSearchRec(F.FindHandle);
  167. GlobFree (GlobSearchRec^.GlobHandle);
  168. Dispose(GlobSearchRec);
  169. end;
  170. Function FileGetDate (Handle : Longint) : Longint;
  171. Var Info : Stat;
  172. begin
  173. If Not(FStat(Handle,Info)) then
  174. Result:=-1
  175. else
  176. Result:=Info.Mtime;
  177. end;
  178. Function FileSetDate (Handle,Age : Longint) : Longint;
  179. begin
  180. // Impossible under Linux from FileHandle !!
  181. FileSetDate:=-1;
  182. end;
  183. Function FileGetAttr (Const FileName : String) : Longint;
  184. Var Info : Stat;
  185. begin
  186. If Not FStat (FileName,Info) then
  187. Result:=-1
  188. Else
  189. Result:=LinuxToWinAttr(FileName[1],Info);
  190. end;
  191. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  192. begin
  193. Result:=-1;
  194. end;
  195. Function DeleteFile (Const FileName : String) : Boolean;
  196. begin
  197. Result:=UnLink (FileName);
  198. end;
  199. Function RenameFile (Const OldName, NewName : String) : Boolean;
  200. begin
  201. RenameFile:=Linux.FRename(OldNAme,NewName);
  202. end;
  203. Function FileSearch (Const Name, DirList : String) : String;
  204. begin
  205. FileSearch:=Linux.FSearch(Name,Dirlist);
  206. end;
  207. {****************************************************************************
  208. Disk Functions
  209. ****************************************************************************}
  210. {
  211. The Diskfree and Disksize functions need a file on the specified drive, since this
  212. is required for the statfs system call.
  213. These filenames are set in drivestr[0..26], and have been preset to :
  214. 0 - '.' (default drive - hence current dir is ok.)
  215. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  216. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  217. 3 - '/' (C: equivalent of dos is the root partition)
  218. 4..26 (can be set by you're own applications)
  219. ! Use AddDisk() to Add new drives !
  220. They both return -1 when a failure occurs.
  221. }
  222. Const
  223. FixDriveStr : array[0..3] of pchar=(
  224. '.',
  225. '/fd0/.',
  226. '/fd1/.',
  227. '/.'
  228. );
  229. var
  230. Drives : byte;
  231. DriveStr : array[4..26] of pchar;
  232. Procedure AddDisk(const path:string);
  233. begin
  234. if not (DriveStr[Drives]=nil) then
  235. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  236. GetMem(DriveStr[Drives],length(Path)+1);
  237. StrPCopy(DriveStr[Drives],path);
  238. inc(Drives);
  239. if Drives>26 then
  240. Drives:=4;
  241. end;
  242. Function DiskFree(Drive: Byte): int64;
  243. var
  244. fs : statfs;
  245. Begin
  246. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  247. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  248. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  249. else
  250. Diskfree:=-1;
  251. End;
  252. Function DiskSize(Drive: Byte): int64;
  253. var
  254. fs : statfs;
  255. Begin
  256. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  257. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  258. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  259. else
  260. DiskSize:=-1;
  261. End;
  262. Function GetCurrentDir : String;
  263. begin
  264. GetDir (0,Result);
  265. end;
  266. Function SetCurrentDir (Const NewDir : String) : Boolean;
  267. begin
  268. {$I-}
  269. ChDir(NewDir);
  270. {$I+}
  271. result := (IOResult = 0);
  272. end;
  273. Function CreateDir (Const NewDir : String) : Boolean;
  274. begin
  275. {$I-}
  276. MkDir(NewDir);
  277. {$I+}
  278. result := (IOResult = 0);
  279. end;
  280. Function RemoveDir (Const Dir : String) : Boolean;
  281. begin
  282. {$I-}
  283. RmDir(Dir);
  284. {$I+}
  285. result := (IOResult = 0);
  286. end;
  287. {****************************************************************************
  288. Misc Functions
  289. ****************************************************************************}
  290. procedure Beep;
  291. begin
  292. end;
  293. {****************************************************************************
  294. Locale Functions
  295. ****************************************************************************}
  296. Procedure GetLocalTime(var SystemTime: TSystemTime);
  297. begin
  298. linux.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  299. linux.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  300. SystemTime.MilliSecond := 0;
  301. end ;
  302. Procedure InitAnsi;
  303. Var
  304. i : longint;
  305. begin
  306. { Fill table entries 0 to 127 }
  307. for i := 0 to 96 do
  308. UpperCaseTable[i] := chr(i);
  309. for i := 97 to 122 do
  310. UpperCaseTable[i] := chr(i - 32);
  311. for i := 123 to 191 do
  312. UpperCaseTable[i] := chr(i);
  313. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  314. for i := 0 to 64 do
  315. LowerCaseTable[i] := chr(i);
  316. for i := 65 to 90 do
  317. LowerCaseTable[i] := chr(i + 32);
  318. for i := 91 to 191 do
  319. LowerCaseTable[i] := chr(i);
  320. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  321. end;
  322. Procedure InitInternational;
  323. begin
  324. InitAnsi;
  325. end;
  326. function SysErrorMessage(ErrorCode: Integer): String;
  327. begin
  328. Result:=StrError(ErrorCode);
  329. end;
  330. {****************************************************************************
  331. Initialization code
  332. ****************************************************************************}
  333. Initialization
  334. InitExceptions; { Initialize exceptions. OS independent }
  335. InitInternational; { Initialize internationalization settings }
  336. Finalization
  337. OutOfMemory.Free;
  338. InValidPointer.Free;
  339. end.
  340. {
  341. $Log$
  342. Revision 1.4 2000-12-18 14:01:42 jonas
  343. * fixed constant range error
  344. Revision 1.3 2000/11/28 20:06:12 michael
  345. + merged fix for findfirst/findnext/findclose
  346. Revision 1.2 2000/09/18 13:14:51 marco
  347. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  348. Revision 1.3 2000/08/29 17:58:13 michael
  349. Merged syserrormsg fix
  350. Revision 1.2 2000/08/20 15:46:46 peter
  351. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  352. Revision 1.1.2.2 2000/11/28 20:01:22 michael
  353. + Fixed findfirst/findnext/findclose
  354. Revision 1.1.2.1 2000/09/14 13:38:26 marco
  355. * Moved from Linux dir. now start of generic unix dir, from which the
  356. really exotic features should be moved to the target specific dirs.
  357. }