2
0

sysutils.pp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. Function GlobToTSearchRec (Var Info : TSearchRec) : Boolean;
  100. Var SInfo : Stat;
  101. p : Pglob;
  102. TAttr : Longint;
  103. begin
  104. TAttr:=$ffffffff;
  105. P:=pglob(Info.FindHandle);
  106. Result:=P<>Nil;
  107. If Result then
  108. begin
  109. Info.FindHandle:=Longint(P^.Next);
  110. Result:=Fstat(p^.name,SInfo);
  111. If Result then
  112. begin
  113. Info.Attr:=LinuxToWinAttr(p^.name[0],SInfo);
  114. Result:=(Info.ExcludeAttr and TAttr)<>0;
  115. If Result Then
  116. With Info do
  117. begin
  118. Attr:=Info.Attr;
  119. If P^.Name<>Nil then
  120. Name:=strpas(p^.name);
  121. Time:=Sinfo.mtime;
  122. Size:=Sinfo.Size;
  123. end;
  124. end;
  125. P^.Next:=Nil;
  126. GlobFree(P);
  127. end;
  128. end;
  129. Function DoFind(Var Rslt : TSearchRec) : Longint;
  130. begin
  131. Result:=-1;
  132. If Rslt.FindHandle<>0 then
  133. While (Rslt.FindHandle<>0) and not (Result=0) do
  134. If GlobToTSearchRec(Rslt) Then Result:=0;
  135. end;
  136. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  137. begin
  138. Rslt.ExcludeAttr:=Attr; //!! Not correct !!
  139. Rslt.FindHandle:=Longint(Glob(Path));
  140. Result:=DoFind (Rslt);
  141. end;
  142. Function FindNext (Var Rslt : TSearchRec) : Longint;
  143. begin
  144. Result:=DoFind (Rslt);
  145. end;
  146. Procedure FindClose (Var F : TSearchrec);
  147. begin
  148. GlobFree (PGlob(F.FindHandle));
  149. end;
  150. Function FileGetDate (Handle : Longint) : Longint;
  151. Var Info : Stat;
  152. begin
  153. If Not(FStat(Handle,Info)) then
  154. Result:=-1
  155. else
  156. Result:=Info.Mtime;
  157. end;
  158. Function FileSetDate (Handle,Age : Longint) : Longint;
  159. begin
  160. // Impossible under Linux from FileHandle !!
  161. FileSetDate:=-1;
  162. end;
  163. Function FileGetAttr (Const FileName : String) : Longint;
  164. Var Info : Stat;
  165. begin
  166. If Not FStat (FileName,Info) then
  167. Result:=-1
  168. Else
  169. Result:=LinuxToWinAttr(FileName[1],Info);
  170. end;
  171. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  172. begin
  173. Result:=-1;
  174. end;
  175. Function DeleteFile (Const FileName : String) : Boolean;
  176. begin
  177. Result:=UnLink (FileName);
  178. end;
  179. Function RenameFile (Const OldName, NewName : String) : Boolean;
  180. begin
  181. RenameFile:=Linux.FRename(OldNAme,NewName);
  182. end;
  183. Function FileSearch (Const Name, DirList : String) : String;
  184. begin
  185. FileSearch:=Linux.FSearch(Name,Dirlist);
  186. end;
  187. {****************************************************************************
  188. Disk Functions
  189. ****************************************************************************}
  190. {
  191. The Diskfree and Disksize functions need a file on the specified drive, since this
  192. is required for the statfs system call.
  193. These filenames are set in drivestr[0..26], and have been preset to :
  194. 0 - '.' (default drive - hence current dir is ok.)
  195. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  196. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  197. 3 - '/' (C: equivalent of dos is the root partition)
  198. 4..26 (can be set by you're own applications)
  199. ! Use AddDisk() to Add new drives !
  200. They both return -1 when a failure occurs.
  201. }
  202. Const
  203. FixDriveStr : array[0..3] of pchar=(
  204. '.',
  205. '/fd0/.',
  206. '/fd1/.',
  207. '/.'
  208. );
  209. var
  210. Drives : byte;
  211. DriveStr : array[4..26] of pchar;
  212. Procedure AddDisk(const path:string);
  213. begin
  214. if not (DriveStr[Drives]=nil) then
  215. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  216. GetMem(DriveStr[Drives],length(Path)+1);
  217. StrPCopy(DriveStr[Drives],path);
  218. inc(Drives);
  219. if Drives>26 then
  220. Drives:=4;
  221. end;
  222. Function DiskFree(Drive: Byte): int64;
  223. var
  224. fs : statfs;
  225. Begin
  226. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  227. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  228. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  229. else
  230. Diskfree:=-1;
  231. End;
  232. Function DiskSize(Drive: Byte): int64;
  233. var
  234. fs : statfs;
  235. Begin
  236. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  237. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  238. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  239. else
  240. DiskSize:=-1;
  241. End;
  242. Function GetCurrentDir : String;
  243. begin
  244. GetDir (0,Result);
  245. end;
  246. Function SetCurrentDir (Const NewDir : String) : Boolean;
  247. begin
  248. {$I-}
  249. ChDir(NewDir);
  250. {$I+}
  251. result := (IOResult = 0);
  252. end;
  253. Function CreateDir (Const NewDir : String) : Boolean;
  254. begin
  255. {$I-}
  256. MkDir(NewDir);
  257. {$I+}
  258. result := (IOResult = 0);
  259. end;
  260. Function RemoveDir (Const Dir : String) : Boolean;
  261. begin
  262. {$I-}
  263. RmDir(Dir);
  264. {$I+}
  265. result := (IOResult = 0);
  266. end;
  267. {****************************************************************************
  268. Misc Functions
  269. ****************************************************************************}
  270. procedure Beep;
  271. begin
  272. end;
  273. {****************************************************************************
  274. Locale Functions
  275. ****************************************************************************}
  276. Procedure GetLocalTime(var SystemTime: TSystemTime);
  277. begin
  278. linux.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  279. linux.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  280. SystemTime.MilliSecond := 0;
  281. end ;
  282. Procedure InitAnsi;
  283. Var
  284. i : longint;
  285. begin
  286. { Fill table entries 0 to 127 }
  287. for i := 0 to 96 do
  288. UpperCaseTable[i] := chr(i);
  289. for i := 97 to 122 do
  290. UpperCaseTable[i] := chr(i - 32);
  291. for i := 123 to 191 do
  292. UpperCaseTable[i] := chr(i);
  293. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  294. for i := 0 to 64 do
  295. LowerCaseTable[i] := chr(i);
  296. for i := 65 to 90 do
  297. LowerCaseTable[i] := chr(i + 32);
  298. for i := 91 to 191 do
  299. LowerCaseTable[i] := chr(i);
  300. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  301. end;
  302. Procedure InitInternational;
  303. begin
  304. InitAnsi;
  305. end;
  306. function SysErrorMessage(ErrorCode: Integer): String;
  307. begin
  308. Result:=StrError(ErrorCode);
  309. end;
  310. {****************************************************************************
  311. Initialization code
  312. ****************************************************************************}
  313. Initialization
  314. InitExceptions; { Initialize exceptions. OS independent }
  315. InitInternational; { Initialize internationalization settings }
  316. Finalization
  317. OutOfMemory.Free;
  318. InValidPointer.Free;
  319. end.
  320. {
  321. $Log$
  322. Revision 1.2 2000-09-18 13:14:51 marco
  323. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  324. Revision 1.3 2000/08/29 17:58:13 michael
  325. Merged syserrormsg fix
  326. Revision 1.2 2000/08/20 15:46:46 peter
  327. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  328. Revision 1.1.2.3 2000/08/22 19:21:48 michael
  329. + Implemented syserrormessage. Made dummies for go32v2 and OS/2
  330. * Changed linux/errors.pp so it uses pchars for storage.
  331. Revision 1.1.2.2 2000/08/20 15:22:57 peter
  332. * removed beep from interface
  333. Revision 1.1.2.1 2000/08/20 15:08:32 peter
  334. * forgot the add command :(
  335. }