dos.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. {****************************************************************************
  2. $Id$
  3. Free Pascal Runtime-Library
  4. DOS unit for OS/2
  5. Copyright (c) 1997,1999-2000 by Daniel Mantione,
  6. member of the Free Pascal development team
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. ****************************************************************************}
  13. unit dos;
  14. {$ASMMODE ATT}
  15. {***************************************************************************}
  16. interface
  17. {***************************************************************************}
  18. {$PACKRECORDS 1}
  19. uses Strings, DosCalls;
  20. const
  21. FileNameLen = 255;
  22. Type
  23. {Search record which is used by findfirst and findnext:}
  24. searchrec=record
  25. case boolean of
  26. false: (handle:longint; {Used in os_OS2 mode}
  27. FStat:PFileFindBuf3;
  28. fill2:array[1..21-SizeOf(longint)-SizeOf(pointer)] of byte;
  29. attr2:byte;
  30. time2:longint;
  31. size2:longint;
  32. name2:string); {Filenames can be long in OS/2!}
  33. true: (fill:array[1..21] of byte;
  34. attr:byte;
  35. time:longint;
  36. size:longint;
  37. name:string); {Filenames can be long in OS/2!}
  38. end;
  39. {Data structure for the registers needed by msdos and intr:}
  40. registers=packed record
  41. case i:integer of
  42. 0:(ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,
  43. f8,flags,fs,gs:word);
  44. 1:(al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh:byte);
  45. 2:(eax,ebx,ecx,edx,ebp,esi,edi:longint);
  46. end;
  47. {Flags for the exec procedure:
  48. }
  49. {$ifdef HASTHREADVAR}
  50. threadvar
  51. {$else HASTHREADVAR}
  52. var
  53. {$endif HASTHREADVAR}
  54. (* For compatibility with VP/2, used for runflags in Exec procedure. *)
  55. ExecFlags: cardinal;
  56. {$i dosh.inc}
  57. {OS/2 specific functions}
  58. function GetEnvPChar (EnvVar: string): PChar;
  59. function DosErrorModuleName: string;
  60. (* In case of an error in Dos.Exec returns the name of the module *)
  61. (* causing the problem - e.g. name of a missing or corrupted DLL. *)
  62. implementation
  63. {$ifdef HASTHREADVAR}
  64. threadvar
  65. {$else HASTHREADVAR}
  66. var
  67. {$endif HASTHREADVAR}
  68. LastDosExitCode: longint;
  69. LastDosErrorModuleName: string;
  70. type TBA = array [1..SizeOf (SearchRec)] of byte;
  71. PBA = ^TBA;
  72. const FindResvdMask = $00003737; {Allowed bits in attribute
  73. specification for DosFindFirst call.}
  74. function fsearch(path:pathstr;dirlist:string):pathstr;
  75. Var
  76. A: array [0..255] of char;
  77. D, P: AnsiString;
  78. begin
  79. P:=Path;
  80. D:=DirList;
  81. DosError:=DosSearchPath(0, PChar(D), PChar(P), @A, 255);
  82. fsearch := StrPas (@A);
  83. end;
  84. procedure getftime(var f;var time:longint);
  85. var
  86. FStat: TFileStatus3;
  87. begin
  88. DosError := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  89. SizeOf (FStat));
  90. if DosError=0 then
  91. begin
  92. Time := FStat.TimeLastWrite + longint (FStat.DateLastWrite) shl 16;
  93. if Time = 0 then
  94. Time := FStat.TimeCreation + longint (FStat.DateCreation) shl 16;
  95. end else
  96. Time:=0;
  97. end;
  98. procedure SetFTime (var F; Time: longint);
  99. var FStat: TFileStatus3;
  100. RC: cardinal;
  101. begin
  102. RC := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  103. SizeOf (FStat));
  104. if RC = 0 then
  105. begin
  106. FStat.DateLastAccess := Hi (Time);
  107. FStat.DateLastWrite := Hi (Time);
  108. FStat.TimeLastAccess := Lo (Time);
  109. FStat.TimeLastWrite := Lo (Time);
  110. RC := DosSetFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  111. SizeOf (FStat));
  112. end;
  113. DosError := integer (RC);
  114. end;
  115. procedure Exec (const Path: PathStr; const ComLine: ComStr);
  116. {Execute a program.}
  117. var Args: PByteArray;
  118. ArgSize: word;
  119. Res: TResultCodes;
  120. ObjName: string;
  121. const
  122. MaxArgsSize = 2048; (* Amount of memory reserved for arguments in bytes. *)
  123. begin
  124. { LastDosExitCode := Exec (Path, ExecRunFlags (ExecFlags), efDefault, ComLine);}
  125. GetMem (Args, MaxArgsSize);
  126. ArgSize := 0;
  127. Move (Path [1], Args^ [ArgSize], Length (Path));
  128. Inc (ArgSize, Length (Path));
  129. Args^ [ArgSize] := 0;
  130. Inc (ArgSize);
  131. {Now do the real arguments.}
  132. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  133. Inc (ArgSize, Length (ComLine));
  134. Args^ [ArgSize] := 0;
  135. Inc (ArgSize);
  136. Args^ [ArgSize] := 0;
  137. DosError := DosExecPgm (ObjName, cardinal (ExecFlags), Args, nil, Res, Path);
  138. if DosError = 0 then
  139. begin
  140. LastDosExitCode := Res.ExitCode;
  141. LastDosErrorModuleName := '';
  142. end
  143. else
  144. begin
  145. LastDosErrorModuleName := ObjName;
  146. LastDosExitCode := 0; (* Needed for TP/BP compatibility *)
  147. end;
  148. FreeMem (Args, MaxArgsSize);
  149. end;
  150. function DosExitCode: word;
  151. begin
  152. DosExitCode := LastDosExitCode and $FFFF;
  153. end;
  154. function DosErrorModuleName: string;
  155. begin
  156. DosErrorModuleName := LastDosErrorModuleName;
  157. end;
  158. function dosversion:word;
  159. {Returns OS/2 version}
  160. var
  161. Minor, Major: Cardinal;
  162. begin
  163. DosQuerySysInfo(svMajorVersion, svMajorVersion, Major, 4);
  164. DosQuerySysInfo(svMinorVersion, svMinorVersion, Minor, 4);
  165. DosVersion:=Major or Minor shl 8;
  166. end;
  167. procedure GetDate (var Year, Month, MDay, WDay: word);
  168. Var
  169. dt: TDateTime;
  170. begin
  171. DosGetDateTime(dt);
  172. Year:=dt.year;
  173. Month:=dt.month;
  174. MDay:=dt.Day;
  175. WDay:=dt.Weekday;
  176. end;
  177. procedure SetDate (Year, Month, Day: word);
  178. var
  179. DT: TDateTime;
  180. begin
  181. DosGetDateTime (DT);
  182. DT.Year := Year;
  183. DT.Month := byte (Month);
  184. DT.Day := byte (Day);
  185. DosSetDateTime (DT);
  186. end;
  187. procedure GetTime (var Hour, Minute, Second, Sec100: word);
  188. var
  189. dt: TDateTime;
  190. begin
  191. DosGetDateTime(dt);
  192. Hour:=dt.Hour;
  193. Minute:=dt.Minute;
  194. Second:=dt.Second;
  195. Sec100:=dt.Hundredths;
  196. end;
  197. procedure SetTime (Hour, Minute, Second, Sec100: word);
  198. var
  199. DT: TDateTime;
  200. begin
  201. DosGetDateTime (DT);
  202. DT.Hour := byte (Hour);
  203. DT.Minute := byte (Minute);
  204. DT.Second := byte (Second);
  205. DT.Sec100 := byte (Sec100);
  206. DosSetDateTime (DT);
  207. end;
  208. procedure getcbreak(var breakvalue:boolean);
  209. begin
  210. breakvalue := True;
  211. end;
  212. procedure setcbreak(breakvalue:boolean);
  213. begin
  214. end;
  215. procedure getverify(var verify:boolean);
  216. begin
  217. verify := true;
  218. end;
  219. procedure setverify(verify:boolean);
  220. begin
  221. end;
  222. function DiskFree (Drive: byte): int64;
  223. var FI: TFSinfo;
  224. RC: cardinal;
  225. begin
  226. {In OS/2, we use the filesystem information.}
  227. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  228. if RC = 0 then
  229. DiskFree := int64 (FI.Free_Clusters) *
  230. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  231. else
  232. DiskFree := -1;
  233. end;
  234. function DiskSize (Drive: byte): int64;
  235. var FI: TFSinfo;
  236. RC: cardinal;
  237. begin
  238. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  239. if RC = 0 then
  240. DiskSize := int64 (FI.Total_Clusters) *
  241. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  242. else
  243. DiskSize := -1;
  244. end;
  245. procedure SearchRec2DosSearchRec (var F: SearchRec);
  246. begin
  247. end;
  248. procedure DosSearchRec2SearchRec (var F: SearchRec);
  249. type
  250. TRec = record
  251. T, D: word;
  252. end;
  253. begin
  254. with F do
  255. begin
  256. Name := FStat^.Name;
  257. Size := FStat^.FileSize;
  258. Attr := byte(FStat^.AttrFile and $FF);
  259. TRec (Time).T := FStat^.TimeLastWrite;
  260. TRec (Time).D := FStat^.DateLastWrite;
  261. end;
  262. end;
  263. procedure FindFirst (const Path: PathStr; Attr: word; var F: SearchRec);
  264. var Count: cardinal;
  265. begin
  266. {No error.}
  267. DosError := 0;
  268. New (F.FStat);
  269. F.Handle := longint ($FFFFFFFF);
  270. Count := 1;
  271. DosError := integer (DosFindFirst (Path, F.Handle,
  272. Attr and FindResvdMask, F.FStat, SizeOf (F.FStat^),
  273. Count, ilStandard));
  274. if (DosError = 0) and (Count = 0) then DosError := 18;
  275. DosSearchRec2SearchRec (F);
  276. end;
  277. procedure FindNext (var F: SearchRec);
  278. var
  279. Count: cardinal;
  280. begin
  281. {No error}
  282. DosError := 0;
  283. SearchRec2DosSearchRec (F);
  284. Count := 1;
  285. DosError := integer (DosFindNext (F.Handle, F.FStat, SizeOf (F.FStat^),
  286. Count));
  287. if (DosError = 0) and (Count = 0) then DosError := 18;
  288. DosSearchRec2SearchRec (F);
  289. end;
  290. procedure FindClose (var F: SearchRec);
  291. begin
  292. if F.Handle <> $FFFFFFFF then DosError := DosFindClose (F.Handle);
  293. Dispose (F.FStat);
  294. end;
  295. procedure swapvectors;
  296. {For TP compatibility, this exists.}
  297. begin
  298. end;
  299. function envcount:longint;
  300. begin
  301. envcount:=envc;
  302. end;
  303. function envstr (index : longint) : string;
  304. var hp:Pchar;
  305. begin
  306. if (index<=0) or (index>envcount) then
  307. begin
  308. envstr:='';
  309. exit;
  310. end;
  311. hp:=EnvP[index-1];
  312. envstr:=strpas(hp);
  313. end;
  314. function GetEnvPChar (EnvVar: string): PChar;
  315. (* The assembler version is more than three times as fast as Pascal. *)
  316. var
  317. P: PChar;
  318. begin
  319. EnvVar := UpCase (EnvVar);
  320. {$ASMMODE INTEL}
  321. asm
  322. cld
  323. mov edi, Environment
  324. lea esi, EnvVar
  325. xor eax, eax
  326. lodsb
  327. @NewVar:
  328. cmp byte ptr [edi], 0
  329. jz @Stop
  330. push eax { eax contains length of searched variable name }
  331. push esi { esi points to the beginning of the variable name }
  332. mov ecx, -1 { our character ('=' - see below) _must_ be found }
  333. mov edx, edi { pointer to beginning of variable name saved in edx }
  334. mov al, '=' { searching until '=' (end of variable name) }
  335. repne
  336. scasb { scan until '=' not found }
  337. neg ecx { what was the name length? }
  338. dec ecx { corrected }
  339. dec ecx { exclude the '=' character }
  340. pop esi { restore pointer to beginning of variable name }
  341. pop eax { restore length of searched variable name }
  342. push eax { and save both of them again for later use }
  343. push esi
  344. cmp ecx, eax { compare length of searched variable name with name }
  345. jnz @NotEqual { ... of currently found variable, jump if different }
  346. xchg edx, edi { pointer to current variable name restored in edi }
  347. repe
  348. cmpsb { compare till the end of variable name }
  349. xchg edx, edi { pointer to beginning of variable contents in edi }
  350. jz @Equal { finish if they're equal }
  351. @NotEqual:
  352. xor eax, eax { look for 00h }
  353. mov ecx, -1 { it _must_ be found }
  354. repne
  355. scasb { scan until found }
  356. pop esi { restore pointer to beginning of variable name }
  357. pop eax { restore length of searched variable name }
  358. jmp @NewVar { ... or continue with new variable otherwise }
  359. @Stop:
  360. xor eax, eax
  361. mov P, eax { Not found - return nil }
  362. jmp @End
  363. @Equal:
  364. pop esi { restore the stack position }
  365. pop eax
  366. mov P, edi { place pointer to variable contents in P }
  367. @End:
  368. end ['eax','ecx','edx','esi','edi'];
  369. GetEnvPChar := P;
  370. end;
  371. {$ASMMODE ATT}
  372. Function GetEnv(envvar: string): string;
  373. (* The assembler version is more than three times as fast as Pascal. *)
  374. begin
  375. GetEnv := StrPas (GetEnvPChar (EnvVar));
  376. end;
  377. procedure fsplit(path:pathstr;var dir:dirstr;var name:namestr;
  378. var ext:extstr);
  379. var p1,i : longint;
  380. dotpos : integer;
  381. begin
  382. { allow slash as backslash }
  383. for i:=1 to length(path) do
  384. if path[i]='/' then path[i]:='\';
  385. {Get drive name}
  386. p1:=pos(':',path);
  387. if p1>0 then
  388. begin
  389. dir:=path[1]+':';
  390. delete(path,1,p1);
  391. end
  392. else
  393. dir:='';
  394. { split the path and the name, there are no more path informtions }
  395. { if path contains no backslashes }
  396. while true do
  397. begin
  398. p1:=pos('\',path);
  399. if p1=0 then
  400. break;
  401. dir:=dir+copy(path,1,p1);
  402. delete(path,1,p1);
  403. end;
  404. { try to find out a extension }
  405. Ext:='';
  406. i:=Length(Path);
  407. DotPos:=256;
  408. While (i>0) Do
  409. Begin
  410. If (Path[i]='.') Then
  411. begin
  412. DotPos:=i;
  413. break;
  414. end;
  415. Dec(i);
  416. end;
  417. Ext:=Copy(Path,DotPos,255);
  418. Name:=Copy(Path,1,DotPos - 1);
  419. end;
  420. (*
  421. function FExpand (const Path: PathStr): PathStr;
  422. - declared in fexpand.inc
  423. *)
  424. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  425. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  426. {$I fexpand.inc}
  427. {$UNDEF FPC_FEXPAND_DRIVES}
  428. {$UNDEF FPC_FEXPAND_UNC}
  429. procedure packtime(var t:datetime;var p:longint);
  430. var zs:longint;
  431. begin
  432. p:=-1980;
  433. p:=p+t.year and 127;
  434. p:=p shl 4;
  435. p:=p+t.month;
  436. p:=p shl 5;
  437. p:=p+t.day;
  438. p:=p shl 16;
  439. zs:=t.hour;
  440. zs:=zs shl 6;
  441. zs:=zs+t.min;
  442. zs:=zs shl 5;
  443. zs:=zs+t.sec div 2;
  444. p:=p+(zs and $ffff);
  445. end;
  446. procedure unpacktime (p:longint;var t:datetime);
  447. begin
  448. t.sec:=(p and 31) * 2;
  449. p:=p shr 5;
  450. t.min:=p and 63;
  451. p:=p shr 6;
  452. t.hour:=p and 31;
  453. p:=p shr 5;
  454. t.day:=p and 31;
  455. p:=p shr 5;
  456. t.month:=p and 15;
  457. p:=p shr 4;
  458. t.year:=p+1980;
  459. end;
  460. procedure GetFAttr (var F; var Attr: word);
  461. var
  462. PathInfo: TFileStatus3;
  463. RC: cardinal;
  464. begin
  465. Attr := 0;
  466. RC := DosQueryPathInfo (FileRec (F).Name, ilStandard,
  467. @PathInfo, SizeOf (PathInfo));
  468. DosError := integer (RC);
  469. if RC = 0 then
  470. Attr := PathInfo.AttrFile;
  471. end;
  472. procedure SetFAttr (var F; Attr: word);
  473. var
  474. PathInfo: TFileStatus3;
  475. RC: cardinal;
  476. begin
  477. RC := DosQueryPathInfo (FileRec (F).Name, ilStandard,
  478. @PathInfo, SizeOf (PathInfo));
  479. if RC = 0 then
  480. begin
  481. PathInfo.AttrFile := Attr;
  482. RC := DosSetPathInfo (FileRec (F).Name, ilStandard, @PathInfo,
  483. SizeOf (PathInfo), doWriteThru);
  484. end;
  485. DosError := integer (RC);
  486. end;
  487. {******************************************************************************
  488. --- Not Supported ---
  489. ******************************************************************************}
  490. procedure Keep (ExitCode: word);
  491. begin
  492. end;
  493. procedure GetIntVec (IntNo: byte; var Vector: pointer);
  494. begin
  495. end;
  496. procedure SetIntVec (IntNo: byte; Vector: pointer);
  497. begin
  498. end;
  499. procedure Intr (IntNo: byte; var Regs: Registers);
  500. begin
  501. end;
  502. procedure MsDos (var Regs: Registers);
  503. begin
  504. end;
  505. function GetShortName(var p : String) : boolean;
  506. begin
  507. GetShortName:=true;
  508. {$WARNING EA .shortname support (see FAT32 driver) should be probably added here!}
  509. end;
  510. function GetLongName(var p : String) : boolean;
  511. begin
  512. GetLongName:=true;
  513. {$WARNING EA .longname support should be probably added here!}
  514. end;
  515. begin
  516. LastDosExitCode := 0;
  517. LastDosErrorModuleName := '';
  518. ExecFlags := 0;
  519. end.
  520. {
  521. $Log$
  522. Revision 1.40 2004-03-21 20:22:20 hajny
  523. * Exec cleanup
  524. Revision 1.39 2004/02/22 15:01:49 hajny
  525. * lots of fixes (regcall, THandle, string operations in sysutils, longint2cardinal according to OS/2 docs, dosh.inc, ...)
  526. Revision 1.38 2004/02/17 17:37:26 daniel
  527. * Enable threadvars again
  528. Revision 1.37 2004/02/16 22:16:59 hajny
  529. * LastDosExitCode changed back from threadvar temporarily
  530. Revision 1.36 2004/02/15 21:34:06 hajny
  531. * overloaded ExecuteProcess added, EnvStr param changed to longint
  532. Revision 1.35 2004/02/15 08:02:44 yuri
  533. * fixes for dosh.inc
  534. * Executeprocess iverloaded function
  535. * updated todo
  536. Revision 1.34 2004/02/09 12:03:16 michael
  537. + Switched to single interface in dosh.inc
  538. Revision 1.33 2003/11/05 09:13:59 yuri
  539. * exec fix
  540. * unused units removed
  541. Revision 1.32 2003/11/02 09:45:32 hajny
  542. SetFTime fix
  543. Revision 1.31 2003/11/01 18:35:12 hajny
  544. * GetFTime correction for case of no previous write access
  545. Revision 1.30 2003/10/25 23:55:22 hajny
  546. * Exec fix
  547. Revision 1.29 2003/10/25 22:45:37 hajny
  548. * file handling related fixes
  549. Revision 1.28 2003/10/05 22:06:43 hajny
  550. * result buffers must be allocated
  551. Revision 1.27 2003/10/03 21:46:41 peter
  552. * stdcall fixes
  553. Revision 1.26 2003/09/24 08:59:16 yuri
  554. * Prepared for native target (emx code replaced)
  555. Revision 1.25 2003/02/20 17:37:00 hajny
  556. * correction for previous mistyping
  557. Revision 1.24 2003/02/20 17:09:49 hajny
  558. * fixes for OS/2 v2.1 incompatibility
  559. Revision 1.23 2003/01/04 15:43:50 hajny
  560. + GetEnvPChar added
  561. Revision 1.22 2002/12/07 19:46:56 hajny
  562. * mistyping fixed
  563. Revision 1.21 2002/12/07 19:17:13 hajny
  564. * GetEnv correction, better PM support, ...
  565. Revision 1.20 2002/11/18 19:51:00 hajny
  566. * another bunch of type corrections
  567. Revision 1.19 2002/09/07 16:01:24 peter
  568. * old logs removed and tabs fixed
  569. Revision 1.18 2002/07/11 16:00:05 hajny
  570. * FindFirst fix (invalid attribute bits masked out)
  571. Revision 1.17 2002/07/07 18:00:48 hajny
  572. * DosGetInfoBlock modification to allow overloaded version (in DosCalls)
  573. Revision 1.16 2002/03/03 11:19:20 hajny
  574. * GetEnv rewritten to assembly - 3x faster now
  575. }