sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. var
  161. Attr: longint;
  162. begin
  163. FileExists:=false;
  164. Attr:=FileGetAttr(FileName);
  165. if Attr < 0 then
  166. exit;
  167. result:=(Attr and (faVolumeID or faDirectory)) = 0;
  168. end;
  169. type
  170. PInternalFindData = ^TInternalFindData;
  171. TInternalFindData = record
  172. dta_original: pointer;
  173. dta_search: TDTA;
  174. end;
  175. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  176. var
  177. dosResult: longint;
  178. IFD: PInternalFindData;
  179. begin
  180. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  181. new(IFD);
  182. IFD^.dta_original:=gemdos_getdta;
  183. gemdos_setdta(@IFD^.dta_search);
  184. Rslt.FindHandle:=nil;
  185. dosResult:=gemdos_fsfirst(pchar(path), Attr and faAnyFile);
  186. if dosResult < 0 then
  187. begin
  188. InternalFindClose(IFD);
  189. exit;
  190. end;
  191. Rslt.FindHandle:=IFD;
  192. with IFD^.dta_search do
  193. begin
  194. Name:=d_fname;
  195. SetCodePage(Name,DefaultFileSystemCodePage,false);
  196. LongRec(Rslt.Time).hi:=d_date;
  197. LongRec(Rslt.Time).lo:=d_time;
  198. Rslt.Size:=d_length;
  199. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  200. Rslt.Attr := 128 or d_attrib;
  201. end;
  202. result:=0;
  203. end;
  204. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  205. var
  206. dosResult: longint;
  207. IFD: PInternalFindData;
  208. begin
  209. result:=-1;
  210. IFD:=PInternalFindData(Rslt.FindHandle);
  211. if not assigned(IFD) then
  212. exit;
  213. dosResult:=gemdos_fsnext;
  214. if dosResult < 0 then
  215. exit;
  216. with IFD^.dta_search do
  217. begin
  218. Name:=d_fname;
  219. SetCodePage(Name,DefaultFileSystemCodePage,false);
  220. LongRec(Rslt.Time).hi:=d_date;
  221. LongRec(Rslt.Time).lo:=d_time;
  222. Rslt.Size:=d_length;
  223. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  224. Rslt.Attr := 128 or d_attrib;
  225. end;
  226. result:=0;
  227. end;
  228. Procedure InternalFindClose(var Handle: Pointer);
  229. var
  230. IFD: PInternalFindData;
  231. begin
  232. IFD:=PInternalFindData(Handle);
  233. if not assigned(IFD) then
  234. exit;
  235. gemdos_setdta(IFD^.dta_original);
  236. dispose(IFD);
  237. IFD:=nil;
  238. end;
  239. (****** end of non portable routines ******)
  240. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  241. begin
  242. FileGetAttr:=gemdos_fattrib(pchar(FileName),0,0);
  243. end;
  244. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  245. begin
  246. FileSetAttr:=gemdos_fattrib(pchar(FileName),1,Attr and faAnyFile);
  247. if FileSetAttr < -1 then
  248. FileSetAttr:=-1
  249. else
  250. FileSetAttr:=0;
  251. end;
  252. {****************************************************************************
  253. Disk Functions
  254. ****************************************************************************}
  255. function DiskSize(Drive: Byte): Int64;
  256. var
  257. dosResult: longint;
  258. di: TDISKINFO;
  259. begin
  260. DiskSize := -1;
  261. dosResult:=gemdos_dfree(@di,drive);
  262. if dosResult < 0 then
  263. exit;
  264. DiskSize:=di.b_total * di.b_secsiz * di.b_clsiz;
  265. end;
  266. function DiskFree(Drive: Byte): Int64;
  267. var
  268. dosResult: longint;
  269. di: TDISKINFO;
  270. begin
  271. DiskFree := -1;
  272. dosResult:=gemdos_dfree(@di,drive);
  273. if dosResult < 0 then
  274. exit;
  275. DiskFree:=di.b_free * di.b_secsiz * di.b_clsiz;
  276. end;
  277. function DirectoryExists(const Directory: RawByteString): Boolean;
  278. var
  279. Attr: longint;
  280. begin
  281. DirectoryExists:=false;
  282. Attr:=FileGetAttr(Directory);
  283. if Attr < 0 then
  284. exit;
  285. result:=(Attr and faDirectory) <> 0;
  286. end;
  287. {****************************************************************************
  288. Locale Functions
  289. ****************************************************************************}
  290. Procedure GetLocalTime(var SystemTime: TSystemTime);
  291. var
  292. TOSTime: Longint;
  293. begin
  294. LongRec(TOSTime).hi:=gemdos_tgetdate;
  295. LongRec(TOSTime).lo:=gemdos_tgettime;
  296. DateTimeToSystemTime(FileDateToDateTime(TOSTime),SystemTime);
  297. end;
  298. Procedure InitAnsi;
  299. Var
  300. i : longint;
  301. begin
  302. { Fill table entries 0 to 127 }
  303. for i := 0 to 96 do
  304. UpperCaseTable[i] := chr(i);
  305. for i := 97 to 122 do
  306. UpperCaseTable[i] := chr(i - 32);
  307. for i := 123 to 191 do
  308. UpperCaseTable[i] := chr(i);
  309. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  310. for i := 0 to 64 do
  311. LowerCaseTable[i] := chr(i);
  312. for i := 65 to 90 do
  313. LowerCaseTable[i] := chr(i + 32);
  314. for i := 91 to 191 do
  315. LowerCaseTable[i] := chr(i);
  316. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  317. end;
  318. Procedure InitInternational;
  319. begin
  320. InitInternationalGeneric;
  321. InitAnsi;
  322. end;
  323. function SysErrorMessage(ErrorCode: Integer): String;
  324. begin
  325. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  326. end;
  327. function GetLastOSError: Integer;
  328. begin
  329. result:=-1;
  330. end;
  331. {****************************************************************************
  332. OS utility functions
  333. ****************************************************************************}
  334. function GetPathString: String;
  335. begin
  336. {writeln('Unimplemented GetPathString');}
  337. result := '';
  338. end;
  339. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  340. begin
  341. {writeln('Unimplemented GetEnvironmentVariable');}
  342. result:='';
  343. end;
  344. Function GetEnvironmentVariableCount : Integer;
  345. begin
  346. {writeln('Unimplemented GetEnvironmentVariableCount');}
  347. result:=0;
  348. end;
  349. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  350. begin
  351. {writeln('Unimplemented GetEnvironmentString');}
  352. result:='';
  353. end;
  354. function ExecuteProcess (const Path: RawByteString; const ComLine: RawByteString;Flags:TExecuteFlags=[]):
  355. integer;
  356. var
  357. tmpPath: RawByteString;
  358. pcmdline: ShortString;
  359. CommandLine: RawByteString;
  360. E: EOSError;
  361. begin
  362. tmpPath:=ToSingleByteFileSystemEncodedFileName(Path);
  363. pcmdline:=ToSingleByteFileSystemEncodedFileName(ComLine);
  364. { the zero offset for cmdline is actually correct here. pexec() expects
  365. pascal formatted string for cmdline, so length in first byte }
  366. result:=gemdos_pexec(0,PChar(tmpPath),@pcmdline[0],nil);
  367. if result < 0 then begin
  368. if ComLine = '' then
  369. CommandLine := Path
  370. else
  371. CommandLine := Path + ' ' + ComLine;
  372. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, result]);
  373. E.ErrorCode := result;
  374. raise E;
  375. end;
  376. end;
  377. function ExecuteProcess (const Path: RawByteString;
  378. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  379. var
  380. CommandLine: RawByteString;
  381. I: integer;
  382. begin
  383. Commandline := '';
  384. for I := 0 to High (ComLine) do
  385. if Pos (' ', ComLine [I]) <> 0 then
  386. CommandLine := CommandLine + ' ' + '"' + ToSingleByteFileSystemEncodedFileName(ComLine [I]) + '"'
  387. else
  388. CommandLine := CommandLine + ' ' + ToSingleByteFileSystemEncodedFileName(Comline [I]);
  389. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  390. end;
  391. procedure Sleep(Milliseconds: cardinal);
  392. begin
  393. {writeln('Unimplemented Sleep');}
  394. end;
  395. {****************************************************************************
  396. Initialization code
  397. ****************************************************************************}
  398. Initialization
  399. InitExceptions;
  400. InitInternational; { Initialize internationalization settings }
  401. OnBeep:=Nil; { No SysBeep() on Atari for now. }
  402. Finalization
  403. DoneExceptions;
  404. end.