sysutils.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. FileSeek:=dosSeek(Handle, FOffset, seekMode);
  210. end;
  211. function FileSeek(Handle: LongInt; FOffset: Int64; Origin: Longint): Int64;
  212. begin
  213. {$WARNING Need to add 64bit call }
  214. FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
  215. end;
  216. procedure FileClose(Handle: LongInt);
  217. begin
  218. if (Handle=0) or (Handle=-1) then exit;
  219. dosClose(Handle);
  220. RemoveFromList(ASYS_fileList,Handle);
  221. end;
  222. function FileTruncate(Handle: THandle; Size: Int64): Boolean;
  223. var
  224. dosResult: LongInt;
  225. begin
  226. FileTruncate:=False;
  227. if Size > high (longint) then exit;
  228. {$WARNING Possible support for 64-bit FS to be checked!}
  229. if (Handle=0) or (Handle=-1) then exit;
  230. dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
  231. if (dosResult<0) then exit;
  232. FileTruncate:=True;
  233. end;
  234. function DeleteFile(const FileName: RawByteString) : Boolean;
  235. var
  236. SystemFileName: RawByteString;
  237. begin
  238. SystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  239. DeleteFile:=dosDeleteFile(PChar(SystemFileName));
  240. end;
  241. function RenameFile(const OldName, NewName: RawByteString): Boolean;
  242. var
  243. OldSystemFileName, NewSystemFileName: RawByteString;
  244. begin
  245. OldSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(OldName));
  246. NewSystemFileName:=PathConv(ToSingleByteFileSystemEncodedFileName(NewName));
  247. RenameFile:=dosRename(PChar(OldSystemFileName), PChar(NewSystemFileName)) <> 0;
  248. end;
  249. (****** end of non portable routines ******)
  250. function FileAge (const FileName : RawByteString): Longint;
  251. var
  252. tmpLock: Longint;
  253. tmpFIB : PFileInfoBlock;
  254. tmpDateTime: TDateTime;
  255. validFile: boolean;
  256. SystemFileName: RawByteString;
  257. begin
  258. validFile:=false;
  259. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  260. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  261. if (tmpLock <> 0) then begin
  262. new(tmpFIB);
  263. if Examine(tmpLock,tmpFIB) <> 0 then begin
  264. tmpDateTime:=AmigaFileDateToDateTime(tmpFIB^.fib_Date,validFile);
  265. end;
  266. Unlock(tmpLock);
  267. dispose(tmpFIB);
  268. end;
  269. if validFile then
  270. result:=DateTimeToFileDate(tmpDateTime)
  271. else
  272. result:=-1;
  273. end;
  274. function FileExists (const FileName : RawByteString) : Boolean;
  275. var
  276. tmpLock: LongInt;
  277. tmpFIB : PFileInfoBlock;
  278. SystemFileName: RawByteString;
  279. begin
  280. result:=false;
  281. SystemFileName := PathConv(ToSingleByteFileSystemEncodedFileName(FileName));
  282. tmpLock := Lock(PChar(SystemFileName), SHARED_LOCK);
  283. if (tmpLock <> 0) then begin
  284. new(tmpFIB);
  285. if (Examine(tmpLock,tmpFIB) <> 0) and (tmpFIB^.fib_DirEntryType <= 0) then
  286. result:=true;
  287. Unlock(tmpLock);
  288. dispose(tmpFIB);
  289. end;
  290. end;
  291. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  292. var
  293. tmpStr: RawByteString;
  294. Anchor: PAnchorPath;
  295. tmpDateTime: TDateTime;
  296. validDate: boolean;
  297. begin
  298. result:=-1; { We emulate Linux/Unix behaviour, and return -1 on errors. }
  299. tmpStr:=PathConv(ToSingleByteFileSystemEncodedFileName(Path));
  300. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  301. Rslt.ExcludeAttr := (not Attr) and ($1e);
  302. Rslt.FindHandle := 0;
  303. new(Anchor);
  304. FillChar(Anchor^,sizeof(TAnchorPath),#0);
  305. if MatchFirst(pchar(tmpStr),Anchor)<>0 then exit;
  306. Rslt.FindHandle := longint(Anchor);
  307. with Anchor^.ap_Info do begin
  308. Name := fib_FileName;
  309. SetCodePage(Name,DefaultFileSystemCodePage,false);
  310. Rslt.Size := fib_Size;
  311. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  312. if not validDate then exit;
  313. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  314. Rslt.Attr := 128;
  315. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  316. if ((fib_Protection and FIBF_READ) <> 0) and
  317. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  318. result:=0; { Return zero if everything went OK }
  319. end;
  320. end;
  321. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  322. var
  323. Anchor: PAnchorPath;
  324. validDate: boolean;
  325. begin
  326. result:=-1;
  327. Anchor:=PAnchorPath(Rslt.FindHandle);
  328. if not assigned(Anchor) then exit;
  329. if MatchNext(Anchor) <> 0 then exit;
  330. with Anchor^.ap_Info do begin
  331. Name := fib_FileName;
  332. SetCodePage(Name,DefaultFileSystemCodePage,false);
  333. Rslt.Size := fib_Size;
  334. Rslt.Time := DateTimeToFileDate(AmigaFileDateToDateTime(fib_Date,validDate));
  335. if not validDate then exit;
  336. { "128" is Windows "NORMALFILE" attribute. Some buggy code depend on this... :( (KB) }
  337. Rslt.Attr := 128;
  338. if fib_DirEntryType > 0 then Rslt.Attr:=Rslt.Attr or faDirectory;
  339. if ((fib_Protection and FIBF_READ) <> 0) and
  340. ((fib_Protection and FIBF_WRITE) = 0) then Rslt.Attr:=Rslt.Attr or faReadOnly;
  341. result:=0; { Return zero if everything went OK }
  342. end;
  343. end;
  344. Procedure InternalFindClose(var Handle: THandle);
  345. var
  346. Anchor: PAnchorPath;
  347. begin
  348. Anchor:=PAnchorPath(Handle);
  349. if not assigned(Anchor) then exit;
  350. MatchEnd(Anchor);
  351. Dispose(Anchor);
  352. Handle:=THandle(nil);
  353. end;
  354. (****** end of non portable routines ******)
  355. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  356. var
  357. F: file;
  358. attr: word;
  359. begin
  360. Assign(F,FileName);
  361. dos.GetFAttr(F,attr);
  362. if DosError <> 0 then
  363. FileGetAttr := -1
  364. else
  365. FileGetAttr := Attr;
  366. end;
  367. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  368. var
  369. F: file;
  370. begin
  371. Assign(F, FileName);
  372. Dos.SetFAttr(F, Attr and $ffff);
  373. FileSetAttr := DosError;
  374. end;
  375. {****************************************************************************
  376. Disk Functions
  377. ****************************************************************************}
  378. {
  379. The Diskfree and Disksize functions need a file on the specified drive, since this
  380. is required for the statfs system call.
  381. These filenames are set in drivestr[0..26], and have been preset to :
  382. 0 - '.' (default drive - hence current dir is ok.)
  383. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  384. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  385. 3 - '/' (C: equivalent of dos is the root partition)
  386. 4..26 (can be set by you're own applications)
  387. ! Use AddDisk() to Add new drives !
  388. They both return -1 when a failure occurs.
  389. }
  390. Const
  391. FixDriveStr : array[0..3] of pchar=(
  392. '.',
  393. '/fd0/.',
  394. '/fd1/.',
  395. '/.'
  396. );
  397. var
  398. Drives : byte;
  399. DriveStr : array[4..26] of pchar;
  400. Procedure AddDisk(const path:string);
  401. begin
  402. if not (DriveStr[Drives]=nil) then
  403. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  404. GetMem(DriveStr[Drives],length(Path)+1);
  405. StrPCopy(DriveStr[Drives],path);
  406. inc(Drives);
  407. if Drives>26 then
  408. Drives:=4;
  409. end;
  410. Function DiskFree(Drive: Byte): int64;
  411. Begin
  412. DiskFree := dos.diskFree(Drive);
  413. End;
  414. Function DiskSize(Drive: Byte): int64;
  415. Begin
  416. DiskSize := dos.DiskSize(Drive);
  417. End;
  418. function DirectoryExists(const Directory: RawByteString): Boolean;
  419. var
  420. tmpLock: LongInt;
  421. FIB : PFileInfoBlock;
  422. SystemDirName: RawByteString;
  423. begin
  424. result:=false;
  425. if (Directory='') or (InOutRes<>0) then exit;
  426. SystemDirName:=PathConv(ToSingleByteFileSystemEncodedFileName(Directory));
  427. tmpLock:=Lock(PChar(SystemDirName),SHARED_LOCK);
  428. if tmpLock=0 then exit;
  429. FIB:=nil; new(FIB);
  430. if (Examine(tmpLock,FIB) <> 0) and (FIB^.fib_DirEntryType>0) then
  431. result:=True;
  432. if tmpLock<>0 then Unlock(tmpLock);
  433. if assigned(FIB) then dispose(FIB);
  434. end;
  435. {****************************************************************************
  436. Locale Functions
  437. ****************************************************************************}
  438. Procedure GetLocalTime(var SystemTime: TSystemTime);
  439. var
  440. dayOfWeek: word;
  441. begin
  442. dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
  443. dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
  444. end;
  445. Procedure InitAnsi;
  446. Var
  447. i : longint;
  448. begin
  449. { Fill table entries 0 to 127 }
  450. for i := 0 to 96 do
  451. UpperCaseTable[i] := chr(i);
  452. for i := 97 to 122 do
  453. UpperCaseTable[i] := chr(i - 32);
  454. for i := 123 to 191 do
  455. UpperCaseTable[i] := chr(i);
  456. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  457. for i := 0 to 64 do
  458. LowerCaseTable[i] := chr(i);
  459. for i := 65 to 90 do
  460. LowerCaseTable[i] := chr(i + 32);
  461. for i := 91 to 191 do
  462. LowerCaseTable[i] := chr(i);
  463. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  464. end;
  465. Procedure InitInternational;
  466. begin
  467. InitInternationalGeneric;
  468. InitAnsi;
  469. end;
  470. function SysErrorMessage(ErrorCode: Integer): String;
  471. begin
  472. { Result:=StrError(ErrorCode);}
  473. end;
  474. function GetLastOSError: Integer;
  475. begin
  476. result:=-1;
  477. end;
  478. {****************************************************************************
  479. OS utility functions
  480. ****************************************************************************}
  481. var
  482. StrOfPaths: String;
  483. function GetPathString: String;
  484. var
  485. f : text;
  486. s : string;
  487. tmpBat: string;
  488. tmpList: string;
  489. begin
  490. s := '';
  491. result := '';
  492. tmpBat:='T:'+HexStr(FindTask(nil));
  493. tmpList:=tmpBat+'_path.tmp';
  494. tmpBat:=tmpBat+'_path.sh';
  495. assign(f,tmpBat);
  496. rewrite(f);
  497. writeln(f,'path >'+tmpList);
  498. close(f);
  499. exec('C:Execute',tmpBat);
  500. erase(f);
  501. assign(f,tmpList);
  502. reset(f);
  503. { skip the first line, garbage }
  504. if not eof(f) then readln(f,s);
  505. while not eof(f) do begin
  506. readln(f,s);
  507. if result = '' then
  508. result := s
  509. else
  510. result := result + ';' + s;
  511. end;
  512. close(f);
  513. erase(f);
  514. end;
  515. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  516. begin
  517. if UpCase(envvar) = 'PATH' then begin
  518. if StrOfpaths = '' then StrOfPaths := GetPathString;
  519. Result:=StrOfPaths;
  520. end else
  521. Result:=Dos.Getenv(shortstring(EnvVar));
  522. end;
  523. Function GetEnvironmentVariableCount : Integer;
  524. begin
  525. // Result:=FPCCountEnvVar(EnvP);
  526. Result:=Dos.envCount;
  527. end;
  528. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  529. begin
  530. // Result:=FPCGetEnvStrFromP(Envp,Index);
  531. Result:=Dos.EnvStr(Index);
  532. end;
  533. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  534. integer;
  535. var
  536. tmpPath: AnsiString;
  537. convPath: AnsiString;
  538. CommandLine: AnsiString;
  539. tmpLock: longint;
  540. E: EOSError;
  541. begin
  542. DosError:= 0;
  543. convPath:=PathConv(Path);
  544. tmpPath:=convPath+' '+ComLine;
  545. { Here we must first check if the command we wish to execute }
  546. { actually exists, because this is NOT handled by the }
  547. { _SystemTagList call (program will abort!!) }
  548. { Try to open with shared lock }
  549. tmpLock:=Lock(PChar(convPath),SHARED_LOCK);
  550. if tmpLock<>0 then
  551. begin
  552. { File exists - therefore unlock it }
  553. Unlock(tmpLock);
  554. result:=SystemTagList(PChar(tmpPath),nil);
  555. { on return of -1 the shell could not be executed }
  556. { probably because there was not enough memory }
  557. if result = -1 then
  558. DosError:=8;
  559. end
  560. else
  561. DosError:=3;
  562. if DosError <> 0 then begin
  563. if ComLine = '' then
  564. CommandLine := Path
  565. else
  566. CommandLine := Path + ' ' + ComLine;
  567. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  568. E.ErrorCode := DosError;
  569. raise E;
  570. end;
  571. end;
  572. function ExecuteProcess (const Path: AnsiString;
  573. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  574. var
  575. CommandLine: AnsiString;
  576. I: integer;
  577. begin
  578. Commandline := '';
  579. for I := 0 to High (ComLine) do
  580. if Pos (' ', ComLine [I]) <> 0 then
  581. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  582. else
  583. CommandLine := CommandLine + ' ' + Comline [I];
  584. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  585. end;
  586. procedure Sleep(Milliseconds: cardinal);
  587. begin
  588. // Amiga dos.library Delay() has precision of 1/50 seconds
  589. Delay(Milliseconds div 20);
  590. end;
  591. {****************************************************************************
  592. Initialization code
  593. ****************************************************************************}
  594. Initialization
  595. InitExceptions;
  596. InitInternational; { Initialize internationalization settings }
  597. OnBeep:=Nil; { No SysBeep() on Amiga, for now. Figure out if we want
  598. to use intuition.library/DisplayBeep() for this (KB) }
  599. StrOfPaths:='';
  600. Finalization
  601. DoneExceptions;
  602. end.