sysutils.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2023 by Free Pascal development team
  4. Sysutils unit for Human 68k (Sharp X68000)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$IFNDEF FPC_DOTTEDUNITS}
  12. unit sysutils;
  13. {$ENDIF FPC_DOTTEDUNITS}
  14. interface
  15. {$MODE objfpc}
  16. {$MODESWITCH OUT}
  17. { force ansistrings }
  18. {$H+}
  19. {$modeswitch typehelpers}
  20. {$modeswitch advancedrecords}
  21. {$DEFINE OS_FILESETDATEBYNAME}
  22. {$DEFINE HAS_SLEEP}
  23. {$DEFINE HAS_OSERROR}
  24. {OS has only 1 byte version for ExecuteProcess}
  25. {$define executeprocuni}
  26. { used OS file system APIs use ansistring }
  27. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  28. { OS has an ansistring/single byte environment variable API }
  29. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  30. { Include platform independent interface part }
  31. {$i sysutilh.inc}
  32. { Platform dependent calls }
  33. implementation
  34. {$IFDEF FPC_DOTTEDUNITS}
  35. uses
  36. System.SysConst;
  37. {$ELSE FPC_DOTTEDUNITS}
  38. uses
  39. sysconst;
  40. {$ENDIF FPC_DOTTEDUNITS}
  41. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  42. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  43. { Include platform independent implementation part }
  44. {$i sysutils.inc}
  45. {****************************************************************************
  46. File Functions
  47. ****************************************************************************}
  48. {$I-}{ Required for correct usage of these routines }
  49. (****** non portable routines ******)
  50. function FileOpen(const FileName: rawbytestring; Mode: Integer): THandle;
  51. begin
  52. FileOpen:=-1;
  53. if FileOpen < -1 then
  54. FileOpen:=-1;
  55. end;
  56. function FileGetDate(Handle: THandle) : Int64;
  57. begin
  58. result:=-1;
  59. end;
  60. function FileSetDate(Handle: THandle; Age: Int64) : LongInt;
  61. begin
  62. result:=0;
  63. end;
  64. function FileSetDate(const FileName: RawByteString; Age: Int64) : LongInt;
  65. var
  66. f: THandle;
  67. begin
  68. result:=-1;
  69. f:=FileOpen(FileName,fmOpenReadWrite);
  70. if f < 0 then
  71. exit;
  72. result:=FileSetDate(f,Age);
  73. FileClose(f);
  74. end;
  75. function FileCreate(const FileName: RawByteString) : THandle;
  76. begin
  77. FileCreate:=-1;
  78. if FileCreate < -1 then
  79. FileCreate:=-1;
  80. end;
  81. function FileCreate(const FileName: RawByteString; Rights: integer): THandle;
  82. begin
  83. { FIX ME: we map this to FileCreate(), and ignore rights! }
  84. FileCreate:=FileCreate(FileName);
  85. end;
  86. function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : integer): THandle;
  87. begin
  88. { FIX ME: we map this to FileCreate(), and ignore rights and sharemode! }
  89. FileCreate:=FileCreate(FileName);
  90. end;
  91. function FileRead(Handle: THandle; out Buffer; Count: LongInt): LongInt;
  92. begin
  93. FileRead:=-1;
  94. if (Count<=0) then
  95. exit;
  96. FileRead:=-1;
  97. if FileRead < -1 then
  98. FileRead:=-1;
  99. end;
  100. function FileWrite(Handle: THandle; const Buffer; Count: LongInt): LongInt;
  101. begin
  102. FileWrite:=-1;
  103. if (Count<=0) then
  104. exit;
  105. FileWrite:=-1;
  106. if FileWrite < -1 then
  107. FileWrite:=-1;
  108. end;
  109. function FileSeek(Handle: THandle; FOffset, Origin: LongInt) : LongInt;
  110. var
  111. dosResult: longint;
  112. begin
  113. FileSeek:=-1;
  114. dosResult:=-1;
  115. if dosResult < 0 then
  116. exit;
  117. FileSeek:=dosResult;
  118. end;
  119. function FileSeek(Handle: THandle; FOffset: Int64; Origin: Longint): Int64;
  120. begin
  121. FileSeek:=FileSeek(Handle,LongInt(FOffset),Origin);
  122. end;
  123. procedure FileClose(Handle: THandle);
  124. begin
  125. end;
  126. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  127. begin
  128. FileTruncate:=False;
  129. end;
  130. function DeleteFile(const FileName: RawByteString) : Boolean;
  131. begin
  132. DeleteFile:=false;
  133. end;
  134. function RenameFile(const OldName, NewName: RawByteString): Boolean;
  135. begin
  136. RenameFile:=false;
  137. end;
  138. (****** end of non portable routines ******)
  139. function FileAge (const FileName : RawByteString): Int64;
  140. var
  141. f: THandle;
  142. begin
  143. FileAge:=-1;
  144. f:=FileOpen(FileName,fmOpenRead);
  145. if f < 0 then
  146. exit;
  147. FileAge:=FileGetDate(f);
  148. FileClose(f);
  149. end;
  150. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  151. begin
  152. Result := False;
  153. end;
  154. function FileExists (const FileName : RawByteString; FollowLink : Boolean) : Boolean;
  155. var
  156. Attr: longint;
  157. begin
  158. FileExists:=false;
  159. Attr:=FileGetAttr(FileName);
  160. if Attr < 0 then
  161. exit;
  162. result:=(Attr and (faVolumeID or faDirectory)) = 0;
  163. end;
  164. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  165. var
  166. dosResult: longint;
  167. begin
  168. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  169. Rslt.FindHandle:=-1;
  170. dosResult:=-1; { add findfirst here }
  171. if dosResult < 0 then
  172. begin
  173. InternalFindClose(Rslt.FindHandle);
  174. exit;
  175. end;
  176. Rslt.FindHandle:=-1;
  177. Name:='';
  178. SetCodePage(Name,DefaultFileSystemCodePage,false);
  179. Rslt.Time:=0;
  180. Rslt.Size:=0;
  181. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  182. Rslt.Attr := 128 or 0;
  183. result:=0;
  184. end;
  185. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  186. var
  187. dosResult: longint;
  188. begin
  189. result:=-1;
  190. dosResult:=-1;
  191. if dosResult < 0 then
  192. exit;
  193. Name:='';
  194. SetCodePage(Name,DefaultFileSystemCodePage,false);
  195. Rslt.Time:=0;
  196. Rslt.Size:=0;
  197. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  198. Rslt.Attr := 128 or 0;
  199. result:=0;
  200. end;
  201. Procedure InternalFindClose(var Handle: Longint);
  202. begin
  203. end;
  204. (****** end of non portable routines ******)
  205. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  206. begin
  207. FileGetAttr:=0;
  208. end;
  209. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  210. begin
  211. FileSetAttr:=-1;
  212. if FileSetAttr < -1 then
  213. FileSetAttr:=-1
  214. else
  215. FileSetAttr:=0;
  216. end;
  217. {****************************************************************************
  218. Disk Functions
  219. ****************************************************************************}
  220. function DiskSize(Drive: Byte): Int64;
  221. var
  222. dosResult: longint;
  223. begin
  224. DiskSize := -1;
  225. dosResult:=-1;
  226. if dosResult < 0 then
  227. exit;
  228. DiskSize:=0;
  229. end;
  230. function DiskFree(Drive: Byte): Int64;
  231. var
  232. dosResult: longint;
  233. begin
  234. DiskFree := -1;
  235. dosResult:=-1;
  236. if dosResult < 0 then
  237. exit;
  238. DiskFree:=0;
  239. end;
  240. function DirectoryExists(const Directory: RawByteString; FollowLink : Boolean): Boolean;
  241. var
  242. Attr: longint;
  243. begin
  244. DirectoryExists:=false;
  245. Attr:=FileGetAttr(Directory);
  246. if Attr < 0 then
  247. exit;
  248. result:=(Attr and faDirectory) <> 0;
  249. end;
  250. {****************************************************************************
  251. Locale Functions
  252. ****************************************************************************}
  253. Procedure GetLocalTime(var SystemTime: TSystemTime);
  254. begin
  255. DateTimeToSystemTime(FileDateToDateTime(0),SystemTime);
  256. end;
  257. Procedure InitAnsi;
  258. Var
  259. i : longint;
  260. begin
  261. { Fill table entries 0 to 127 }
  262. for i := 0 to 96 do
  263. UpperCaseTable[i] := chr(i);
  264. for i := 97 to 122 do
  265. UpperCaseTable[i] := chr(i - 32);
  266. for i := 123 to 191 do
  267. UpperCaseTable[i] := chr(i);
  268. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  269. for i := 0 to 64 do
  270. LowerCaseTable[i] := chr(i);
  271. for i := 65 to 90 do
  272. LowerCaseTable[i] := chr(i + 32);
  273. for i := 91 to 191 do
  274. LowerCaseTable[i] := chr(i);
  275. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  276. end;
  277. Procedure InitInternational;
  278. begin
  279. InitInternationalGeneric;
  280. InitAnsi;
  281. end;
  282. function SysErrorMessage(ErrorCode: Integer): String;
  283. begin
  284. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  285. end;
  286. function GetLastOSError: Integer;
  287. begin
  288. result:=-1;
  289. end;
  290. {****************************************************************************
  291. OS utility functions
  292. ****************************************************************************}
  293. function GetPathString: String;
  294. begin
  295. {writeln('Unimplemented GetPathString');}
  296. result := '';
  297. end;
  298. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  299. begin
  300. {writeln('Unimplemented GetEnvironmentVariable');}
  301. result:='';
  302. end;
  303. Function GetEnvironmentVariableCount : Integer;
  304. begin
  305. {writeln('Unimplemented GetEnvironmentVariableCount');}
  306. result:=0;
  307. end;
  308. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  309. begin
  310. {writeln('Unimplemented GetEnvironmentString');}
  311. result:='';
  312. end;
  313. function ExecuteProcess (const Path: RawByteString; const ComLine: RawByteString;Flags:TExecuteFlags=[]):
  314. integer;
  315. var
  316. tmpPath: RawByteString;
  317. pcmdline: ShortString;
  318. CommandLine: RawByteString;
  319. E: EOSError;
  320. begin
  321. tmpPath:=ToSingleByteFileSystemEncodedFileName(Path);
  322. pcmdline:=ToSingleByteFileSystemEncodedFileName(ComLine);
  323. result:=-1; { execute here }
  324. if result < 0 then begin
  325. if ComLine = '' then
  326. CommandLine := Path
  327. else
  328. CommandLine := Path + ' ' + ComLine;
  329. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, result]);
  330. E.ErrorCode := result;
  331. raise E;
  332. end;
  333. end;
  334. function ExecuteProcess (const Path: RawByteString;
  335. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  336. var
  337. CommandLine: RawByteString;
  338. I: integer;
  339. begin
  340. Commandline := '';
  341. for I := 0 to High (ComLine) do
  342. if Pos (' ', ComLine [I]) <> 0 then
  343. CommandLine := CommandLine + ' ' + '"' + ToSingleByteFileSystemEncodedFileName(ComLine [I]) + '"'
  344. else
  345. CommandLine := CommandLine + ' ' + ToSingleByteFileSystemEncodedFileName(Comline [I]);
  346. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  347. end;
  348. procedure Sleep(Milliseconds: cardinal);
  349. begin
  350. {writeln('Unimplemented sleep');}
  351. end;
  352. {****************************************************************************
  353. Initialization code
  354. ****************************************************************************}
  355. Initialization
  356. InitExceptions;
  357. InitInternational; { Initialize internationalization settings }
  358. OnBeep:=Nil; { No SysBeep() on Human 68k for now }
  359. Finalization
  360. FreeTerminateProcs;
  361. DoneExceptions;
  362. end.