sysutils.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for Go32v2
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$inline on}
  13. unit sysutils;
  14. interface
  15. {$MODE objfpc}
  16. {$MODESWITCH out}
  17. { force ansistrings }
  18. {$H+}
  19. uses
  20. go32,dos;
  21. {$DEFINE HAS_SLEEP}
  22. { Include platform independent interface part }
  23. {$i sysutilh.inc}
  24. implementation
  25. uses
  26. sysconst;
  27. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  28. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  29. { Include platform independent implementation part }
  30. {$i sysutils.inc}
  31. {****************************************************************************
  32. File Functions
  33. ****************************************************************************}
  34. { some internal constants }
  35. const
  36. ofRead = $0000; { Open for reading }
  37. ofWrite = $0001; { Open for writing }
  38. ofReadWrite = $0002; { Open for reading/writing }
  39. faFail = $0000; { Fail if file does not exist }
  40. faCreate = $0010; { Create if file does not exist }
  41. faOpen = $0001; { Open if file exists }
  42. faOpenReplace = $0002; { Clear if file exists }
  43. Type
  44. PSearchrec = ^Searchrec;
  45. { converts S to a pchar and copies it to the transfer-buffer. }
  46. procedure StringToTB(const S: string);
  47. var
  48. P: pchar;
  49. Len: integer;
  50. begin
  51. Len := Length(S) + 1;
  52. P := StrPCopy(StrAlloc(Len), S);
  53. SysCopyToDos(longint(P), Len);
  54. StrDispose(P);
  55. end ;
  56. { Native OpenFile function.
  57. if return value <> 0 call failed. }
  58. function OpenFile(const FileName: string; var Handle: longint; Mode, Action: word): longint;
  59. var
  60. Regs: registers;
  61. begin
  62. result := 0;
  63. Handle := UnusedHandle;
  64. StringToTB(FileName);
  65. if LFNSupport then
  66. begin
  67. Regs.Eax := $716c; { Use LFN Open/Create API }
  68. Regs.Edx := Action; { Action if file does/doesn't exist }
  69. Regs.Esi := tb_offset;
  70. Regs.Ebx := $2000 + (Mode and $ff); { File open mode }
  71. end
  72. else
  73. begin
  74. if (Action and $00f0) <> 0 then
  75. Regs.Eax := $3c00 { Map to Create/Replace API }
  76. else
  77. Regs.Eax := $3d00 + (Mode and $ff); { Map to Open_Existing API }
  78. Regs.Edx := tb_offset;
  79. end;
  80. Regs.Ds := tb_segment;
  81. Regs.Ecx := $20; { Attributes }
  82. RealIntr($21, Regs);
  83. if (Regs.Flags and CarryFlag) <> 0 then
  84. result := Regs.Ax
  85. else
  86. Handle := Regs.Ax;
  87. end;
  88. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  89. var
  90. e: integer;
  91. Begin
  92. e := OpenFile(FileName, result, Mode, faOpen);
  93. if e <> 0 then
  94. result := -1;
  95. end;
  96. Function FileCreate (Const FileName : String) : Longint;
  97. var
  98. e: integer;
  99. begin
  100. e := OpenFile(FileName, result, ofReadWrite, faCreate or faOpenReplace);
  101. if e <> 0 then
  102. result := -1;
  103. end;
  104. Function FileCreate (Const FileName : String; ShareMode:longint; Rights : longint) : Longint;
  105. begin
  106. FileCreate:=FileCreate(FileName);
  107. end;
  108. Function FileCreate (Const FileName : String; Rights:longint) : Longint;
  109. begin
  110. FileCreate:=FileCreate(FileName);
  111. end;
  112. Function FileRead (Handle : Longint; Out Buffer; Count : longint) : Longint;
  113. var
  114. regs : registers;
  115. size,
  116. readsize : longint;
  117. begin
  118. readsize:=0;
  119. while Count > 0 do
  120. begin
  121. if Count>tb_size then
  122. size:=tb_size
  123. else
  124. size:=Count;
  125. regs.realecx:=size;
  126. regs.realedx:=tb_offset;
  127. regs.realds:=tb_segment;
  128. regs.realebx:=Handle;
  129. regs.realeax:=$3f00;
  130. RealIntr($21,regs);
  131. if (regs.realflags and carryflag) <> 0 then
  132. begin
  133. Result:=-1;
  134. exit;
  135. end;
  136. syscopyfromdos(Longint(dword(@Buffer)+readsize),lo(regs.realeax));
  137. inc(readsize,lo(regs.realeax));
  138. dec(Count,lo(regs.realeax));
  139. { stop when not the specified size is read }
  140. if lo(regs.realeax)<size then
  141. break;
  142. end;
  143. Result:=readsize;
  144. end;
  145. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  146. var
  147. regs : registers;
  148. size,
  149. writesize : longint;
  150. begin
  151. writesize:=0;
  152. while Count > 0 do
  153. begin
  154. if Count>tb_size then
  155. size:=tb_size
  156. else
  157. size:=Count;
  158. syscopytodos(Longint(dword(@Buffer)+writesize),size);
  159. regs.realecx:=size;
  160. regs.realedx:=tb_offset;
  161. regs.realds:=tb_segment;
  162. regs.realebx:=Handle;
  163. regs.realeax:=$4000;
  164. RealIntr($21,regs);
  165. if (regs.realflags and carryflag) <> 0 then
  166. begin
  167. Result:=-1;
  168. exit;
  169. end;
  170. inc(writesize,lo(regs.realeax));
  171. dec(Count,lo(regs.realeax));
  172. { stop when not the specified size is written }
  173. if lo(regs.realeax)<size then
  174. break;
  175. end;
  176. Result:=WriteSize;
  177. end;
  178. Function FileSeek (Handle, FOffset, Origin : Longint) : Longint;
  179. var
  180. Regs: registers;
  181. begin
  182. Regs.Eax := $4200;
  183. Regs.Al := Origin;
  184. Regs.Edx := Lo(FOffset);
  185. Regs.Ecx := Hi(FOffset);
  186. Regs.Ebx := Handle;
  187. RealIntr($21, Regs);
  188. if Regs.Flags and CarryFlag <> 0 then
  189. result := -1
  190. else begin
  191. LongRec(result).Lo := Regs.Ax;
  192. LongRec(result).Hi := Regs.Dx;
  193. end ;
  194. end;
  195. Function FileSeek (Handle : Longint; FOffset: Int64; Origin: Integer) : Int64;
  196. begin
  197. {$warning need to add 64bit call }
  198. FileSeek:=FileSeek(Handle,Longint(FOffset),Longint(Origin));
  199. end;
  200. Procedure FileClose (Handle : Longint);
  201. var
  202. Regs: registers;
  203. begin
  204. if Handle<=4 then
  205. exit;
  206. Regs.Eax := $3e00;
  207. Regs.Ebx := Handle;
  208. RealIntr($21, Regs);
  209. end;
  210. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  211. var
  212. regs : trealregs;
  213. begin
  214. if Size > high (longint) then
  215. FileTruncate := false
  216. else
  217. begin
  218. FileSeek(Handle,Size,0);
  219. Regs.realecx := 0;
  220. Regs.realedx := tb_offset;
  221. Regs.ds := tb_segment;
  222. Regs.ebx := Handle;
  223. Regs.eax:=$4000;
  224. RealIntr($21, Regs);
  225. FileTruncate:=(regs.realflags and carryflag)=0;
  226. end;
  227. end;
  228. Function FileAge (Const FileName : String): Longint;
  229. var Handle: longint;
  230. begin
  231. Handle := FileOpen(FileName, 0);
  232. if Handle <> -1 then
  233. begin
  234. result := FileGetDate(Handle);
  235. FileClose(Handle);
  236. end
  237. else
  238. result := -1;
  239. end;
  240. Function FileExists (Const FileName : String) : Boolean;
  241. Var
  242. Sr : Searchrec;
  243. begin
  244. DOS.FindFirst(FileName,$3f,sr);
  245. if DosError = 0 then
  246. begin
  247. { No volumeid,directory }
  248. Result:=(sr.attr and $18)=0;
  249. Dos.FindClose(sr);
  250. end
  251. else
  252. Result:=false;
  253. end;
  254. Function DirectoryExists (Const Directory : String) : Boolean;
  255. Var
  256. Dir : String;
  257. drive : byte;
  258. StoredIORes : longint;
  259. begin
  260. Dir:=Directory;
  261. if (length(dir)=2) and (dir[2]=':') and
  262. ((dir[1] in ['A'..'Z']) or (dir[1] in ['a'..'z'])) then
  263. begin
  264. { We want to test GetCurDir }
  265. if dir[1] in ['A'..'Z'] then
  266. drive:=ord(dir[1])-ord('A')+1
  267. else
  268. drive:=ord(dir[1])-ord('a')+1;
  269. {$undef OPT_I}
  270. {$ifopt I+}
  271. {$define OPT_I}
  272. {$endif}
  273. {$I-}
  274. StoredIORes:=InOutRes;
  275. InOutRes:=0;
  276. GetDir(drive,dir);
  277. if InOutRes <> 0 then
  278. begin
  279. InOutRes:=StoredIORes;
  280. result:=false;
  281. exit;
  282. end;
  283. end;
  284. {$ifdef OPT_I}
  285. {$I+}
  286. {$endif}
  287. if (Length (Dir) > 1) and
  288. (Dir [Length (Dir)] in AllowDirectorySeparators) and
  289. (* Do not remove '\' after ':' (root directory of a drive)
  290. or in '\\' (invalid path, possibly broken UNC path). *)
  291. not (Dir [Length (Dir) - 1] in (AllowDriveSeparators + AllowDirectorySeparators)) then
  292. dir:=copy(dir,1,length(dir)-1);
  293. Result := FileGetAttr (Dir) and faDirectory = faDirectory;
  294. end;
  295. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  296. Var Sr : PSearchrec;
  297. begin
  298. //!! Sr := New(PSearchRec);
  299. getmem(sr,sizeof(searchrec));
  300. Rslt.FindHandle := longint(Sr);
  301. DOS.FindFirst(Path, Attr, Sr^);
  302. result := -DosError;
  303. if result = 0 then
  304. begin
  305. Rslt.Time := Sr^.Time;
  306. Rslt.Size := Sr^.Size;
  307. Rslt.Attr := Sr^.Attr;
  308. Rslt.ExcludeAttr := 0;
  309. Rslt.Name := Sr^.Name;
  310. end ;
  311. end;
  312. Function FindNext (Var Rslt : TSearchRec) : Longint;
  313. var
  314. Sr: PSearchRec;
  315. begin
  316. Sr := PSearchRec(Rslt.FindHandle);
  317. if Sr <> nil then
  318. begin
  319. DOS.FindNext(Sr^);
  320. result := -DosError;
  321. if result = 0 then
  322. begin
  323. Rslt.Time := Sr^.Time;
  324. Rslt.Size := Sr^.Size;
  325. Rslt.Attr := Sr^.Attr;
  326. Rslt.ExcludeAttr := 0;
  327. Rslt.Name := Sr^.Name;
  328. end;
  329. end;
  330. end;
  331. Procedure FindClose (Var F : TSearchrec);
  332. var
  333. Sr: PSearchRec;
  334. begin
  335. Sr := PSearchRec(F.FindHandle);
  336. if Sr <> nil then
  337. begin
  338. //!! Dispose(Sr);
  339. // This call is non dummy if LFNSupport is true PM
  340. DOS.FindClose(SR^);
  341. freemem(sr,sizeof(searchrec));
  342. end;
  343. F.FindHandle := 0;
  344. end;
  345. Function FileGetDate (Handle : Longint) : Longint;
  346. var
  347. Regs: registers;
  348. begin
  349. //!! for win95 an alternative function is available.
  350. Regs.Ebx := Handle;
  351. Regs.Eax := $5700;
  352. RealIntr($21, Regs);
  353. if Regs.Flags and CarryFlag <> 0 then
  354. result := -1
  355. else
  356. begin
  357. LongRec(result).Lo := Regs.cx;
  358. LongRec(result).Hi := Regs.dx;
  359. end ;
  360. end;
  361. Function FileSetDate (Handle, Age : Longint) : Longint;
  362. var
  363. Regs: registers;
  364. begin
  365. Regs.Ebx := Handle;
  366. Regs.Eax := $5701;
  367. Regs.Ecx := Lo(Age);
  368. Regs.Edx := Hi(Age);
  369. RealIntr($21, Regs);
  370. if Regs.Flags and CarryFlag <> 0 then
  371. result := -Regs.Ax
  372. else
  373. result := 0;
  374. end;
  375. Function FileGetAttr (Const FileName : String) : Longint;
  376. var
  377. Regs: registers;
  378. begin
  379. StringToTB(FileName);
  380. Regs.Edx := tb_offset;
  381. Regs.Ds := tb_segment;
  382. if LFNSupport then
  383. begin
  384. Regs.Ax := $7143;
  385. Regs.Bx := 0;
  386. end
  387. else
  388. Regs.Ax := $4300;
  389. RealIntr($21, Regs);
  390. if Regs.Flags and CarryFlag <> 0 then
  391. result := -1
  392. else
  393. result := Regs.Cx;
  394. end;
  395. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  396. var
  397. Regs: registers;
  398. begin
  399. StringToTB(FileName);
  400. Regs.Edx := tb_offset;
  401. Regs.Ds := tb_segment;
  402. if LFNSupport then
  403. begin
  404. Regs.Ax := $7143;
  405. Regs.Bx := 1;
  406. end
  407. else
  408. Regs.Ax := $4301;
  409. Regs.Cx := Attr;
  410. RealIntr($21, Regs);
  411. if Regs.Flags and CarryFlag <> 0 then
  412. result := -Regs.Ax
  413. else
  414. result := 0;
  415. end;
  416. Function DeleteFile (Const FileName : String) : Boolean;
  417. var
  418. Regs: registers;
  419. begin
  420. StringToTB(FileName);
  421. Regs.Edx := tb_offset;
  422. Regs.Ds := tb_segment;
  423. if LFNSupport then
  424. Regs.Eax := $7141
  425. else
  426. Regs.Eax := $4100;
  427. Regs.Esi := 0;
  428. Regs.Ecx := 0;
  429. RealIntr($21, Regs);
  430. result := (Regs.Flags and CarryFlag = 0);
  431. end;
  432. Function RenameFile (Const OldName, NewName : String) : Boolean;
  433. var
  434. Regs: registers;
  435. begin
  436. StringToTB(OldName + #0 + NewName);
  437. Regs.Edx := tb_offset;
  438. Regs.Ds := tb_segment;
  439. Regs.Edi := tb_offset + Length(OldName) + 1;
  440. Regs.Es := tb_segment;
  441. if LFNSupport then
  442. Regs.Eax := $7156
  443. else
  444. Regs.Eax := $5600;
  445. Regs.Ecx := $ff;
  446. RealIntr($21, Regs);
  447. result := (Regs.Flags and CarryFlag = 0);
  448. end;
  449. {****************************************************************************
  450. Disk Functions
  451. ****************************************************************************}
  452. TYPE ExtendedFat32FreeSpaceRec=packed Record
  453. RetSize : WORD; { (ret) size of returned structure}
  454. Strucversion : WORD; {(call) structure version (0000h)
  455. (ret) actual structure version (0000h)}
  456. SecPerClus, {number of sectors per cluster}
  457. BytePerSec, {number of bytes per sector}
  458. AvailClusters, {number of available clusters}
  459. TotalClusters, {total number of clusters on the drive}
  460. AvailPhysSect, {physical sectors available on the drive}
  461. TotalPhysSect, {total physical sectors on the drive}
  462. AvailAllocUnits, {Available allocation units}
  463. TotalAllocUnits : DWORD; {Total allocation units}
  464. Dummy,Dummy2 : DWORD; {8 bytes reserved}
  465. END;
  466. function do_diskdata(drive : byte; Free : BOOLEAN) : Int64;
  467. VAR S : String;
  468. Rec : ExtendedFat32FreeSpaceRec;
  469. regs : registers;
  470. procedure OldDosDiskData;
  471. begin
  472. regs.dl:=drive;
  473. regs.ah:=$36;
  474. msdos(regs);
  475. if regs.ax<>$FFFF then
  476. begin
  477. if Free then
  478. Do_DiskData:=int64(regs.ax)*regs.bx*regs.cx
  479. else
  480. Do_DiskData:=int64(regs.ax)*regs.cx*regs.dx;
  481. end
  482. else
  483. do_diskdata:=-1;
  484. end;
  485. BEGIN
  486. if LFNSupport then
  487. begin
  488. S:='C:\'#0;
  489. if Drive=0 then
  490. begin
  491. GetDir(Drive,S);
  492. Setlength(S,4);
  493. S[4]:=#0;
  494. end
  495. else
  496. S[1]:=chr(Drive+64);
  497. Rec.Strucversion:=0;
  498. Rec.RetSize := 0;
  499. dosmemput(tb_segment,tb_offset,Rec,SIZEOF(ExtendedFat32FreeSpaceRec));
  500. dosmemput(tb_segment,tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1,S[1],4);
  501. regs.dx:=tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1;
  502. regs.ds:=tb_segment;
  503. regs.di:=tb_offset;
  504. regs.es:=tb_segment;
  505. regs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  506. regs.ax:=$7303;
  507. msdos(regs);
  508. if (regs.flags and fcarry) = 0 then {No error clausule in int except cf}
  509. begin
  510. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  511. if Rec.RetSize = 0 then (* Error - "FAT32" function not supported! *)
  512. OldDosDiskData
  513. else
  514. if Free then
  515. Do_DiskData:=int64(rec.AvailAllocUnits)*rec.SecPerClus*rec.BytePerSec
  516. else
  517. Do_DiskData:=int64(rec.TotalAllocUnits)*rec.SecPerClus*rec.BytePerSec;
  518. end
  519. else
  520. OldDosDiskData;
  521. end
  522. else
  523. OldDosDiskData;
  524. end;
  525. function diskfree(drive : byte) : int64;
  526. begin
  527. diskfree:=Do_DiskData(drive,TRUE);
  528. end;
  529. function disksize(drive : byte) : int64;
  530. begin
  531. disksize:=Do_DiskData(drive,false);
  532. end;
  533. Function GetCurrentDir : String;
  534. begin
  535. GetDir(0, result);
  536. end;
  537. Function SetCurrentDir (Const NewDir : String) : Boolean;
  538. begin
  539. {$I-}
  540. ChDir(NewDir);
  541. {$I+}
  542. result := (IOResult = 0);
  543. end;
  544. Function CreateDir (Const NewDir : String) : Boolean;
  545. begin
  546. {$I-}
  547. MkDir(NewDir);
  548. {$I+}
  549. result := (IOResult = 0);
  550. end;
  551. Function RemoveDir (Const Dir : String) : Boolean;
  552. begin
  553. {$I-}
  554. RmDir(Dir);
  555. {$I+}
  556. result := (IOResult = 0);
  557. end;
  558. {****************************************************************************
  559. Time Functions
  560. ****************************************************************************}
  561. Procedure GetLocalTime(var SystemTime: TSystemTime);
  562. var
  563. Regs: Registers;
  564. begin
  565. Regs.ah := $2C;
  566. RealIntr($21, Regs);
  567. SystemTime.Hour := Regs.Ch;
  568. SystemTime.Minute := Regs.Cl;
  569. SystemTime.Second := Regs.Dh;
  570. SystemTime.MilliSecond := Regs.Dl*10;
  571. Regs.ah := $2A;
  572. RealIntr($21, Regs);
  573. SystemTime.Year := Regs.Cx;
  574. SystemTime.Month := Regs.Dh;
  575. SystemTime.Day := Regs.Dl;
  576. end ;
  577. {****************************************************************************
  578. Misc Functions
  579. ****************************************************************************}
  580. procedure sysBeep;
  581. begin
  582. end;
  583. {****************************************************************************
  584. Locale Functions
  585. ****************************************************************************}
  586. { Codepage constants }
  587. const
  588. CP_US = 437;
  589. CP_MultiLingual = 850;
  590. CP_SlavicLatin2 = 852;
  591. CP_Turkish = 857;
  592. CP_Portugal = 860;
  593. CP_IceLand = 861;
  594. CP_Canada = 863;
  595. CP_NorwayDenmark = 865;
  596. { CountryInfo }
  597. type
  598. TCountryInfo = packed record
  599. InfoId: byte;
  600. case integer of
  601. 1: ( Size: word;
  602. CountryId: word;
  603. CodePage: word;
  604. CountryInfo: array[0..33] of byte );
  605. 2: ( UpperCaseTable: longint );
  606. 4: ( FilenameUpperCaseTable: longint );
  607. 5: ( FilecharacterTable: longint );
  608. 6: ( CollatingTable: longint );
  609. 7: ( DBCSLeadByteTable: longint );
  610. end ;
  611. procedure GetExtendedCountryInfo(InfoId: integer; CodePage, CountryId: word; var CountryInfo: TCountryInfo);
  612. Var Regs: Registers;
  613. begin
  614. Regs.AH := $65;
  615. Regs.AL := InfoId;
  616. Regs.BX := CodePage;
  617. Regs.DX := CountryId;
  618. Regs.ES := transfer_buffer div 16;
  619. Regs.DI := transfer_buffer and 15;
  620. Regs.CX := SizeOf(TCountryInfo);
  621. RealIntr($21, Regs);
  622. DosMemGet(transfer_buffer div 16,
  623. transfer_buffer and 15,
  624. CountryInfo, Regs.CX );
  625. end;
  626. procedure InitAnsi;
  627. var
  628. CountryInfo: TCountryInfo; i: integer;
  629. begin
  630. { Fill table entries 0 to 127 }
  631. for i := 0 to 96 do
  632. UpperCaseTable[i] := chr(i);
  633. for i := 97 to 122 do
  634. UpperCaseTable[i] := chr(i - 32);
  635. for i := 123 to 127 do
  636. UpperCaseTable[i] := chr(i);
  637. for i := 0 to 64 do
  638. LowerCaseTable[i] := chr(i);
  639. for i := 65 to 90 do
  640. LowerCaseTable[i] := chr(i + 32);
  641. for i := 91 to 255 do
  642. LowerCaseTable[i] := chr(i);
  643. { Get country and codepage info }
  644. GetExtendedCountryInfo(1, $FFFF, $FFFF, CountryInfo);
  645. if CountryInfo.CodePage = 850 then
  646. begin
  647. { Special, known case }
  648. Move(CP850UCT, UpperCaseTable[128], 128);
  649. Move(CP850LCT, LowerCaseTable[128], 128);
  650. end
  651. else
  652. begin
  653. { this needs to be checked !!
  654. this is correct only if UpperCaseTable is
  655. and Offset:Segment word record (PM) }
  656. { get the uppercase table from dosmemory }
  657. GetExtendedCountryInfo(2, $FFFF, $FFFF, CountryInfo);
  658. DosMemGet(CountryInfo.UpperCaseTable shr 16, 2 + CountryInfo.UpperCaseTable and 65535, UpperCaseTable[128], 128);
  659. for i := 128 to 255 do
  660. begin
  661. if UpperCaseTable[i] <> chr(i) then
  662. LowerCaseTable[ord(UpperCaseTable[i])] := chr(i);
  663. end;
  664. end;
  665. end;
  666. Procedure InitInternational;
  667. begin
  668. InitInternationalGeneric;
  669. InitAnsi;
  670. end;
  671. function SysErrorMessage(ErrorCode: Integer): String;
  672. begin
  673. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  674. end;
  675. {****************************************************************************
  676. Os utils
  677. ****************************************************************************}
  678. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  679. begin
  680. Result:=FPCGetEnvVarFromP(envp,EnvVar);
  681. end;
  682. Function GetEnvironmentVariableCount : Integer;
  683. begin
  684. Result:=FPCCountEnvVar(EnvP);
  685. end;
  686. Function GetEnvironmentString(Index : Integer) : String;
  687. begin
  688. Result:=FPCGetEnvStrFromP(Envp,Index);
  689. end;
  690. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  691. var
  692. e : EOSError;
  693. CommandLine: AnsiString;
  694. begin
  695. dos.exec_ansistring(path,comline);
  696. if (Dos.DosError <> 0) then
  697. begin
  698. if ComLine <> '' then
  699. CommandLine := Path + ' ' + ComLine
  700. else
  701. CommandLine := Path;
  702. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,Dos.DosError]);
  703. e.ErrorCode:=Dos.DosError;
  704. raise e;
  705. end;
  706. Result := DosExitCode;
  707. end;
  708. function ExecuteProcess (const Path: AnsiString;
  709. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  710. var
  711. CommandLine: AnsiString;
  712. I: integer;
  713. begin
  714. Commandline := '';
  715. for I := 0 to High (ComLine) do
  716. if Pos (' ', ComLine [I]) <> 0 then
  717. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  718. else
  719. CommandLine := CommandLine + ' ' + Comline [I];
  720. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  721. end;
  722. {*************************************************************************
  723. Sleep
  724. *************************************************************************}
  725. procedure Sleep (MilliSeconds: Cardinal);
  726. var
  727. R: Registers;
  728. T0, T1, T2: int64;
  729. DayOver: boolean;
  730. begin
  731. (* Sleep is supposed to give up time slice - DOS Idle Interrupt chosen
  732. because it should be supported in all DOS versions. Not precise at all,
  733. though - the smallest step is 10 ms even in the best case. *)
  734. R.AH := $2C;
  735. RealIntr($21, R);
  736. T0 := R.CH * 3600000 + R.CL * 60000 + R.DH * 1000 + R.DL * 10;
  737. T2 := T0 + MilliSeconds;
  738. DayOver := T2 > (24 * 3600000);
  739. repeat
  740. Intr ($28, R);
  741. (* R.AH := $2C; - should be preserved. *)
  742. RealIntr($21, R);
  743. T1 := R.CH * 3600000 + R.CL * 60000 + R.DH * 1000 + R.DL * 10;
  744. if DayOver and (T1 < T0) then
  745. Inc (T1, 24 * 3600000);
  746. until T1 >= T2;
  747. end;
  748. {****************************************************************************
  749. Initialization code
  750. ****************************************************************************}
  751. Initialization
  752. InitExceptions; { Initialize exceptions. OS independent }
  753. InitInternational; { Initialize internationalization settings }
  754. OnBeep:=@SysBeep;
  755. Finalization
  756. DoneExceptions;
  757. end.