sysutils.pp 12 KB

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