dos.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. Type
  21. {Search record which is used by findfirst and findnext:}
  22. SearchRec = record
  23. case boolean of
  24. false: (Handle: THandle; {Used in os_OS2 mode}
  25. FStat: PFileFindBuf3;
  26. Fill: array [1..21 - SizeOf (THandle) - SizeOf (pointer)]
  27. of byte;
  28. Attr: byte;
  29. Time: longint;
  30. Size: longint;
  31. Name: string); {Filenames can be long in OS/2!}
  32. true: (Fill2: array [1..21] of byte;
  33. Attr2: byte;
  34. Time2: longint;
  35. Size2: longint;
  36. Name2: string); {Filenames can be long in OS/2!}
  37. end;
  38. {Flags for the exec procedure:
  39. }
  40. {$ifdef HASTHREADVAR}
  41. threadvar
  42. {$else HASTHREADVAR}
  43. var
  44. {$endif HASTHREADVAR}
  45. (* For compatibility with VP/2, used for runflags in Exec procedure. *)
  46. ExecFlags: cardinal;
  47. {$i dosh.inc}
  48. {OS/2 specific functions}
  49. function GetEnvPChar (EnvVar: string): PChar;
  50. function DosErrorModuleName: string;
  51. (* In case of an error in Dos.Exec returns the name of the module *)
  52. (* causing the problem - e.g. name of a missing or corrupted DLL. *)
  53. implementation
  54. {$DEFINE HAS_GETMSCOUNT}
  55. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  56. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  57. {$DEFINE FPC_FEXPAND_GETENV_PCHAR}
  58. {$I dos.inc}
  59. {$ifdef HASTHREADVAR}
  60. threadvar
  61. {$else HASTHREADVAR}
  62. var
  63. {$endif HASTHREADVAR}
  64. LastDosErrorModuleName: string;
  65. const FindResvdMask = $00003737; {Allowed bits in attribute
  66. specification for DosFindFirst call.}
  67. function GetMsCount: int64;
  68. var
  69. L: cardinal;
  70. begin
  71. DosQuerySysInfo (svMsCount, svMsCount, L, 4);
  72. GetMsCount := L;
  73. end;
  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. RC: longint;
  122. HQ: THandle;
  123. SPID, STID, QName: string;
  124. SD: TStartData;
  125. SID, PID: cardinal;
  126. RD: TRequestData;
  127. PCI: PChildInfo;
  128. CISize: cardinal;
  129. Prio: byte;
  130. const
  131. MaxArgsSize = 2048; (* Amount of memory reserved for arguments in bytes. *)
  132. begin
  133. { LastDosExitCode := Exec (Path, ExecRunFlags (ExecFlags), efDefault, ComLine);}
  134. QName := FExpand (Path);
  135. if ComLine = '' then
  136. Args := nil
  137. else
  138. begin
  139. GetMem (Args, MaxArgsSize);
  140. ArgSize := 0;
  141. Move (QName [1], Args^ [ArgSize], Length (QName));
  142. Inc (ArgSize, Length (QName));
  143. Args^ [ArgSize] := 0;
  144. Inc (ArgSize);
  145. {Now do the real arguments.}
  146. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  147. Inc (ArgSize, Length (ComLine));
  148. Args^ [ArgSize] := 0;
  149. Inc (ArgSize);
  150. Args^ [ArgSize] := 0;
  151. end;
  152. RC := DosExecPgm (ObjName, cardinal (ExecFlags), Args, nil, Res, Path);
  153. if RC = 0 then
  154. begin
  155. LastDosExitCode := Res.ExitCode;
  156. LastDosErrorModuleName := '';
  157. end
  158. else
  159. if (RC = 190) or (RC = 191) then
  160. begin
  161. FillChar (SD, SizeOf (SD), 0);
  162. SD.Length := 24;
  163. SD.Related := ssf_Related_Child;
  164. if Args = nil then
  165. (* No parameters passed, Args not allocated for DosExecPgm, so allocate now. *)
  166. begin
  167. GetMem (Args, MaxArgsSize);
  168. Move (QName [1], Args^ [0], Length (QName));
  169. Args^ [Length (QName)] := 0;
  170. end
  171. else
  172. SD.PgmInputs := PChar (@Args^ [Length (QName) + 1]);
  173. SD.PgmName := PChar (Args);
  174. SD.InheritOpt := ssf_InhertOpt_Parent;
  175. Str (GetProcessID, SPID);
  176. Str (ThreadID, STID);
  177. QName := '\QUEUES\FPC_Dos_Exec_p' + SPID + 't' + STID + '.QUE'#0;
  178. SD.TermQ := @QName [1];
  179. RC := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  180. if RC = 0 then
  181. begin
  182. RC := DosStartSession (SD, SID, PID);
  183. if (RC = 0) or (RC = 457) then
  184. begin
  185. RC := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  186. if RC = 0 then
  187. begin
  188. LastDosExitCode := PCI^.Return;
  189. DosCloseQueue (HQ);
  190. DosFreeMem (PCI);
  191. end
  192. else
  193. DosCloseQueue (HQ);
  194. end
  195. else
  196. DosCloseQueue (HQ);
  197. end;
  198. end
  199. else
  200. LastDosErrorModuleName := ObjName;
  201. if RC <> 0 then
  202. LastDosExitCode := 0; (* Needed for TP/BP compatibility *)
  203. DosError := RC;
  204. if Args <> nil then
  205. FreeMem (Args, MaxArgsSize);
  206. end;
  207. function DosErrorModuleName: string;
  208. begin
  209. DosErrorModuleName := LastDosErrorModuleName;
  210. end;
  211. function dosversion:word;
  212. {Returns OS/2 version}
  213. var
  214. Minor, Major: Cardinal;
  215. begin
  216. DosQuerySysInfo(svMajorVersion, svMajorVersion, Major, 4);
  217. DosQuerySysInfo(svMinorVersion, svMinorVersion, Minor, 4);
  218. DosVersion:=Major or Minor shl 8;
  219. end;
  220. procedure GetDate (var Year, Month, MDay, WDay: word);
  221. Var
  222. dt: TDateTime;
  223. begin
  224. DosGetDateTime(dt);
  225. Year:=dt.year;
  226. Month:=dt.month;
  227. MDay:=dt.Day;
  228. WDay:=dt.Weekday;
  229. end;
  230. procedure SetDate (Year, Month, Day: word);
  231. var
  232. DT: TDateTime;
  233. begin
  234. DosGetDateTime (DT);
  235. DT.Year := Year;
  236. DT.Month := byte (Month);
  237. DT.Day := byte (Day);
  238. DosSetDateTime (DT);
  239. end;
  240. procedure GetTime (var Hour, Minute, Second, Sec100: word);
  241. var
  242. dt: TDateTime;
  243. begin
  244. DosGetDateTime(dt);
  245. Hour:=dt.Hour;
  246. Minute:=dt.Minute;
  247. Second:=dt.Second;
  248. Sec100:=dt.Hundredths;
  249. end;
  250. procedure SetTime (Hour, Minute, Second, Sec100: word);
  251. var
  252. DT: TDateTime;
  253. begin
  254. DosGetDateTime (DT);
  255. DT.Hour := byte (Hour);
  256. DT.Minute := byte (Minute);
  257. DT.Second := byte (Second);
  258. DT.Sec100 := byte (Sec100);
  259. DosSetDateTime (DT);
  260. end;
  261. function DiskFree (Drive: byte): int64;
  262. var FI: TFSinfo;
  263. RC: cardinal;
  264. begin
  265. {In OS/2, we use the filesystem information.}
  266. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  267. if RC = 0 then
  268. DiskFree := int64 (FI.Free_Clusters) *
  269. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  270. else
  271. DiskFree := -1;
  272. end;
  273. function DiskSize (Drive: byte): int64;
  274. var FI: TFSinfo;
  275. RC: cardinal;
  276. begin
  277. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  278. if RC = 0 then
  279. DiskSize := int64 (FI.Total_Clusters) *
  280. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  281. else
  282. DiskSize := -1;
  283. end;
  284. procedure DosSearchRec2SearchRec (var F: SearchRec);
  285. type
  286. TRec = record
  287. T, D: word;
  288. end;
  289. begin
  290. with F do
  291. begin
  292. Name := FStat^.Name;
  293. Size := FStat^.FileSize;
  294. Attr := byte(FStat^.AttrFile and $FF);
  295. TRec (Time).T := FStat^.TimeLastWrite;
  296. TRec (Time).D := FStat^.DateLastWrite;
  297. end;
  298. end;
  299. procedure FindFirst (const Path: PathStr; Attr: word; var F: SearchRec);
  300. var Count: cardinal;
  301. begin
  302. {No error.}
  303. DosError := 0;
  304. New (F.FStat);
  305. F.Handle := THandle ($FFFFFFFF);
  306. Count := 1;
  307. DosError := integer (DosFindFirst (Path, F.Handle,
  308. Attr and FindResvdMask, F.FStat, SizeOf (F.FStat^),
  309. Count, ilStandard));
  310. if (DosError = 0) and (Count = 0) then DosError := 18;
  311. DosSearchRec2SearchRec (F);
  312. end;
  313. procedure FindNext (var F: SearchRec);
  314. var
  315. Count: cardinal;
  316. begin
  317. {No error}
  318. DosError := 0;
  319. Count := 1;
  320. DosError := integer (DosFindNext (F.Handle, F.FStat, SizeOf (F.FStat^),
  321. Count));
  322. if (DosError = 0) and (Count = 0) then DosError := 18;
  323. DosSearchRec2SearchRec (F);
  324. end;
  325. procedure FindClose (var F: SearchRec);
  326. begin
  327. if F.Handle <> THandle ($FFFFFFFF) then DosError := DosFindClose (F.Handle);
  328. Dispose (F.FStat);
  329. end;
  330. function envcount:longint;
  331. begin
  332. envcount:=envc;
  333. end;
  334. function envstr (index : longint) : string;
  335. var hp:Pchar;
  336. begin
  337. if (index<=0) or (index>envcount) then
  338. begin
  339. envstr:='';
  340. exit;
  341. end;
  342. hp:=EnvP[index-1];
  343. envstr:=strpas(hp);
  344. end;
  345. function GetEnvPChar (EnvVar: string): PChar;
  346. (* The assembler version is more than three times as fast as Pascal. *)
  347. var
  348. P: PChar;
  349. begin
  350. EnvVar := UpCase (EnvVar);
  351. {$ASMMODE INTEL}
  352. asm
  353. cld
  354. mov edi, Environment
  355. lea esi, EnvVar
  356. xor eax, eax
  357. lodsb
  358. @NewVar:
  359. cmp byte ptr [edi], 0
  360. jz @Stop
  361. push eax { eax contains length of searched variable name }
  362. push esi { esi points to the beginning of the variable name }
  363. mov ecx, -1 { our character ('=' - see below) _must_ be found }
  364. mov edx, edi { pointer to beginning of variable name saved in edx }
  365. mov al, '=' { searching until '=' (end of variable name) }
  366. repne
  367. scasb { scan until '=' not found }
  368. neg ecx { what was the name length? }
  369. dec ecx { corrected }
  370. dec ecx { exclude the '=' character }
  371. pop esi { restore pointer to beginning of variable name }
  372. pop eax { restore length of searched variable name }
  373. push eax { and save both of them again for later use }
  374. push esi
  375. cmp ecx, eax { compare length of searched variable name with name }
  376. jnz @NotEqual { ... of currently found variable, jump if different }
  377. xchg edx, edi { pointer to current variable name restored in edi }
  378. repe
  379. cmpsb { compare till the end of variable name }
  380. xchg edx, edi { pointer to beginning of variable contents in edi }
  381. jz @Equal { finish if they're equal }
  382. @NotEqual:
  383. xor eax, eax { look for 00h }
  384. mov ecx, -1 { it _must_ be found }
  385. repne
  386. scasb { scan until found }
  387. pop esi { restore pointer to beginning of variable name }
  388. pop eax { restore length of searched variable name }
  389. jmp @NewVar { ... or continue with new variable otherwise }
  390. @Stop:
  391. xor eax, eax
  392. mov P, eax { Not found - return nil }
  393. jmp @End
  394. @Equal:
  395. pop esi { restore the stack position }
  396. pop eax
  397. mov P, edi { place pointer to variable contents in P }
  398. @End:
  399. end ['eax','ecx','edx','esi','edi'];
  400. GetEnvPChar := P;
  401. end;
  402. {$ASMMODE ATT}
  403. Function GetEnv(envvar: string): string;
  404. (* The assembler version is more than three times as fast as Pascal. *)
  405. begin
  406. GetEnv := StrPas (GetEnvPChar (EnvVar));
  407. end;
  408. procedure GetFAttr (var F; var Attr: word);
  409. var
  410. PathInfo: TFileStatus3;
  411. RC: cardinal;
  412. begin
  413. Attr := 0;
  414. RC := DosQueryPathInfo (@FileRec (F).Name, ilStandard,
  415. @PathInfo, SizeOf (PathInfo));
  416. DosError := integer (RC);
  417. if RC = 0 then
  418. Attr := PathInfo.AttrFile;
  419. end;
  420. procedure SetFAttr (var F; Attr: word);
  421. var
  422. PathInfo: TFileStatus3;
  423. RC: cardinal;
  424. begin
  425. RC := DosQueryPathInfo (@FileRec (F).Name, ilStandard,
  426. @PathInfo, SizeOf (PathInfo));
  427. if RC = 0 then
  428. begin
  429. PathInfo.AttrFile := Attr;
  430. RC := DosSetPathInfo (@FileRec (F).Name, ilStandard, @PathInfo,
  431. SizeOf (PathInfo), doWriteThru);
  432. end;
  433. DosError := integer (RC);
  434. end;
  435. {function GetShortName(var p : String) : boolean;
  436. begin
  437. GetShortName:=true;}
  438. {$WARNING EA .shortname support (see FAT32 driver) should be probably added here!}
  439. {end;
  440. function GetLongName(var p : String) : boolean;
  441. begin
  442. GetLongName:=true;}
  443. {$WARNING EA .longname support should be probably added here!}
  444. {end;}
  445. begin
  446. LastDosExitCode := 0;
  447. LastDosErrorModuleName := '';
  448. ExecFlags := 0;
  449. end.
  450. {
  451. $Log$
  452. Revision 1.43 2004-12-06 21:50:04 hajny
  453. * allow running any type of session from Exec
  454. Revision 1.42 2004/12/05 19:16:54 hajny
  455. * GetMsCount added, platform independent routines moved to single include file
  456. Revision 1.41 2004/05/23 21:47:34 hajny
  457. * final part of longint2cardinal fixes for doscalls
  458. Revision 1.40 2004/03/21 20:22:20 hajny
  459. * Exec cleanup
  460. Revision 1.39 2004/02/22 15:01:49 hajny
  461. * lots of fixes (regcall, THandle, string operations in sysutils, longint2cardinal according to OS/2 docs, dosh.inc, ...)
  462. Revision 1.38 2004/02/17 17:37:26 daniel
  463. * Enable threadvars again
  464. Revision 1.37 2004/02/16 22:16:59 hajny
  465. * LastDosExitCode changed back from threadvar temporarily
  466. Revision 1.36 2004/02/15 21:34:06 hajny
  467. * overloaded ExecuteProcess added, EnvStr param changed to longint
  468. Revision 1.35 2004/02/15 08:02:44 yuri
  469. * fixes for dosh.inc
  470. * Executeprocess iverloaded function
  471. * updated todo
  472. Revision 1.34 2004/02/09 12:03:16 michael
  473. + Switched to single interface in dosh.inc
  474. Revision 1.33 2003/11/05 09:13:59 yuri
  475. * exec fix
  476. * unused units removed
  477. Revision 1.32 2003/11/02 09:45:32 hajny
  478. SetFTime fix
  479. Revision 1.31 2003/11/01 18:35:12 hajny
  480. * GetFTime correction for case of no previous write access
  481. Revision 1.30 2003/10/25 23:55:22 hajny
  482. * Exec fix
  483. Revision 1.29 2003/10/25 22:45:37 hajny
  484. * file handling related fixes
  485. Revision 1.28 2003/10/05 22:06:43 hajny
  486. * result buffers must be allocated
  487. Revision 1.27 2003/10/03 21:46:41 peter
  488. * stdcall fixes
  489. Revision 1.26 2003/09/24 08:59:16 yuri
  490. * Prepared for native target (emx code replaced)
  491. Revision 1.25 2003/02/20 17:37:00 hajny
  492. * correction for previous mistyping
  493. Revision 1.24 2003/02/20 17:09:49 hajny
  494. * fixes for OS/2 v2.1 incompatibility
  495. Revision 1.23 2003/01/04 15:43:50 hajny
  496. + GetEnvPChar added
  497. Revision 1.22 2002/12/07 19:46:56 hajny
  498. * mistyping fixed
  499. Revision 1.21 2002/12/07 19:17:13 hajny
  500. * GetEnv correction, better PM support, ...
  501. Revision 1.20 2002/11/18 19:51:00 hajny
  502. * another bunch of type corrections
  503. Revision 1.19 2002/09/07 16:01:24 peter
  504. * old logs removed and tabs fixed
  505. Revision 1.18 2002/07/11 16:00:05 hajny
  506. * FindFirst fix (invalid attribute bits masked out)
  507. Revision 1.17 2002/07/07 18:00:48 hajny
  508. * DosGetInfoBlock modification to allow overloaded version (in DosCalls)
  509. Revision 1.16 2002/03/03 11:19:20 hajny
  510. * GetEnv rewritten to assembly - 3x faster now
  511. }