2
0

sysutils.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. L: longint;
  243. begin
  244. if FileName = '' then
  245. Result := false
  246. else
  247. begin
  248. L := FileGetAttr (FileName);
  249. Result := (L >= 0) and (L and (faDirectory or faVolumeID) = 0);
  250. (* Neither VolumeIDs nor directories are files. *)
  251. end;
  252. end;
  253. Function DirectoryExists (Const Directory : String) : Boolean;
  254. Var
  255. Dir : String;
  256. drive : byte;
  257. FADir, StoredIORes : longint;
  258. begin
  259. Dir:=Directory;
  260. if (length(dir)=2) and (dir[2]=':') and
  261. ((dir[1] in ['A'..'Z']) or (dir[1] in ['a'..'z'])) then
  262. begin
  263. { We want to test GetCurDir }
  264. if dir[1] in ['A'..'Z'] then
  265. drive:=ord(dir[1])-ord('A')+1
  266. else
  267. drive:=ord(dir[1])-ord('a')+1;
  268. {$undef OPT_I}
  269. {$ifopt I+}
  270. {$define OPT_I}
  271. {$endif}
  272. {$I-}
  273. StoredIORes:=InOutRes;
  274. InOutRes:=0;
  275. GetDir(drive,dir);
  276. if InOutRes <> 0 then
  277. begin
  278. InOutRes:=StoredIORes;
  279. result:=false;
  280. exit;
  281. end;
  282. end;
  283. {$ifdef OPT_I}
  284. {$I+}
  285. {$endif}
  286. if (Length (Dir) > 1) and
  287. (Dir [Length (Dir)] in AllowDirectorySeparators) and
  288. (* Do not remove '\' after ':' (root directory of a drive)
  289. or in '\\' (invalid path, possibly broken UNC path). *)
  290. not (Dir [Length (Dir) - 1] in (AllowDriveSeparators + AllowDirectorySeparators)) then
  291. dir:=copy(dir,1,length(dir)-1);
  292. (* FileGetAttr returns -1 on error *)
  293. FADir := FileGetAttr (Dir);
  294. Result := (FADir <> -1) and
  295. ((FADir and faDirectory) = faDirectory);
  296. end;
  297. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  298. Var Sr : PSearchrec;
  299. begin
  300. //!! Sr := New(PSearchRec);
  301. getmem(sr,sizeof(searchrec));
  302. Rslt.FindHandle := longint(Sr);
  303. DOS.FindFirst(Path, Attr, Sr^);
  304. result := -DosError;
  305. if result = 0 then
  306. begin
  307. Rslt.Time := Sr^.Time;
  308. Rslt.Size := Sr^.Size;
  309. Rslt.Attr := Sr^.Attr;
  310. Rslt.ExcludeAttr := 0;
  311. Rslt.Name := Sr^.Name;
  312. end ;
  313. end;
  314. Function FindNext (Var Rslt : TSearchRec) : Longint;
  315. var
  316. Sr: PSearchRec;
  317. begin
  318. Sr := PSearchRec(Rslt.FindHandle);
  319. if Sr <> nil then
  320. begin
  321. DOS.FindNext(Sr^);
  322. result := -DosError;
  323. if result = 0 then
  324. begin
  325. Rslt.Time := Sr^.Time;
  326. Rslt.Size := Sr^.Size;
  327. Rslt.Attr := Sr^.Attr;
  328. Rslt.ExcludeAttr := 0;
  329. Rslt.Name := Sr^.Name;
  330. end;
  331. end;
  332. end;
  333. Procedure FindClose (Var F : TSearchrec);
  334. var
  335. Sr: PSearchRec;
  336. begin
  337. Sr := PSearchRec(F.FindHandle);
  338. if Sr <> nil then
  339. begin
  340. //!! Dispose(Sr);
  341. // This call is non dummy if LFNSupport is true PM
  342. DOS.FindClose(SR^);
  343. freemem(sr,sizeof(searchrec));
  344. end;
  345. F.FindHandle := 0;
  346. end;
  347. Function FileGetDate (Handle : Longint) : Longint;
  348. var
  349. Regs: registers;
  350. begin
  351. //!! for win95 an alternative function is available.
  352. Regs.Ebx := Handle;
  353. Regs.Eax := $5700;
  354. RealIntr($21, Regs);
  355. if Regs.Flags and CarryFlag <> 0 then
  356. result := -1
  357. else
  358. begin
  359. LongRec(result).Lo := Regs.cx;
  360. LongRec(result).Hi := Regs.dx;
  361. end ;
  362. end;
  363. Function FileSetDate (Handle, Age : Longint) : Longint;
  364. var
  365. Regs: registers;
  366. begin
  367. Regs.Ebx := Handle;
  368. Regs.Eax := $5701;
  369. Regs.Ecx := Lo(Age);
  370. Regs.Edx := Hi(Age);
  371. RealIntr($21, Regs);
  372. if Regs.Flags and CarryFlag <> 0 then
  373. result := -Regs.Ax
  374. else
  375. result := 0;
  376. end;
  377. Function FileGetAttr (Const FileName : String) : Longint;
  378. var
  379. Regs: registers;
  380. begin
  381. StringToTB(FileName);
  382. Regs.Edx := tb_offset;
  383. Regs.Ds := tb_segment;
  384. if LFNSupport then
  385. begin
  386. Regs.Ax := $7143;
  387. Regs.Bx := 0;
  388. end
  389. else
  390. Regs.Ax := $4300;
  391. RealIntr($21, Regs);
  392. if Regs.Flags and CarryFlag <> 0 then
  393. result := -1
  394. else
  395. result := Regs.Cx;
  396. end;
  397. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  398. var
  399. Regs: registers;
  400. begin
  401. StringToTB(FileName);
  402. Regs.Edx := tb_offset;
  403. Regs.Ds := tb_segment;
  404. if LFNSupport then
  405. begin
  406. Regs.Ax := $7143;
  407. Regs.Bx := 1;
  408. end
  409. else
  410. Regs.Ax := $4301;
  411. Regs.Cx := Attr;
  412. RealIntr($21, Regs);
  413. if Regs.Flags and CarryFlag <> 0 then
  414. result := -Regs.Ax
  415. else
  416. result := 0;
  417. end;
  418. Function DeleteFile (Const FileName : String) : Boolean;
  419. var
  420. Regs: registers;
  421. begin
  422. StringToTB(FileName);
  423. Regs.Edx := tb_offset;
  424. Regs.Ds := tb_segment;
  425. if LFNSupport then
  426. Regs.Eax := $7141
  427. else
  428. Regs.Eax := $4100;
  429. Regs.Esi := 0;
  430. Regs.Ecx := 0;
  431. RealIntr($21, Regs);
  432. result := (Regs.Flags and CarryFlag = 0);
  433. end;
  434. Function RenameFile (Const OldName, NewName : String) : Boolean;
  435. var
  436. Regs: registers;
  437. begin
  438. StringToTB(OldName + #0 + NewName);
  439. Regs.Edx := tb_offset;
  440. Regs.Ds := tb_segment;
  441. Regs.Edi := tb_offset + Length(OldName) + 1;
  442. Regs.Es := tb_segment;
  443. if LFNSupport then
  444. Regs.Eax := $7156
  445. else
  446. Regs.Eax := $5600;
  447. Regs.Ecx := $ff;
  448. RealIntr($21, Regs);
  449. result := (Regs.Flags and CarryFlag = 0);
  450. end;
  451. {****************************************************************************
  452. Disk Functions
  453. ****************************************************************************}
  454. TYPE ExtendedFat32FreeSpaceRec=packed Record
  455. RetSize : WORD; { (ret) size of returned structure}
  456. Strucversion : WORD; {(call) structure version (0000h)
  457. (ret) actual structure version (0000h)}
  458. SecPerClus, {number of sectors per cluster}
  459. BytePerSec, {number of bytes per sector}
  460. AvailClusters, {number of available clusters}
  461. TotalClusters, {total number of clusters on the drive}
  462. AvailPhysSect, {physical sectors available on the drive}
  463. TotalPhysSect, {total physical sectors on the drive}
  464. AvailAllocUnits, {Available allocation units}
  465. TotalAllocUnits : DWORD; {Total allocation units}
  466. Dummy,Dummy2 : DWORD; {8 bytes reserved}
  467. END;
  468. function do_diskdata(drive : byte; Free : BOOLEAN) : Int64;
  469. VAR S : String;
  470. Rec : ExtendedFat32FreeSpaceRec;
  471. regs : registers;
  472. procedure OldDosDiskData;
  473. begin
  474. regs.dl:=drive;
  475. regs.ah:=$36;
  476. msdos(regs);
  477. if regs.ax<>$FFFF then
  478. begin
  479. if Free then
  480. Do_DiskData:=int64(regs.ax)*regs.bx*regs.cx
  481. else
  482. Do_DiskData:=int64(regs.ax)*regs.cx*regs.dx;
  483. end
  484. else
  485. do_diskdata:=-1;
  486. end;
  487. BEGIN
  488. if LFNSupport then
  489. begin
  490. S:='C:\'#0;
  491. if Drive=0 then
  492. begin
  493. GetDir(Drive,S);
  494. Setlength(S,4);
  495. S[4]:=#0;
  496. end
  497. else
  498. S[1]:=chr(Drive+64);
  499. Rec.Strucversion:=0;
  500. Rec.RetSize := 0;
  501. dosmemput(tb_segment,tb_offset,Rec,SIZEOF(ExtendedFat32FreeSpaceRec));
  502. dosmemput(tb_segment,tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1,S[1],4);
  503. regs.dx:=tb_offset+Sizeof(ExtendedFat32FreeSpaceRec)+1;
  504. regs.ds:=tb_segment;
  505. regs.di:=tb_offset;
  506. regs.es:=tb_segment;
  507. regs.cx:=Sizeof(ExtendedFat32FreeSpaceRec);
  508. regs.ax:=$7303;
  509. msdos(regs);
  510. if (regs.flags and fcarry) = 0 then {No error clausule in int except cf}
  511. begin
  512. copyfromdos(rec,Sizeof(ExtendedFat32FreeSpaceRec));
  513. if Rec.RetSize = 0 then (* Error - "FAT32" function not supported! *)
  514. OldDosDiskData
  515. else
  516. if Free then
  517. Do_DiskData:=int64(rec.AvailAllocUnits)*rec.SecPerClus*rec.BytePerSec
  518. else
  519. Do_DiskData:=int64(rec.TotalAllocUnits)*rec.SecPerClus*rec.BytePerSec;
  520. end
  521. else
  522. OldDosDiskData;
  523. end
  524. else
  525. OldDosDiskData;
  526. end;
  527. function diskfree(drive : byte) : int64;
  528. begin
  529. diskfree:=Do_DiskData(drive,TRUE);
  530. end;
  531. function disksize(drive : byte) : int64;
  532. begin
  533. disksize:=Do_DiskData(drive,false);
  534. end;
  535. Function GetCurrentDir : String;
  536. begin
  537. GetDir(0, result);
  538. end;
  539. Function SetCurrentDir (Const NewDir : String) : Boolean;
  540. begin
  541. {$I-}
  542. ChDir(NewDir);
  543. {$I+}
  544. result := (IOResult = 0);
  545. end;
  546. Function CreateDir (Const NewDir : String) : Boolean;
  547. begin
  548. {$I-}
  549. MkDir(NewDir);
  550. {$I+}
  551. result := (IOResult = 0);
  552. end;
  553. Function RemoveDir (Const Dir : String) : Boolean;
  554. begin
  555. {$I-}
  556. RmDir(Dir);
  557. {$I+}
  558. result := (IOResult = 0);
  559. end;
  560. {****************************************************************************
  561. Time Functions
  562. ****************************************************************************}
  563. Procedure GetLocalTime(var SystemTime: TSystemTime);
  564. var
  565. Regs: Registers;
  566. begin
  567. Regs.ah := $2C;
  568. RealIntr($21, Regs);
  569. SystemTime.Hour := Regs.Ch;
  570. SystemTime.Minute := Regs.Cl;
  571. SystemTime.Second := Regs.Dh;
  572. SystemTime.MilliSecond := Regs.Dl*10;
  573. Regs.ah := $2A;
  574. RealIntr($21, Regs);
  575. SystemTime.Year := Regs.Cx;
  576. SystemTime.Month := Regs.Dh;
  577. SystemTime.Day := Regs.Dl;
  578. end ;
  579. {****************************************************************************
  580. Misc Functions
  581. ****************************************************************************}
  582. procedure sysBeep;
  583. begin
  584. end;
  585. {****************************************************************************
  586. Locale Functions
  587. ****************************************************************************}
  588. { Codepage constants }
  589. const
  590. CP_US = 437;
  591. CP_MultiLingual = 850;
  592. CP_SlavicLatin2 = 852;
  593. CP_Turkish = 857;
  594. CP_Portugal = 860;
  595. CP_IceLand = 861;
  596. CP_Canada = 863;
  597. CP_NorwayDenmark = 865;
  598. { CountryInfo }
  599. type
  600. TCountryInfo = packed record
  601. InfoId: byte;
  602. case integer of
  603. 1: ( Size: word;
  604. CountryId: word;
  605. CodePage: word;
  606. CountryInfo: array[0..33] of byte );
  607. 2: ( UpperCaseTable: longint );
  608. 4: ( FilenameUpperCaseTable: longint );
  609. 5: ( FilecharacterTable: longint );
  610. 6: ( CollatingTable: longint );
  611. 7: ( DBCSLeadByteTable: longint );
  612. end ;
  613. procedure GetExtendedCountryInfo(InfoId: integer; CodePage, CountryId: word; var CountryInfo: TCountryInfo);
  614. Var Regs: Registers;
  615. begin
  616. Regs.AH := $65;
  617. Regs.AL := InfoId;
  618. Regs.BX := CodePage;
  619. Regs.DX := CountryId;
  620. Regs.ES := transfer_buffer div 16;
  621. Regs.DI := transfer_buffer and 15;
  622. Regs.CX := SizeOf(TCountryInfo);
  623. RealIntr($21, Regs);
  624. DosMemGet(transfer_buffer div 16,
  625. transfer_buffer and 15,
  626. CountryInfo, Regs.CX );
  627. end;
  628. procedure InitAnsi;
  629. var
  630. CountryInfo: TCountryInfo; i: integer;
  631. begin
  632. { Fill table entries 0 to 127 }
  633. for i := 0 to 96 do
  634. UpperCaseTable[i] := chr(i);
  635. for i := 97 to 122 do
  636. UpperCaseTable[i] := chr(i - 32);
  637. for i := 123 to 127 do
  638. UpperCaseTable[i] := chr(i);
  639. for i := 0 to 64 do
  640. LowerCaseTable[i] := chr(i);
  641. for i := 65 to 90 do
  642. LowerCaseTable[i] := chr(i + 32);
  643. for i := 91 to 255 do
  644. LowerCaseTable[i] := chr(i);
  645. { Get country and codepage info }
  646. GetExtendedCountryInfo(1, $FFFF, $FFFF, CountryInfo);
  647. if CountryInfo.CodePage = 850 then
  648. begin
  649. { Special, known case }
  650. Move(CP850UCT, UpperCaseTable[128], 128);
  651. Move(CP850LCT, LowerCaseTable[128], 128);
  652. end
  653. else
  654. begin
  655. { this needs to be checked !!
  656. this is correct only if UpperCaseTable is
  657. and Offset:Segment word record (PM) }
  658. { get the uppercase table from dosmemory }
  659. GetExtendedCountryInfo(2, $FFFF, $FFFF, CountryInfo);
  660. DosMemGet(CountryInfo.UpperCaseTable shr 16, 2 + CountryInfo.UpperCaseTable and 65535, UpperCaseTable[128], 128);
  661. for i := 128 to 255 do
  662. begin
  663. if UpperCaseTable[i] <> chr(i) then
  664. LowerCaseTable[ord(UpperCaseTable[i])] := chr(i);
  665. end;
  666. end;
  667. end;
  668. Procedure InitInternational;
  669. begin
  670. InitInternationalGeneric;
  671. InitAnsi;
  672. end;
  673. function SysErrorMessage(ErrorCode: Integer): String;
  674. begin
  675. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  676. end;
  677. {****************************************************************************
  678. Os utils
  679. ****************************************************************************}
  680. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  681. begin
  682. Result:=FPCGetEnvVarFromP(envp,EnvVar);
  683. end;
  684. Function GetEnvironmentVariableCount : Integer;
  685. begin
  686. Result:=FPCCountEnvVar(EnvP);
  687. end;
  688. Function GetEnvironmentString(Index : Integer) : String;
  689. begin
  690. Result:=FPCGetEnvStrFromP(Envp,Index);
  691. end;
  692. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  693. var
  694. e : EOSError;
  695. CommandLine: AnsiString;
  696. begin
  697. dos.exec_ansistring(path,comline);
  698. if (Dos.DosError <> 0) then
  699. begin
  700. if ComLine <> '' then
  701. CommandLine := Path + ' ' + ComLine
  702. else
  703. CommandLine := Path;
  704. e:=EOSError.CreateFmt(SExecuteProcessFailed,[CommandLine,Dos.DosError]);
  705. e.ErrorCode:=Dos.DosError;
  706. raise e;
  707. end;
  708. Result := DosExitCode;
  709. end;
  710. function ExecuteProcess (const Path: AnsiString;
  711. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  712. var
  713. CommandLine: AnsiString;
  714. I: integer;
  715. begin
  716. Commandline := '';
  717. for I := 0 to High (ComLine) do
  718. if Pos (' ', ComLine [I]) <> 0 then
  719. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  720. else
  721. CommandLine := CommandLine + ' ' + Comline [I];
  722. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  723. end;
  724. {*************************************************************************
  725. Sleep
  726. *************************************************************************}
  727. procedure Sleep (MilliSeconds: Cardinal);
  728. var
  729. R: Registers;
  730. T0, T1, T2: int64;
  731. DayOver: boolean;
  732. begin
  733. (* Sleep is supposed to give up time slice - DOS Idle Interrupt chosen
  734. because it should be supported in all DOS versions. Not precise at all,
  735. though - the smallest step is 10 ms even in the best case. *)
  736. R.AH := $2C;
  737. RealIntr($21, R);
  738. T0 := R.CH * 3600000 + R.CL * 60000 + R.DH * 1000 + R.DL * 10;
  739. T2 := T0 + MilliSeconds;
  740. DayOver := T2 > (24 * 3600000);
  741. repeat
  742. Intr ($28, R);
  743. (* R.AH := $2C; - should be preserved. *)
  744. RealIntr($21, R);
  745. T1 := R.CH * 3600000 + R.CL * 60000 + R.DH * 1000 + R.DL * 10;
  746. if DayOver and (T1 < T0) then
  747. Inc (T1, 24 * 3600000);
  748. until T1 >= T2;
  749. end;
  750. {****************************************************************************
  751. Initialization code
  752. ****************************************************************************}
  753. Initialization
  754. InitExceptions; { Initialize exceptions. OS independent }
  755. InitInternational; { Initialize internationalization settings }
  756. OnBeep:=@SysBeep;
  757. Finalization
  758. DoneExceptions;
  759. end.