sysutils.pp 19 KB

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