2
0

sysutils.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2014 by Free Pascal development team
  4. Sysutils unit for AmigaOS & clones
  5. Based on Amiga 1.x version by Carl Eric Codere, and other
  6. parts of the RTL
  7. AmigaOS and MorphOS support by Karoly Balogh
  8. AROS support by Marcus Sackrow
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. unit sysutils;
  16. interface
  17. {$MODE objfpc}
  18. {$MODESWITCH OUT}
  19. { force ansistrings }
  20. {$H+}
  21. {$modeswitch typehelpers}
  22. {$modeswitch advancedrecords}
  23. {$DEFINE OS_FILESETDATEBYNAME}
  24. {$DEFINE HAS_SLEEP}
  25. {$DEFINE HAS_OSERROR}
  26. {OS has only 1 byte version for ExecuteProcess}
  27. {$define executeprocuni}
  28. { used OS file system APIs use ansistring }
  29. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  30. { OS has an ansistring/single byte environment variable API }
  31. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  32. { Include platform independent interface part }
  33. {$i sysutilh.inc}
  34. { Platform dependent calls }
  35. function DeviceByIdx(Idx: Integer): string;
  36. function AddDisk(Const Path: string): Integer;
  37. function RefreshDeviceList: Integer;
  38. function DiskSize(Drive: AnsiString): Int64;
  39. function DiskFree(Drive: AnsiString): Int64;
  40. implementation
  41. uses
  42. dos, sysconst;
  43. {$DEFINE FPC_FEXPAND_VOLUMES} (* Full paths begin with drive specification *)
  44. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  45. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  46. {$DEFINE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  47. { Include platform independent implementation part }
  48. {$i sysutils.inc}
  49. { * Include system specific includes * }
  50. {$include execd.inc}
  51. {$include execf.inc}
  52. {$include timerd.inc}
  53. {$include doslibd.inc}
  54. {$include doslibf.inc}
  55. {$include utilf.inc}
  56. { * Followings are implemented in the system unit! * }
  57. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  58. function PathConv(path: RawByteString): RawByteString; external name 'PATHCONVRBS';
  59. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  60. function RemoveFromList(var l: Pointer; h: LongInt): boolean; external name 'REMOVEFROMLIST';
  61. function CheckInList(var l: Pointer; h: LongInt): pointer; external name 'CHECKINLIST';
  62. var
  63. ASYS_FileList: Pointer; external name 'ASYS_FILELIST';
  64. function BADDR(bval: BPTR): Pointer; Inline;
  65. begin
  66. {$if defined(AROS)} // deactivated for now //and (not defined(AROS_BINCOMPAT))}
  67. BADDR := Pointer(bval);
  68. {$else}
  69. BADDR:=Pointer(bval Shl 2);
  70. {$endif}
  71. end;
  72. function BSTR2STRING(s : Pointer): PChar; Inline;
  73. begin
  74. {$if defined(AROS)} // deactivated for now //and (not defined(AROS_BINCOMPAT))}
  75. BSTR2STRING:=PChar(s);
  76. {$else}
  77. BSTR2STRING:=PChar(BADDR(PtrInt(s)))+1;
  78. {$endif}
  79. end;
  80. function BSTR2STRING(s : BPTR): PChar; Inline;
  81. begin
  82. {$if defined(AROS)} // deactivated for now //and (not defined(AROS_BINCOMPAT))}
  83. BSTR2STRING:=PChar(s);
  84. {$else}
  85. BSTR2STRING:=PChar(BADDR(s))+1;
  86. {$endif}
  87. end;
  88. function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
  89. var
  90. tmpSecs: DWord;
  91. tmpDate: TDateTime;
  92. tmpTime: TDateTime;
  93. clockData: TClockData;
  94. begin
  95. with aDate do
  96. tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
  97. Amiga2Date(tmpSecs,@clockData);
  98. {$HINT TODO: implement msec values, if possible}
  99. with clockData do begin
  100. success:=TryEncodeDate(year,month,mday,tmpDate) and
  101. TryEncodeTime(hour,min,sec,0,tmpTime);
  102. end;
  103. result:=ComposeDateTime(tmpDate,tmpTime);
  104. end;
  105. function DateTimeToAmigaDateStamp(dateTime: TDateTime): TDateStamp;
  106. var
  107. tmpSecs: DWord;
  108. clockData: TClockData;
  109. tmpMSec: Word;
  110. begin
  111. {$HINT TODO: implement msec values, if possible}
  112. with clockData do begin
  113. DecodeDate(dateTime,year,month,mday);
  114. DecodeTime(dateTime,hour,min,sec,tmpMSec);
  115. end;
  116. tmpSecs:=Date2Amiga(@clockData);
  117. with result do begin
  118. ds_Days:= tmpSecs div (24 * 60 * 60);
  119. ds_Minute:= (tmpSecs div 60) mod ds_Days;
  120. ds_Tick:= (((tmpSecs mod 60) mod ds_Minute) mod ds_Days) * TICKS_PER_SECOND;
  121. end;
  122. end;
  123. {****************************************************************************
  124. File Functions
  125. ****************************************************************************}
  126. {$I-}{ Required for correct usage of these routines }
  127. (****** non portable routines ******)
  128. function FileOpen(const FileName: rawbytestring; Mode: Integer): THandle;
  129. var
  130. SystemFileName: RawByteString;
  131. dosResult: LongInt;
  132. begin
  133. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  134. {$WARNING FIX ME! To do: FileOpen Access Modes}
  135. dosResult:=Open(PChar(SystemFileName),MODE_OLDFILE);
  136. if dosResult=0 then
  137. dosResult:=-1
  138. else
  139. AddToList(ASYS_fileList,dosResult);
  140. FileOpen:=dosResult;
  141. end;
  142. function FileGetDate(Handle: THandle) : LongInt;
  143. var
  144. tmpFIB : PFileInfoBlock;
  145. tmpDateTime: TDateTime;
  146. validFile: boolean;
  147. begin
  148. validFile:=false;
  149. if (Handle <> 0) then begin
  150. new(tmpFIB);
  151. if ExamineFH(BPTR(Handle),tmpFIB) then begin
  152. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  153. end;
  154. dispose(tmpFIB);
  155. end;
  156. if validFile then
  157. result:=DateTimeToFileDate(tmpDateTime)
  158. else
  159. result:=-1;
  160. end;
  161. function FileSetDate(Handle: THandle; Age: LongInt) : LongInt;
  162. var
  163. tmpDateStamp: TDateStamp;
  164. tmpName: array[0..255] of char;
  165. begin
  166. result:=0;
  167. if (Handle <> 0) then begin
  168. if NameFromFH(BPTR(Handle), @tmpName, 256) then begin
  169. tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
  170. if not SetFileDate(@tmpName,@tmpDateStamp) then begin
  171. IoErr(); // dump the error code for now (TODO)
  172. result:=-1;
  173. end;
  174. end;
  175. end;
  176. end;
  177. function FileSetDate(const FileName: RawByteString; Age: LongInt) : LongInt;
  178. var
  179. tmpDateStamp: TDateStamp;
  180. SystemFileName: RawByteString;
  181. begin
  182. result:=0;
  183. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  184. tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
  185. if not SetFileDate(PChar(SystemFileName),@tmpDateStamp) then begin
  186. IoErr(); // dump the error code for now (TODO)
  187. result:=-1;
  188. end;
  189. end;
  190. function FileCreate(const FileName: RawByteString) : THandle;
  191. var
  192. SystemFileName: RawByteString;
  193. dosResult: LongInt;
  194. begin
  195. dosResult:=-1;
  196. { Open file in MODDE_READWRITE, then truncate it by hand rather than
  197. opening it in MODE_NEWFILE, because that returns an exclusive lock
  198. so some operations might fail with it (KB) }
  199. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  200. dosResult:=Open(PChar(SystemFileName),MODE_READWRITE);
  201. if dosResult = 0 then exit;
  202. if SetFileSize(dosResult, 0, OFFSET_BEGINNING) = 0 then
  203. AddToList(ASYS_fileList,dosResult)
  204. else begin
  205. dosClose(dosResult);
  206. dosResult:=-1;
  207. end;
  208. FileCreate:=dosResult;
  209. end;
  210. function FileCreate(const FileName: RawByteString; Rights: integer): THandle;
  211. begin
  212. {$WARNING FIX ME! To do: FileCreate Access Modes}
  213. FileCreate:=FileCreate(FileName);
  214. end;
  215. function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : integer): THandle;
  216. begin
  217. {$WARNING FIX ME! To do: FileCreate Access Modes}
  218. FileCreate:=FileCreate(FileName);
  219. end;
  220. function FileRead(Handle: THandle; out Buffer; Count: LongInt): LongInt;
  221. begin
  222. FileRead:=-1;
  223. if (Count<=0) or (Handle=0) or (Handle=-1) then exit;
  224. FileRead:=dosRead(Handle,@Buffer,Count);
  225. end;
  226. function FileWrite(Handle: THandle; const Buffer; Count: LongInt): LongInt;
  227. begin
  228. FileWrite:=-1;
  229. if (Count<=0) or (Handle=0) or (Handle=-1) then exit;
  230. FileWrite:=dosWrite(Handle,@Buffer,Count);
  231. end;
  232. function FileSeek(Handle: THandle; FOffset, Origin: LongInt) : LongInt;
  233. var
  234. seekMode: LongInt;
  235. begin
  236. FileSeek:=-1;
  237. if (Handle=0) or (Handle=-1) then exit;
  238. case Origin of
  239. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  240. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  241. fsFromEnd : seekMode:=OFFSET_END;
  242. end;
  243. dosSeek(Handle, FOffset, seekMode);
  244. { get the current position when FileSeek ends, which should return
  245. the *NEW* position, while Amiga Seek() returns the old one }
  246. FileSeek:=dosSeek(Handle, 0, OFFSET_CURRENT);
  247. end;
  248. function FileSeek(Handle: THandle; FOffset: Int64; Origin: Longint): Int64;
  249. begin
  250. {$WARNING Need to add 64bit call }
  251. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  252. end;
  253. procedure FileClose(Handle: THandle);
  254. begin
  255. if (Handle=0) or (Handle=-1) then exit;
  256. dosClose(Handle);
  257. RemoveFromList(ASYS_fileList,Handle);
  258. end;
  259. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  260. var
  261. dosResult: LongInt;
  262. begin
  263. FileTruncate:=False;
  264. if Size > high (longint) then exit;
  265. {$WARNING Possible support for 64-bit FS to be checked!}
  266. if (Handle=0) or (Handle=-1) then exit;
  267. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  268. if (dosResult<0) then exit;
  269. FileTruncate:=True;
  270. end;
  271. function DeleteFile(const FileName: RawByteString) : Boolean;
  272. var
  273. SystemFileName: RawByteString;
  274. begin
  275. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  276. DeleteFile:=dosDeleteFile(PChar(SystemFileName));
  277. end;
  278. function RenameFile(const OldName, NewName: RawByteString): Boolean;
  279. var
  280. OldSystemFileName, NewSystemFileName: RawByteString;
  281. begin
  282. OldSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(OldName));
  283. NewSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(NewName));
  284. RenameFile:=dosRename(PChar(OldSystemFileName), PChar(NewSystemFileName)) <> 0;
  285. end;
  286. (****** end of non portable routines ******)
  287. function FileAge (const FileName : RawByteString): Longint;
  288. var
  289. tmpLock: BPTR;
  290. tmpFIB : PFileInfoBlock;
  291. tmpDateTime: TDateTime;
  292. validFile: boolean;
  293. SystemFileName: RawByteString;
  294. begin
  295. validFile:=false;
  296. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  297. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  298. if (tmpLock <> 0) then begin
  299. new(tmpFIB);
  300. if Examine(tmpLock,tmpFIB) <> 0 then begin
  301. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  302. end;
  303. Unlock(tmpLock);
  304. dispose(tmpFIB);
  305. end;
  306. if validFile then
  307. result:=DateTimeToFileDate(tmpDateTime)
  308. else
  309. result:=-1;
  310. end;
  311. function FileExists (const FileName : RawByteString) : Boolean;
  312. var
  313. tmpLock: BPTR;
  314. tmpFIB : PFileInfoBlock;
  315. SystemFileName: RawByteString;
  316. begin
  317. result:=false;
  318. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  319. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  320. if (tmpLock <> 0) then begin
  321. new(tmpFIB);
  322. if (Examine(tmpLock,tmpFIB) <> 0) and (tmpFIB^.fib_DirEntryType <= 0) then
  323. result:=true;
  324. Unlock(tmpLock);
  325. dispose(tmpFIB);
  326. end;
  327. end;
  328. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  329. var
  330. tmpStr: RawByteString;
  331. Anchor: PAnchorPath;
  332. tmpDateTime: TDateTime;
  333. validDate: boolean;
  334. begin
  335. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  336. tmpStr:=PathConv(ToSingleByteFileSystemEncodedFileName(Path));
  337. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  338. Rslt.ExcludeAttr := (not Attr) and ($1e);
  339. Rslt.FindHandle := nil;
  340. new(Anchor);
  341. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  342. if MatchFirst(pchar(tmpStr),Anchor)<>0 then exit;
  343. Rslt.FindHandle := Anchor;
  344. with Anchor^.ap_Info do begin
  345. Name := fib_FileName;
  346. SetCodePage(Name,DefaultFileSystemCodePage,false);
  347. Rslt.Size := fib_Size;
  348. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  349. if not validDate then exit;
  350. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  351. Rslt.Attr := 128;
  352. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  353. if ((fib_Protection and FIBF_READ) <> 0) and
  354. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  355. result:=0; { Return zero if everything went OK }
  356. end;
  357. end;
  358. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  359. var
  360. Anchor: PAnchorPath;
  361. validDate: boolean;
  362. begin
  363. result:=-1;
  364. Anchor:=PAnchorPath(Rslt.FindHandle);
  365. if not assigned(Anchor) then exit;
  366. if MatchNext(Anchor) <> 0 then exit;
  367. with Anchor^.ap_Info do begin
  368. Name := fib_FileName;
  369. SetCodePage(Name,DefaultFileSystemCodePage,false);
  370. Rslt.Size := fib_Size;
  371. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  372. if not validDate then exit;
  373. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  374. Rslt.Attr := 128;
  375. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  376. if ((fib_Protection and FIBF_READ) <> 0) and
  377. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  378. result:=0; { Return zero if everything went OK }
  379. end;
  380. end;
  381. Procedure InternalFindClose(var Handle: Pointer);
  382. var
  383. Anchor: PAnchorPath;
  384. begin
  385. Anchor:=PAnchorPath(Handle);
  386. if not assigned(Anchor) then exit;
  387. MatchEnd(Anchor);
  388. Dispose(Anchor);
  389. Handle:=nil;
  390. end;
  391. (****** end of non portable routines ******)
  392. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  393. var
  394. F: file;
  395. attr: word;
  396. begin
  397. Assign(F,FileName);
  398. dos.GetFAttr(F,attr);
  399. if DosError <> 0 then
  400. FileGetAttr := -1
  401. else
  402. FileGetAttr := Attr;
  403. end;
  404. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  405. var
  406. F: file;
  407. begin
  408. Assign(F, FileName);
  409. Dos.SetFAttr(F, Attr and $ffff);
  410. FileSetAttr := DosError;
  411. end;
  412. {****************************************************************************
  413. Disk Functions
  414. ****************************************************************************}
  415. {
  416. The Diskfree and Disksize functions need a file on the specified drive, since this
  417. is required for the statfs system call.
  418. These filenames are set in drivestr[0..26], and have been preset to :
  419. 0 - ':' (default drive - hence current dir is ok.)
  420. 1 - 'DF0:' (floppy drive 1 - should be adapted to local system )
  421. 2 - 'DF1:' (floppy drive 2 - should be adapted to local system )
  422. 3 - 'SYS:' (C: equivalent of dos is the SYS: partition)
  423. 4..26 (can be set by you're own applications)
  424. ! Use AddDisk() to Add new drives !
  425. They both return -1 when a failure occurs.
  426. }
  427. var
  428. DeviceList: array[0..26] of string[20];
  429. NumDevices: Integer = 0;
  430. const
  431. IllegalDevices: array[0..12] of string =(
  432. 'PED:',
  433. 'PRJ:',
  434. 'PIPE:', // Pipes
  435. 'XPIPE:', // Extented Pipe
  436. 'CON:', // Console
  437. 'RAW:', // RAW: Console
  438. 'KCON:', // KingCON Console
  439. 'KRAW:', // KingCON RAW
  440. 'SER:', // serial Ports
  441. 'SER0:',
  442. 'SER1:',
  443. 'PAR:', // Parallel Porty
  444. 'PRT:'); // Printer
  445. function IsIllegalDevice(DeviceName: string): Boolean;
  446. var
  447. i: Integer;
  448. Str: AnsiString;
  449. begin
  450. IsIllegalDevice := False;
  451. Str := UpperCase(DeviceName);
  452. for i := Low(IllegalDevices) to High(IllegalDevices) do
  453. begin
  454. if Str = IllegalDevices[i] then
  455. begin
  456. IsIllegalDevice := True;
  457. Exit;
  458. end;
  459. end;
  460. end;
  461. function DeviceByIdx(Idx: Integer): string;
  462. begin
  463. DeviceByIdx := '';
  464. if (Idx < 0) or (Idx >= NumDevices) then
  465. Exit;
  466. DeviceByIdx := DeviceList[Idx];
  467. end;
  468. function AddDisk(const Path: string): Integer;
  469. begin
  470. // if hit border, restart at 4
  471. if NumDevices > 26 then
  472. NumDevices := 4;
  473. // set the device
  474. DeviceList[NumDevices] := Copy(Path, 1, 20);
  475. // return the Index increment for next run
  476. AddDisk := NumDevices;
  477. Inc(NumDevices);
  478. end;
  479. function RefreshDeviceList: Integer;
  480. var
  481. List: PDosList;
  482. Temp: PChar;
  483. Str: string;
  484. begin
  485. NumDevices := 0;
  486. AddDisk(':'); // Index 0
  487. AddDisk('DF0:'); // Index 1
  488. AddDisk('DF1:'); // Index 2
  489. AddDisk('SYS:'); // Index 3
  490. // Lock the List
  491. List := LockDosList(LDF_DEVICES or LDF_READ);
  492. // Inspect the List
  493. repeat
  494. List := NextDosEntry(List, LDF_DEVICES);
  495. if List <> nil then
  496. begin
  497. Temp := BSTR2STRING(List^.dol_Name);
  498. Str := strpas(Temp) + ':';
  499. if not IsIllegalDevice(str) then
  500. AddDisk(Str);
  501. end;
  502. until List = nil;
  503. UnLockDosList(LDF_DEVICES or LDF_READ);
  504. RefreshDeviceList := NumDevices;
  505. end;
  506. // New easier DiskSize()
  507. //
  508. function DiskSize(Drive: AnsiString): Int64;
  509. var
  510. DirLock: BPTR;
  511. Inf: TInfoData;
  512. MyProc: PProcess;
  513. OldWinPtr: Pointer;
  514. begin
  515. DiskSize := -1;
  516. //
  517. MyProc := PProcess(FindTask(Nil));
  518. OldWinPtr := MyProc^.pr_WindowPtr;
  519. MyProc^.pr_WindowPtr := Pointer(-1);
  520. //
  521. DirLock := Lock(PChar(Drive), SHARED_LOCK);
  522. if DirLock <> 0 then
  523. begin
  524. if Info(DirLock, @Inf) <> 0 then
  525. DiskSize := Int64(Inf.id_NumBlocks) * Inf.id_BytesPerBlock;
  526. UnLock(DirLock);
  527. end;
  528. if OldWinPtr <> Pointer(-1) then
  529. MyProc^.pr_WindowPtr := OldWinPtr;
  530. end;
  531. function DiskSize(Drive: Byte): Int64;
  532. begin
  533. DiskSize := -1;
  534. if (Drive < 0) or (Drive >= NumDevices) then
  535. Exit;
  536. DiskSize := DiskSize(DeviceList[Drive]);
  537. end;
  538. // New easier DiskFree()
  539. //
  540. function DiskFree(Drive: AnsiString): Int64;
  541. var
  542. DirLock: BPTR;
  543. Inf: TInfoData;
  544. MyProc: PProcess;
  545. OldWinPtr: Pointer;
  546. begin
  547. DiskFree := -1;
  548. //
  549. MyProc := PProcess(FindTask(Nil));
  550. OldWinPtr := MyProc^.pr_WindowPtr;
  551. MyProc^.pr_WindowPtr := Pointer(-1);
  552. //
  553. DirLock := Lock(PChar(Drive), SHARED_LOCK);
  554. if DirLock <> 0 then
  555. begin
  556. if Info(DirLock, @Inf) <> 0 then
  557. DiskFree := Int64(Inf.id_NumBlocks - Inf.id_NumBlocksUsed) * Inf.id_BytesPerBlock;
  558. UnLock(DirLock);
  559. end;
  560. if OldWinPtr <> Pointer(-1) then
  561. MyProc^.pr_WindowPtr := OldWinPtr;
  562. end;
  563. function DiskFree(Drive: Byte): Int64;
  564. begin
  565. DiskFree := -1;
  566. if (Drive < 0) or (Drive >= NumDevices) then
  567. Exit;
  568. DiskFree := DiskFree(DeviceList[Drive]);
  569. end;
  570. function DirectoryExists(const Directory: RawByteString): Boolean;
  571. var
  572. tmpLock: BPTR;
  573. FIB : PFileInfoBlock;
  574. SystemDirName: RawByteString;
  575. begin
  576. result:=false;
  577. if (Directory='') or (InOutRes<>0) then exit;
  578. SystemDirName:=PathConv(ToSingleByteFileSystemEncodedFileName(Directory));
  579. tmpLock:=Lock(PChar(SystemDirName),SHARED_LOCK);
  580. if tmpLock=0 then exit;
  581. FIB:=nil; new(FIB);
  582. if (Examine(tmpLock,FIB) <> 0) and (FIB^.fib_DirEntryType>0) then
  583. result:=True;
  584. if tmpLock<>0 then Unlock(tmpLock);
  585. if assigned(FIB) then dispose(FIB);
  586. end;
  587. {****************************************************************************
  588. Locale Functions
  589. ****************************************************************************}
  590. Procedure GetLocalTime(var SystemTime: TSystemTime);
  591. var
  592. dayOfWeek: word;
  593. Sec100: Word;
  594. begin
  595. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, Sec100);
  596. SystemTime.Millisecond := Sec100 * 10;
  597. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  598. end;
  599. Procedure InitAnsi;
  600. Var
  601. i : longint;
  602. begin
  603. { Fill table entries 0 to 127 }
  604. for i := 0 to 96 do
  605. UpperCaseTable[i] := chr(i);
  606. for i := 97 to 122 do
  607. UpperCaseTable[i] := chr(i - 32);
  608. for i := 123 to 191 do
  609. UpperCaseTable[i] := chr(i);
  610. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  611. for i := 0 to 64 do
  612. LowerCaseTable[i] := chr(i);
  613. for i := 65 to 90 do
  614. LowerCaseTable[i] := chr(i + 32);
  615. for i := 91 to 191 do
  616. LowerCaseTable[i] := chr(i);
  617. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  618. end;
  619. Procedure InitInternational;
  620. begin
  621. InitInternationalGeneric;
  622. InitAnsi;
  623. end;
  624. function SysErrorMessage(ErrorCode: Integer): String;
  625. begin
  626. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  627. end;
  628. function GetLastOSError: Integer;
  629. begin
  630. result:=-1;
  631. end;
  632. {****************************************************************************
  633. OS utility functions
  634. ****************************************************************************}
  635. var
  636. StrOfPaths: String;
  637. function SystemTags(const command: PChar; const tags: array of PtrUInt): LongInt;
  638. begin
  639. SystemTags:=SystemTagList(command,@tags);
  640. end;
  641. function GetPathString: String;
  642. var
  643. f : text;
  644. s : string;
  645. begin
  646. s := '';
  647. result := '';
  648. { Alternatively, this could use PIPE: handler on systems which
  649. have this by default (not the case on classic Amiga), but then
  650. the child process should be started async, which for a simple
  651. Path command probably isn't worth the trouble. (KB) }
  652. assign(f,'T:'+HexStr(FindTask(nil))+'_path.tmp');
  653. rewrite(f);
  654. { This is a pretty ugly stunt, combining Pascal and Amiga system
  655. functions, but works... }
  656. SystemTags('C:Path',[SYS_Input, 0, SYS_Output, TextRec(f).Handle, TAG_END]);
  657. close(f);
  658. reset(f);
  659. { skip the first line, garbage }
  660. if not eof(f) then readln(f,s);
  661. while not eof(f) do begin
  662. readln(f,s);
  663. if result = '' then
  664. result := s
  665. else
  666. result := result + ';' + s;
  667. end;
  668. close(f);
  669. erase(f);
  670. end;
  671. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  672. begin
  673. if UpCase(envvar) = 'PATH' then begin
  674. if StrOfpaths = '' then StrOfPaths := GetPathString;
  675. Result:=StrOfPaths;
  676. end else
  677. Result:=Dos.Getenv(shortstring(EnvVar));
  678. end;
  679. Function GetEnvironmentVariableCount : Integer;
  680. begin
  681. // Result:=FPCCountEnvVar(EnvP);
  682. Result:=Dos.envCount;
  683. end;
  684. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  685. begin
  686. // Result:=FPCGetEnvStrFromP(Envp,Index);
  687. Result:=Dos.EnvStr(Index);
  688. end;
  689. function ExecuteProcess (const Path: RawByteString; const ComLine: RawByteString;Flags:TExecuteFlags=[]):
  690. integer;
  691. var
  692. tmpPath,
  693. convPath: RawByteString;
  694. CommandLine: AnsiString;
  695. tmpLock: BPTR;
  696. E: EOSError;
  697. begin
  698. DosError:= 0;
  699. convPath:=PathConv(ToSingleByteFileSystemEncodedFileName(Path));
  700. tmpPath:=convPath+' '+ToSingleByteFileSystemEncodedFileName(ComLine);
  701. { Here we must first check if the command we wish to execute }
  702. { actually exists, because this is NOT handled by the }
  703. { _SystemTagList call (program will abort!!) }
  704. { Try to open with shared lock }
  705. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  706. if tmpLock<>0 then
  707. begin
  708. { File exists - therefore unlock it }
  709. Unlock(tmpLock);
  710. result:=SystemTagList(PChar(tmpPath),nil);
  711. { on return of -1 the shell could not be executed }
  712. { probably because there was not enough memory }
  713. if result = -1 then
  714. DosError:=8;
  715. end
  716. else
  717. DosError:=3;
  718. if DosError <> 0 then begin
  719. if ComLine = '' then
  720. CommandLine := Path
  721. else
  722. CommandLine := Path + ' ' + ComLine;
  723. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  724. E.ErrorCode := DosError;
  725. raise E;
  726. end;
  727. end;
  728. function ExecuteProcess (const Path: RawByteString;
  729. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  730. var
  731. CommandLine: RawByteString;
  732. I: integer;
  733. begin
  734. Commandline := '';
  735. for I := 0 to High (ComLine) do
  736. if Pos (' ', ComLine [I]) <> 0 then
  737. CommandLine := CommandLine + ' ' + '"' + ToSingleByteFileSystemEncodedFileName(ComLine [I]) + '"'
  738. else
  739. CommandLine := CommandLine + ' ' + ToSingleByteFileSystemEncodedFileName(Comline [I]);
  740. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  741. end;
  742. procedure Sleep(Milliseconds: cardinal);
  743. begin
  744. // Amiga dos.library Delay() has precision of 1/50 seconds
  745. DOSDelay(Milliseconds div 20);
  746. end;
  747. {****************************************************************************
  748. Initialization code
  749. ****************************************************************************}
  750. Initialization
  751. InitExceptions;
  752. InitInternational; { Initialize internationalization settings }
  753. OnBeep:=Nil; { No SysBeep() on Amiga, for now. Figure out if we want
  754. to use intuition.library/DisplayBeep() for this (KB) }
  755. StrOfPaths:='';
  756. RefreshDeviceList;
  757. Finalization
  758. DoneExceptions;
  759. end.