sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2016 by Free Pascal development team
  4. Sysutils unit for Atari
  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. unit sysutils;
  12. interface
  13. {$MODE objfpc}
  14. {$MODESWITCH OUT}
  15. { force ansistrings }
  16. {$H+}
  17. {$modeswitch typehelpers}
  18. {$modeswitch advancedrecords}
  19. {$DEFINE OS_FILESETDATEBYNAME}
  20. {$DEFINE HAS_SLEEP}
  21. {$DEFINE HAS_OSERROR}
  22. {OS has only 1 byte version for ExecuteProcess}
  23. {$define executeprocuni}
  24. { used OS file system APIs use ansistring }
  25. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  26. { OS has an ansistring/single byte environment variable API }
  27. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  28. { Include platform independent interface part }
  29. {$i sysutilh.inc}
  30. { Platform dependent calls }
  31. implementation
  32. uses
  33. { dos,} sysconst;
  34. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  35. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  36. { Include platform independent implementation part }
  37. {$i sysutils.inc}
  38. {$i gemdos.inc}
  39. {****************************************************************************
  40. File Functions
  41. ****************************************************************************}
  42. {$I-}{ Required for correct usage of these routines }
  43. (****** non portable routines ******)
  44. function FileOpen(const FileName: rawbytestring; Mode: Integer): THandle;
  45. begin
  46. { Mode has some Share modes. Maybe something for MiNT? }
  47. { Lower three bits of Mode are actually TOS compatible }
  48. FileOpen:=gemdos_fopen(pchar(FileName), Mode and 3);
  49. if FileOpen < -1 then
  50. FileOpen:=-1;
  51. end;
  52. function FileGetDate(Handle: THandle) : LongInt;
  53. var
  54. td: TDOSTIME;
  55. begin
  56. { Fdatime doesn't report errors... }
  57. gemdos_fdatime(@td,handle,0);
  58. LongRec(result).hi:=td.date;
  59. LongRec(result).lo:=td.time;
  60. end;
  61. function FileSetDate(Handle: THandle; Age: LongInt) : LongInt;
  62. var
  63. td: TDOSTIME;
  64. begin
  65. td.date:=LongRec(Age).hi;
  66. td.time:=LongRec(Age).lo;
  67. gemdos_fdatime(@td,handle,1);
  68. { Fdatime doesn't report errors... }
  69. result:=0;
  70. end;
  71. function FileSetDate(const FileName: RawByteString; Age: LongInt) : LongInt;
  72. var
  73. f: THandle;
  74. begin
  75. FileSetDate:=-1;
  76. f:=FileOpen(FileName,fmOpenReadWrite);
  77. if f < 0 then
  78. exit;
  79. FileSetDate(f,Age);
  80. FileClose(f);
  81. end;
  82. function FileCreate(const FileName: RawByteString) : THandle;
  83. begin
  84. FileCreate:=gemdos_fcreate(pchar(FileName),0);
  85. if FileCreate < -1 then
  86. FileCreate:=-1;
  87. end;
  88. function FileCreate(const FileName: RawByteString; Rights: integer): THandle;
  89. begin
  90. { Rights are Un*x extension. Maybe something for MiNT? }
  91. FileCreate:=FileCreate(FileName);
  92. end;
  93. function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : integer): THandle;
  94. begin
  95. { Rights and ShareMode are Un*x extension. Maybe something for MiNT? }
  96. FileCreate:=FileCreate(FileName);
  97. end;
  98. function FileRead(Handle: THandle; out Buffer; Count: LongInt): LongInt;
  99. begin
  100. FileRead:=-1;
  101. if (Count<=0) then
  102. exit;
  103. FileRead:=gemdos_fread(handle, count, @buffer);
  104. if FileRead < -1 then
  105. FileRead:=-1;
  106. end;
  107. function FileWrite(Handle: THandle; const Buffer; Count: LongInt): LongInt;
  108. begin
  109. FileWrite:=-1;
  110. if (Count<=0) then
  111. exit;
  112. FileWrite:=gemdos_fwrite(handle, count, @buffer);
  113. if FileWrite < -1 then
  114. FileWrite:=-1;
  115. end;
  116. function FileSeek(Handle: THandle; FOffset, Origin: LongInt) : LongInt;
  117. var
  118. dosResult: longint;
  119. begin
  120. FileSeek:=-1;
  121. { TOS seek mode flags are actually compatible to DOS/TP }
  122. dosResult:=gemdos_fseek(FOffset, Handle, Origin);
  123. if dosResult < 0 then
  124. exit;
  125. FileSeek:=dosResult;
  126. end;
  127. function FileSeek(Handle: THandle; FOffset: Int64; Origin: Longint): Int64;
  128. begin
  129. FileSeek:=FileSeek(Handle,LongInt(FOffset),Origin);
  130. end;
  131. procedure FileClose(Handle: THandle);
  132. begin
  133. gemdos_fclose(handle);
  134. end;
  135. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  136. begin
  137. FileTruncate:=False;
  138. end;
  139. function DeleteFile(const FileName: RawByteString) : Boolean;
  140. begin
  141. DeleteFile:=gemdos_fdelete(pchar(FileName)) >= 0;
  142. end;
  143. function RenameFile(const OldName, NewName: RawByteString): Boolean;
  144. begin
  145. RenameFile:=gemdos_frename(0,pchar(oldname),pchar(newname)) >= 0;
  146. end;
  147. (****** end of non portable routines ******)
  148. function FileAge (const FileName : RawByteString): Longint;
  149. var
  150. f: THandle;
  151. begin
  152. FileAge:=-1;
  153. f:=FileOpen(FileName,fmOpenRead);
  154. if f < 0 then
  155. exit;
  156. FileAge:=FileGetDate(f);
  157. FileClose(f);
  158. end;
  159. function FileExists (const FileName : RawByteString) : Boolean;
  160. begin
  161. result:=false;
  162. end;
  163. type
  164. PInternalFindData = ^TInternalFindData;
  165. TInternalFindData = record
  166. dta_original: pointer;
  167. dta_search: TDTA;
  168. end;
  169. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  170. var
  171. dosResult: longint;
  172. IFD: PInternalFindData;
  173. begin
  174. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  175. new(IFD);
  176. IFD^.dta_original:=gemdos_getdta;
  177. gemdos_setdta(@IFD^.dta_search);
  178. Rslt.FindHandle:=nil;
  179. dosResult:=gemdos_fsfirst(pchar(path), Attr and faAnyFile);
  180. if dosResult < 0 then
  181. begin
  182. InternalFindClose(IFD);
  183. exit;
  184. end;
  185. Rslt.FindHandle:=IFD;
  186. with IFD^.dta_search do
  187. begin
  188. Name:=d_fname;
  189. SetCodePage(Name,DefaultFileSystemCodePage,false);
  190. LongRec(Rslt.Time).hi:=d_date;
  191. LongRec(Rslt.Time).lo:=d_time;
  192. Rslt.Size:=d_length;
  193. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  194. Rslt.Attr := 128 or d_attrib;
  195. end;
  196. result:=0;
  197. end;
  198. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  199. var
  200. dosResult: longint;
  201. IFD: PInternalFindData;
  202. begin
  203. result:=-1;
  204. IFD:=PInternalFindData(Rslt.FindHandle);
  205. if not assigned(IFD) then
  206. exit;
  207. dosResult:=gemdos_fsnext;
  208. if dosResult < 0 then
  209. exit;
  210. with IFD^.dta_search do
  211. begin
  212. Name:=d_fname;
  213. SetCodePage(Name,DefaultFileSystemCodePage,false);
  214. LongRec(Rslt.Time).hi:=d_date;
  215. LongRec(Rslt.Time).lo:=d_time;
  216. Rslt.Size:=d_length;
  217. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  218. Rslt.Attr := 128 or d_attrib;
  219. end;
  220. result:=0;
  221. end;
  222. Procedure InternalFindClose(var Handle: Pointer);
  223. var
  224. IFD: PInternalFindData;
  225. begin
  226. IFD:=PInternalFindData(Handle);
  227. if not assigned(IFD) then
  228. exit;
  229. gemdos_setdta(IFD^.dta_original);
  230. dispose(IFD);
  231. IFD:=nil;
  232. end;
  233. (****** end of non portable routines ******)
  234. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  235. begin
  236. FileGetAttr := -1
  237. end;
  238. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  239. begin
  240. FileSetAttr := -1;
  241. end;
  242. {****************************************************************************
  243. Disk Functions
  244. ****************************************************************************}
  245. function DiskSize(Drive: Byte): Int64;
  246. var
  247. dosResult: longint;
  248. di: TDISKINFO;
  249. begin
  250. DiskSize := -1;
  251. dosResult:=gemdos_dfree(@di,drive);
  252. if dosResult < 0 then
  253. exit;
  254. DiskSize:=di.b_total * di.b_secsiz * di.b_clsiz;
  255. end;
  256. function DiskFree(Drive: Byte): Int64;
  257. var
  258. dosResult: longint;
  259. di: TDISKINFO;
  260. begin
  261. DiskFree := -1;
  262. dosResult:=gemdos_dfree(@di,drive);
  263. if dosResult < 0 then
  264. exit;
  265. DiskFree:=di.b_free * di.b_secsiz * di.b_clsiz;
  266. end;
  267. function DirectoryExists(const Directory: RawByteString): Boolean;
  268. begin
  269. result:=false;
  270. end;
  271. {****************************************************************************
  272. Locale Functions
  273. ****************************************************************************}
  274. Procedure GetLocalTime(var SystemTime: TSystemTime);
  275. begin
  276. end;
  277. Procedure InitAnsi;
  278. Var
  279. i : longint;
  280. begin
  281. { Fill table entries 0 to 127 }
  282. for i := 0 to 96 do
  283. UpperCaseTable[i] := chr(i);
  284. for i := 97 to 122 do
  285. UpperCaseTable[i] := chr(i - 32);
  286. for i := 123 to 191 do
  287. UpperCaseTable[i] := chr(i);
  288. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  289. for i := 0 to 64 do
  290. LowerCaseTable[i] := chr(i);
  291. for i := 65 to 90 do
  292. LowerCaseTable[i] := chr(i + 32);
  293. for i := 91 to 191 do
  294. LowerCaseTable[i] := chr(i);
  295. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  296. end;
  297. Procedure InitInternational;
  298. begin
  299. InitInternationalGeneric;
  300. InitAnsi;
  301. end;
  302. function SysErrorMessage(ErrorCode: Integer): String;
  303. begin
  304. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  305. end;
  306. function GetLastOSError: Integer;
  307. begin
  308. result:=-1;
  309. end;
  310. {****************************************************************************
  311. OS utility functions
  312. ****************************************************************************}
  313. function GetPathString: String;
  314. begin
  315. result := '';
  316. end;
  317. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  318. begin
  319. end;
  320. Function GetEnvironmentVariableCount : Integer;
  321. begin
  322. end;
  323. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  324. begin
  325. end;
  326. function ExecuteProcess (const Path: RawByteString; const ComLine: RawByteString;Flags:TExecuteFlags=[]):
  327. integer;
  328. begin
  329. end;
  330. function ExecuteProcess (const Path: RawByteString;
  331. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  332. var
  333. CommandLine: RawByteString;
  334. I: integer;
  335. begin
  336. Commandline := '';
  337. for I := 0 to High (ComLine) do
  338. if Pos (' ', ComLine [I]) <> 0 then
  339. CommandLine := CommandLine + ' ' + '"' + ToSingleByteFileSystemEncodedFileName(ComLine [I]) + '"'
  340. else
  341. CommandLine := CommandLine + ' ' + ToSingleByteFileSystemEncodedFileName(Comline [I]);
  342. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  343. end;
  344. procedure Sleep(Milliseconds: cardinal);
  345. begin
  346. end;
  347. {****************************************************************************
  348. Initialization code
  349. ****************************************************************************}
  350. Initialization
  351. InitExceptions;
  352. InitInternational; { Initialize internationalization settings }
  353. OnBeep:=Nil; { No SysBeep() on Atari for now. }
  354. Finalization
  355. DoneExceptions;
  356. end.