system.pp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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. const
  40. { Default filehandles }
  41. UnusedHandle = -1;
  42. StdInputHandle = 0;
  43. StdOutputHandle = 1;
  44. StdErrorHandle = 2;
  45. FileNameCaseSensitive : boolean = false;
  46. sLineBreak = LineEnding;
  47. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  48. { Default memory segments (Tp7 compatibility) }
  49. seg0040 = $0040;
  50. segA000 = $A000;
  51. segB000 = $B000;
  52. segB800 = $B800;
  53. var
  54. { Mem[] support }
  55. mem : array[0..$7fffffff] of byte absolute $0:$0;
  56. memw : array[0..$7fffffff div sizeof(word)] of word absolute $0:$0;
  57. meml : array[0..$7fffffff div sizeof(longint)] of longint absolute $0:$0;
  58. { C-compatible arguments and environment }
  59. argc : longint;
  60. argv : ppchar;
  61. envp : ppchar;
  62. dos_argv0 : pchar;
  63. {$ifndef RTLLITE}
  64. { System info }
  65. LFNSupport : boolean;
  66. {$ELSE RTLLITE}
  67. const
  68. LFNSupport = false;
  69. {$endif RTLLITE}
  70. type
  71. { Dos Extender info }
  72. p_stub_info = ^t_stub_info;
  73. t_stub_info = packed record
  74. magic : array[0..15] of char;
  75. size : longint;
  76. minstack : longint;
  77. memory_handle : longint;
  78. initial_size : longint;
  79. minkeep : word;
  80. ds_selector : word;
  81. ds_segment : word;
  82. psp_selector : word;
  83. cs_selector : word;
  84. env_size : word;
  85. basename : array[0..7] of char;
  86. argv0 : array [0..15] of char;
  87. dpmi_server : array [0..15] of char;
  88. end;
  89. p_go32_info_block = ^t_go32_info_block;
  90. t_go32_info_block = packed record
  91. size_of_this_structure_in_bytes : longint; {offset 0}
  92. linear_address_of_primary_screen : longint; {offset 4}
  93. linear_address_of_secondary_screen : longint; {offset 8}
  94. linear_address_of_transfer_buffer : longint; {offset 12}
  95. size_of_transfer_buffer : longint; {offset 16}
  96. pid : longint; {offset 20}
  97. master_interrupt_controller_base : byte; {offset 24}
  98. slave_interrupt_controller_base : byte; {offset 25}
  99. selector_for_linear_memory : word; {offset 26}
  100. linear_address_of_stub_info_structure : longint; {offset 28}
  101. linear_address_of_original_psp : longint; {offset 32}
  102. run_mode : word; {offset 36}
  103. run_mode_info : word; {offset 38}
  104. end;
  105. var
  106. stub_info : p_stub_info;
  107. go32_info_block : t_go32_info_block;
  108. {$ifdef SYSTEMDEBUG}
  109. const
  110. accept_sbrk : boolean = true;
  111. {$endif}
  112. {
  113. necessary for objects.pas, should be removed (at least from the interface
  114. to the implementation)
  115. }
  116. type
  117. trealregs=record
  118. realedi,realesi,realebp,realres,
  119. realebx,realedx,realecx,realeax : longint;
  120. realflags,
  121. reales,realds,realfs,realgs,
  122. realip,realcs,realsp,realss : word;
  123. end;
  124. function do_write(h:longint;addr:pointer;len : longint) : longint;
  125. function do_read(h:longint;addr:pointer;len : longint) : longint;
  126. procedure syscopyfromdos(addr : longint; len : longint);
  127. procedure syscopytodos(addr : longint; len : longint);
  128. procedure sysrealintr(intnr : word;var regs : trealregs);
  129. function tb : longint;
  130. implementation
  131. { include system independent routines }
  132. {$I system.inc}
  133. const
  134. carryflag = 1;
  135. type
  136. tseginfo=packed record
  137. offset : pointer;
  138. segment : word;
  139. end;
  140. var
  141. old_int00 : tseginfo;cvar;
  142. old_int75 : tseginfo;cvar;
  143. {$asmmode ATT}
  144. {*****************************************************************************
  145. Go32 Helpers
  146. *****************************************************************************}
  147. function far_strlen(selector : word;linear_address : longint) : longint;assembler;
  148. asm
  149. movl linear_address,%edx
  150. movl %edx,%ecx
  151. movw selector,%gs
  152. .Larg19:
  153. movb %gs:(%edx),%al
  154. testb %al,%al
  155. je .Larg20
  156. incl %edx
  157. jmp .Larg19
  158. .Larg20:
  159. movl %edx,%eax
  160. subl %ecx,%eax
  161. end;
  162. function tb : longint;
  163. begin
  164. tb:=go32_info_block.linear_address_of_transfer_buffer;
  165. end;
  166. function tb_segment : longint;
  167. begin
  168. tb_segment:=go32_info_block.linear_address_of_transfer_buffer shr 4;
  169. end;
  170. function tb_offset : longint;
  171. begin
  172. tb_offset:=go32_info_block.linear_address_of_transfer_buffer and $f;
  173. end;
  174. function tb_size : longint;
  175. begin
  176. tb_size:=go32_info_block.size_of_transfer_buffer;
  177. end;
  178. function dos_selector : word;
  179. begin
  180. dos_selector:=go32_info_block.selector_for_linear_memory;
  181. end;
  182. function get_ds : word;assembler;
  183. asm
  184. movw %ds,%ax
  185. end;
  186. function get_cs : word;assembler;
  187. asm
  188. movw %cs,%ax
  189. end;
  190. procedure sysseg_move(sseg : word;source : longint;dseg : word;dest : longint;count : longint);
  191. begin
  192. if count=0 then
  193. exit;
  194. if (sseg<>dseg) or ((sseg=dseg) and (source>dest)) then
  195. asm
  196. pushl %esi
  197. pushl %edi
  198. pushw %es
  199. pushw %ds
  200. cld
  201. movl count,%ecx
  202. movl source,%esi
  203. movl dest,%edi
  204. movw dseg,%ax
  205. movw %ax,%es
  206. movw sseg,%ax
  207. movw %ax,%ds
  208. movl %ecx,%eax
  209. shrl $2,%ecx
  210. rep
  211. movsl
  212. movl %eax,%ecx
  213. andl $3,%ecx
  214. rep
  215. movsb
  216. popw %ds
  217. popw %es
  218. popl %edi
  219. popl %esi
  220. end
  221. else if (source<dest) then
  222. { copy backward for overlapping }
  223. asm
  224. pushl %esi
  225. pushl %edi
  226. pushw %es
  227. pushw %ds
  228. std
  229. movl count,%ecx
  230. movl source,%esi
  231. movl dest,%edi
  232. movw dseg,%ax
  233. movw %ax,%es
  234. movw sseg,%ax
  235. movw %ax,%ds
  236. addl %ecx,%esi
  237. addl %ecx,%edi
  238. movl %ecx,%eax
  239. andl $3,%ecx
  240. orl %ecx,%ecx
  241. jz .LSEG_MOVE1
  242. { calculate esi and edi}
  243. decl %esi
  244. decl %edi
  245. rep
  246. movsb
  247. incl %esi
  248. incl %edi
  249. .LSEG_MOVE1:
  250. subl $4,%esi
  251. subl $4,%edi
  252. movl %eax,%ecx
  253. shrl $2,%ecx
  254. rep
  255. movsl
  256. cld
  257. popw %ds
  258. popw %es
  259. popl %edi
  260. popl %esi
  261. end;
  262. end;
  263. var
  264. _args : ppchar;external name '_args';
  265. procedure setup_arguments;
  266. type
  267. arrayword = array [0..255] of word;
  268. var
  269. psp : word;
  270. proxy_s : string[50];
  271. proxy_argc,proxy_seg,proxy_ofs,lin : longint;
  272. rm_argv : ^arrayword;
  273. argv0len : longint;
  274. useproxy : boolean;
  275. hp : ppchar;
  276. doscmd : string[129]; { Dos commandline copied from PSP, max is 128 chars +1 for terminating zero }
  277. arglen,
  278. count : longint;
  279. argstart,
  280. pc,arg : pchar;
  281. quote : char;
  282. argvlen : longint;
  283. function atohex(s : pchar) : longint;
  284. var
  285. rv : longint;
  286. v : byte;
  287. begin
  288. rv:=0;
  289. while (s^<>#0) do
  290. begin
  291. v:=byte(s^)-byte('0');
  292. if (v > 9) then
  293. dec(v,7);
  294. v:=v and 15; { in case it's lower case }
  295. rv:=(rv shl 4) or v;
  296. inc(longint(s));
  297. end;
  298. atohex:=rv;
  299. end;
  300. procedure allocarg(idx,len:longint);
  301. begin
  302. if idx>=argvlen then
  303. begin
  304. argvlen:=(idx+8) and (not 7);
  305. sysreallocmem(argv,argvlen*sizeof(pointer));
  306. end;
  307. { use realloc to reuse already existing memory }
  308. if len<>0 then
  309. sysreallocmem(argv[idx],len+1);
  310. end;
  311. begin
  312. count:=0;
  313. argc:=1;
  314. argv:=nil;
  315. argvlen:=0;
  316. { load commandline from psp }
  317. psp:=stub_info^.psp_selector;
  318. sysseg_move(psp, 128, get_ds, longint(@doscmd), 128);
  319. doscmd[length(doscmd)+1]:=#0;
  320. {$IfDef SYSTEM_DEBUG_STARTUP}
  321. Writeln(stderr,'Dos command line is #',doscmd,'# size = ',length(doscmd));
  322. {$EndIf }
  323. { create argv[0] }
  324. argv0len:=strlen(dos_argv0);
  325. allocarg(count,argv0len);
  326. move(dos_argv0^,argv[count]^,argv0len);
  327. inc(count);
  328. { setup cmdline variable }
  329. cmdline:=Getmem(argv0len+length(doscmd)+2);
  330. move(dos_argv0^,cmdline^,argv0len);
  331. cmdline[argv0len]:=' ';
  332. inc(argv0len);
  333. move(doscmd[1],cmdline[argv0len],length(doscmd));
  334. cmdline[argv0len+length(doscmd)+1]:=#0;
  335. { parse dos commandline }
  336. pc:=@doscmd[1];
  337. while pc^<>#0 do
  338. begin
  339. { skip leading spaces }
  340. while pc^ in [#1..#32] do
  341. inc(pc);
  342. if pc^=#0 then
  343. break;
  344. { calc argument length }
  345. quote:=' ';
  346. argstart:=pc;
  347. arglen:=0;
  348. while (pc^<>#0) do
  349. begin
  350. case pc^ of
  351. #1..#32 :
  352. begin
  353. if quote<>' ' then
  354. inc(arglen)
  355. else
  356. break;
  357. end;
  358. '"' :
  359. begin
  360. if quote<>'''' then
  361. begin
  362. if pchar(pc+1)^<>'"' then
  363. begin
  364. if quote='"' then
  365. quote:=' '
  366. else
  367. quote:='"';
  368. end
  369. else
  370. inc(pc);
  371. end
  372. else
  373. inc(arglen);
  374. end;
  375. '''' :
  376. begin
  377. if quote<>'"' then
  378. begin
  379. if pchar(pc+1)^<>'''' then
  380. begin
  381. if quote='''' then
  382. quote:=' '
  383. else
  384. quote:='''';
  385. end
  386. else
  387. inc(pc);
  388. end
  389. else
  390. inc(arglen);
  391. end;
  392. else
  393. inc(arglen);
  394. end;
  395. inc(pc);
  396. end;
  397. { copy argument }
  398. allocarg(count,arglen);
  399. quote:=' ';
  400. pc:=argstart;
  401. arg:=argv[count];
  402. while (pc^<>#0) do
  403. begin
  404. case pc^ of
  405. #1..#32 :
  406. begin
  407. if quote<>' ' then
  408. begin
  409. arg^:=pc^;
  410. inc(arg);
  411. end
  412. else
  413. break;
  414. end;
  415. '"' :
  416. begin
  417. if quote<>'''' then
  418. begin
  419. if pchar(pc+1)^<>'"' then
  420. begin
  421. if quote='"' then
  422. quote:=' '
  423. else
  424. quote:='"';
  425. end
  426. else
  427. inc(pc);
  428. end
  429. else
  430. begin
  431. arg^:=pc^;
  432. inc(arg);
  433. end;
  434. end;
  435. '''' :
  436. begin
  437. if quote<>'"' then
  438. begin
  439. if pchar(pc+1)^<>'''' then
  440. begin
  441. if quote='''' then
  442. quote:=' '
  443. else
  444. quote:='''';
  445. end
  446. else
  447. inc(pc);
  448. end
  449. else
  450. begin
  451. arg^:=pc^;
  452. inc(arg);
  453. end;
  454. end;
  455. else
  456. begin
  457. arg^:=pc^;
  458. inc(arg);
  459. end;
  460. end;
  461. inc(pc);
  462. end;
  463. arg^:=#0;
  464. {$IfDef SYSTEM_DEBUG_STARTUP}
  465. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  466. {$EndIf SYSTEM_DEBUG_STARTUP}
  467. inc(count);
  468. end;
  469. argc:=count;
  470. { check for !proxy for long commandlines passed using environment }
  471. hp:=envp;
  472. useproxy:=false;
  473. while assigned(hp^) do
  474. begin
  475. if (hp^[0]=' ') then
  476. begin
  477. proxy_s:=strpas(hp^);
  478. if Copy(proxy_s,1,7)=' !proxy' then
  479. begin
  480. proxy_s[13]:=#0;
  481. proxy_s[18]:=#0;
  482. proxy_s[23]:=#0;
  483. argv[2]:=@proxy_s[9];
  484. argv[3]:=@proxy_s[14];
  485. argv[4]:=@proxy_s[19];
  486. useproxy:=true;
  487. break;
  488. end;
  489. end;
  490. inc(hp);
  491. end;
  492. { check for !proxy for long commandlines passed using commandline }
  493. if (not useproxy) and
  494. (argc > 1) and (far_strlen(get_ds,longint(argv[1])) = 6) then
  495. begin
  496. move(argv[1]^,proxy_s[1],6);
  497. proxy_s[0] := #6;
  498. if (proxy_s = '!proxy') then
  499. useproxy:=true;
  500. end;
  501. { use proxy when found }
  502. if useproxy then
  503. begin
  504. proxy_argc:=atohex(argv[2]);
  505. proxy_seg:=atohex(argv[3]);
  506. proxy_ofs:=atohex(argv[4]);
  507. {$IfDef SYSTEM_DEBUG_STARTUP}
  508. Writeln(stderr,'proxy command line found');
  509. writeln(stderr,'argc: ',proxy_argc,' seg: ',proxy_seg,' ofs: ',proxy_ofs);
  510. {$EndIf SYSTEM_DEBUG_STARTUP}
  511. rm_argv:=SysGetmem(proxy_argc*sizeof(word));
  512. sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
  513. for count:=0 to proxy_argc - 1 do
  514. begin
  515. lin:=proxy_seg*16+rm_argv^[count];
  516. arglen:=far_strlen(dos_selector,lin);
  517. allocarg(count,arglen);
  518. sysseg_move(dos_selector,lin,get_ds,longint(argv[count]),arglen+1);
  519. {$IfDef SYSTEM_DEBUG_STARTUP}
  520. Writeln(stderr,'arg ',count,' #',rm_argv^[count],'#',arglen,'#',argv[count],'#');
  521. {$EndIf SYSTEM_DEBUG_STARTUP}
  522. end;
  523. SysFreemem(rm_argv);
  524. argc:=proxy_argc;
  525. end;
  526. { create an nil entry }
  527. allocarg(argc,0);
  528. { free unused memory }
  529. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  530. _args:=argv;
  531. end;
  532. function strcopy(dest,source : pchar) : pchar;assembler;
  533. var
  534. saveeax,saveesi,saveedi : longint;
  535. asm
  536. movl %edi,saveedi
  537. movl %esi,saveesi
  538. {$ifdef REGCALL}
  539. movl %eax,saveeax
  540. movl %edx,%edi
  541. {$else}
  542. movl source,%edi
  543. {$endif}
  544. testl %edi,%edi
  545. jz .LStrCopyDone
  546. leal 3(%edi),%ecx
  547. andl $-4,%ecx
  548. movl %edi,%esi
  549. subl %edi,%ecx
  550. {$ifdef REGCALL}
  551. movl %eax,%edi
  552. {$else}
  553. movl dest,%edi
  554. {$endif}
  555. jz .LStrCopyAligned
  556. .LStrCopyAlignLoop:
  557. movb (%esi),%al
  558. incl %edi
  559. incl %esi
  560. testb %al,%al
  561. movb %al,-1(%edi)
  562. jz .LStrCopyDone
  563. decl %ecx
  564. jnz .LStrCopyAlignLoop
  565. .balign 16
  566. .LStrCopyAligned:
  567. movl (%esi),%eax
  568. movl %eax,%edx
  569. leal 0x0fefefeff(%eax),%ecx
  570. notl %edx
  571. addl $4,%esi
  572. andl %edx,%ecx
  573. andl $0x080808080,%ecx
  574. jnz .LStrCopyEndFound
  575. movl %eax,(%edi)
  576. addl $4,%edi
  577. jmp .LStrCopyAligned
  578. .LStrCopyEndFound:
  579. testl $0x0ff,%eax
  580. jz .LStrCopyByte
  581. testl $0x0ff00,%eax
  582. jz .LStrCopyWord
  583. testl $0x0ff0000,%eax
  584. jz .LStrCopy3Bytes
  585. movl %eax,(%edi)
  586. jmp .LStrCopyDone
  587. .LStrCopy3Bytes:
  588. xorb %dl,%dl
  589. movw %ax,(%edi)
  590. movb %dl,2(%edi)
  591. jmp .LStrCopyDone
  592. .LStrCopyWord:
  593. movw %ax,(%edi)
  594. jmp .LStrCopyDone
  595. .LStrCopyByte:
  596. movb %al,(%edi)
  597. .LStrCopyDone:
  598. {$ifdef REGCALL}
  599. movl saveeax,%eax
  600. {$else}
  601. movl dest,%eax
  602. {$endif}
  603. movl saveedi,%edi
  604. movl saveesi,%esi
  605. end;
  606. var
  607. __stubinfo : p_stub_info;external name '__stubinfo';
  608. ___dos_argv0 : pchar;external name '___dos_argv0';
  609. procedure setup_environment;
  610. var env_selector : word;
  611. env_count : longint;
  612. dos_env,cp : pchar;
  613. begin
  614. stub_info:=__stubinfo;
  615. dos_env := sysgetmem(stub_info^.env_size);
  616. env_count:=0;
  617. sysseg_move(stub_info^.psp_selector,$2c, get_ds, longint(@env_selector), 2);
  618. sysseg_move(env_selector, 0, get_ds, longint(dos_env), stub_info^.env_size);
  619. cp:=dos_env;
  620. while cp ^ <> #0 do
  621. begin
  622. inc(env_count);
  623. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  624. inc(longint(cp)); { skip to next character }
  625. end;
  626. envp := sysgetmem((env_count+1) * sizeof(pchar));
  627. if (envp = nil) then exit;
  628. cp:=dos_env;
  629. env_count:=0;
  630. while cp^ <> #0 do
  631. begin
  632. envp[env_count] := sysgetmem(strlen(cp)+1);
  633. strcopy(envp[env_count], cp);
  634. {$IfDef SYSTEM_DEBUG_STARTUP}
  635. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  636. {$EndIf SYSTEM_DEBUG_STARTUP}
  637. inc(env_count);
  638. while (cp^ <> #0) do
  639. inc(longint(cp)); { skip to NUL }
  640. inc(longint(cp)); { skip to next character }
  641. end;
  642. envp[env_count]:=nil;
  643. longint(cp):=longint(cp)+3;
  644. dos_argv0 := sysgetmem(strlen(cp)+1);
  645. if (dos_argv0 = nil) then halt;
  646. strcopy(dos_argv0, cp);
  647. { update ___dos_argv0 also }
  648. ___dos_argv0:=dos_argv0
  649. end;
  650. procedure syscopytodos(addr : longint; len : longint);
  651. begin
  652. if len > tb_size then
  653. HandleError(217);
  654. sysseg_move(get_ds,addr,dos_selector,tb,len);
  655. end;
  656. procedure syscopyfromdos(addr : longint; len : longint);
  657. begin
  658. if len > tb_size then
  659. HandleError(217);
  660. sysseg_move(dos_selector,tb,get_ds,addr,len);
  661. end;
  662. procedure sysrealintr(intnr : word;var regs : trealregs);
  663. begin
  664. regs.realsp:=0;
  665. regs.realss:=0;
  666. asm
  667. pushl %ebx
  668. pushl %edi
  669. movw intnr,%bx
  670. xorl %ecx,%ecx
  671. movl regs,%edi
  672. movw $0x300,%ax
  673. int $0x31
  674. popl %edi
  675. popl %ebx
  676. end;
  677. end;
  678. procedure set_pm_interrupt(vector : byte;const intaddr : tseginfo);
  679. begin
  680. asm
  681. pushl %ebx
  682. movl intaddr,%eax
  683. movl (%eax),%edx
  684. movw 4(%eax),%cx
  685. movl $0x205,%eax
  686. movb vector,%bl
  687. int $0x31
  688. popl %ebx
  689. end;
  690. end;
  691. procedure get_pm_interrupt(vector : byte;var intaddr : tseginfo);
  692. begin
  693. asm
  694. pushl %ebx
  695. movb vector,%bl
  696. movl $0x204,%eax
  697. int $0x31
  698. movl intaddr,%eax
  699. movl %edx,(%eax)
  700. movw %cx,4(%eax)
  701. popl %ebx
  702. end;
  703. end;
  704. procedure getinoutres(def : word);
  705. var
  706. regs : trealregs;
  707. begin
  708. regs.realeax:=$5900;
  709. regs.realebx:=$0;
  710. sysrealintr($21,regs);
  711. InOutRes:=lo(regs.realeax);
  712. case InOutRes of
  713. 19 : InOutRes:=150;
  714. 21 : InOutRes:=152;
  715. 32 : InOutRes:=5;
  716. end;
  717. if InOutRes=0 then
  718. InOutRes:=Def;
  719. end;
  720. { Keep Track of open files }
  721. const
  722. max_files = 50;
  723. var
  724. openfiles : array [0..max_files-1] of boolean;
  725. {$ifdef SYSTEMDEBUG}
  726. opennames : array [0..max_files-1] of pchar;
  727. const
  728. free_closed_names : boolean = true;
  729. {$endif SYSTEMDEBUG}
  730. {*****************************************************************************
  731. System Dependent Exit code
  732. *****************************************************************************}
  733. procedure ___exit(exitcode:longint);cdecl;external name '___exit';
  734. procedure do_close(handle : longint);forward;
  735. Procedure system_exit;
  736. var
  737. h : byte;
  738. begin
  739. for h:=0 to max_files-1 do
  740. if openfiles[h] then
  741. begin
  742. {$ifdef SYSTEMDEBUG}
  743. writeln(stderr,'file ',opennames[h],' not closed at exit');
  744. {$endif SYSTEMDEBUG}
  745. if h>=5 then
  746. do_close(h);
  747. end;
  748. { halt is not allways called !! }
  749. { not on normal exit !! PM }
  750. set_pm_interrupt($00,old_int00);
  751. {$ifndef EXCEPTIONS_IN_SYSTEM}
  752. set_pm_interrupt($75,old_int75);
  753. {$endif EXCEPTIONS_IN_SYSTEM}
  754. ___exit(exitcode);
  755. end;
  756. procedure new_int00;
  757. begin
  758. HandleError(200);
  759. end;
  760. {$ifndef EXCEPTIONS_IN_SYSTEM}
  761. procedure new_int75;
  762. begin
  763. asm
  764. xorl %eax,%eax
  765. outb %al,$0x0f0
  766. movb $0x20,%al
  767. outb %al,$0x0a0
  768. outb %al,$0x020
  769. end;
  770. HandleError(200);
  771. end;
  772. {$endif EXCEPTIONS_IN_SYSTEM}
  773. var
  774. __stkbottom : pointer;external name '__stkbottom';
  775. {*****************************************************************************
  776. ParamStr/Randomize
  777. *****************************************************************************}
  778. function paramcount : longint;
  779. begin
  780. paramcount := argc - 1;
  781. end;
  782. function paramstr(l : longint) : string;
  783. begin
  784. if (l>=0) and (l+1<=argc) then
  785. paramstr:=strpas(argv[l])
  786. else
  787. paramstr:='';
  788. end;
  789. procedure randomize;
  790. var
  791. hl : longint;
  792. regs : trealregs;
  793. begin
  794. regs.realeax:=$2c00;
  795. sysrealintr($21,regs);
  796. hl:=lo(regs.realedx);
  797. randseed:=hl*$10000+ lo(regs.realecx);
  798. end;
  799. {*****************************************************************************
  800. Heap Management
  801. *****************************************************************************}
  802. var
  803. int_heap : longint;external name 'HEAP';
  804. int_heapsize : longint;external name 'HEAPSIZE';
  805. function getheapstart:pointer;
  806. begin
  807. getheapstart:=@int_heap;
  808. end;
  809. function getheapsize:longint;
  810. begin
  811. getheapsize:=int_heapsize;
  812. end;
  813. function ___sbrk(size:longint):longint;cdecl;external name '___sbrk';
  814. function Sbrk(size : longint):pointer;assembler;
  815. asm
  816. {$ifdef SYSTEMDEBUG}
  817. cmpb $1,accept_sbrk
  818. je .Lsbrk
  819. movl $0,%eax
  820. jmp .Lsbrk_fail
  821. .Lsbrk:
  822. {$endif}
  823. movl size,%eax
  824. pushl %eax
  825. call ___sbrk
  826. addl $4,%esp
  827. {$ifdef SYSTEMDEBUG}
  828. .Lsbrk_fail:
  829. {$endif}
  830. end;
  831. {*****************************************************************************
  832. OS Memory allocation / deallocation
  833. ****************************************************************************}
  834. function SysOSAlloc(size: ptrint): pointer;
  835. begin
  836. result := sbrk(size);
  837. end;
  838. {$define HAS_SYSOSFREE}
  839. procedure SysOSFree(p: pointer; size: ptrint);
  840. begin
  841. fpmunmap(p, size);
  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:longint):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.36 2004-06-17 16:16:13 peter
  1460. * New heapmanager that releases memory back to the OS, donated
  1461. by Micha Nelissen
  1462. Revision 1.35 2004/05/16 18:51:20 peter
  1463. * use thandle in do_*
  1464. Revision 1.34 2004/04/22 21:10:56 peter
  1465. * do_read/do_write addr argument changed to pointer
  1466. Revision 1.33 2004/01/25 13:05:08 jonas
  1467. * fixed compilation errors
  1468. Revision 1.32 2004/01/20 23:09:14 hajny
  1469. * ExecuteProcess fixes, ProcessID and ThreadID added
  1470. Revision 1.31 2004/01/10 10:49:24 jonas
  1471. * fixed compilation
  1472. Revision 1.30 2003/12/17 20:40:38 hajny
  1473. * 'conservative' version of the do_open patch by Joe da Silva
  1474. Revision 1.29 2003/12/04 21:42:07 peter
  1475. * register calling updates
  1476. Revision 1.28 2003/11/03 09:42:27 marco
  1477. * Peter's Cardinal<->Longint fixes patch
  1478. Revision 1.27 2003/10/16 15:43:13 peter
  1479. * THandle is platform dependent
  1480. Revision 1.26 2003/10/03 21:46:25 peter
  1481. * stdcall fixes
  1482. Revision 1.25 2003/09/29 18:39:59 hajny
  1483. * append fix applied to GO32v2, OS/2 and EMX
  1484. Revision 1.24 2003/09/27 11:52:35 peter
  1485. * sbrk returns pointer
  1486. Revision 1.23 2002/10/14 19:39:16 peter
  1487. * threads unit added for thread support
  1488. Revision 1.22 2002/10/13 09:28:44 florian
  1489. + call to initvariantmanager inserted
  1490. Revision 1.21 2002/09/07 21:32:08 carl
  1491. - removed unused defines
  1492. Revision 1.20 2002/09/07 16:01:19 peter
  1493. * old logs removed and tabs fixed
  1494. Revision 1.19 2002/07/01 16:29:05 peter
  1495. * sLineBreak changed to normal constant like Kylix
  1496. Revision 1.18 2002/05/05 10:23:54 peter
  1497. * fixed memw and meml array sizes
  1498. Revision 1.17 2002/04/21 15:52:58 carl
  1499. + initialize some global variables
  1500. Revision 1.16 2002/04/12 17:34:05 carl
  1501. + generic stack checking
  1502. Revision 1.15 2002/03/11 19:10:33 peter
  1503. * Regenerated with updated fpcmake
  1504. }