sysutils.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. {$DEFINE OS_FILESETDATEBYNAME}
  22. {$DEFINE HAS_SLEEP}
  23. {$DEFINE HAS_OSERROR}
  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. Procedure AddDisk(const path:string);
  32. implementation
  33. uses dos,sysconst;
  34. {$DEFINE FPC_FEXPAND_VOLUMES} (* Full paths begin with drive specification *)
  35. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  36. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  37. { Include platform independent implementation part }
  38. {$i sysutils.inc}
  39. { * Include sytem specific includes * }
  40. {$include execd.inc}
  41. {$include execf.inc}
  42. {$include timerd.inc}
  43. {$include doslibd.inc}
  44. {$include doslibf.inc}
  45. {$include utilf.inc}
  46. { * Followings are implemented in the system unit! * }
  47. function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
  48. function PathConv(path: RawByteString): RawByteString; external name 'PATHCONVRBS';
  49. procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
  50. function RemoveFromList(var l: Pointer; h: LongInt): boolean; external name 'REMOVEFROMLIST';
  51. function CheckInList(var l: Pointer; h: LongInt): pointer; external name 'CHECKINLIST';
  52. var
  53. ASYS_FileList: Pointer; external name 'ASYS_FILELIST';
  54. function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
  55. var
  56. tmpSecs: DWord;
  57. tmpDate: TDateTime;
  58. tmpTime: TDateTime;
  59. clockData: TClockData;
  60. begin
  61. with aDate do
  62. tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
  63. Amiga2Date(tmpSecs,@clockData);
  64. {$HINT TODO: implement msec values, if possible}
  65. with clockData do begin
  66. success:=TryEncodeDate(year,month,mday,tmpDate) and
  67. TryEncodeTime(hour,min,sec,0,tmpTime);
  68. end;
  69. result:=ComposeDateTime(tmpDate,tmpTime);
  70. end;
  71. function DateTimeToAmigaDateStamp(dateTime: TDateTime): TDateStamp;
  72. var
  73. tmpSecs: DWord;
  74. clockData: TClockData;
  75. tmpMSec: Word;
  76. begin
  77. {$HINT TODO: implement msec values, if possible}
  78. with clockData do begin
  79. DecodeDate(dateTime,year,month,mday);
  80. DecodeTime(dateTime,hour,min,sec,tmpMSec);
  81. end;
  82. tmpSecs:=Date2Amiga(@clockData);
  83. with result do begin
  84. ds_Days:= tmpSecs div (24 * 60 * 60);
  85. ds_Minute:= (tmpSecs div 60) mod ds_Days;
  86. ds_Tick:= (((tmpSecs mod 60) mod ds_Minute) mod ds_Days) * TICKS_PER_SECOND;
  87. end;
  88. end;
  89. {****************************************************************************
  90. File Functions
  91. ****************************************************************************}
  92. {$I-}{ Required for correct usage of these routines }
  93. (****** non portable routines ******)
  94. function FileOpen(const FileName: rawbytestring; Mode: Integer): LongInt;
  95. var
  96. SystemFileName: RawByteString;
  97. dosResult: LongInt;
  98. begin
  99. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  100. {$WARNING FIX ME! To do: FileOpen Access Modes}
  101. dosResult:=Open(PChar(SystemFileName),MODE_OLDFILE);
  102. if dosResult=0 then
  103. dosResult:=-1
  104. else
  105. AddToList(ASYS_fileList,dosResult);
  106. FileOpen:=dosResult;
  107. end;
  108. function FileGetDate(Handle: LongInt) : LongInt;
  109. var
  110. tmpFIB : PFileInfoBlock;
  111. tmpDateTime: TDateTime;
  112. validFile: boolean;
  113. begin
  114. validFile:=false;
  115. if (Handle <> 0) then begin
  116. new(tmpFIB);
  117. if ExamineFH(BPTR(Handle),tmpFIB) then begin
  118. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  119. end;
  120. dispose(tmpFIB);
  121. end;
  122. if validFile then
  123. result:=DateTimeToFileDate(tmpDateTime)
  124. else
  125. result:=-1;
  126. end;
  127. function FileSetDate(Handle, Age: LongInt) : LongInt;
  128. var
  129. tmpDateStamp: TDateStamp;
  130. tmpName: array[0..255] of char;
  131. begin
  132. result:=0;
  133. if (Handle <> 0) then begin
  134. if NameFromFH(BPTR(Handle), @tmpName, 256) then begin
  135. tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
  136. if not SetFileDate(@tmpName,@tmpDateStamp) then begin
  137. IoErr(); // dump the error code for now (TODO)
  138. result:=-1;
  139. end;
  140. end;
  141. end;
  142. end;
  143. function FileSetDate(const FileName: RawByteString; Age: LongInt) : LongInt;
  144. var
  145. tmpDateStamp: TDateStamp;
  146. SystemFileName: RawByteString;
  147. begin
  148. result:=0;
  149. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  150. tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
  151. if not SetFileDate(PChar(SystemFileName),@tmpDateStamp) then begin
  152. IoErr(); // dump the error code for now (TODO)
  153. result:=-1;
  154. end;
  155. end;
  156. function FileCreate(const FileName: RawByteString) : LongInt;
  157. var
  158. SystemFileName: RawByteString;
  159. dosResult: LongInt;
  160. begin
  161. dosResult:=-1;
  162. { Open file in MODDE_READWRITE, then truncate it by hand rather than
  163. opening it in MODE_NEWFILE, because that returns an exclusive lock
  164. so some operations might fail with it (KB) }
  165. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  166. dosResult:=Open(PChar(SystemFileName),MODE_READWRITE);
  167. if dosResult = 0 then exit;
  168. if SetFileSize(dosResult, 0, OFFSET_BEGINNING) = 0 then
  169. AddToList(ASYS_fileList,dosResult)
  170. else begin
  171. dosClose(dosResult);
  172. dosResult:=-1;
  173. end;
  174. FileCreate:=dosResult;
  175. end;
  176. function FileCreate(const FileName: RawByteString; Rights: integer): LongInt;
  177. begin
  178. {$WARNING FIX ME! To do: FileCreate Access Modes}
  179. FileCreate:=FileCreate(FileName);
  180. end;
  181. function FileCreate(const FileName: RawByteString; ShareMode: integer; Rights : integer): LongInt;
  182. begin
  183. {$WARNING FIX ME! To do: FileCreate Access Modes}
  184. FileCreate:=FileCreate(FileName);
  185. end;
  186. function FileRead(Handle: LongInt; out Buffer; Count: LongInt): LongInt;
  187. begin
  188. FileRead:=-1;
  189. if (Count<=0) or (Handle=0) or (Handle=-1) then exit;
  190. FileRead:=dosRead(Handle,@Buffer,Count);
  191. end;
  192. function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
  193. begin
  194. FileWrite:=-1;
  195. if (Count<=0) or (Handle=0) or (Handle=-1) then exit;
  196. FileWrite:=dosWrite(Handle,@Buffer,Count);
  197. end;
  198. function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
  199. var
  200. seekMode: LongInt;
  201. begin
  202. FileSeek:=-1;
  203. if (Handle=0) or (Handle=-1) then exit;
  204. case Origin of
  205. fsFromBeginning: seekMode:=OFFSET_BEGINNING;
  206. fsFromCurrent : seekMode:=OFFSET_CURRENT;
  207. fsFromEnd : seekMode:=OFFSET_END;
  208. end;
  209. dosSeek(Handle, FOffset, seekMode);
  210. { get the current position when FileSeek ends, which should return
  211. the *NEW* position, while Amiga Seek() returns the old one }
  212. FileSeek:=dosSeek(Handle, 0, OFFSET_CURRENT);
  213. end;
  214. function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64;
  215. begin
  216. {$WARNING Need to add 64bit call }
  217. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  218. end;
  219. procedure FileClose(Handle: LongInt);
  220. begin
  221. if (Handle=0) or (Handle=-1) then exit;
  222. dosClose(Handle);
  223. RemoveFromList(ASYS_fileList,Handle);
  224. end;
  225. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  226. var
  227. dosResult: LongInt;
  228. begin
  229. FileTruncate:=False;
  230. if Size > high (longint) then exit;
  231. {$WARNING Possible support for 64-bit FS to be checked!}
  232. if (Handle=0) or (Handle=-1) then exit;
  233. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  234. if (dosResult<0) then exit;
  235. FileTruncate:=True;
  236. end;
  237. function DeleteFile(const FileName: RawByteString) : Boolean;
  238. var
  239. SystemFileName: RawByteString;
  240. begin
  241. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  242. DeleteFile:=dosDeleteFile(PChar(SystemFileName));
  243. end;
  244. function RenameFile(const OldName, NewName: RawByteString): Boolean;
  245. var
  246. OldSystemFileName, NewSystemFileName: RawByteString;
  247. begin
  248. OldSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(OldName));
  249. NewSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(NewName));
  250. RenameFile:=dosRename(PChar(OldSystemFileName), PChar(NewSystemFileName)) <> 0;
  251. end;
  252. (****** end of non portable routines ******)
  253. function FileAge (const FileName : RawByteString): Longint;
  254. var
  255. tmpLock: Longint;
  256. tmpFIB : PFileInfoBlock;
  257. tmpDateTime: TDateTime;
  258. validFile: boolean;
  259. SystemFileName: RawByteString;
  260. begin
  261. validFile:=false;
  262. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  263. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  264. if (tmpLock <> 0) then begin
  265. new(tmpFIB);
  266. if Examine(tmpLock,tmpFIB) <> 0 then begin
  267. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  268. end;
  269. Unlock(tmpLock);
  270. dispose(tmpFIB);
  271. end;
  272. if validFile then
  273. result:=DateTimeToFileDate(tmpDateTime)
  274. else
  275. result:=-1;
  276. end;
  277. function FileExists (const FileName : RawByteString) : Boolean;
  278. var
  279. tmpLock: LongInt;
  280. tmpFIB : PFileInfoBlock;
  281. SystemFileName: RawByteString;
  282. begin
  283. result:=false;
  284. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  285. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  286. if (tmpLock <> 0) then begin
  287. new(tmpFIB);
  288. if (Examine(tmpLock,tmpFIB) <> 0) and (tmpFIB^.fib_DirEntryType <= 0) then
  289. result:=true;
  290. Unlock(tmpLock);
  291. dispose(tmpFIB);
  292. end;
  293. end;
  294. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  295. var
  296. tmpStr: RawByteString;
  297. Anchor: PAnchorPath;
  298. tmpDateTime: TDateTime;
  299. validDate: boolean;
  300. begin
  301. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  302. tmpStr:=PathConv(ToSingleByteFileSystemEncodedFileName(Path));
  303. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  304. Rslt.ExcludeAttr := (not Attr) and ($1e);
  305. Rslt.FindHandle := 0;
  306. new(Anchor);
  307. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  308. if MatchFirst(pchar(tmpStr),Anchor)<>0 then exit;
  309. Rslt.FindHandle := longint(Anchor);
  310. with Anchor^.ap_Info do begin
  311. Name := fib_FileName;
  312. SetCodePage(Name,DefaultFileSystemCodePage,false);
  313. Rslt.Size := fib_Size;
  314. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  315. if not validDate then exit;
  316. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  317. Rslt.Attr := 128;
  318. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  319. if ((fib_Protection and FIBF_READ) <> 0) and
  320. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  321. result:=0; { Return zero if everything went OK }
  322. end;
  323. end;
  324. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  325. var
  326. Anchor: PAnchorPath;
  327. validDate: boolean;
  328. begin
  329. result:=-1;
  330. Anchor:=PAnchorPath(Rslt.FindHandle);
  331. if not assigned(Anchor) then exit;
  332. if MatchNext(Anchor) <> 0 then exit;
  333. with Anchor^.ap_Info do begin
  334. Name := fib_FileName;
  335. SetCodePage(Name,DefaultFileSystemCodePage,false);
  336. Rslt.Size := fib_Size;
  337. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  338. if not validDate then exit;
  339. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  340. Rslt.Attr := 128;
  341. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  342. if ((fib_Protection and FIBF_READ) <> 0) and
  343. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  344. result:=0; { Return zero if everything went OK }
  345. end;
  346. end;
  347. Procedure InternalFindClose(var Handle: THandle);
  348. var
  349. Anchor: PAnchorPath;
  350. begin
  351. Anchor:=PAnchorPath(Handle);
  352. if not assigned(Anchor) then exit;
  353. MatchEnd(Anchor);
  354. Dispose(Anchor);
  355. Handle:=THandle(nil);
  356. end;
  357. (****** end of non portable routines ******)
  358. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  359. var
  360. F: file;
  361. attr: word;
  362. begin
  363. Assign(F,FileName);
  364. dos.GetFAttr(F,attr);
  365. if DosError <> 0 then
  366. FileGetAttr := -1
  367. else
  368. FileGetAttr := Attr;
  369. end;
  370. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  371. var
  372. F: file;
  373. begin
  374. Assign(F, FileName);
  375. Dos.SetFAttr(F, Attr and $ffff);
  376. FileSetAttr := DosError;
  377. end;
  378. {****************************************************************************
  379. Disk Functions
  380. ****************************************************************************}
  381. {
  382. The Diskfree and Disksize functions need a file on the specified drive, since this
  383. is required for the statfs system call.
  384. These filenames are set in drivestr[0..26], and have been preset to :
  385. 0 - '.' (default drive - hence current dir is ok.)
  386. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  387. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  388. 3 - '/' (C: equivalent of dos is the root partition)
  389. 4..26 (can be set by you're own applications)
  390. ! Use AddDisk() to Add new drives !
  391. They both return -1 when a failure occurs.
  392. }
  393. Const
  394. FixDriveStr : array[0..3] of pchar=(
  395. '.',
  396. '/fd0/.',
  397. '/fd1/.',
  398. '/.'
  399. );
  400. var
  401. Drives : byte;
  402. DriveStr : array[4..26] of pchar;
  403. Procedure AddDisk(const path:string);
  404. begin
  405. if not (DriveStr[Drives]=nil) then
  406. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  407. GetMem(DriveStr[Drives],length(Path)+1);
  408. StrPCopy(DriveStr[Drives],path);
  409. inc(Drives);
  410. if Drives>26 then
  411. Drives:=4;
  412. end;
  413. Function DiskFree(Drive: Byte): int64;
  414. Begin
  415. DiskFree := dos.diskFree(Drive);
  416. End;
  417. Function DiskSize(Drive: Byte): int64;
  418. Begin
  419. DiskSize := dos.DiskSize(Drive);
  420. End;
  421. function DirectoryExists(const Directory: RawByteString): Boolean;
  422. var
  423. tmpLock: LongInt;
  424. FIB : PFileInfoBlock;
  425. SystemDirName: RawByteString;
  426. begin
  427. result:=false;
  428. if (Directory='') or (InOutRes<>0) then exit;
  429. SystemDirName:=PathConv(ToSingleByteFileSystemEncodedFileName(Directory));
  430. tmpLock:=Lock(PChar(SystemDirName),SHARED_LOCK);
  431. if tmpLock=0 then exit;
  432. FIB:=nil; new(FIB);
  433. if (Examine(tmpLock,FIB) <> 0) and (FIB^.fib_DirEntryType>0) then
  434. result:=True;
  435. if tmpLock<>0 then Unlock(tmpLock);
  436. if assigned(FIB) then dispose(FIB);
  437. end;
  438. {****************************************************************************
  439. Locale Functions
  440. ****************************************************************************}
  441. Procedure GetLocalTime(var SystemTime: TSystemTime);
  442. var
  443. dayOfWeek: word;
  444. begin
  445. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  446. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  447. end;
  448. Procedure InitAnsi;
  449. Var
  450. i : longint;
  451. begin
  452. { Fill table entries 0 to 127 }
  453. for i := 0 to 96 do
  454. UpperCaseTable[i] := chr(i);
  455. for i := 97 to 122 do
  456. UpperCaseTable[i] := chr(i - 32);
  457. for i := 123 to 191 do
  458. UpperCaseTable[i] := chr(i);
  459. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  460. for i := 0 to 64 do
  461. LowerCaseTable[i] := chr(i);
  462. for i := 65 to 90 do
  463. LowerCaseTable[i] := chr(i + 32);
  464. for i := 91 to 191 do
  465. LowerCaseTable[i] := chr(i);
  466. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  467. end;
  468. Procedure InitInternational;
  469. begin
  470. InitInternationalGeneric;
  471. InitAnsi;
  472. end;
  473. function SysErrorMessage(ErrorCode: Integer): String;
  474. begin
  475. { Result:=StrError(ErrorCode);}
  476. end;
  477. function GetLastOSError: Integer;
  478. begin
  479. result:=-1;
  480. end;
  481. {****************************************************************************
  482. OS utility functions
  483. ****************************************************************************}
  484. var
  485. StrOfPaths: String;
  486. function SystemTags(const command: PChar; const tags: array of DWord): LongInt;
  487. begin
  488. SystemTags:=SystemTagList(command,@tags);
  489. end;
  490. function GetPathString: String;
  491. var
  492. f : text;
  493. s : string;
  494. begin
  495. s := '';
  496. result := '';
  497. { Alternatively, this could use PIPE: handler on systems which
  498. have this by default (not the case on classic Amiga), but then
  499. the child process should be started async, which for a simple
  500. Path command probably isn't worth the trouble. (KB) }
  501. assign(f,'T:'+HexStr(FindTask(nil))+'_path.tmp');
  502. rewrite(f);
  503. { This is a pretty ugly stunt, combining Pascal and Amiga system
  504. functions, but works... }
  505. SystemTags('C:Path',[SYS_Input, 0, SYS_Output, TextRec(f).Handle, TAG_END]);
  506. close(f);
  507. reset(f);
  508. { skip the first line, garbage }
  509. if not eof(f) then readln(f,s);
  510. while not eof(f) do begin
  511. readln(f,s);
  512. if result = '' then
  513. result := s
  514. else
  515. result := result + ';' + s;
  516. end;
  517. close(f);
  518. erase(f);
  519. end;
  520. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  521. begin
  522. if UpCase(envvar) = 'PATH' then begin
  523. if StrOfpaths = '' then StrOfPaths := GetPathString;
  524. Result:=StrOfPaths;
  525. end else
  526. Result:=Dos.Getenv(shortstring(EnvVar));
  527. end;
  528. Function GetEnvironmentVariableCount : Integer;
  529. begin
  530. // Result:=FPCCountEnvVar(EnvP);
  531. Result:=Dos.envCount;
  532. end;
  533. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  534. begin
  535. // Result:=FPCGetEnvStrFromP(Envp,Index);
  536. Result:=Dos.EnvStr(Index);
  537. end;
  538. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  539. integer;
  540. var
  541. tmpPath: AnsiString;
  542. convPath: AnsiString;
  543. CommandLine: AnsiString;
  544. tmpLock: longint;
  545. E: EOSError;
  546. begin
  547. DosError:= 0;
  548. convPath:=PathConv(Path);
  549. tmpPath:=convPath+' '+ComLine;
  550. { Here we must first check if the command we wish to execute }
  551. { actually exists, because this is NOT handled by the }
  552. { _SystemTagList call (program will abort!!) }
  553. { Try to open with shared lock }
  554. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  555. if tmpLock<>0 then
  556. begin
  557. { File exists - therefore unlock it }
  558. Unlock(tmpLock);
  559. result:=SystemTagList(PChar(tmpPath),nil);
  560. { on return of -1 the shell could not be executed }
  561. { probably because there was not enough memory }
  562. if result = -1 then
  563. DosError:=8;
  564. end
  565. else
  566. DosError:=3;
  567. if DosError <> 0 then begin
  568. if ComLine = '' then
  569. CommandLine := Path
  570. else
  571. CommandLine := Path + ' ' + ComLine;
  572. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  573. E.ErrorCode := DosError;
  574. raise E;
  575. end;
  576. end;
  577. function ExecuteProcess (const Path: AnsiString;
  578. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  579. var
  580. CommandLine: AnsiString;
  581. I: integer;
  582. begin
  583. Commandline := '';
  584. for I := 0 to High (ComLine) do
  585. if Pos (' ', ComLine [I]) <> 0 then
  586. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  587. else
  588. CommandLine := CommandLine + ' ' + Comline [I];
  589. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  590. end;
  591. procedure Sleep(Milliseconds: cardinal);
  592. begin
  593. // Amiga dos.library Delay() has precision of 1/50 seconds
  594. Delay(Milliseconds div 20);
  595. end;
  596. {****************************************************************************
  597. Initialization code
  598. ****************************************************************************}
  599. Initialization
  600. InitExceptions;
  601. InitInternational; { Initialize internationalization settings }
  602. OnBeep:=Nil; { No SysBeep() on Amiga, for now. Figure out if we want
  603. to use intuition.library/DisplayBeep() for this (KB) }
  604. StrOfPaths:='';
  605. Finalization
  606. DoneExceptions;
  607. end.