sysutils.pp 12 KB

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