sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 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 : Pchar; Const Info : Stat) : Longint;
  82. begin
  83. Result:=faArchive;
  84. If (Info.Mode and STAT_IFDIR)=STAT_IFDIR then
  85. Result:=Result or faDirectory;
  86. If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
  87. Result:=Result or faHidden;
  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. GlobSearchRec : PGlobSearchrec;
  109. begin
  110. GlobSearchRec:=PGlobSearchrec(Info.FindHandle);
  111. P:=GlobSearchRec^.GlobHandle;
  112. Result:=P<>Nil;
  113. If Result then
  114. begin
  115. GlobSearchRec^.GlobHandle:=P^.Next;
  116. Result:=Fstat(GlobSearchRec^.Path+StrPas(p^.name),SInfo);
  117. If Result then
  118. begin
  119. Info.Attr:=LinuxToWinAttr(p^.name,SInfo);
  120. Result:=(Info.ExcludeAttr and Info.Attr)=0;
  121. If Result Then
  122. With Info do
  123. begin
  124. Attr:=Info.Attr;
  125. If P^.Name<>Nil then
  126. Name:=strpas(p^.name);
  127. Time:=Sinfo.mtime;
  128. Size:=Sinfo.Size;
  129. end;
  130. end;
  131. P^.Next:=Nil;
  132. GlobFree(P);
  133. end;
  134. end;
  135. Function DoFind(Var Rslt : TSearchRec) : Longint;
  136. Var
  137. GlobSearchRec : PGlobSearchRec;
  138. begin
  139. Result:=-1;
  140. GlobSearchRec:=PGlobSearchRec(Rslt.FindHandle);
  141. If (GlobSearchRec^.GlobHandle<>Nil) then
  142. While (GlobSearchRec^.GlobHandle<>Nil) and not (Result=0) do
  143. If GlobToTSearchRec(Rslt) Then Result:=0;
  144. end;
  145. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  146. Var
  147. GlobSearchRec : PGlobSearchRec;
  148. begin
  149. New(GlobSearchRec);
  150. GlobSearchRec^.Path:=ExpandFileName(ExtractFilePath(Path));
  151. GlobSearchRec^.GlobHandle:=Glob(Path);
  152. Rslt.ExcludeAttr:=Not Attr; //!! Not correct !!
  153. Rslt.FindHandle:=Longint(GlobSearchRec);
  154. Result:=DoFind (Rslt);
  155. end;
  156. Function FindNext (Var Rslt : TSearchRec) : Longint;
  157. begin
  158. Result:=DoFind (Rslt);
  159. end;
  160. Procedure FindClose (Var F : TSearchrec);
  161. Var
  162. GlobSearchRec : PGlobSearchRec;
  163. begin
  164. GlobSearchRec:=PGlobSearchRec(F.FindHandle);
  165. GlobFree (GlobSearchRec^.GlobHandle);
  166. Dispose(GlobSearchRec);
  167. end;
  168. Function FileGetDate (Handle : Longint) : Longint;
  169. Var Info : Stat;
  170. begin
  171. If Not(FStat(Handle,Info)) then
  172. Result:=-1
  173. else
  174. Result:=Info.Mtime;
  175. end;
  176. Function FileSetDate (Handle,Age : Longint) : Longint;
  177. begin
  178. // Impossible under Linux from FileHandle !!
  179. FileSetDate:=-1;
  180. end;
  181. Function FileGetAttr (Const FileName : String) : Longint;
  182. Var Info : Stat;
  183. begin
  184. If Not FStat (FileName,Info) then
  185. Result:=-1
  186. Else
  187. Result:=LinuxToWinAttr(Pchar(FileName),Info);
  188. end;
  189. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  190. begin
  191. Result:=-1;
  192. end;
  193. Function DeleteFile (Const FileName : String) : Boolean;
  194. begin
  195. Result:=UnLink (FileName);
  196. end;
  197. Function RenameFile (Const OldName, NewName : String) : Boolean;
  198. begin
  199. RenameFile:=Unix.FRename(OldNAme,NewName);
  200. end;
  201. Function FileSearch (Const Name, DirList : String) : String;
  202. begin
  203. FileSearch:=Unix.FSearch(Name,Dirlist);
  204. end;
  205. {****************************************************************************
  206. Disk Functions
  207. ****************************************************************************}
  208. {
  209. The Diskfree and Disksize functions need a file on the specified drive, since this
  210. is required for the statfs system call.
  211. These filenames are set in drivestr[0..26], and have been preset to :
  212. 0 - '.' (default drive - hence current dir is ok.)
  213. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  214. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  215. 3 - '/' (C: equivalent of dos is the root partition)
  216. 4..26 (can be set by you're own applications)
  217. ! Use AddDisk() to Add new drives !
  218. They both return -1 when a failure occurs.
  219. }
  220. Const
  221. FixDriveStr : array[0..3] of pchar=(
  222. '.',
  223. '/fd0/.',
  224. '/fd1/.',
  225. '/.'
  226. );
  227. var
  228. Drives : byte;
  229. DriveStr : array[4..26] of pchar;
  230. Procedure AddDisk(const path:string);
  231. begin
  232. if not (DriveStr[Drives]=nil) then
  233. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  234. GetMem(DriveStr[Drives],length(Path)+1);
  235. StrPCopy(DriveStr[Drives],path);
  236. inc(Drives);
  237. if Drives>26 then
  238. Drives:=4;
  239. end;
  240. Function DiskFree(Drive: Byte): int64;
  241. var
  242. fs : tstatfs;
  243. Begin
  244. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  245. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  246. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  247. else
  248. Diskfree:=-1;
  249. End;
  250. Function DiskSize(Drive: Byte): int64;
  251. var
  252. fs : tstatfs;
  253. Begin
  254. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and statfs(StrPas(fixdrivestr[drive]),fs)) or
  255. ((not (drivestr[Drive]=nil)) and statfs(StrPas(drivestr[drive]),fs)) then
  256. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  257. else
  258. DiskSize:=-1;
  259. End;
  260. Function GetCurrentDir : String;
  261. begin
  262. GetDir (0,Result);
  263. end;
  264. Function SetCurrentDir (Const NewDir : String) : Boolean;
  265. begin
  266. {$I-}
  267. ChDir(NewDir);
  268. {$I+}
  269. result := (IOResult = 0);
  270. end;
  271. Function CreateDir (Const NewDir : String) : Boolean;
  272. begin
  273. {$I-}
  274. MkDir(NewDir);
  275. {$I+}
  276. result := (IOResult = 0);
  277. end;
  278. Function RemoveDir (Const Dir : String) : Boolean;
  279. begin
  280. {$I-}
  281. RmDir(Dir);
  282. {$I+}
  283. result := (IOResult = 0);
  284. end;
  285. {****************************************************************************
  286. Misc Functions
  287. ****************************************************************************}
  288. procedure Beep;
  289. begin
  290. end;
  291. {****************************************************************************
  292. Locale Functions
  293. ****************************************************************************}
  294. Procedure GetLocalTime(var SystemTime: TSystemTime);
  295. begin
  296. Unix.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second);
  297. Unix.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  298. SystemTime.MilliSecond := 0;
  299. end ;
  300. Procedure InitAnsi;
  301. Var
  302. i : longint;
  303. begin
  304. { Fill table entries 0 to 127 }
  305. for i := 0 to 96 do
  306. UpperCaseTable[i] := chr(i);
  307. for i := 97 to 122 do
  308. UpperCaseTable[i] := chr(i - 32);
  309. for i := 123 to 191 do
  310. UpperCaseTable[i] := chr(i);
  311. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  312. for i := 0 to 64 do
  313. LowerCaseTable[i] := chr(i);
  314. for i := 65 to 90 do
  315. LowerCaseTable[i] := chr(i + 32);
  316. for i := 91 to 191 do
  317. LowerCaseTable[i] := chr(i);
  318. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  319. end;
  320. Procedure InitInternational;
  321. begin
  322. InitAnsi;
  323. end;
  324. function SysErrorMessage(ErrorCode: Integer): String;
  325. begin
  326. Result:=StrError(ErrorCode);
  327. end;
  328. {****************************************************************************
  329. OS utility functions
  330. ****************************************************************************}
  331. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  332. begin
  333. Result:=StrPas(Unix.Getenv(PChar(EnvVar)));
  334. end;
  335. {****************************************************************************
  336. Initialization code
  337. ****************************************************************************}
  338. Initialization
  339. InitExceptions; { Initialize exceptions. OS independent }
  340. InitInternational; { Initialize internationalization settings }
  341. Finalization
  342. DoneExceptions;
  343. end.
  344. {
  345. $Log$
  346. Revision 1.10 2001-06-03 20:19:09 peter
  347. * FSStat to StatFS
  348. * StatFS structure to TStatFS
  349. Revision 1.9 2001/06/03 15:18:01 peter
  350. * eoutofmemory and einvalidpointer fix
  351. Revision 1.8 2001/02/20 22:19:38 peter
  352. * always test before commiting after merging, linux -> unix change
  353. Revision 1.7 2001/02/20 22:14:19 peter
  354. * merged getenvironmentvariable
  355. Revision 1.6 2001/01/21 20:21:40 marco
  356. * Rename fest II. Rtl OK
  357. Revision 1.5 2000/12/28 20:50:04 peter
  358. * merged fixes from 1.0.x
  359. Revision 1.4 2000/12/18 14:01:42 jonas
  360. * fixed constant range error
  361. Revision 1.3 2000/11/28 20:06:12 michael
  362. + merged fix for findfirst/findnext/findclose
  363. Revision 1.2 2000/09/18 13:14:51 marco
  364. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  365. Revision 1.3 2000/08/29 17:58:13 michael
  366. Merged syserrormsg fix
  367. Revision 1.2 2000/08/20 15:46:46 peter
  368. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  369. Revision 1.1.2.2 2000/11/28 20:01:22 michael
  370. + Fixed findfirst/findnext/findclose
  371. Revision 1.1.2.1 2000/09/14 13:38:26 marco
  372. * Moved from Linux dir. now start of generic unix dir, from which the
  373. really exotic features should be moved to the target specific dirs.
  374. }