system.pp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2004 by Karoly Balogh for Genesi S.a.r.l.
  5. System unit for MorphOS/PowerPC
  6. Uses parts of the Commodore Amiga/68k port by Carl Eric Codere
  7. and Nils Sjoholm
  8. MorphOS port was done on a free Pegasos II/G4 machine
  9. provided by Genesi S.a.r.l. <www.genesi.lu>
  10. See the file COPYING.FPC, included in this distribution,
  11. for details about the copyright.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. **********************************************************************}
  16. unit {$ifdef VER1_0}SysMorph{$else}System{$endif};
  17. interface
  18. {$define FPC_IS_SYSTEM}
  19. {$I systemh.inc}
  20. type
  21. THandle = LongInt;
  22. {$I heaph.inc}
  23. const
  24. LineEnding = #10;
  25. LFNSupport = True;
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. PathSeparator = ';';
  29. const
  30. UnusedHandle : LongInt = -1;
  31. StdInputHandle : LongInt = 0;
  32. StdOutputHandle : LongInt = 0;
  33. StdErrorHandle : LongInt = 0;
  34. FileNameCaseSensitive : Boolean = False;
  35. sLineBreak : string[1] = LineEnding;
  36. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  37. BreakOn : Boolean = True;
  38. {*****************************************************************************
  39. MorphOS structures
  40. *****************************************************************************}
  41. type
  42. PClockData = ^TClockData;
  43. TClockData = packed Record
  44. sec : Word;
  45. min : Word;
  46. hour : Word;
  47. mday : Word;
  48. month: Word;
  49. year : Word;
  50. wday : Word;
  51. end;
  52. TDateStamp = packed record
  53. ds_Days : LongInt; { Number of days since Jan. 1, 1978 }
  54. ds_Minute : LongInt; { Number of minutes past midnight }
  55. ds_Tick : LongInt; { Number of ticks past minute }
  56. end;
  57. PDateStamp = ^TDateStamp;
  58. PFileInfoBlock = ^TFileInfoBlock;
  59. TFileInfoBlock = packed record
  60. fib_DiskKey : LongInt;
  61. fib_DirEntryType : LongInt;
  62. { Type of Directory. If < 0, then a plain file. If > 0 a directory }
  63. fib_FileName : Array [0..107] of Char;
  64. { Null terminated. Max 30 chars used for now }
  65. fib_Protection : LongInt;
  66. { bit mask of protection, rwxd are 3-0. }
  67. fib_EntryType : LongInt;
  68. fib_Size : LongInt; { Number of bytes in file }
  69. fib_NumBlocks : LongInt; { Number of blocks in file }
  70. fib_Date : TDateStamp; { Date file last changed }
  71. fib_Comment : Array [0..79] of Char;
  72. { Null terminated comment associated with file }
  73. fib_Reserved : Array [0..35] of Char;
  74. end;
  75. PNode = ^TNode;
  76. TNode = packed record
  77. ln_Succ, { Pointer to next (successor) }
  78. ln_Pred: pNode; { Pointer to previous (predecessor) }
  79. ln_Type: Byte;
  80. ln_Pri : Shortint; { Priority, for sorting }
  81. ln_Name: PChar; { ID string, null terminated }
  82. end; { Note: Integer aligned }
  83. PMinNode = ^TMinNode;
  84. tMinNode = packed record
  85. mln_Succ,
  86. mln_Pred: pMinNode;
  87. end;
  88. PList = ^TList;
  89. tList = packed record
  90. lh_Head : pNode;
  91. lh_Tail : pNode;
  92. lh_TailPred: pNode;
  93. lh_Type : Byte;
  94. l_pad : Byte;
  95. end;
  96. PMinList = ^TMinList;
  97. TMinList = packed record
  98. mlh_Head : PMinNode;
  99. mlh_Tail : PMinNode;
  100. mlh_TailPred: PMinNode;
  101. end;
  102. PMsgPort = ^TMsgPort;
  103. TMsgPort = packed record
  104. mp_Node : TNode;
  105. mp_Flags : Byte;
  106. mp_SigBit : Byte; { signal bit number }
  107. mp_SigTask: Pointer; { task to be signalled (TaskPtr) }
  108. mp_MsgList: TList; { message linked list }
  109. end;
  110. PMessage = ^TMessage;
  111. TMessage = packed record
  112. mn_Node : TNode;
  113. mn_ReplyPort: PMsgPort;
  114. mn_Length : Word;
  115. end;
  116. PTask = ^TTask;
  117. TTask = packed record
  118. tc_Node : TNode;
  119. tc_Flags : Byte;
  120. tc_State : Byte;
  121. tc_IDNestCnt : Shortint; { intr disabled nesting }
  122. tc_TDNestCnt : Shortint; { task disabled nesting }
  123. tc_SigAlloc : DWord; { sigs allocated }
  124. tc_SigWait : DWord; { sigs we are waiting for }
  125. tc_SigRecvd : DWord; { sigs we have received }
  126. tc_SigExcept : DWord; { sigs we will take excepts for }
  127. tc_TrapAlloc : Word; { traps allocated }
  128. tc_TrapAble : Word; { traps enabled }
  129. tc_ExceptData: Pointer; { points to except data }
  130. tc_ExceptCode: Pointer; { points to except code }
  131. tc_TrapData : Pointer; { points to trap data }
  132. tc_TrapCode : Pointer; { points to trap code }
  133. tc_SPReg : Pointer; { stack pointer }
  134. tc_SPLower : Pointer; { stack lower bound }
  135. tc_SPUpper : Pointer; { stack upper bound + 2 }
  136. tc_Switch : Pointer; { task losing CPU }
  137. tc_Launch : Pointer; { task getting CPU }
  138. tc_MemEntry : TList; { allocated memory }
  139. tc_UserData : Pointer; { per task data }
  140. end;
  141. PProcess = ^TProcess;
  142. TProcess = packed record
  143. pr_Task : TTask;
  144. pr_MsgPort : TMsgPort; { This is BPTR address from DOS functions }
  145. pr_Pad : Word; { Remaining variables on 4 byte boundaries }
  146. pr_SegList : Pointer; { Array of seg lists used by this process }
  147. pr_StackSize : Longint; { Size of process stack in bytes }
  148. pr_GlobVec : Pointer; { Global vector for this process (BCPL) }
  149. pr_TaskNum : Longint; { CLI task number of zero if not a CLI }
  150. pr_StackBase : DWord; { Ptr to high memory end of process stack }
  151. pr_Result2 : Longint; { Value of secondary result from last call }
  152. pr_CurrentDir : DWord; { Lock associated with current directory }
  153. pr_CIS : DWord; { Current CLI Input Stream }
  154. pr_COS : DWord; { Current CLI Output Stream }
  155. pr_ConsoleTask : Pointer; { Console handler process for current window }
  156. pr_FileSystemTask: Pointer; { File handler process for current drive }
  157. pr_CLI : DWord; { pointer to ConsoleLineInterpreter }
  158. pr_ReturnAddr : Pointer; { pointer to previous stack frame }
  159. pr_PktWait : Pointer; { Function to be called when awaiting msg }
  160. pr_WindowPtr : Pointer; { Window for error printing }
  161. { following definitions are new with 2.0 }
  162. pr_HomeDir : DWord; { Home directory of executing program }
  163. pr_Flags : Longint; { flags telling dos about process }
  164. pr_ExitCode : Pointer; { code to call on exit of program OR NULL }
  165. pr_ExitData : Longint; { Passed as an argument to pr_ExitCode. }
  166. pr_Arguments : PChar; { Arguments passed to the process at start }
  167. pr_LocalVars : TMinList; { Local environment variables }
  168. pr_ShellPrivate : Longint; { for the use of the current shell }
  169. pr_CES : DWord; { Error stream - IF NULL, use pr_COS }
  170. end;
  171. PLibrary = ^TLibrary;
  172. TLibrary = packed record
  173. lib_Node : tNode;
  174. lib_Flags,
  175. lib_pad : Byte;
  176. lib_NegSize, { number of bytes before library }
  177. lib_PosSize, { number of bytes after library }
  178. lib_Version, { major }
  179. lib_Revision: Word; { minor }
  180. lib_IdString: PChar; { ASCII identification }
  181. lib_Sum : LongInt; { the checksum itself }
  182. lib_OpenCnt : Word; { number of current opens }
  183. end; { * Warning: size is not a longword multiple ! * }
  184. PDevice = ^TDevice;
  185. tDevice = packed record
  186. dd_Library: TLibrary;
  187. end;
  188. PUnit = ^tUnit;
  189. TUnit = packed record
  190. unit_MsgPort: TMsgPort; { queue for unprocessed messages }
  191. { instance of msgport is recommended }
  192. unit_flags,
  193. unit_pad : Byte;
  194. unit_OpenCnt: Word; { number of active opens }
  195. end;
  196. PIORequest = ^TIORequest;
  197. TIORequest = packed record
  198. io_Message: TMessage;
  199. io_Device : PDevice; { device node pointer }
  200. io_Unit : PUnit; { unit (driver private)}
  201. io_Command: Word; { device command }
  202. io_Flags : Byte;
  203. io_Error : Shortint; { error or warning num }
  204. end;
  205. PInfoData = ^TInfoData;
  206. TInfoData = packed record
  207. id_NumSoftErrors: LongInt; { number of soft errors on disk }
  208. id_UnitNumber : LongInt; { Which unit disk is (was) mounted on }
  209. id_DiskState : LongInt; { See defines below }
  210. id_NumBlocks : LongInt; { Number of blocks on disk }
  211. id_NumBlocksUsed: LongInt; { Number of block in use }
  212. id_BytesPerBlock: LongInt;
  213. id_DiskType : LongInt; { Disk Type code }
  214. id_VolumeNode : LongInt; { BCPL pointer to volume node }
  215. id_InUse : LongInt; { Flag, zero if not in use }
  216. end;
  217. PChain = ^TChain;
  218. TChain = packed record
  219. an_Child : PChain;
  220. an_Parent: PChain;
  221. an_Lock : LongInt;
  222. an_info : TFileInfoBlock;
  223. an_Flags : ShortInt;
  224. an_string: Array[0..0] of char;
  225. end;
  226. PAnchorPath = ^TAnchorPath;
  227. TAnchorPath = packed record
  228. ap_Base : PChain; { pointer to first anchor }
  229. ap_First : PChain; { pointer to last anchor }
  230. ap_BreakBits: LongInt; { Bits we want to break on }
  231. ap_FondBreak: LongInt; { Bits we broke on. Also returns ERROR_BREAK }
  232. ap_Flags : ShortInt; { New use for extra word. }
  233. ap_reserved : Byte;
  234. ap_StrLen : Word;
  235. ap_Info : TFileInfoBlock;
  236. ap_Buf : array[0..0] of Char; { Buffer for path name, allocated by user }
  237. end;
  238. PDOSList = ^TDOSList;
  239. TDOSList = packed record
  240. dol_Next: LongInt; { bptr to next device on list }
  241. dol_Type: LongInt; { see DLT below }
  242. dol_Task: Pointer; { ptr to handler task }
  243. dol_Lock: LongInt;
  244. dol_Misc: array[0..23] of ShortInt;
  245. dol_Name: LongInt; { bptr to bcpl name }
  246. end;
  247. var
  248. MOS_ExecBase : Pointer; external name '_ExecBase';
  249. MOS_DOSBase : Pointer;
  250. MOS_UtilityBase: Pointer;
  251. MOS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  252. MOS_origDir : LongInt; { original directory on startup }
  253. MOS_ambMsg : PMessage;
  254. MOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  255. MOS_ConHandle: LongInt;
  256. argc: LongInt;
  257. argv: PPChar;
  258. envp: PPChar;
  259. {*****************************************************************************
  260. MorphOS functions
  261. *****************************************************************************}
  262. { exec.library functions }
  263. function exec_OpenLibrary(libname: PChar location 'a1';
  264. libver: LongInt location 'd0'): Pointer; SysCall MOS_ExecBase 552;
  265. procedure exec_CloseLibrary(libhandle: Pointer location 'a1'); SysCall MOS_ExecBase 414;
  266. function exec_CreatePool(memflags: LongInt location 'd0';
  267. puddleSize: LongInt location 'd1';
  268. threshSize: LongInt location 'd2'): Pointer; SysCall MOS_ExecBase 696;
  269. procedure exec_DeletePool(poolHeader: Pointer location 'a0'); SysCall MOS_ExecBase 702;
  270. function exec_AllocPooled(poolHeader: Pointer location 'a0';
  271. memSize: LongInt location 'd0'): Pointer; SysCall MOS_ExecBase 708;
  272. function exec_SetSignal(newSignals: LongInt location 'd0';
  273. signalMask: LongInt location 'd1'): LongInt; SysCall MOS_ExecBase 306;
  274. function exec_FindTask(tname: PChar location 'a1'): PTask; SysCall MOS_ExecBase 294;
  275. function exec_GetMsg(port: PMsgPort location 'a0'): PMessage; SysCall MOS_ExecBase 372;
  276. function exec_WaitPort(port: PMsgPort location 'a0'): PMessage; SysCall MOS_ExecBase 384;
  277. function exec_AllocMem(byteSize: LongInt location 'd0';
  278. requirements: LongInt location 'd1'): Pointer; SysCall MOS_ExecBase 198;
  279. procedure exec_FreeMem(memoryBlock: Pointer location 'a1';
  280. byteSize: LongInt location 'd0'); SysCall MOS_ExecBase 210;
  281. function exec_AllocSignal(signalNum: LongInt location 'd0'): ShortInt; SysCall MOS_ExecBase 330;
  282. procedure exec_FreeSignal(signalNum: LongInt location 'd0'); SysCall MOS_ExecBase 336;
  283. procedure exec_AddPort(port: PMsgPort location 'a1'); SysCall MOS_ExecBase 354;
  284. procedure exec_RemPort(port: PMsgPort location 'a1'); SysCall MOS_ExecBase 360;
  285. function exec_DoIO(ioRequest: PIORequest location 'a1'): ShortInt; SysCall MOS_ExecBase 456;
  286. function exec_OpenDevice(const devName: PChar location 'a0';
  287. unite: LongInt location 'd0';
  288. ioRequest: PIORequest location 'a1';
  289. flags: LongInt location 'd1'): ShortInt; SysCall MOS_ExecBase 444;
  290. procedure exec_CloseDevice(ioRequest: PIORequest location 'a1'); SysCall MOS_ExecBase 450;
  291. { dos.library functions }
  292. function dos_Output: LongInt; SysCall MOS_DOSBase 60;
  293. function dos_Input: LongInt; SysCall MOS_DOSBase 54;
  294. function dos_IoErr: LongInt; SysCall MOS_DOSBase 132;
  295. function dos_GetArgStr: PChar; SysCall MOS_DOSBase 534;
  296. function dos_Open(fname: PChar location 'd1';
  297. accessMode: LongInt location 'd2'): LongInt; SysCall MOS_DOSBase 30;
  298. function dos_Close(fileh: LongInt location 'd1'): Boolean; SysCall MOS_DOSBase 36;
  299. function dos_Seek(fileh: LongInt location 'd1';
  300. position: LongInt location 'd2';
  301. posmode: LongInt location 'd3'): LongInt; SysCall MOS_DOSBase 66;
  302. function dos_SetFileSize(fileh: LongInt location 'd1';
  303. position: LongInt location 'd2';
  304. posmode: LongInt location 'd3'): LongInt; SysCall MOS_DOSBase 456;
  305. function dos_Read(fileh: LongInt location 'd1';
  306. buffer: Pointer location 'd2';
  307. length: LongInt location 'd3'): LongInt; SysCall MOS_DOSBase 42;
  308. function dos_Write(fileh: LongInt location 'd1';
  309. buffer: Pointer location 'd2';
  310. length: LongInt location 'd3'): LongInt; SysCall MOS_DOSBase 48;
  311. function dos_WriteChars(buf: PChar location 'd1';
  312. buflen: LongInt location 'd2'): LongInt; SysCall MOS_DOSBase 942;
  313. function dos_Rename(oldName: PChar location 'd1';
  314. newName: PChar location 'd2'): Boolean; SysCall MOS_DOSBase 78;
  315. function dos_DeleteFile(fname: PChar location 'd1'): Boolean; SysCall MOS_DOSBase 72;
  316. function dos_GetCurrentDirName(buf: PChar location 'd1';
  317. len: LongInt location 'd2'): Boolean; SysCall MOS_DOSBase 564;
  318. function dos_Lock(lname: PChar location 'd1';
  319. accessMode: LongInt location 'd2'): LongInt; SysCall MOS_DOSBase 84;
  320. procedure dos_Unlock(lock: LongInt location 'd1'); SysCall MOS_DOSBase 90;
  321. function dos_CurrentDir(lock: LongInt location 'd1'): LongInt; SysCall MOS_DOSBase 126;
  322. function dos_Examine(lock: LongInt location 'd1';
  323. FileInfoBlock: Pointer location 'd2'): Boolean; SysCall MOS_DOSBase 102;
  324. function dos_NameFromLock(lock: LongInt location 'd1';
  325. buffer: PChar location 'd2';
  326. len: LongInt location 'd3'): Boolean; SysCall MOS_DOSBase 402;
  327. function dos_Info(lock: LongInt location 'd1';
  328. parameterBlock: PInfoData location 'd2'): Boolean; SysCall MOS_DOSBase 114;
  329. function dos_CreateDir(dname: PChar location 'd1'): LongInt; SysCall MOS_DOSBase 120;
  330. function dos_DateStamp(var ds: TDateStamp location 'd1'): LongInt; SysCall MOS_DOSBase 192;
  331. function dos_SystemTagList(command: PChar location 'd1';
  332. tags: Pointer location 'd2'): LongInt; SysCall MOS_DOSBase 606;
  333. function dos_GetVar(vname: PChar location 'd1';
  334. buffer: PChar location 'd2';
  335. size: LongInt location 'd3';
  336. flags: LongInt location 'd4'): LongInt; SysCall MOS_DOSBase 906;
  337. function dos_MatchFirst(pat: PChar location 'd1';
  338. anchor: PAnchorPath location 'd2'): LongInt; SysCall MOS_DOSBase 822;
  339. function dos_MatchNext(anchor: PAnchorPath location 'd1'): LongInt; SysCall MOS_DOSBase 828;
  340. procedure dos_MatchEnd(anchor: PAnchorPath location 'd1') SysCall MOS_DOSBase 834;
  341. function dos_LockDosList(flags: LongInt location 'd1'): PDOSList; SysCall MOS_DOSBase 654;
  342. procedure dos_UnLockDosList(flags: LongInt location 'd2'); SysCall MOS_DOSBase 660;
  343. function dos_NextDosEntry(dlist: PDOSList location 'd1';
  344. flags: LongInt location 'd2'): PDOSList; SysCall MOS_DOSBase 690;
  345. function dos_SetProtection(name: PChar location 'd1';
  346. mask: LongInt location 'd2'): Boolean; SysCall MOS_DOSBase 186;
  347. function dos_SetFileDate(name: PChar location 'd1';
  348. date: PDateStamp location 'd2'): Boolean; SysCall MOS_DOSBase 396;
  349. function dos_GetProgramDir: LongInt; SysCall MOS_DOSBase 600;
  350. function dos_GetProgramName(buf: PChar location 'd1';
  351. len: LongInt location 'd2'): Boolean; SysCall MOS_DOSBase 576;
  352. { utility.library functions }
  353. function util_Date2Amiga(date: PClockData location 'a0'): LongInt; SysCall MOS_UtilityBase 126;
  354. procedure util_Amiga2Date(amigatime: LongInt location 'd0';
  355. resultat: PClockData location 'a0'); SysCall MOS_UtilityBase 120;
  356. implementation
  357. {$I system.inc}
  358. {*****************************************************************************
  359. System Dependent Structures/Consts
  360. *****************************************************************************}
  361. { Errors from dos_IoErr(), etc. }
  362. const
  363. ERROR_NO_FREE_STORE = 103;
  364. ERROR_TASK_TABLE_FULL = 105;
  365. ERROR_BAD_TEMPLATE = 114;
  366. ERROR_BAD_NUMBER = 115;
  367. ERROR_REQUIRED_ARG_MISSING = 116;
  368. ERROR_KEY_NEEDS_ARG = 117;
  369. ERROR_TOO_MANY_ARGS = 118;
  370. ERROR_UNMATCHED_QUOTES = 119;
  371. ERROR_LINE_TOO_LONG = 120;
  372. ERROR_FILE_NOT_OBJECT = 121;
  373. ERROR_INVALID_RESIDENT_LIBRARY = 122;
  374. ERROR_NO_DEFAULT_DIR = 201;
  375. ERROR_OBJECT_IN_USE = 202;
  376. ERROR_OBJECT_EXISTS = 203;
  377. ERROR_DIR_NOT_FOUND = 204;
  378. ERROR_OBJECT_NOT_FOUND = 205;
  379. ERROR_BAD_STREAM_NAME = 206;
  380. ERROR_OBJECT_TOO_LARGE = 207;
  381. ERROR_ACTION_NOT_KNOWN = 209;
  382. ERROR_INVALID_COMPONENT_NAME = 210;
  383. ERROR_INVALID_LOCK = 211;
  384. ERROR_OBJECT_WRONG_TYPE = 212;
  385. ERROR_DISK_NOT_VALIDATED = 213;
  386. ERROR_DISK_WRITE_PROTECTED = 214;
  387. ERROR_RENAME_ACROSS_DEVICES = 215;
  388. ERROR_DIRECTORY_NOT_EMPTY = 216;
  389. ERROR_TOO_MANY_LEVELS = 217;
  390. ERROR_DEVICE_NOT_MOUNTED = 218;
  391. ERROR_SEEK_ERROR = 219;
  392. ERROR_COMMENT_TOO_BIG = 220;
  393. ERROR_DISK_FULL = 221;
  394. ERROR_DELETE_PROTECTED = 222;
  395. ERROR_WRITE_PROTECTED = 223;
  396. ERROR_READ_PROTECTED = 224;
  397. ERROR_NOT_A_DOS_DISK = 225;
  398. ERROR_NO_DISK = 226;
  399. ERROR_NO_MORE_ENTRIES = 232;
  400. { added for AOS 1.4 }
  401. ERROR_IS_SOFT_LINK = 233;
  402. ERROR_OBJECT_LINKED = 234;
  403. ERROR_BAD_HUNK = 235;
  404. ERROR_NOT_IMPLEMENTED = 236;
  405. ERROR_RECORD_NOT_LOCKED = 240;
  406. ERROR_LOCK_COLLISION = 241;
  407. ERROR_LOCK_TIMEOUT = 242;
  408. ERROR_UNLOCK_ERROR = 243;
  409. { DOS file offset modes }
  410. const
  411. OFFSET_BEGINNING = -1;
  412. OFFSET_CURRENT = 0;
  413. OFFSET_END = 1;
  414. { Lock AccessMode }
  415. const
  416. SHARED_LOCK = -2;
  417. ACCESS_READ = SHARED_LOCK;
  418. EXCLUSIVE_LOCK = -1;
  419. ACCESS_WRITE = EXCLUSIVE_LOCK;
  420. { Memory flags }
  421. const
  422. MEMF_ANY = 0;
  423. MEMF_PUBLIC = 1 Shl 0;
  424. MEMF_CHIP = 1 Shl 1;
  425. MEMF_FAST = 1 Shl 2;
  426. MEMF_LOCAL = 1 Shl 8;
  427. MEMF_24BITDMA = 1 Shl 9;
  428. MEMF_KICK = 1 Shl 10;
  429. MEMF_CLEAR = 1 Shl 16;
  430. MEMF_LARGEST = 1 Shl 17;
  431. MEMF_REVERSE = 1 Shl 18;
  432. MEMF_TOTAL = 1 Shl 19;
  433. MEMF_NO_EXPUNGE = 1 Shl 31;
  434. const
  435. CTRL_C = 20; { Error code on CTRL-C press }
  436. SIGBREAKF_CTRL_C = $1000; { CTRL-C signal flags }
  437. {*****************************************************************************
  438. MorphOS File-handling Support Functions
  439. *****************************************************************************}
  440. type
  441. { AmigaOS does not automatically close opened files on exit back to }
  442. { the operating system, therefore as a precuation we close all files }
  443. { manually on exit. }
  444. PFileList = ^TFileList;
  445. TFileList = record { no packed, must be correctly aligned }
  446. handle : LongInt; { Handle to file }
  447. next : PFileList; { Next file in list }
  448. end;
  449. var
  450. MOS_fileList: PFileList; { List pointer to opened files }
  451. { Function to be called at program shutdown, to close all opened files }
  452. procedure CloseList(l: PFileList);
  453. var
  454. tmpNext : PFileList;
  455. tmpHandle : LongInt;
  456. begin
  457. if l=nil then exit;
  458. { First, close all tracked files }
  459. tmpNext:=l^.next;
  460. while tmpNext<>nil do begin
  461. tmpHandle:=tmpNext^.handle;
  462. if (tmpHandle<>StdInputHandle) and (tmpHandle<>StdOutputHandle)
  463. and (tmpHandle<>StdErrorHandle) then begin
  464. dos_Close(tmpHandle);
  465. end;
  466. tmpNext:=tmpNext^.next;
  467. end;
  468. { Next, erase the linked list }
  469. while l<>nil do begin
  470. tmpNext:=l;
  471. l:=l^.next;
  472. dispose(tmpNext);
  473. end;
  474. end;
  475. { Function to be called to add a file to the opened file list }
  476. procedure AddToList(var l: PFileList; h: LongInt);
  477. var
  478. p : PFileList;
  479. inList: Boolean;
  480. begin
  481. inList:=False;
  482. if l<>nil then begin
  483. { if there is a valid filelist, search for the value }
  484. { in the list to avoid double additions }
  485. p:=l;
  486. while (p^.next<>nil) and (not inList) do
  487. if p^.next^.handle=h then inList:=True
  488. else p:=p^.next;
  489. p:=nil;
  490. end else begin
  491. { if the list is not yet allocated, allocate it. }
  492. New(l);
  493. l^.next:=nil;
  494. end;
  495. if not inList then begin
  496. New(p);
  497. p^.handle:=h;
  498. p^.next:=l^.next;
  499. l^.next:=p;
  500. end;
  501. end;
  502. { Function to be called to remove a file from the list }
  503. procedure RemoveFromList(var l: PFileList; h: longint);
  504. var
  505. p : PFileList;
  506. inList: Boolean;
  507. begin
  508. if l=nil then exit;
  509. inList:=False;
  510. p:=l;
  511. while (p^.next<>nil) and (not inList) do
  512. if p^.next^.handle=h then inList:=True
  513. else p:=p^.next;
  514. if p^.next<>nil then begin
  515. dispose(p^.next);
  516. p^.next:=p^.next^.next;
  517. end;
  518. end;
  519. {*****************************************************************************
  520. Misc. System Dependent Functions
  521. *****************************************************************************}
  522. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  523. procedure System_exit;
  524. begin
  525. { We must remove the CTRL-C FALG here because halt }
  526. { may call I/O routines, which in turn might call }
  527. { halt, so a recursive stack crash }
  528. if BreakOn then begin
  529. if (exec_SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  530. exec_SetSignal(0,SIGBREAKF_CTRL_C);
  531. end;
  532. { Closing opened files }
  533. CloseList(MOS_fileList);
  534. if MOS_UtilityBase<>nil then exec_CloseLibrary(MOS_UtilityBase);
  535. if MOS_DOSBase<>nil then exec_CloseLibrary(MOS_DOSBase);
  536. if MOS_heapPool<>nil then exec_DeletePool(MOS_heapPool);
  537. haltproc(ExitCode);
  538. end;
  539. { Converts a MorphOS dos.library error code to a TP compatible error code }
  540. { Based on 1.0.x Amiga RTL }
  541. procedure dosError2InOut(errno: LongInt);
  542. begin
  543. case errno of
  544. ERROR_BAD_NUMBER,
  545. ERROR_ACTION_NOT_KNOWN,
  546. ERROR_NOT_IMPLEMENTED : InOutRes := 1;
  547. ERROR_OBJECT_NOT_FOUND : InOutRes := 2;
  548. ERROR_DIR_NOT_FOUND : InOutRes := 3;
  549. ERROR_DISK_WRITE_PROTECTED : InOutRes := 150;
  550. ERROR_OBJECT_WRONG_TYPE : InOutRes := 151;
  551. ERROR_OBJECT_EXISTS,
  552. ERROR_DELETE_PROTECTED,
  553. ERROR_WRITE_PROTECTED,
  554. ERROR_READ_PROTECTED,
  555. ERROR_OBJECT_IN_USE,
  556. ERROR_DIRECTORY_NOT_EMPTY : InOutRes := 5;
  557. ERROR_NO_MORE_ENTRIES : InOutRes := 18;
  558. ERROR_RENAME_ACROSS_DEVICES : InOutRes := 17;
  559. ERROR_DISK_FULL : InOutRes := 101;
  560. ERROR_INVALID_RESIDENT_LIBRARY : InoutRes := 153;
  561. ERROR_BAD_HUNK : InOutRes := 153;
  562. ERROR_NOT_A_DOS_DISK : InOutRes := 157;
  563. ERROR_NO_DISK,
  564. ERROR_DISK_NOT_VALIDATED,
  565. ERROR_DEVICE_NOT_MOUNTED : InOutRes := 152;
  566. ERROR_SEEK_ERROR : InOutRes := 156;
  567. ERROR_LOCK_COLLISION,
  568. ERROR_LOCK_TIMEOUT,
  569. ERROR_UNLOCK_ERROR,
  570. ERROR_INVALID_LOCK,
  571. ERROR_INVALID_COMPONENT_NAME,
  572. ERROR_BAD_STREAM_NAME,
  573. ERROR_FILE_NOT_OBJECT : InOutRes := 6;
  574. else
  575. InOutres := errno;
  576. end;
  577. end;
  578. { Used for CTRL_C checking in I/O calls }
  579. procedure checkCTRLC;
  580. begin
  581. if BreakOn then begin
  582. if (exec_SetSignal(0,0) And SIGBREAKF_CTRL_C)<>0 then begin
  583. { Clear CTRL-C signal }
  584. exec_SetSignal(0,SIGBREAKF_CTRL_C);
  585. Halt(CTRL_C);
  586. end;
  587. end;
  588. end;
  589. { Generates correct argument array on startup }
  590. procedure GenerateArgs;
  591. var
  592. argvlen : longint;
  593. procedure allocarg(idx,len:longint);
  594. var
  595. i,oldargvlen : longint;
  596. begin
  597. if idx>=argvlen then
  598. begin
  599. oldargvlen:=argvlen;
  600. argvlen:=(idx+8) and (not 7);
  601. sysreallocmem(argv,argvlen*sizeof(pointer));
  602. for i:=oldargvlen to argvlen-1 do
  603. argv[i]:=nil;
  604. end;
  605. { use realloc to reuse already existing memory }
  606. sysreallocmem(argv[idx],len+1);
  607. end;
  608. var
  609. count: word;
  610. start: word;
  611. localindex: word;
  612. p : pchar;
  613. temp : string;
  614. begin
  615. p:=dos_GetArgStr;
  616. argvlen:=0;
  617. { Set argv[0] }
  618. temp:=paramstr(0);
  619. allocarg(0,length(temp));
  620. move(temp[1],argv[0]^,length(temp));
  621. argv[0][length(temp)]:=#0;
  622. { check if we're started from Ambient }
  623. if MOS_ambMsg<>nil then
  624. begin
  625. argc:=0;
  626. exit;
  627. end;
  628. { Handle the other args }
  629. count:=0;
  630. { first index is one }
  631. localindex:=1;
  632. while (p[count]<>#0) do
  633. begin
  634. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  635. start:=count;
  636. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  637. if (count-start>0) then
  638. begin
  639. allocarg(localindex,count-start);
  640. move(p[start],argv[localindex]^,count-start);
  641. argv[localindex][count-start]:=#0;
  642. inc(localindex);
  643. end;
  644. end;
  645. argc:=localindex;
  646. end;
  647. function GetProgramDir: String;
  648. var
  649. s1 : String;
  650. alock : LongInt;
  651. counter: Byte;
  652. begin
  653. GetProgramDir:='';
  654. FillChar(s1,255,#0);
  655. { GetLock of program directory }
  656. alock:=dos_GetProgramDir;
  657. if alock<>0 then begin
  658. if dos_NameFromLock(alock,@s1[1],255) then begin
  659. counter:=1;
  660. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  661. s1[0]:=Char(counter-1);
  662. GetProgramDir:=s1;
  663. end;
  664. end;
  665. end;
  666. function GetProgramName: String;
  667. { Returns ONLY the program name }
  668. var
  669. s1 : String;
  670. counter: Byte;
  671. begin
  672. GetProgramName:='';
  673. FillChar(s1,255,#0);
  674. if dos_GetProgramName(@s1[1],255) then begin
  675. { now check out and assign the length of the string }
  676. counter := 1;
  677. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  678. s1[0]:=Char(counter-1);
  679. { now remove any component path which should not be there }
  680. for counter:=length(s1) downto 1 do
  681. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  682. { readjust counterv to point to character }
  683. if counter<>1 then Inc(counter);
  684. GetProgramName:=copy(s1,counter,length(s1));
  685. end;
  686. end;
  687. {*****************************************************************************
  688. ParamStr/Randomize
  689. *****************************************************************************}
  690. { number of args }
  691. function paramcount : longint;
  692. begin
  693. if MOS_ambMsg<>nil then
  694. paramcount:=0
  695. else
  696. paramcount:=argc-1;
  697. end;
  698. { argument number l }
  699. function paramstr(l : longint) : string;
  700. var
  701. s1: String;
  702. begin
  703. paramstr:='';
  704. if MOS_ambMsg<>nil then exit;
  705. if l=0 then begin
  706. s1:=GetProgramDir;
  707. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  708. else paramstr:=s1+'/'+GetProgramName;
  709. end else begin
  710. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  711. end;
  712. end;
  713. { set randseed to a new pseudo random value }
  714. procedure randomize;
  715. var tmpTime: TDateStamp;
  716. begin
  717. dos_DateStamp(tmpTime);
  718. randseed:=tmpTime.ds_tick;
  719. end;
  720. {*****************************************************************************
  721. Heap Management
  722. *****************************************************************************}
  723. var
  724. int_heap : LongInt; external name 'HEAP';
  725. int_heapsize : LongInt; external name 'HEAPSIZE';
  726. { first address of heap }
  727. function getheapstart:pointer;
  728. begin
  729. getheapstart:=@int_heap;
  730. end;
  731. { current length of heap }
  732. function getheapsize:longint;
  733. begin
  734. getheapsize:=int_heapsize;
  735. end;
  736. { function to allocate size bytes more for the program }
  737. { must return the first address of new data space or nil if fail }
  738. function Sbrk(size : longint):pointer;
  739. begin
  740. Sbrk:=exec_AllocPooled(MOS_heapPool,size);
  741. end;
  742. {$I heap.inc}
  743. {*****************************************************************************
  744. Directory Handling
  745. *****************************************************************************}
  746. procedure mkdir(const s : string);[IOCheck];
  747. var
  748. buffer : array[0..255] of char;
  749. j : Integer;
  750. tmpStr : string;
  751. tmpLock : LongInt;
  752. begin
  753. checkCTRLC;
  754. if (s='') or (InOutRes<>0) then exit;
  755. tmpStr:=s;
  756. for j:=1 to length(tmpStr) do
  757. if tmpStr[j]='\' then tmpStr[j]:='/';
  758. move(tmpStr[1],buffer,length(tmpStr));
  759. buffer[length(tmpStr)]:=#0;
  760. tmpLock:=dos_CreateDir(buffer);
  761. if tmpLock=0 then begin
  762. dosError2InOut(dos_IoErr);
  763. exit;
  764. end;
  765. dos_UnLock(tmpLock);
  766. end;
  767. procedure rmdir(const s : string);[IOCheck];
  768. var
  769. buffer : array[0..255] of char;
  770. j : Integer;
  771. tmpStr : string;
  772. begin
  773. checkCTRLC;
  774. if (s='.') then InOutRes:=16;
  775. If (s='') or (InOutRes<>0) then exit;
  776. tmpStr:=s;
  777. for j:=1 to length(tmpStr) do
  778. if tmpStr[j] = '\' then tmpStr[j] := '/';
  779. move(tmpStr[1],buffer,length(tmpStr));
  780. buffer[length(tmpStr)]:=#0;
  781. if not dos_DeleteFile(buffer) then
  782. dosError2InOut(dos_IoErr);
  783. end;
  784. procedure chdir(const s : string);[IOCheck];
  785. var
  786. buffer : array[0..255] of char;
  787. alock : LongInt;
  788. FIB : PFileInfoBlock;
  789. j : Integer;
  790. tmpStr : string;
  791. begin
  792. checkCTRLC;
  793. If (s='') or (InOutRes<>0) then exit;
  794. tmpStr:=s;
  795. for j:=1 to length(tmpStr) do
  796. if tmpStr[j]='\' then tmpStr[j]:='/';
  797. { Return parent directory }
  798. if s='..' then begin
  799. getdir(0,tmpStr);
  800. j:=length(tmpStr);
  801. { Look through the previous paths }
  802. while (tmpStr[j]<>'/') and (tmpStr[j]<>':') and (j>0) do
  803. dec(j);
  804. if j>0 then
  805. tmpStr:=copy(tmpStr,1,j);
  806. end;
  807. alock:=0;
  808. move(tmpStr[1],buffer,length(tmpStr));
  809. buffer[length(tmpStr)]:=#0;
  810. { Changing the directory is a pretty complicated affair }
  811. { 1) Obtain a lock on the directory }
  812. { 2) CurrentDir the lock }
  813. alock:=dos_Lock(buffer,SHARED_LOCK);
  814. if alock=0 then begin
  815. dosError2InOut(dos_IoErr);
  816. exit;
  817. end;
  818. FIB:=nil;
  819. new(FIB);
  820. if (dos_Examine(alock,FIB)=True) and (FIB^.fib_DirEntryType>0) then begin
  821. alock := dos_CurrentDir(alock);
  822. if MOS_OrigDir=0 then begin
  823. MOS_OrigDir:=alock;
  824. alock:=0;
  825. end;
  826. end;
  827. if alock<>0 then dos_Unlock(alock);
  828. if assigned(FIB) then dispose(FIB)
  829. end;
  830. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  831. var tmpbuf: array[0..255] of char;
  832. begin
  833. checkCTRLC;
  834. Dir:='';
  835. if not dos_GetCurrentDirName(tmpbuf,256) then
  836. dosError2InOut(dos_IoErr)
  837. else
  838. Dir:=strpas(tmpbuf);
  839. end;
  840. {****************************************************************************
  841. Low level File Routines
  842. All these functions can set InOutRes on errors
  843. ****************************************************************************}
  844. { close a file from the handle value }
  845. procedure do_close(handle : longint);
  846. begin
  847. RemoveFromList(MOS_fileList,handle);
  848. { Do _NOT_ check CTRL_C on Close, because it will conflict
  849. with System_Exit! }
  850. if not dos_Close(handle) then
  851. dosError2InOut(dos_IoErr);
  852. end;
  853. procedure do_erase(p : pchar);
  854. begin
  855. checkCTRLC;
  856. if not dos_DeleteFile(p) then
  857. dosError2InOut(dos_IoErr);
  858. end;
  859. procedure do_rename(p1,p2 : pchar);
  860. begin
  861. checkCTRLC;
  862. if not dos_Rename(p1,p2) then
  863. dosError2InOut(dos_IoErr);
  864. end;
  865. function do_write(h:longint; addr: pointer; len: longint) : longint;
  866. var dosResult: LongInt;
  867. begin
  868. checkCTRLC;
  869. do_write:=0;
  870. if len<=0 then exit;
  871. dosResult:=dos_Write(h,addr,len);
  872. if dosResult<0 then begin
  873. dosError2InOut(dos_IoErr);
  874. end else begin
  875. do_write:=dosResult;
  876. end;
  877. end;
  878. function do_read(h:longint; addr: pointer; len: longint) : longint;
  879. var dosResult: LongInt;
  880. begin
  881. checkCTRLC;
  882. do_read:=0;
  883. if len<=0 then exit;
  884. dosResult:=dos_Read(h,addr,len);
  885. if dosResult<0 then begin
  886. dosError2InOut(dos_IoErr);
  887. end else begin
  888. do_read:=dosResult;
  889. end
  890. end;
  891. function do_filepos(handle : longint) : longint;
  892. var dosResult: LongInt;
  893. begin
  894. checkCTRLC;
  895. do_filepos:=0;
  896. { Seeking zero from OFFSET_CURRENT to find out where we are }
  897. dosResult:=dos_Seek(handle,0,OFFSET_CURRENT);
  898. if dosResult<0 then begin
  899. dosError2InOut(dos_IoErr);
  900. end else begin
  901. do_filepos:=dosResult;
  902. end;
  903. end;
  904. procedure do_seek(handle,pos : longint);
  905. begin
  906. checkCTRLC;
  907. { Seeking from OFFSET_BEGINNING }
  908. if dos_Seek(handle,pos,OFFSET_BEGINNING)<0 then
  909. dosError2InOut(dos_IoErr);
  910. end;
  911. function do_seekend(handle:longint):longint;
  912. var dosResult: LongInt;
  913. begin
  914. checkCTRLC;
  915. do_seekend:=0;
  916. { Seeking to OFFSET_END }
  917. dosResult:=dos_Seek(handle,0,OFFSET_END);
  918. if dosResult<0 then begin
  919. dosError2InOut(dos_IoErr);
  920. end else begin
  921. do_seekend:=dosResult;
  922. end
  923. end;
  924. function do_filesize(handle : longint) : longint;
  925. var currfilepos: longint;
  926. begin
  927. checkCTRLC;
  928. currfilepos:=do_filepos(handle);
  929. { We have to do this twice, because seek returns the OLD position }
  930. do_filesize:=do_seekend(handle);
  931. do_filesize:=do_seekend(handle);
  932. do_seek(handle,currfilepos)
  933. end;
  934. { truncate at a given position }
  935. procedure do_truncate (handle,pos:longint);
  936. begin
  937. checkCTRLC;
  938. { Seeking from OFFSET_BEGINNING }
  939. if dos_SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  940. dosError2InOut(dos_IoErr);
  941. end;
  942. procedure do_open(var f;p:pchar;flags:longint);
  943. {
  944. filerec and textrec have both handle and mode as the first items so
  945. they could use the same routine for opening/creating.
  946. when (flags and $10) the file will be append
  947. when (flags and $100) the file will be truncate/rewritten
  948. when (flags and $1000) there is no check for close (needed for textfiles)
  949. }
  950. var
  951. i,j : LongInt;
  952. openflags : LongInt;
  953. path : String;
  954. buffer : array[0..255] of Char;
  955. index : Integer;
  956. s : String;
  957. begin
  958. path:=strpas(p);
  959. for index:=1 to length(path) do
  960. if path[index]='\' then path[index]:='/';
  961. { remove any dot characters and replace by their current }
  962. { directory equivalent. }
  963. { look for parent directory }
  964. if pos('../',path) = 1 then
  965. begin
  966. delete(path,1,3);
  967. getdir(0,s);
  968. j:=length(s);
  969. while (s[j]<>'/') and (s[j]<>':') and (j>0) do
  970. dec(j);
  971. if j > 0 then
  972. s:=copy(s,1,j);
  973. path:=s+path;
  974. end
  975. else
  976. { look for current directory }
  977. if pos('./',path) = 1 then
  978. begin
  979. delete(path,1,2);
  980. getdir(0,s);
  981. if (s[length(s)]<>'/') and (s[length(s)]<>':') then
  982. s:=s+'/';
  983. path:=s+path;
  984. end;
  985. move(path[1],buffer,length(path));
  986. buffer[length(path)]:=#0;
  987. { close first if opened }
  988. if ((flags and $10000)=0) then
  989. begin
  990. case filerec(f).mode of
  991. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  992. fmclosed : ;
  993. else begin
  994. inoutres:=102; {not assigned}
  995. exit;
  996. end;
  997. end;
  998. end;
  999. { reset file handle }
  1000. filerec(f).handle:=UnusedHandle;
  1001. { convert filemode to filerec modes }
  1002. { READ/WRITE on existing file }
  1003. { RESET/APPEND }
  1004. openflags := 1005;
  1005. case (flags and 3) of
  1006. 0 : filerec(f).mode:=fminput;
  1007. 1 : filerec(f).mode:=fmoutput;
  1008. 2 : filerec(f).mode:=fminout;
  1009. end;
  1010. { rewrite (create a new file) }
  1011. if (flags and $1000)<>0 then openflags := 1006;
  1012. { empty name is special }
  1013. if p[0]=#0 then
  1014. begin
  1015. case filerec(f).mode of
  1016. fminput :
  1017. filerec(f).handle:=StdInputHandle;
  1018. fmappend,
  1019. fmoutput : begin
  1020. filerec(f).handle:=StdOutputHandle;
  1021. filerec(f).mode:=fmoutput; {fool fmappend}
  1022. end;
  1023. end;
  1024. exit;
  1025. end;
  1026. i:=dos_Open(buffer,openflags);
  1027. if i=0 then
  1028. begin
  1029. dosError2InOut(dos_IoErr);
  1030. end else begin
  1031. AddToList(MOS_fileList,i);
  1032. filerec(f).handle:=i;
  1033. end;
  1034. { append mode }
  1035. if ((Flags and $100)<>0) and (FileRec(F).Handle<>UnusedHandle) then
  1036. begin
  1037. do_seekend(filerec(f).handle);
  1038. filerec(f).mode:=fmoutput; {fool fmappend}
  1039. end;
  1040. end;
  1041. function do_isdevice(handle:longint):boolean;
  1042. begin
  1043. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  1044. (handle=StdErrorHandle) then
  1045. do_isdevice:=True
  1046. else
  1047. do_isdevice:=False;
  1048. end;
  1049. {*****************************************************************************
  1050. UnTyped File Handling
  1051. *****************************************************************************}
  1052. {$i file.inc}
  1053. {*****************************************************************************
  1054. Typed File Handling
  1055. *****************************************************************************}
  1056. {$i typefile.inc}
  1057. {*****************************************************************************
  1058. Text File Handling
  1059. *****************************************************************************}
  1060. {$I text.inc}
  1061. { MorphOS specific startup }
  1062. procedure SysInitMorphOS;
  1063. var self: PProcess;
  1064. begin
  1065. self:=PProcess(exec_FindTask(nil));
  1066. if self^.pr_CLI=0 then begin
  1067. { if we're running from Ambient/Workbench, we catch its message }
  1068. exec_WaitPort(@self^.pr_MsgPort);
  1069. MOS_ambMsg:=exec_GetMsg(@self^.pr_MsgPort);
  1070. end;
  1071. MOS_DOSBase:=exec_OpenLibrary('dos.library',50);
  1072. if MOS_DOSBase=nil then Halt(1);
  1073. MOS_UtilityBase:=exec_OpenLibrary('utility.library',50);
  1074. if MOS_UtilityBase=nil then Halt(1);
  1075. { Creating the memory pool for growing heap }
  1076. MOS_heapPool:=exec_CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  1077. if MOS_heapPool=nil then Halt(1);
  1078. if MOS_ambMsg=nil then begin
  1079. StdInputHandle:=dos_Input;
  1080. StdOutputHandle:=dos_Output;
  1081. end else begin
  1082. MOS_ConHandle:=dos_Open(MOS_ConName,1005);
  1083. if MOS_ConHandle<>0 then begin
  1084. StdInputHandle:=MOS_ConHandle;
  1085. StdOutputHandle:=MOS_ConHandle;
  1086. end else
  1087. Halt(1);
  1088. end;
  1089. end;
  1090. procedure SysInitStdIO;
  1091. begin
  1092. OpenStdIO(Input,fmInput,StdInputHandle);
  1093. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1094. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1095. { * MorphOS doesn't have a separate stderr, just like AmigaOS (???) * }
  1096. StdErrorHandle:=StdOutputHandle;
  1097. // OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1098. end;
  1099. {procedure SysInitExecPath;
  1100. var
  1101. hs : string[16];
  1102. link : string;
  1103. i : longint;
  1104. begin
  1105. str(Fpgetpid,hs);
  1106. hs:='/proc/'+hs+'/exe'#0;
  1107. i:=Fpreadlink(@hs[1],@link[1],high(link));
  1108. { it must also be an absolute filename, linux 2.0 points to a memory
  1109. location so this will skip that }
  1110. if (i>0) and (link[1]='/') then
  1111. begin
  1112. link[0]:=chr(i);
  1113. ExecPathStr:=link;
  1114. end;
  1115. end;
  1116. }
  1117. Begin
  1118. IsConsole := TRUE;
  1119. IsLibrary := FALSE;
  1120. StackLength := InitialStkLen;
  1121. StackBottom := Sptr - StackLength;
  1122. { OS specific startup }
  1123. MOS_ambMsg:=nil;
  1124. MOS_origDir:=0;
  1125. MOS_fileList:=nil;
  1126. envp:=nil;
  1127. SysInitMorphOS;
  1128. { Set up signals handlers }
  1129. // InstallSignals;
  1130. { Setup heap }
  1131. InitHeap;
  1132. SysInitExceptions;
  1133. { Setup stdin, stdout and stderr }
  1134. SysInitStdIO;
  1135. { Reset IO Error }
  1136. InOutRes:=0;
  1137. { Arguments }
  1138. // SetupCmdLine;
  1139. // SysInitExecPath;
  1140. GenerateArgs;
  1141. (* This should be changed to a real value during *)
  1142. (* thread driver initialization if appropriate. *)
  1143. ThreadID := 1;
  1144. {$ifdef HASVARIANT}
  1145. initvariantmanager;
  1146. {$endif HASVARIANT}
  1147. End.
  1148. {
  1149. $Log$
  1150. Revision 1.11 2004-06-06 19:18:05 karoly
  1151. + added support for paramstr(0)
  1152. Revision 1.10 2004/06/05 19:49:19 karoly
  1153. + added console I/O support when running from Ambient
  1154. Revision 1.9 2004/05/12 23:18:54 karoly
  1155. * fixed do_read and dos_Read from being nonsense
  1156. Revision 1.8 2004/05/12 20:26:04 karoly
  1157. + added syscalls and structures necessary for DOS unit
  1158. Revision 1.7 2004/05/12 15:34:16 karoly
  1159. * fixed startup code from endless wait when not started from Ambient
  1160. Revision 1.6 2004/05/09 14:42:59 karoly
  1161. * again, few more new things added
  1162. Revision 1.5 2004/05/09 02:02:42 karoly
  1163. * more things got implemented
  1164. Revision 1.4 2004/05/02 02:06:57 karoly
  1165. + most of file I/O calls implemented
  1166. Revision 1.3 2004/05/01 15:09:47 karoly
  1167. * first working system unit (very limited yet)
  1168. Revision 1.2  2004/04/08 06:28:29  karoly
  1169. * first steps to have a morphos system unit
  1170. Revision 1.1 2004/02/13 07:19:53 karoly
  1171. * quick hack from Linux system unit
  1172. }