sysutils.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. Rslt.FindHandle := Anchor;
  343. if MatchFirst(pchar(tmpStr),Anchor)<>0 then
  344. begin
  345. InternalFindClose(Rslt.FindHandle);
  346. exit;
  347. end;
  348. with Anchor^.ap_Info do begin
  349. Name := fib_FileName;
  350. SetCodePage(Name,DefaultFileSystemCodePage,false);
  351. Rslt.Size := fib_Size;
  352. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  353. if not validDate then
  354. begin
  355. InternalFindClose(Rslt.FindHandle);
  356. exit;
  357. end;
  358. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  359. Rslt.Attr := 128;
  360. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  361. if ((fib_Protection and FIBF_READ) <> 0) and
  362. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  363. result:=0; { Return zero if everything went OK }
  364. end;
  365. end;
  366. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  367. var
  368. Anchor: PAnchorPath;
  369. validDate: boolean;
  370. begin
  371. result:=-1;
  372. Anchor:=PAnchorPath(Rslt.FindHandle);
  373. if not assigned(Anchor) then exit;
  374. if MatchNext(Anchor) <> 0 then exit;
  375. with Anchor^.ap_Info do begin
  376. Name := fib_FileName;
  377. SetCodePage(Name,DefaultFileSystemCodePage,false);
  378. Rslt.Size := fib_Size;
  379. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  380. if not validDate then exit;
  381. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  382. Rslt.Attr := 128;
  383. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  384. if ((fib_Protection and FIBF_READ) <> 0) and
  385. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  386. result:=0; { Return zero if everything went OK }
  387. end;
  388. end;
  389. Procedure InternalFindClose(var Handle: Pointer);
  390. var
  391. Anchor: PAnchorPath absolute Handle;
  392. begin
  393. if not assigned(Anchor) then
  394. exit;
  395. MatchEnd(Anchor);
  396. Dispose(Anchor);
  397. Handle:=nil;
  398. end;
  399. (****** end of non portable routines ******)
  400. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  401. var
  402. F: file;
  403. attr: word;
  404. begin
  405. Assign(F,FileName);
  406. dos.GetFAttr(F,attr);
  407. if DosError <> 0 then
  408. FileGetAttr := -1
  409. else
  410. FileGetAttr := Attr;
  411. end;
  412. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  413. var
  414. F: file;
  415. begin
  416. Assign(F, FileName);
  417. Dos.SetFAttr(F, Attr and $ffff);
  418. FileSetAttr := DosError;
  419. end;
  420. {****************************************************************************
  421. Disk Functions
  422. ****************************************************************************}
  423. {
  424. The Diskfree and Disksize functions need a file on the specified drive, since this
  425. is required for the statfs system call.
  426. These filenames are set in drivestr[0..26], and have been preset to :
  427. 0 - ':' (default drive - hence current dir is ok.)
  428. 1 - 'DF0:' (floppy drive 1 - should be adapted to local system )
  429. 2 - 'DF1:' (floppy drive 2 - should be adapted to local system )
  430. 3 - 'SYS:' (C: equivalent of dos is the SYS: partition)
  431. 4..26 (can be set by you're own applications)
  432. ! Use AddDisk() to Add new drives !
  433. They both return -1 when a failure occurs.
  434. }
  435. var
  436. DeviceList: array[0..26] of string[20];
  437. NumDevices: Integer = 0;
  438. const
  439. IllegalDevices: array[0..12] of string =(
  440. 'PED:',
  441. 'PRJ:',
  442. 'PIPE:', // Pipes
  443. 'XPIPE:', // Extented Pipe
  444. 'CON:', // Console
  445. 'RAW:', // RAW: Console
  446. 'KCON:', // KingCON Console
  447. 'KRAW:', // KingCON RAW
  448. 'SER:', // serial Ports
  449. 'SER0:',
  450. 'SER1:',
  451. 'PAR:', // Parallel Porty
  452. 'PRT:'); // Printer
  453. function IsIllegalDevice(DeviceName: string): Boolean;
  454. var
  455. i: Integer;
  456. Str: AnsiString;
  457. begin
  458. IsIllegalDevice := False;
  459. Str := UpperCase(DeviceName);
  460. for i := Low(IllegalDevices) to High(IllegalDevices) do
  461. begin
  462. if Str = IllegalDevices[i] then
  463. begin
  464. IsIllegalDevice := True;
  465. Exit;
  466. end;
  467. end;
  468. end;
  469. function DeviceByIdx(Idx: Integer): string;
  470. begin
  471. DeviceByIdx := '';
  472. if (Idx < 0) or (Idx >= NumDevices) then
  473. Exit;
  474. DeviceByIdx := DeviceList[Idx];
  475. end;
  476. function AddDisk(const Path: string): Integer;
  477. begin
  478. // if hit border, restart at 4
  479. if NumDevices > 26 then
  480. NumDevices := 4;
  481. // set the device
  482. DeviceList[NumDevices] := Copy(Path, 1, 20);
  483. // return the Index increment for next run
  484. AddDisk := NumDevices;
  485. Inc(NumDevices);
  486. end;
  487. function RefreshDeviceList: Integer;
  488. var
  489. List: PDosList;
  490. Temp: PChar;
  491. Str: string;
  492. begin
  493. NumDevices := 0;
  494. AddDisk(':'); // Index 0
  495. AddDisk('DF0:'); // Index 1
  496. AddDisk('DF1:'); // Index 2
  497. AddDisk('SYS:'); // Index 3
  498. // Lock the List
  499. List := LockDosList(LDF_DEVICES or LDF_READ);
  500. // Inspect the List
  501. repeat
  502. List := NextDosEntry(List, LDF_DEVICES);
  503. if List <> nil then
  504. begin
  505. Temp := BSTR2STRING(List^.dol_Name);
  506. Str := strpas(Temp) + ':';
  507. if not IsIllegalDevice(str) then
  508. AddDisk(Str);
  509. end;
  510. until List = nil;
  511. UnLockDosList(LDF_DEVICES or LDF_READ);
  512. RefreshDeviceList := NumDevices;
  513. end;
  514. // New easier DiskSize()
  515. //
  516. function DiskSize(Drive: AnsiString): Int64;
  517. var
  518. DirLock: BPTR;
  519. Inf: TInfoData;
  520. MyProc: PProcess;
  521. OldWinPtr: Pointer;
  522. begin
  523. DiskSize := -1;
  524. //
  525. MyProc := PProcess(FindTask(Nil));
  526. OldWinPtr := MyProc^.pr_WindowPtr;
  527. MyProc^.pr_WindowPtr := Pointer(-1);
  528. //
  529. DirLock := Lock(PChar(Drive), SHARED_LOCK);
  530. if DirLock <> 0 then
  531. begin
  532. if Info(DirLock, @Inf) <> 0 then
  533. DiskSize := Int64(Inf.id_NumBlocks) * Inf.id_BytesPerBlock;
  534. UnLock(DirLock);
  535. end;
  536. if OldWinPtr <> Pointer(-1) then
  537. MyProc^.pr_WindowPtr := OldWinPtr;
  538. end;
  539. function DiskSize(Drive: Byte): Int64;
  540. begin
  541. DiskSize := -1;
  542. if (Drive < 0) or (Drive >= NumDevices) then
  543. Exit;
  544. DiskSize := DiskSize(DeviceList[Drive]);
  545. end;
  546. // New easier DiskFree()
  547. //
  548. function DiskFree(Drive: AnsiString): Int64;
  549. var
  550. DirLock: BPTR;
  551. Inf: TInfoData;
  552. MyProc: PProcess;
  553. OldWinPtr: Pointer;
  554. begin
  555. DiskFree := -1;
  556. //
  557. MyProc := PProcess(FindTask(Nil));
  558. OldWinPtr := MyProc^.pr_WindowPtr;
  559. MyProc^.pr_WindowPtr := Pointer(-1);
  560. //
  561. DirLock := Lock(PChar(Drive), SHARED_LOCK);
  562. if DirLock <> 0 then
  563. begin
  564. if Info(DirLock, @Inf) <> 0 then
  565. DiskFree := Int64(Inf.id_NumBlocks - Inf.id_NumBlocksUsed) * Inf.id_BytesPerBlock;
  566. UnLock(DirLock);
  567. end;
  568. if OldWinPtr <> Pointer(-1) then
  569. MyProc^.pr_WindowPtr := OldWinPtr;
  570. end;
  571. function DiskFree(Drive: Byte): Int64;
  572. begin
  573. DiskFree := -1;
  574. if (Drive < 0) or (Drive >= NumDevices) then
  575. Exit;
  576. DiskFree := DiskFree(DeviceList[Drive]);
  577. end;
  578. function DirectoryExists(const Directory: RawByteString): Boolean;
  579. var
  580. tmpLock: BPTR;
  581. FIB : PFileInfoBlock;
  582. SystemDirName: RawByteString;
  583. begin
  584. result:=false;
  585. if (Directory='') or (InOutRes<>0) then exit;
  586. SystemDirName:=PathConv(ToSingleByteFileSystemEncodedFileName(Directory));
  587. tmpLock:=Lock(PChar(SystemDirName),SHARED_LOCK);
  588. if tmpLock=0 then exit;
  589. FIB:=nil; new(FIB);
  590. if (Examine(tmpLock,FIB) <> 0) and (FIB^.fib_DirEntryType>0) then
  591. result:=True;
  592. if tmpLock<>0 then Unlock(tmpLock);
  593. if assigned(FIB) then dispose(FIB);
  594. end;
  595. {****************************************************************************
  596. Locale Functions
  597. ****************************************************************************}
  598. Procedure GetLocalTime(var SystemTime: TSystemTime);
  599. var
  600. dayOfWeek: word;
  601. Sec100: Word;
  602. begin
  603. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, Sec100);
  604. SystemTime.Millisecond := Sec100 * 10;
  605. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  606. end;
  607. Procedure InitAnsi;
  608. Var
  609. i : longint;
  610. begin
  611. { Fill table entries 0 to 127 }
  612. for i := 0 to 96 do
  613. UpperCaseTable[i] := chr(i);
  614. for i := 97 to 122 do
  615. UpperCaseTable[i] := chr(i - 32);
  616. for i := 123 to 191 do
  617. UpperCaseTable[i] := chr(i);
  618. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  619. for i := 0 to 64 do
  620. LowerCaseTable[i] := chr(i);
  621. for i := 65 to 90 do
  622. LowerCaseTable[i] := chr(i + 32);
  623. for i := 91 to 191 do
  624. LowerCaseTable[i] := chr(i);
  625. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  626. end;
  627. Procedure InitInternational;
  628. begin
  629. InitInternationalGeneric;
  630. InitAnsi;
  631. end;
  632. function SysErrorMessage(ErrorCode: Integer): String;
  633. begin
  634. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  635. end;
  636. function GetLastOSError: Integer;
  637. begin
  638. result:=-1;
  639. end;
  640. {****************************************************************************
  641. OS utility functions
  642. ****************************************************************************}
  643. var
  644. StrOfPaths: String;
  645. function SystemTags(const command: PChar; const tags: array of PtrUInt): LongInt;
  646. begin
  647. SystemTags:=SystemTagList(command,@tags);
  648. end;
  649. function GetPathString: String;
  650. var
  651. f : text;
  652. s : string;
  653. begin
  654. s := '';
  655. result := '';
  656. { Alternatively, this could use PIPE: handler on systems which
  657. have this by default (not the case on classic Amiga), but then
  658. the child process should be started async, which for a simple
  659. Path command probably isn't worth the trouble. (KB) }
  660. assign(f,'T:'+HexStr(FindTask(nil))+'_path.tmp');
  661. rewrite(f);
  662. { This is a pretty ugly stunt, combining Pascal and Amiga system
  663. functions, but works... }
  664. SystemTags('C:Path',[SYS_Input, 0, SYS_Output, TextRec(f).Handle, TAG_END]);
  665. close(f);
  666. reset(f);
  667. { skip the first line, garbage }
  668. if not eof(f) then readln(f,s);
  669. while not eof(f) do begin
  670. readln(f,s);
  671. if result = '' then
  672. result := s
  673. else
  674. result := result + ';' + s;
  675. end;
  676. close(f);
  677. erase(f);
  678. end;
  679. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  680. begin
  681. if UpCase(envvar) = 'PATH' then begin
  682. if StrOfpaths = '' then StrOfPaths := GetPathString;
  683. Result:=StrOfPaths;
  684. end else
  685. Result:=Dos.Getenv(shortstring(EnvVar));
  686. end;
  687. Function GetEnvironmentVariableCount : Integer;
  688. begin
  689. // Result:=FPCCountEnvVar(EnvP);
  690. Result:=Dos.envCount;
  691. end;
  692. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  693. begin
  694. // Result:=FPCGetEnvStrFromP(Envp,Index);
  695. Result:=Dos.EnvStr(Index);
  696. end;
  697. function ExecuteProcess (const Path: RawByteString; const ComLine: RawByteString;Flags:TExecuteFlags=[]):
  698. integer;
  699. var
  700. tmpPath,
  701. convPath: RawByteString;
  702. CommandLine: AnsiString;
  703. tmpLock: BPTR;
  704. E: EOSError;
  705. begin
  706. DosError:= 0;
  707. convPath:=PathConv(ToSingleByteFileSystemEncodedFileName(Path));
  708. tmpPath:=convPath+' '+ToSingleByteFileSystemEncodedFileName(ComLine);
  709. { Here we must first check if the command we wish to execute }
  710. { actually exists, because this is NOT handled by the }
  711. { _SystemTagList call (program will abort!!) }
  712. { Try to open with shared lock }
  713. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  714. if tmpLock<>0 then
  715. begin
  716. { File exists - therefore unlock it }
  717. Unlock(tmpLock);
  718. result:=SystemTagList(PChar(tmpPath),nil);
  719. { on return of -1 the shell could not be executed }
  720. { probably because there was not enough memory }
  721. if result = -1 then
  722. DosError:=8;
  723. end
  724. else
  725. DosError:=3;
  726. if DosError <> 0 then begin
  727. if ComLine = '' then
  728. CommandLine := Path
  729. else
  730. CommandLine := Path + ' ' + ComLine;
  731. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  732. E.ErrorCode := DosError;
  733. raise E;
  734. end;
  735. end;
  736. function ExecuteProcess (const Path: RawByteString;
  737. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  738. var
  739. CommandLine: RawByteString;
  740. I: integer;
  741. begin
  742. Commandline := '';
  743. for I := 0 to High (ComLine) do
  744. if Pos (' ', ComLine [I]) <> 0 then
  745. CommandLine := CommandLine + ' ' + '"' + ToSingleByteFileSystemEncodedFileName(ComLine [I]) + '"'
  746. else
  747. CommandLine := CommandLine + ' ' + ToSingleByteFileSystemEncodedFileName(Comline [I]);
  748. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  749. end;
  750. procedure Sleep(Milliseconds: cardinal);
  751. begin
  752. // Amiga dos.library Delay() has precision of 1/50 seconds
  753. DOSDelay(Milliseconds div 20);
  754. end;
  755. {****************************************************************************
  756. Initialization code
  757. ****************************************************************************}
  758. Initialization
  759. InitExceptions;
  760. InitInternational; { Initialize internationalization settings }
  761. OnBeep:=Nil; { No SysBeep() on Amiga, for now. Figure out if we want
  762. to use intuition.library/DisplayBeep() for this (KB) }
  763. StrOfPaths:='';
  764. RefreshDeviceList;
  765. Finalization
  766. DoneExceptions;
  767. end.