sysutils.pp 22 KB

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