system.pp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit system;
  12. interface
  13. { two debug conditionnals can be used
  14. - SYSTEMDEBUG
  15. -for STACK checks
  16. -for non closed files at exit (or at any time with GDB)
  17. - SYSTEM_DEBUG_STARTUP
  18. specifically for
  19. - proxy command line (DJGPP feature)
  20. - list of args
  21. - list of env variables (PM) }
  22. {$ifndef NO_EXCEPTIONS_IN_SYSTEM}
  23. {$define EXCEPTIONS_IN_SYSTEM}
  24. {$endif NO_EXCEPTIONS_IN_SYSTEM}
  25. { include system-independent routine headers }
  26. {$I systemh.inc}
  27. { include heap support headers }
  28. {$I heaph.inc}
  29. {Platform specific information}
  30. type
  31. THandle = Longint;
  32. const
  33. LineEnding = #13#10;
  34. { LFNSupport is a variable here, defined below!!! }
  35. DirectorySeparator = '\';
  36. DriveSeparator = ':';
  37. PathSeparator = ';';
  38. { FileNameCaseSensitive is defined separately below!!! }
  39. maxExitCode = 255;
  40. const
  41. { Default filehandles }
  42. UnusedHandle = -1;
  43. StdInputHandle = 0;
  44. StdOutputHandle = 1;
  45. StdErrorHandle = 2;
  46. FileNameCaseSensitive : boolean = false;
  47. sLineBreak = LineEnding;
  48. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  49. { Default memory segments (Tp7 compatibility) }
  50. seg0040 = $0040;
  51. segA000 = $A000;
  52. segB000 = $B000;
  53. segB800 = $B800;
  54. var
  55. { Mem[] support }
  56. mem : array[0..$7fffffff] of byte absolute $0:$0;
  57. memw : array[0..$7fffffff div sizeof(word)] of word absolute $0:$0;
  58. meml : array[0..$7fffffff div sizeof(longint)] of longint absolute $0:$0;
  59. { C-compatible arguments and environment }
  60. argc : longint;
  61. argv : ppchar;
  62. envp : ppchar;
  63. dos_argv0 : pchar;
  64. {$ifndef RTLLITE}
  65. { System info }
  66. LFNSupport : boolean;
  67. {$ELSE RTLLITE}
  68. const
  69. LFNSupport = false;
  70. {$endif RTLLITE}
  71. type
  72. { Dos Extender info }
  73. p_stub_info = ^t_stub_info;
  74. t_stub_info = packed record
  75. magic : array[0..15] of char;
  76. size : longint;
  77. minstack : longint;
  78. memory_handle : longint;
  79. initial_size : longint;
  80. minkeep : word;
  81. ds_selector : word;
  82. ds_segment : word;
  83. psp_selector : word;
  84. cs_selector : word;
  85. env_size : word;
  86. basename : array[0..7] of char;
  87. argv0 : array [0..15] of char;
  88. dpmi_server : array [0..15] of char;
  89. end;
  90. p_go32_info_block = ^t_go32_info_block;
  91. t_go32_info_block = packed record
  92. size_of_this_structure_in_bytes : longint; {offset 0}
  93. linear_address_of_primary_screen : longint; {offset 4}
  94. linear_address_of_secondary_screen : longint; {offset 8}
  95. linear_address_of_transfer_buffer : longint; {offset 12}
  96. size_of_transfer_buffer : longint; {offset 16}
  97. pid : longint; {offset 20}
  98. master_interrupt_controller_base : byte; {offset 24}
  99. slave_interrupt_controller_base : byte; {offset 25}
  100. selector_for_linear_memory : word; {offset 26}
  101. linear_address_of_stub_info_structure : longint; {offset 28}
  102. linear_address_of_original_psp : longint; {offset 32}
  103. run_mode : word; {offset 36}
  104. run_mode_info : word; {offset 38}
  105. end;
  106. var
  107. stub_info : p_stub_info;
  108. go32_info_block : t_go32_info_block;
  109. {$ifdef SYSTEMDEBUG}
  110. const
  111. accept_sbrk : boolean = true;
  112. {$endif}
  113. {
  114. necessary for objects.pas, should be removed (at least from the interface
  115. to the implementation)
  116. }
  117. type
  118. trealregs=record
  119. realedi,realesi,realebp,realres,
  120. realebx,realedx,realecx,realeax : longint;
  121. realflags,
  122. reales,realds,realfs,realgs,
  123. realip,realcs,realsp,realss : word;
  124. end;
  125. function do_write(h:longint;addr:pointer;len : longint) : longint;
  126. function do_read(h:longint;addr:pointer;len : longint) : longint;
  127. procedure syscopyfromdos(addr : longint; len : longint);
  128. procedure syscopytodos(addr : longint; len : longint);
  129. procedure sysrealintr(intnr : word;var regs : trealregs);
  130. function tb : longint;
  131. implementation
  132. { include system independent routines }
  133. {$I system.inc}
  134. const
  135. carryflag = 1;
  136. type
  137. tseginfo=packed record
  138. offset : pointer;
  139. segment : word;
  140. end;
  141. var
  142. old_int00 : tseginfo;cvar;
  143. old_int75 : tseginfo;cvar;
  144. {$asmmode ATT}
  145. {*****************************************************************************
  146. Go32 Helpers
  147. *****************************************************************************}
  148. function far_strlen(selector : word;linear_address : longint) : longint;assembler;
  149. asm
  150. movl linear_address,%edx
  151. movl %edx,%ecx
  152. movw selector,%gs
  153. .Larg19:
  154. movb %gs:(%edx),%al
  155. testb %al,%al
  156. je .Larg20
  157. incl %edx
  158. jmp .Larg19
  159. .Larg20:
  160. movl %edx,%eax
  161. subl %ecx,%eax
  162. end;
  163. function tb : longint;
  164. begin
  165. tb:=go32_info_block.linear_address_of_transfer_buffer;
  166. end;
  167. function tb_segment : longint;
  168. begin
  169. tb_segment:=go32_info_block.linear_address_of_transfer_buffer shr 4;
  170. end;
  171. function tb_offset : longint;
  172. begin
  173. tb_offset:=go32_info_block.linear_address_of_transfer_buffer and $f;
  174. end;
  175. function tb_size : longint;
  176. begin
  177. tb_size:=go32_info_block.size_of_transfer_buffer;
  178. end;
  179. function dos_selector : word;
  180. begin
  181. dos_selector:=go32_info_block.selector_for_linear_memory;
  182. end;
  183. function get_ds : word;assembler;
  184. asm
  185. movw %ds,%ax
  186. end;
  187. function get_cs : word;assembler;
  188. asm
  189. movw %cs,%ax
  190. end;
  191. procedure sysseg_move(sseg : word;source : longint;dseg : word;dest : longint;count : longint);
  192. begin
  193. if count=0 then
  194. exit;
  195. if (sseg<>dseg) or ((sseg=dseg) and (source>dest)) then
  196. asm
  197. pushl %esi
  198. pushl %edi
  199. pushw %es
  200. pushw %ds
  201. cld
  202. movl count,%ecx
  203. movl source,%esi
  204. movl dest,%edi
  205. movw dseg,%ax
  206. movw %ax,%es
  207. movw sseg,%ax
  208. movw %ax,%ds
  209. movl %ecx,%eax
  210. shrl $2,%ecx
  211. rep
  212. movsl
  213. movl %eax,%ecx
  214. andl $3,%ecx
  215. rep
  216. movsb
  217. popw %ds
  218. popw %es
  219. popl %edi
  220. popl %esi
  221. end
  222. else if (source<dest) then
  223. { copy backward for overlapping }
  224. asm
  225. pushl %esi
  226. pushl %edi
  227. pushw %es
  228. pushw %ds
  229. std
  230. movl count,%ecx
  231. movl source,%esi
  232. movl dest,%edi
  233. movw dseg,%ax
  234. movw %ax,%es
  235. movw sseg,%ax
  236. movw %ax,%ds
  237. addl %ecx,%esi
  238. addl %ecx,%edi
  239. movl %ecx,%eax
  240. andl $3,%ecx
  241. orl %ecx,%ecx
  242. jz .LSEG_MOVE1
  243. { calculate esi and edi}
  244. decl %esi
  245. decl %edi
  246. rep
  247. movsb
  248. incl %esi
  249. incl %edi
  250. .LSEG_MOVE1:
  251. subl $4,%esi
  252. subl $4,%edi
  253. movl %eax,%ecx
  254. shrl $2,%ecx
  255. rep
  256. movsl
  257. cld
  258. popw %ds
  259. popw %es
  260. popl %edi
  261. popl %esi
  262. end;
  263. end;
  264. var
  265. _args : ppchar;external name '_args';
  266. procedure setup_arguments;
  267. type
  268. arrayword = array [0..255] of word;
  269. var
  270. psp : word;
  271. proxy_s : string[50];
  272. proxy_argc,proxy_seg,proxy_ofs,lin : longint;
  273. rm_argv : ^arrayword;
  274. argv0len : longint;
  275. useproxy : boolean;
  276. hp : ppchar;
  277. doscmd : string[129]; { Dos commandline copied from PSP, max is 128 chars +1 for terminating zero }
  278. arglen,
  279. count : longint;
  280. argstart,
  281. pc,arg : pchar;
  282. quote : char;
  283. argvlen : longint;
  284. function atohex(s : pchar) : longint;
  285. var
  286. rv : longint;
  287. v : byte;
  288. begin
  289. rv:=0;
  290. while (s^<>#0) do
  291. begin
  292. v:=byte(s^)-byte('0');
  293. if (v > 9) then
  294. dec(v,7);
  295. v:=v and 15; { in case it's lower case }
  296. rv:=(rv shl 4) or v;
  297. inc(longint(s));
  298. end;
  299. atohex:=rv;
  300. end;
  301. procedure allocarg(idx,len:longint);
  302. begin
  303. if idx>=argvlen then
  304. begin
  305. argvlen:=(idx+8) and (not 7);
  306. sysreallocmem(argv,argvlen*sizeof(pointer));
  307. end;
  308. { use realloc to reuse already existing memory }
  309. if len<>0 then
  310. sysreallocmem(argv[idx],len+1);
  311. end;
  312. begin
  313. count:=0;
  314. argc:=1;
  315. argv:=nil;
  316. argvlen:=0;
  317. { load commandline from psp }
  318. psp:=stub_info^.psp_selector;
  319. sysseg_move(psp, 128, get_ds, longint(@doscmd), 128);
  320. doscmd[length(doscmd)+1]:=#0;
  321. {$IfDef SYSTEM_DEBUG_STARTUP}
  322. Writeln(stderr,'Dos command line is #',doscmd,'# size = ',length(doscmd));
  323. {$EndIf }
  324. { create argv[0] }
  325. argv0len:=strlen(dos_argv0);
  326. allocarg(count,argv0len);
  327. move(dos_argv0^,argv[count]^,argv0len);
  328. inc(count);
  329. { setup cmdline variable }
  330. cmdline:=Getmem(argv0len+length(doscmd)+2);
  331. move(dos_argv0^,cmdline^,argv0len);
  332. cmdline[argv0len]:=' ';
  333. inc(argv0len);
  334. move(doscmd[1],cmdline[argv0len],length(doscmd));
  335. cmdline[argv0len+length(doscmd)+1]:=#0;
  336. { parse dos commandline }
  337. pc:=@doscmd[1];
  338. while pc^<>#0 do
  339. begin
  340. { skip leading spaces }
  341. while pc^ in [#1..#32] do
  342. inc(pc);
  343. if pc^=#0 then
  344. break;
  345. { calc argument length }
  346. quote:=' ';
  347. argstart:=pc;
  348. arglen:=0;
  349. while (pc^<>#0) do
  350. begin
  351. case pc^ of
  352. #1..#32 :
  353. begin
  354. if quote<>' ' then
  355. inc(arglen)
  356. else
  357. break;
  358. end;
  359. '"' :
  360. begin
  361. if quote<>'''' then
  362. begin
  363. if pchar(pc+1)^<>'"' then
  364. begin
  365. if quote='"' then
  366. quote:=' '
  367. else
  368. quote:='"';
  369. end
  370. else
  371. inc(pc);
  372. end
  373. else
  374. inc(arglen);
  375. end;
  376. '''' :
  377. begin
  378. if quote<>'"' then
  379. begin
  380. if pchar(pc+1)^<>'''' then
  381. begin
  382. if quote='''' then
  383. quote:=' '
  384. else
  385. quote:='''';
  386. end
  387. else
  388. inc(pc);
  389. end
  390. else
  391. inc(arglen);
  392. end;
  393. else
  394. inc(arglen);
  395. end;
  396. inc(pc);
  397. end;
  398. { copy argument }
  399. allocarg(count,arglen);
  400. quote:=' ';
  401. pc:=argstart;
  402. arg:=argv[count];
  403. while (pc^<>#0) do
  404. begin
  405. case pc^ of
  406. #1..#32 :
  407. begin
  408. if quote<>' ' then
  409. begin
  410. arg^:=pc^;
  411. inc(arg);
  412. end
  413. else
  414. break;
  415. end;
  416. '"' :
  417. begin
  418. if quote<>'''' then
  419. begin
  420. if pchar(pc+1)^<>'"' then
  421. begin
  422. if quote='"' then
  423. quote:=' '
  424. else
  425. quote:='"';
  426. end
  427. else
  428. inc(pc);
  429. end
  430. else
  431. begin
  432. arg^:=pc^;
  433. inc(arg);
  434. end;
  435. end;
  436. '''' :
  437. begin
  438. if quote<>'"' then
  439. begin
  440. if pchar(pc+1)^<>'''' then
  441. begin
  442. if quote='''' then
  443. quote:=' '
  444. else
  445. quote:='''';
  446. end
  447. else
  448. inc(pc);
  449. end
  450. else
  451. begin
  452. arg^:=pc^;
  453. inc(arg);
  454. end;
  455. end;
  456. else
  457. begin
  458. arg^:=pc^;
  459. inc(arg);
  460. end;
  461. end;
  462. inc(pc);
  463. end;
  464. arg^:=#0;
  465. {$IfDef SYSTEM_DEBUG_STARTUP}
  466. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  467. {$EndIf SYSTEM_DEBUG_STARTUP}
  468. inc(count);
  469. end;
  470. argc:=count;
  471. { check for !proxy for long commandlines passed using environment }
  472. hp:=envp;
  473. useproxy:=false;
  474. while assigned(hp^) do
  475. begin
  476. if (hp^[0]=' ') then
  477. begin
  478. proxy_s:=strpas(hp^);
  479. if Copy(proxy_s,1,7)=' !proxy' then
  480. begin
  481. proxy_s[13]:=#0;
  482. proxy_s[18]:=#0;
  483. proxy_s[23]:=#0;
  484. argv[2]:=@proxy_s[9];
  485. argv[3]:=@proxy_s[14];
  486. argv[4]:=@proxy_s[19];
  487. useproxy:=true;
  488. break;
  489. end;
  490. end;
  491. inc(hp);
  492. end;
  493. { check for !proxy for long commandlines passed using commandline }
  494. if (not useproxy) and
  495. (argc > 1) and (far_strlen(get_ds,longint(argv[1])) = 6) then
  496. begin
  497. move(argv[1]^,proxy_s[1],6);
  498. proxy_s[0] := #6;
  499. if (proxy_s = '!proxy') then
  500. useproxy:=true;
  501. end;
  502. { use proxy when found }
  503. if useproxy then
  504. begin
  505. proxy_argc:=atohex(argv[2]);
  506. proxy_seg:=atohex(argv[3]);
  507. proxy_ofs:=atohex(argv[4]);
  508. {$IfDef SYSTEM_DEBUG_STARTUP}
  509. Writeln(stderr,'proxy command line found');
  510. writeln(stderr,'argc: ',proxy_argc,' seg: ',proxy_seg,' ofs: ',proxy_ofs);
  511. {$EndIf SYSTEM_DEBUG_STARTUP}
  512. rm_argv:=SysGetmem(proxy_argc*sizeof(word));
  513. sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
  514. for count:=0 to proxy_argc - 1 do
  515. begin
  516. lin:=proxy_seg*16+rm_argv^[count];
  517. arglen:=far_strlen(dos_selector,lin);
  518. allocarg(count,arglen);
  519. sysseg_move(dos_selector,lin,get_ds,longint(argv[count]),arglen+1);
  520. {$IfDef SYSTEM_DEBUG_STARTUP}
  521. Writeln(stderr,'arg ',count,' #',rm_argv^[count],'#',arglen,'#',argv[count],'#');
  522. {$EndIf SYSTEM_DEBUG_STARTUP}
  523. end;
  524. SysFreemem(rm_argv);
  525. argc:=proxy_argc;
  526. end;
  527. { create an nil entry }
  528. allocarg(argc,0);
  529. { free unused memory }
  530. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  531. _args:=argv;
  532. end;
  533. function strcopy(dest,source : pchar) : pchar;assembler;
  534. var
  535. saveeax,saveesi,saveedi : longint;
  536. asm
  537. movl %edi,saveedi
  538. movl %esi,saveesi
  539. {$ifdef REGCALL}
  540. movl %eax,saveeax
  541. movl %edx,%edi
  542. {$else}
  543. movl source,%edi
  544. {$endif}
  545. testl %edi,%edi
  546. jz .LStrCopyDone
  547. leal 3(%edi),%ecx
  548. andl $-4,%ecx
  549. movl %edi,%esi
  550. subl %edi,%ecx
  551. {$ifdef REGCALL}
  552. movl %eax,%edi
  553. {$else}
  554. movl dest,%edi
  555. {$endif}
  556. jz .LStrCopyAligned
  557. .LStrCopyAlignLoop:
  558. movb (%esi),%al
  559. incl %edi
  560. incl %esi
  561. testb %al,%al
  562. movb %al,-1(%edi)
  563. jz .LStrCopyDone
  564. decl %ecx
  565. jnz .LStrCopyAlignLoop
  566. .balign 16
  567. .LStrCopyAligned:
  568. movl (%esi),%eax
  569. movl %eax,%edx
  570. leal 0x0fefefeff(%eax),%ecx
  571. notl %edx
  572. addl $4,%esi
  573. andl %edx,%ecx
  574. andl $0x080808080,%ecx
  575. jnz .LStrCopyEndFound
  576. movl %eax,(%edi)
  577. addl $4,%edi
  578. jmp .LStrCopyAligned
  579. .LStrCopyEndFound:
  580. testl $0x0ff,%eax
  581. jz .LStrCopyByte
  582. testl $0x0ff00,%eax
  583. jz .LStrCopyWord
  584. testl $0x0ff0000,%eax
  585. jz .LStrCopy3Bytes
  586. movl %eax,(%edi)
  587. jmp .LStrCopyDone
  588. .LStrCopy3Bytes:
  589. xorb %dl,%dl
  590. movw %ax,(%edi)
  591. movb %dl,2(%edi)
  592. jmp .LStrCopyDone
  593. .LStrCopyWord:
  594. movw %ax,(%edi)
  595. jmp .LStrCopyDone
  596. .LStrCopyByte:
  597. movb %al,(%edi)
  598. .LStrCopyDone:
  599. {$ifdef REGCALL}
  600. movl saveeax,%eax
  601. {$else}
  602. movl dest,%eax
  603. {$endif}
  604. movl saveedi,%edi
  605. movl saveesi,%esi
  606. end;
  607. var
  608. __stubinfo : p_stub_info;external name '__stubinfo';
  609. ___dos_argv0 : pchar;external name '___dos_argv0';
  610. procedure setup_environment;
  611. var env_selector : word;
  612. env_count : longint;
  613. dos_env,cp : pchar;
  614. begin
  615. stub_info:=__stubinfo;
  616. dos_env := sysgetmem(stub_info^.env_size);
  617. env_count:=0;
  618. sysseg_move(stub_info^.psp_selector,$2c, get_ds, longint(@env_selector), 2);
  619. sysseg_move(env_selector, 0, get_ds, longint(dos_env), stub_info^.env_size);
  620. cp:=dos_env;
  621. while cp ^ <> #0 do
  622. begin
  623. inc(env_count);
  624. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  625. inc(longint(cp)); { skip to next character }
  626. end;
  627. envp := sysgetmem((env_count+1) * sizeof(pchar));
  628. if (envp = nil) then exit;
  629. cp:=dos_env;
  630. env_count:=0;
  631. while cp^ <> #0 do
  632. begin
  633. envp[env_count] := sysgetmem(strlen(cp)+1);
  634. strcopy(envp[env_count], cp);
  635. {$IfDef SYSTEM_DEBUG_STARTUP}
  636. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  637. {$EndIf SYSTEM_DEBUG_STARTUP}
  638. inc(env_count);
  639. while (cp^ <> #0) do
  640. inc(longint(cp)); { skip to NUL }
  641. inc(longint(cp)); { skip to next character }
  642. end;
  643. envp[env_count]:=nil;
  644. longint(cp):=longint(cp)+3;
  645. dos_argv0 := sysgetmem(strlen(cp)+1);
  646. if (dos_argv0 = nil) then halt;
  647. strcopy(dos_argv0, cp);
  648. { update ___dos_argv0 also }
  649. ___dos_argv0:=dos_argv0
  650. end;
  651. procedure syscopytodos(addr : longint; len : longint);
  652. begin
  653. if len > tb_size then
  654. HandleError(217);
  655. sysseg_move(get_ds,addr,dos_selector,tb,len);
  656. end;
  657. procedure syscopyfromdos(addr : longint; len : longint);
  658. begin
  659. if len > tb_size then
  660. HandleError(217);
  661. sysseg_move(dos_selector,tb,get_ds,addr,len);
  662. end;
  663. procedure sysrealintr(intnr : word;var regs : trealregs);
  664. begin
  665. regs.realsp:=0;
  666. regs.realss:=0;
  667. asm
  668. pushl %ebx
  669. pushl %edi
  670. movw intnr,%bx
  671. xorl %ecx,%ecx
  672. movl regs,%edi
  673. movw $0x300,%ax
  674. int $0x31
  675. popl %edi
  676. popl %ebx
  677. end;
  678. end;
  679. procedure set_pm_interrupt(vector : byte;const intaddr : tseginfo);
  680. begin
  681. asm
  682. pushl %ebx
  683. movl intaddr,%eax
  684. movl (%eax),%edx
  685. movw 4(%eax),%cx
  686. movl $0x205,%eax
  687. movb vector,%bl
  688. int $0x31
  689. popl %ebx
  690. end;
  691. end;
  692. procedure get_pm_interrupt(vector : byte;var intaddr : tseginfo);
  693. begin
  694. asm
  695. pushl %ebx
  696. movb vector,%bl
  697. movl $0x204,%eax
  698. int $0x31
  699. movl intaddr,%eax
  700. movl %edx,(%eax)
  701. movw %cx,4(%eax)
  702. popl %ebx
  703. end;
  704. end;
  705. procedure getinoutres(def : word);
  706. var
  707. regs : trealregs;
  708. begin
  709. regs.realeax:=$5900;
  710. regs.realebx:=$0;
  711. sysrealintr($21,regs);
  712. InOutRes:=lo(regs.realeax);
  713. case InOutRes of
  714. 19 : InOutRes:=150;
  715. 21 : InOutRes:=152;
  716. 32 : InOutRes:=5;
  717. end;
  718. if InOutRes=0 then
  719. InOutRes:=Def;
  720. end;
  721. { Keep Track of open files }
  722. const
  723. max_files = 50;
  724. var
  725. openfiles : array [0..max_files-1] of boolean;
  726. {$ifdef SYSTEMDEBUG}
  727. opennames : array [0..max_files-1] of pchar;
  728. const
  729. free_closed_names : boolean = true;
  730. {$endif SYSTEMDEBUG}
  731. {*****************************************************************************
  732. System Dependent Exit code
  733. *****************************************************************************}
  734. procedure ___exit(exitcode:longint);cdecl;external name '___exit';
  735. procedure do_close(handle : longint);forward;
  736. Procedure system_exit;
  737. var
  738. h : byte;
  739. begin
  740. for h:=0 to max_files-1 do
  741. if openfiles[h] then
  742. begin
  743. {$ifdef SYSTEMDEBUG}
  744. writeln(stderr,'file ',opennames[h],' not closed at exit');
  745. {$endif SYSTEMDEBUG}
  746. if h>=5 then
  747. do_close(h);
  748. end;
  749. { halt is not allways called !! }
  750. { not on normal exit !! PM }
  751. set_pm_interrupt($00,old_int00);
  752. {$ifndef EXCEPTIONS_IN_SYSTEM}
  753. set_pm_interrupt($75,old_int75);
  754. {$endif EXCEPTIONS_IN_SYSTEM}
  755. ___exit(exitcode);
  756. end;
  757. procedure new_int00;
  758. begin
  759. HandleError(200);
  760. end;
  761. {$ifndef EXCEPTIONS_IN_SYSTEM}
  762. procedure new_int75;
  763. begin
  764. asm
  765. xorl %eax,%eax
  766. outb %al,$0x0f0
  767. movb $0x20,%al
  768. outb %al,$0x0a0
  769. outb %al,$0x020
  770. end;
  771. HandleError(200);
  772. end;
  773. {$endif EXCEPTIONS_IN_SYSTEM}
  774. var
  775. __stkbottom : pointer;external name '__stkbottom';
  776. {*****************************************************************************
  777. ParamStr/Randomize
  778. *****************************************************************************}
  779. function paramcount : longint;
  780. begin
  781. paramcount := argc - 1;
  782. end;
  783. function paramstr(l : longint) : string;
  784. begin
  785. if (l>=0) and (l+1<=argc) then
  786. paramstr:=strpas(argv[l])
  787. else
  788. paramstr:='';
  789. end;
  790. procedure randomize;
  791. var
  792. hl : longint;
  793. regs : trealregs;
  794. begin
  795. regs.realeax:=$2c00;
  796. sysrealintr($21,regs);
  797. hl:=lo(regs.realedx);
  798. randseed:=hl*$10000+ lo(regs.realecx);
  799. end;
  800. {*****************************************************************************
  801. Heap Management
  802. *****************************************************************************}
  803. var
  804. int_heap : longint;external name 'HEAP';
  805. int_heapsize : longint;external name 'HEAPSIZE';
  806. function getheapstart:pointer;
  807. begin
  808. getheapstart:=@int_heap;
  809. end;
  810. function getheapsize:longint;
  811. begin
  812. getheapsize:=int_heapsize;
  813. end;
  814. function ___sbrk(size:longint):longint;cdecl;external name '___sbrk';
  815. function Sbrk(size : longint):pointer;assembler;
  816. asm
  817. {$ifdef SYSTEMDEBUG}
  818. cmpb $1,accept_sbrk
  819. je .Lsbrk
  820. movl $0,%eax
  821. jmp .Lsbrk_fail
  822. .Lsbrk:
  823. {$endif}
  824. movl size,%eax
  825. pushl %eax
  826. call ___sbrk
  827. addl $4,%esp
  828. {$ifdef SYSTEMDEBUG}
  829. .Lsbrk_fail:
  830. {$endif}
  831. end;
  832. {*****************************************************************************
  833. OS Memory allocation / deallocation
  834. ****************************************************************************}
  835. function SysOSAlloc(size: ptrint): pointer;
  836. begin
  837. result := sbrk(size);
  838. end;
  839. {.$define HAS_SYSOSFREE}
  840. procedure SysOSFree(p: pointer; size: ptrint);
  841. begin
  842. end;
  843. { include standard heap management }
  844. {$I heap.inc}
  845. {****************************************************************************
  846. Low level File Routines
  847. ****************************************************************************}
  848. procedure AllowSlash(p:pchar);
  849. var
  850. i : longint;
  851. begin
  852. { allow slash as backslash }
  853. for i:=0 to strlen(p) do
  854. if p[i]='/' then p[i]:='\';
  855. end;
  856. procedure do_close(handle : thandle);
  857. var
  858. regs : trealregs;
  859. begin
  860. if Handle<=4 then
  861. exit;
  862. regs.realebx:=handle;
  863. if handle<max_files then
  864. begin
  865. openfiles[handle]:=false;
  866. {$ifdef SYSTEMDEBUG}
  867. if assigned(opennames[handle]) and free_closed_names then
  868. begin
  869. sysfreememsize(opennames[handle],strlen(opennames[handle])+1);
  870. opennames[handle]:=nil;
  871. end;
  872. {$endif SYSTEMDEBUG}
  873. end;
  874. regs.realeax:=$3e00;
  875. sysrealintr($21,regs);
  876. if (regs.realflags and carryflag) <> 0 then
  877. GetInOutRes(lo(regs.realeax));
  878. end;
  879. procedure do_erase(p : pchar);
  880. var
  881. regs : trealregs;
  882. begin
  883. AllowSlash(p);
  884. syscopytodos(longint(p),strlen(p)+1);
  885. regs.realedx:=tb_offset;
  886. regs.realds:=tb_segment;
  887. if LFNSupport then
  888. regs.realeax:=$7141
  889. else
  890. regs.realeax:=$4100;
  891. regs.realesi:=0;
  892. regs.realecx:=0;
  893. sysrealintr($21,regs);
  894. if (regs.realflags and carryflag) <> 0 then
  895. GetInOutRes(lo(regs.realeax));
  896. end;
  897. procedure do_rename(p1,p2 : pchar);
  898. var
  899. regs : trealregs;
  900. begin
  901. AllowSlash(p1);
  902. AllowSlash(p2);
  903. if strlen(p1)+strlen(p2)+3>tb_size then
  904. HandleError(217);
  905. sysseg_move(get_ds,longint(p2),dos_selector,tb,strlen(p2)+1);
  906. sysseg_move(get_ds,longint(p1),dos_selector,tb+strlen(p2)+2,strlen(p1)+1);
  907. regs.realedi:=tb_offset;
  908. regs.realedx:=tb_offset + strlen(p2)+2;
  909. regs.realds:=tb_segment;
  910. regs.reales:=tb_segment;
  911. if LFNSupport then
  912. regs.realeax:=$7156
  913. else
  914. regs.realeax:=$5600;
  915. regs.realecx:=$ff; { attribute problem here ! }
  916. sysrealintr($21,regs);
  917. if (regs.realflags and carryflag) <> 0 then
  918. GetInOutRes(lo(regs.realeax));
  919. end;
  920. function do_write(h:thandle;addr:pointer;len : longint) : longint;
  921. var
  922. regs : trealregs;
  923. size,
  924. writesize : longint;
  925. begin
  926. writesize:=0;
  927. while len > 0 do
  928. begin
  929. if len>tb_size then
  930. size:=tb_size
  931. else
  932. size:=len;
  933. syscopytodos(ptrint(addr)+writesize,size);
  934. regs.realecx:=size;
  935. regs.realedx:=tb_offset;
  936. regs.realds:=tb_segment;
  937. regs.realebx:=h;
  938. regs.realeax:=$4000;
  939. sysrealintr($21,regs);
  940. if (regs.realflags and carryflag) <> 0 then
  941. begin
  942. GetInOutRes(lo(regs.realeax));
  943. exit(writesize);
  944. end;
  945. inc(writesize,lo(regs.realeax));
  946. dec(len,lo(regs.realeax));
  947. { stop when not the specified size is written }
  948. if lo(regs.realeax)<size then
  949. break;
  950. end;
  951. Do_Write:=WriteSize;
  952. end;
  953. function do_read(h:thandle;addr:pointer;len : longint) : longint;
  954. var
  955. regs : trealregs;
  956. size,
  957. readsize : longint;
  958. begin
  959. readsize:=0;
  960. while len > 0 do
  961. begin
  962. if len>tb_size then
  963. size:=tb_size
  964. else
  965. size:=len;
  966. regs.realecx:=size;
  967. regs.realedx:=tb_offset;
  968. regs.realds:=tb_segment;
  969. regs.realebx:=h;
  970. regs.realeax:=$3f00;
  971. sysrealintr($21,regs);
  972. if (regs.realflags and carryflag) <> 0 then
  973. begin
  974. GetInOutRes(lo(regs.realeax));
  975. do_read:=0;
  976. exit;
  977. end;
  978. syscopyfromdos(ptrint(addr)+readsize,lo(regs.realeax));
  979. inc(readsize,lo(regs.realeax));
  980. dec(len,lo(regs.realeax));
  981. { stop when not the specified size is read }
  982. if lo(regs.realeax)<size then
  983. break;
  984. end;
  985. do_read:=readsize;
  986. end;
  987. function do_filepos(handle : thandle) : longint;
  988. var
  989. regs : trealregs;
  990. begin
  991. regs.realebx:=handle;
  992. regs.realecx:=0;
  993. regs.realedx:=0;
  994. regs.realeax:=$4201;
  995. sysrealintr($21,regs);
  996. if (regs.realflags and carryflag) <> 0 then
  997. Begin
  998. GetInOutRes(lo(regs.realeax));
  999. do_filepos:=0;
  1000. end
  1001. else
  1002. do_filepos:=lo(regs.realedx) shl 16+lo(regs.realeax);
  1003. end;
  1004. procedure do_seek(handle:thandle;pos : longint);
  1005. var
  1006. regs : trealregs;
  1007. begin
  1008. regs.realebx:=handle;
  1009. regs.realecx:=pos shr 16;
  1010. regs.realedx:=pos and $ffff;
  1011. regs.realeax:=$4200;
  1012. sysrealintr($21,regs);
  1013. if (regs.realflags and carryflag) <> 0 then
  1014. GetInOutRes(lo(regs.realeax));
  1015. end;
  1016. function do_seekend(handle:thandle):longint;
  1017. var
  1018. regs : trealregs;
  1019. begin
  1020. regs.realebx:=handle;
  1021. regs.realecx:=0;
  1022. regs.realedx:=0;
  1023. regs.realeax:=$4202;
  1024. sysrealintr($21,regs);
  1025. if (regs.realflags and carryflag) <> 0 then
  1026. Begin
  1027. GetInOutRes(lo(regs.realeax));
  1028. do_seekend:=0;
  1029. end
  1030. else
  1031. do_seekend:=lo(regs.realedx) shl 16+lo(regs.realeax);
  1032. end;
  1033. function do_filesize(handle : thandle) : longint;
  1034. var
  1035. aktfilepos : longint;
  1036. begin
  1037. aktfilepos:=do_filepos(handle);
  1038. do_filesize:=do_seekend(handle);
  1039. do_seek(handle,aktfilepos);
  1040. end;
  1041. { truncate at a given position }
  1042. procedure do_truncate (handle:thandle;pos:longint);
  1043. var
  1044. regs : trealregs;
  1045. begin
  1046. do_seek(handle,pos);
  1047. regs.realecx:=0;
  1048. regs.realedx:=tb_offset;
  1049. regs.realds:=tb_segment;
  1050. regs.realebx:=handle;
  1051. regs.realeax:=$4000;
  1052. sysrealintr($21,regs);
  1053. if (regs.realflags and carryflag) <> 0 then
  1054. GetInOutRes(lo(regs.realeax));
  1055. end;
  1056. const
  1057. FileHandleCount : longint = 20;
  1058. function Increase_file_handle_count : boolean;
  1059. var
  1060. regs : trealregs;
  1061. begin
  1062. Inc(FileHandleCount,10);
  1063. regs.realebx:=FileHandleCount;
  1064. regs.realeax:=$6700;
  1065. sysrealintr($21,regs);
  1066. if (regs.realflags and carryflag) <> 0 then
  1067. begin
  1068. Increase_file_handle_count:=false;
  1069. Dec (FileHandleCount, 10);
  1070. end
  1071. else
  1072. Increase_file_handle_count:=true;
  1073. end;
  1074. function dos_version : word;
  1075. var
  1076. regs : trealregs;
  1077. begin
  1078. regs.realeax := $3000;
  1079. sysrealintr($21,regs);
  1080. dos_version := regs.realeax
  1081. end;
  1082. procedure do_open(var f;p:pchar;flags:longint);
  1083. {
  1084. filerec and textrec have both handle and mode as the first items so
  1085. they could use the same routine for opening/creating.
  1086. when (flags and $100) the file will be append
  1087. when (flags and $1000) the file will be truncate/rewritten
  1088. when (flags and $10000) there is no check for close (needed for textfiles)
  1089. }
  1090. var
  1091. regs : trealregs;
  1092. action : longint;
  1093. Avoid6c00 : boolean;
  1094. begin
  1095. AllowSlash(p);
  1096. { check if Extended Open/Create API is safe to use }
  1097. Avoid6c00 := lo(dos_version) < 7;
  1098. { close first if opened }
  1099. if ((flags and $10000)=0) then
  1100. begin
  1101. case filerec(f).mode of
  1102. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  1103. fmclosed : ;
  1104. else
  1105. begin
  1106. inoutres:=102; {not assigned}
  1107. exit;
  1108. end;
  1109. end;
  1110. end;
  1111. { reset file handle }
  1112. filerec(f).handle:=UnusedHandle;
  1113. action:=$1;
  1114. { convert filemode to filerec modes }
  1115. case (flags and 3) of
  1116. 0 : filerec(f).mode:=fminput;
  1117. 1 : filerec(f).mode:=fmoutput;
  1118. 2 : filerec(f).mode:=fminout;
  1119. end;
  1120. if (flags and $1000)<>0 then
  1121. action:=$12; {create file function}
  1122. { empty name is special }
  1123. if p[0]=#0 then
  1124. begin
  1125. case FileRec(f).mode of
  1126. fminput :
  1127. FileRec(f).Handle:=StdInputHandle;
  1128. fminout, { this is set by rewrite }
  1129. fmoutput :
  1130. FileRec(f).Handle:=StdOutputHandle;
  1131. fmappend :
  1132. begin
  1133. FileRec(f).Handle:=StdOutputHandle;
  1134. FileRec(f).mode:=fmoutput; {fool fmappend}
  1135. end;
  1136. end;
  1137. exit;
  1138. end;
  1139. { real dos call }
  1140. syscopytodos(longint(p),strlen(p)+1);
  1141. {$ifndef RTLLITE}
  1142. if LFNSupport then
  1143. regs.realeax := $716c { Use LFN Open/Create API }
  1144. else
  1145. {$endif RTLLITE}
  1146. if Avoid6c00 then
  1147. regs.realeax := $3d00 + (flags and $ff) { For now, map to Open API }
  1148. else
  1149. regs.realeax := $6c00; { Use Extended Open/Create API }
  1150. if byte(regs.realeax shr 8) = $3d then
  1151. begin { Using the older Open or Create API's }
  1152. if (action and $00f0) <> 0 then
  1153. regs.realeax := $3c00; { Map to Create/Replace API }
  1154. regs.realds := tb_segment;
  1155. regs.realedx := tb_offset;
  1156. end
  1157. else
  1158. begin { Using LFN or Extended Open/Create API }
  1159. regs.realedx := action; { action if file does/doesn't exist }
  1160. regs.realds := tb_segment;
  1161. regs.realesi := tb_offset;
  1162. regs.realebx := $2000 + (flags and $ff); { file open mode }
  1163. end;
  1164. regs.realecx := $20; { file attributes }
  1165. sysrealintr($21,regs);
  1166. {$ifndef RTLLITE}
  1167. if (regs.realflags and carryflag) <> 0 then
  1168. if lo(regs.realeax)=4 then
  1169. if Increase_file_handle_count then
  1170. begin
  1171. { Try again }
  1172. if LFNSupport then
  1173. regs.realeax := $716c {Use LFN Open/Create API}
  1174. else
  1175. if Avoid6c00 then
  1176. regs.realeax := $3d00+(flags and $ff) {For now, map to Open API}
  1177. else
  1178. regs.realeax := $6c00; {Use Extended Open/Create API}
  1179. if byte(regs.realeax shr 8) = $3d then
  1180. begin { Using the older Open or Create API's }
  1181. if (action and $00f0) <> 0 then
  1182. regs.realeax := $3c00; {Map to Create/Replace API}
  1183. regs.realds := tb_segment;
  1184. regs.realedx := tb_offset;
  1185. end
  1186. else
  1187. begin { Using LFN or Extended Open/Create API }
  1188. regs.realedx := action; {action if file does/doesn't exist}
  1189. regs.realds := tb_segment;
  1190. regs.realesi := tb_offset;
  1191. regs.realebx := $2000+(flags and $ff); {file open mode}
  1192. end;
  1193. regs.realecx := $20; {file attributes}
  1194. sysrealintr($21,regs);
  1195. end;
  1196. {$endif RTLLITE}
  1197. if (regs.realflags and carryflag) <> 0 then
  1198. begin
  1199. GetInOutRes(lo(regs.realeax));
  1200. exit;
  1201. end
  1202. else
  1203. begin
  1204. filerec(f).handle:=lo(regs.realeax);
  1205. {$ifndef RTLLITE}
  1206. { for systems that have more then 20 by default ! }
  1207. if lo(regs.realeax)>FileHandleCount then
  1208. FileHandleCount:=lo(regs.realeax);
  1209. {$endif RTLLITE}
  1210. end;
  1211. if lo(regs.realeax)<max_files then
  1212. begin
  1213. {$ifdef SYSTEMDEBUG}
  1214. if openfiles[lo(regs.realeax)] and
  1215. assigned(opennames[lo(regs.realeax)]) then
  1216. begin
  1217. Writeln(stderr,'file ',opennames[lo(regs.realeax)],'(',lo(regs.realeax),') not closed but handle reused!');
  1218. sysfreememsize(opennames[lo(regs.realeax)],strlen(opennames[lo(regs.realeax)])+1);
  1219. end;
  1220. {$endif SYSTEMDEBUG}
  1221. openfiles[lo(regs.realeax)]:=true;
  1222. {$ifdef SYSTEMDEBUG}
  1223. opennames[lo(regs.realeax)] := sysgetmem(strlen(p)+1);
  1224. move(p^,opennames[lo(regs.realeax)]^,strlen(p)+1);
  1225. {$endif SYSTEMDEBUG}
  1226. end;
  1227. { append mode }
  1228. if (flags and $100)<>0 then
  1229. begin
  1230. do_seekend(filerec(f).handle);
  1231. filerec(f).mode:=fmoutput; {fool fmappend}
  1232. end;
  1233. end;
  1234. function do_isdevice(handle:THandle):boolean;
  1235. var
  1236. regs : trealregs;
  1237. begin
  1238. regs.realebx:=handle;
  1239. regs.realeax:=$4400;
  1240. sysrealintr($21,regs);
  1241. do_isdevice:=(regs.realedx and $80)<>0;
  1242. if (regs.realflags and carryflag) <> 0 then
  1243. GetInOutRes(lo(regs.realeax));
  1244. end;
  1245. {*****************************************************************************
  1246. UnTyped File Handling
  1247. *****************************************************************************}
  1248. {$i file.inc}
  1249. {*****************************************************************************
  1250. Typed File Handling
  1251. *****************************************************************************}
  1252. {$i typefile.inc}
  1253. {*****************************************************************************
  1254. Text File Handling
  1255. *****************************************************************************}
  1256. {$DEFINE EOF_CTRLZ}
  1257. {$i text.inc}
  1258. {*****************************************************************************
  1259. Generic Handling
  1260. *****************************************************************************}
  1261. {$ifdef TEST_GENERIC}
  1262. {$i generic.inc}
  1263. {$endif TEST_GENERIC}
  1264. {*****************************************************************************
  1265. Directory Handling
  1266. *****************************************************************************}
  1267. procedure DosDir(func:byte;const s:string);
  1268. var
  1269. buffer : array[0..255] of char;
  1270. regs : trealregs;
  1271. begin
  1272. move(s[1],buffer,length(s));
  1273. buffer[length(s)]:=#0;
  1274. AllowSlash(pchar(@buffer));
  1275. { True DOS does not like backslashes at end
  1276. Win95 DOS accepts this !!
  1277. but "\" and "c:\" should still be kept and accepted hopefully PM }
  1278. if (length(s)>0) and (buffer[length(s)-1]='\') and
  1279. Not ((length(s)=1) or ((length(s)=3) and (s[2]=':'))) then
  1280. buffer[length(s)-1]:=#0;
  1281. syscopytodos(longint(@buffer),length(s)+1);
  1282. regs.realedx:=tb_offset;
  1283. regs.realds:=tb_segment;
  1284. if LFNSupport then
  1285. regs.realeax:=$7100+func
  1286. else
  1287. regs.realeax:=func shl 8;
  1288. sysrealintr($21,regs);
  1289. if (regs.realflags and carryflag) <> 0 then
  1290. GetInOutRes(lo(regs.realeax));
  1291. end;
  1292. procedure mkdir(const s : string);[IOCheck];
  1293. begin
  1294. If (s='') or (InOutRes <> 0) then
  1295. exit;
  1296. DosDir($39,s);
  1297. end;
  1298. procedure rmdir(const s : string);[IOCheck];
  1299. begin
  1300. if (s = '.' ) then
  1301. InOutRes := 16;
  1302. If (s='') or (InOutRes <> 0) then
  1303. exit;
  1304. DosDir($3a,s);
  1305. end;
  1306. procedure chdir(const s : string);[IOCheck];
  1307. var
  1308. regs : trealregs;
  1309. begin
  1310. If (s='') or (InOutRes <> 0) then
  1311. exit;
  1312. { First handle Drive changes }
  1313. if (length(s)>=2) and (s[2]=':') then
  1314. begin
  1315. regs.realedx:=(ord(s[1]) and (not 32))-ord('A');
  1316. regs.realeax:=$0e00;
  1317. sysrealintr($21,regs);
  1318. regs.realeax:=$1900;
  1319. sysrealintr($21,regs);
  1320. if byte(regs.realeax)<>byte(regs.realedx) then
  1321. begin
  1322. Inoutres:=15;
  1323. exit;
  1324. end;
  1325. { DosDir($3b,'c:') give Path not found error on
  1326. pure DOS PM }
  1327. if length(s)=2 then
  1328. exit;
  1329. end;
  1330. { do the normal dos chdir }
  1331. DosDir($3b,s);
  1332. end;
  1333. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  1334. var
  1335. temp : array[0..255] of char;
  1336. i : longint;
  1337. regs : trealregs;
  1338. begin
  1339. regs.realedx:=drivenr;
  1340. regs.realesi:=tb_offset;
  1341. regs.realds:=tb_segment;
  1342. if LFNSupport then
  1343. regs.realeax:=$7147
  1344. else
  1345. regs.realeax:=$4700;
  1346. sysrealintr($21,regs);
  1347. if (regs.realflags and carryflag) <> 0 then
  1348. Begin
  1349. GetInOutRes (lo(regs.realeax));
  1350. Dir := char (DriveNr + 64) + ':\';
  1351. exit;
  1352. end
  1353. else
  1354. syscopyfromdos(longint(@temp),251);
  1355. { conversion to Pascal string including slash conversion }
  1356. i:=0;
  1357. while (temp[i]<>#0) do
  1358. begin
  1359. if temp[i]='/' then
  1360. temp[i]:='\';
  1361. dir[i+4]:=temp[i];
  1362. inc(i);
  1363. end;
  1364. dir[2]:=':';
  1365. dir[3]:='\';
  1366. dir[0]:=char(i+3);
  1367. { upcase the string }
  1368. if not FileNameCaseSensitive then
  1369. dir:=upcase(dir);
  1370. if drivenr<>0 then { Drive was supplied. We know it }
  1371. dir[1]:=char(65+drivenr-1)
  1372. else
  1373. begin
  1374. { We need to get the current drive from DOS function 19H }
  1375. { because the drive was the default, which can be unknown }
  1376. regs.realeax:=$1900;
  1377. sysrealintr($21,regs);
  1378. i:= (regs.realeax and $ff) + ord('A');
  1379. dir[1]:=chr(i);
  1380. end;
  1381. end;
  1382. {*****************************************************************************
  1383. SystemUnit Initialization
  1384. *****************************************************************************}
  1385. function CheckLFN:boolean;
  1386. var
  1387. regs : TRealRegs;
  1388. RootName : pchar;
  1389. begin
  1390. { Check LFN API on drive c:\ }
  1391. RootName:='C:\';
  1392. syscopytodos(longint(RootName),strlen(RootName)+1);
  1393. { Call 'Get Volume Information' ($71A0) }
  1394. regs.realeax:=$71a0;
  1395. regs.reales:=tb_segment;
  1396. regs.realedi:=tb_offset;
  1397. regs.realecx:=32;
  1398. regs.realds:=tb_segment;
  1399. regs.realedx:=tb_offset;
  1400. regs.realflags:=carryflag;
  1401. sysrealintr($21,regs);
  1402. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  1403. CheckLFN:=(regs.realflags and carryflag=0) and (regs.realebx and $4000=$4000);
  1404. end;
  1405. {$ifdef EXCEPTIONS_IN_SYSTEM}
  1406. {$define IN_SYSTEM}
  1407. {$i dpmiexcp.pp}
  1408. {$endif EXCEPTIONS_IN_SYSTEM}
  1409. procedure SysInitStdIO;
  1410. begin
  1411. OpenStdIO(Input,fmInput,StdInputHandle);
  1412. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1413. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1414. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1415. end;
  1416. var
  1417. temp_int : tseginfo;
  1418. Begin
  1419. StackLength := InitialStkLen;
  1420. StackBottom := __stkbottom;
  1421. { To be set if this is a GUI or console application }
  1422. IsConsole := TRUE;
  1423. { To be set if this is a library and not a program }
  1424. IsLibrary := FALSE;
  1425. { save old int 0 and 75 }
  1426. get_pm_interrupt($00,old_int00);
  1427. get_pm_interrupt($75,old_int75);
  1428. temp_int.segment:=get_cs;
  1429. temp_int.offset:=@new_int00;
  1430. set_pm_interrupt($00,temp_int);
  1431. {$ifndef EXCEPTIONS_IN_SYSTEM}
  1432. temp_int.offset:=@new_int75;
  1433. set_pm_interrupt($75,temp_int);
  1434. {$endif EXCEPTIONS_IN_SYSTEM}
  1435. { Setup heap }
  1436. InitHeap;
  1437. SysInitExceptions;
  1438. { Setup stdin, stdout and stderr }
  1439. SysInitStdIO;
  1440. { Setup environment and arguments }
  1441. Setup_Environment;
  1442. Setup_Arguments;
  1443. { Use LFNSupport LFN }
  1444. LFNSupport:=CheckLFN;
  1445. if LFNSupport then
  1446. FileNameCaseSensitive:=true;
  1447. { Reset IO Error }
  1448. InOutRes:=0;
  1449. {$ifdef EXCEPTIONS_IN_SYSTEM}
  1450. InitDPMIExcp;
  1451. InstallDefaultHandlers;
  1452. {$endif EXCEPTIONS_IN_SYSTEM}
  1453. {$ifdef HASVARIANT}
  1454. initvariantmanager;
  1455. {$endif HASVARIANT}
  1456. End.
  1457. {
  1458. $Log$
  1459. Revision 1.39 2004-09-18 11:17:17 hajny
  1460. * handle type changed to thandle in do_isdevice
  1461. Revision 1.38 2004/09/03 19:25:49 olle
  1462. + added maxExitCode to all System.pp
  1463. * constrained error code to be below maxExitCode in RunError et. al.
  1464. Revision 1.37 2004/06/20 09:24:40 peter
  1465. fixed go32v2 compile
  1466. Revision 1.36 2004/06/17 16:16:13 peter
  1467. * New heapmanager that releases memory back to the OS, donated
  1468. by Micha Nelissen
  1469. Revision 1.35 2004/05/16 18:51:20 peter
  1470. * use thandle in do_*
  1471. Revision 1.34 2004/04/22 21:10:56 peter
  1472. * do_read/do_write addr argument changed to pointer
  1473. Revision 1.33 2004/01/25 13:05:08 jonas
  1474. * fixed compilation errors
  1475. Revision 1.32 2004/01/20 23:09:14 hajny
  1476. * ExecuteProcess fixes, ProcessID and ThreadID added
  1477. Revision 1.31 2004/01/10 10:49:24 jonas
  1478. * fixed compilation
  1479. Revision 1.30 2003/12/17 20:40:38 hajny
  1480. * 'conservative' version of the do_open patch by Joe da Silva
  1481. Revision 1.29 2003/12/04 21:42:07 peter
  1482. * register calling updates
  1483. Revision 1.28 2003/11/03 09:42:27 marco
  1484. * Peter's Cardinal<->Longint fixes patch
  1485. Revision 1.27 2003/10/16 15:43:13 peter
  1486. * THandle is platform dependent
  1487. Revision 1.26 2003/10/03 21:46:25 peter
  1488. * stdcall fixes
  1489. Revision 1.25 2003/09/29 18:39:59 hajny
  1490. * append fix applied to GO32v2, OS/2 and EMX
  1491. Revision 1.24 2003/09/27 11:52:35 peter
  1492. * sbrk returns pointer
  1493. Revision 1.23 2002/10/14 19:39:16 peter
  1494. * threads unit added for thread support
  1495. Revision 1.22 2002/10/13 09:28:44 florian
  1496. + call to initvariantmanager inserted
  1497. Revision 1.21 2002/09/07 21:32:08 carl
  1498. - removed unused defines
  1499. Revision 1.20 2002/09/07 16:01:19 peter
  1500. * old logs removed and tabs fixed
  1501. Revision 1.19 2002/07/01 16:29:05 peter
  1502. * sLineBreak changed to normal constant like Kylix
  1503. Revision 1.18 2002/05/05 10:23:54 peter
  1504. * fixed memw and meml array sizes
  1505. Revision 1.17 2002/04/21 15:52:58 carl
  1506. + initialize some global variables
  1507. Revision 1.16 2002/04/12 17:34:05 carl
  1508. + generic stack checking
  1509. Revision 1.15 2002/03/11 19:10:33 peter
  1510. * Regenerated with updated fpcmake
  1511. }