dos.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. {****************************************************************************
  2. Free Pascal Runtime-Library
  3. DOS unit for OS/2
  4. Copyright (c) 1997,1999-2000 by Daniel Mantione,
  5. member of the Free Pascal development team
  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. unit dos;
  13. {$ASMMODE ATT}
  14. {***************************************************************************}
  15. interface
  16. {***************************************************************************}
  17. {$PACKRECORDS 1}
  18. uses Strings, DosCalls;
  19. Type
  20. {Search record which is used by findfirst and findnext:}
  21. SearchRec = record
  22. case boolean of
  23. false: (Handle: THandle; {Used in os_OS2 mode}
  24. FStat: PFileFindBuf3;
  25. Fill: array [1..21 - SizeOf (THandle) - SizeOf (pointer)]
  26. of byte;
  27. Attr: byte;
  28. Time: longint;
  29. Size: longint;
  30. Name: string); {Filenames can be long in OS/2!}
  31. true: (Fill2: array [1..21] of byte;
  32. Attr2: byte;
  33. Time2: longint;
  34. Size2: longint;
  35. Name2: string); {Filenames can be long in OS/2!}
  36. end;
  37. {Flags for the exec procedure:
  38. }
  39. threadvar
  40. (* For compatibility with VP/2, used for runflags in Exec procedure. *)
  41. ExecFlags: cardinal;
  42. {$i dosh.inc}
  43. {OS/2 specific functions}
  44. function GetEnvPChar (EnvVar: string): PChar;
  45. function DosErrorModuleName: string;
  46. (* In case of an error in Dos.Exec returns the name of the module *)
  47. (* causing the problem - e.g. name of a missing or corrupted DLL. *)
  48. implementation
  49. {$DEFINE HAS_GETMSCOUNT}
  50. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  51. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  52. {$DEFINE FPC_FEXPAND_GETENV_PCHAR}
  53. {$I dos.inc}
  54. threadvar
  55. LastDosErrorModuleName: string;
  56. ExecCounter: cardinal;
  57. const FindResvdMask = $00003737; {Allowed bits in attribute
  58. specification for DosFindFirst call.}
  59. function GetMsCount: int64;
  60. var
  61. L: cardinal;
  62. begin
  63. DosQuerySysInfo (svMsCount, svMsCount, L, 4);
  64. GetMsCount := L;
  65. end;
  66. function fsearch(path:pathstr;dirlist:string):pathstr;
  67. Var
  68. A: array [0..255] of char;
  69. D, P: AnsiString;
  70. begin
  71. P:=Path;
  72. D:=DirList;
  73. DosError := DosSearchPath (dsIgnoreNetErrs, PChar(D), PChar(P), @A, 255);
  74. fsearch := StrPas (@A);
  75. end;
  76. procedure getftime(var f;var time:longint);
  77. var
  78. FStat: TFileStatus3;
  79. begin
  80. DosError := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  81. SizeOf (FStat));
  82. if DosError=0 then
  83. begin
  84. Time := FStat.TimeLastWrite + longint (FStat.DateLastWrite) shl 16;
  85. if Time = 0 then
  86. Time := FStat.TimeCreation + longint (FStat.DateCreation) shl 16;
  87. end else
  88. Time:=0;
  89. end;
  90. procedure SetFTime (var F; Time: longint);
  91. var FStat: TFileStatus3;
  92. RC: cardinal;
  93. begin
  94. RC := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  95. SizeOf (FStat));
  96. if RC = 0 then
  97. begin
  98. FStat.DateLastAccess := Hi (Time);
  99. FStat.DateLastWrite := Hi (Time);
  100. FStat.TimeLastAccess := Lo (Time);
  101. FStat.TimeLastWrite := Lo (Time);
  102. RC := DosSetFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  103. SizeOf (FStat));
  104. end;
  105. DosError := integer (RC);
  106. end;
  107. procedure Exec (const Path: PathStr; const ComLine: ComStr);
  108. {Execute a program.}
  109. var
  110. Args0, Args: PByteArray;
  111. ArgSize: word;
  112. Res: TResultCodes;
  113. ObjName: string;
  114. RC: cardinal;
  115. ExecAppType: cardinal;
  116. HQ: THandle;
  117. SPID, STID, SCtr, QName: string;
  118. SID, PID: cardinal;
  119. SD: TStartData;
  120. RD: TRequestData;
  121. PCI: PChildInfo;
  122. CISize: cardinal;
  123. Prio: byte;
  124. DSS: boolean;
  125. SR: SearchRec;
  126. const
  127. MaxArgsSize = 3072; (* Amount of memory reserved for arguments in bytes. *)
  128. begin
  129. { LastDosExitCode := Exec (Path, ExecRunFlags (ExecFlags), efDefault, ComLine);}
  130. ObjName := '';
  131. (* FExpand should be used only for the DosStartSession part
  132. and only if the executable is in the current directory. *)
  133. FindFirst (Path, AnyFile, SR);
  134. if DosError = 0 then
  135. QName := FExpand (Path)
  136. else
  137. QName := Path;
  138. FindClose (SR);
  139. if ComLine = '' then
  140. begin
  141. Args0 := nil;
  142. Args := nil;
  143. end
  144. else
  145. begin
  146. GetMem (Args0, MaxArgsSize);
  147. Args := Args0;
  148. (* Work around a bug in OS/2 - argument to DosExecPgm *)
  149. (* should not cross 64K boundary. *)
  150. if ((PtrUInt (Args) + 1024) and $FFFF) < 1024 then
  151. Inc (pointer (Args), 1024);
  152. ArgSize := 0;
  153. Move (QName [1], Args^ [ArgSize], Length (QName));
  154. Inc (ArgSize, Length (QName));
  155. Args^ [ArgSize] := 0;
  156. Inc (ArgSize);
  157. {Now do the real arguments.}
  158. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  159. Inc (ArgSize, Length (ComLine));
  160. Args^ [ArgSize] := 0;
  161. Inc (ArgSize);
  162. Args^ [ArgSize] := 0;
  163. end;
  164. if (DosQueryAppType (PChar (Args), ExecAppType) = 0) and
  165. (ApplicationType and 3 = ExecAppType and 3) then
  166. (* DosExecPgm should work... *)
  167. begin
  168. DSS := false;
  169. Res.ExitCode := $FFFFFFFF;
  170. RC := DosExecPgm (ObjName, cardinal (ExecFlags), Args, nil, Res, Path);
  171. if RC = 0 then
  172. begin
  173. LastDosExitCode := Res.ExitCode;
  174. LastDosErrorModuleName := '';
  175. end
  176. else
  177. if (RC = 190) or (RC = 191) then
  178. DSS := true;
  179. end
  180. else
  181. DSS := true;
  182. if DSS then
  183. begin
  184. FillChar (SD, SizeOf (SD), 0);
  185. SD.Length := SizeOf (SD);
  186. SD.Related := ssf_Related_Child;
  187. if Args = nil then
  188. (* No parameters passed, Args not allocated for DosExecPgm, so allocate now. *)
  189. begin
  190. GetMem (Args0, MaxArgsSize);
  191. Args := Args0;
  192. Move (QName [1], Args^ [0], Length (QName));
  193. Args^ [Length (QName)] := 0;
  194. end
  195. else
  196. SD.PgmInputs := PChar (@Args^ [Length (QName) + 1]);
  197. SD.PgmName := PChar (Args);
  198. SD.InheritOpt := ssf_InhertOpt_Parent;
  199. Str (GetProcessID, SPID);
  200. Str (ThreadID, STID);
  201. Str (ExecCounter, SCtr);
  202. Inc (ExecCounter);
  203. QName := '\QUEUES\FPC_Dos_Exec_p' + SPID + 't' + STID + '.QUE'#0;
  204. SD.TermQ := @QName [1];
  205. SD.ObjectBuffer := @ObjName [1];
  206. SD.ObjectBuffLen := SizeOf (ObjName) - 1;
  207. RC := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  208. if RC <> 0 then
  209. ObjName := Copy (QName, 1, Pred (Length (QName)))
  210. else
  211. begin
  212. RC := DosStartSession (SD, SID, PID);
  213. if (RC = 0) or (RC = 457) then
  214. begin
  215. RC := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  216. if RC = 0 then
  217. begin
  218. LastDosExitCode := PCI^.Return;
  219. DosCloseQueue (HQ);
  220. DosFreeMem (PCI);
  221. end
  222. else
  223. DosCloseQueue (HQ);
  224. end
  225. else
  226. DosCloseQueue (HQ);
  227. end;
  228. end;
  229. if RC <> 0 then
  230. begin
  231. LastDosErrorModuleName := ObjName;
  232. LastDosExitCode := 0; (* Needed for TP/BP compatibility *)
  233. end;
  234. DosError := RC;
  235. if Args0 <> nil then
  236. FreeMem (Args0, MaxArgsSize);
  237. end;
  238. function DosErrorModuleName: string;
  239. begin
  240. DosErrorModuleName := LastDosErrorModuleName;
  241. end;
  242. function dosversion:word;
  243. {Returns OS/2 version}
  244. var
  245. Minor, Major: Cardinal;
  246. begin
  247. DosQuerySysInfo(svMajorVersion, svMajorVersion, Major, 4);
  248. DosQuerySysInfo(svMinorVersion, svMinorVersion, Minor, 4);
  249. DosVersion:=Major or Minor shl 8;
  250. end;
  251. procedure GetDate (var Year, Month, MDay, WDay: word);
  252. Var
  253. dt: TDateTime;
  254. begin
  255. DosGetDateTime(dt);
  256. Year:=dt.year;
  257. Month:=dt.month;
  258. MDay:=dt.Day;
  259. WDay:=dt.Weekday;
  260. end;
  261. procedure SetDate (Year, Month, Day: word);
  262. var
  263. DT: TDateTime;
  264. begin
  265. DosGetDateTime (DT);
  266. DT.Year := Year;
  267. DT.Month := byte (Month);
  268. DT.Day := byte (Day);
  269. DosSetDateTime (DT);
  270. end;
  271. procedure GetTime (var Hour, Minute, Second, Sec100: word);
  272. var
  273. dt: TDateTime;
  274. begin
  275. DosGetDateTime(dt);
  276. Hour:=dt.Hour;
  277. Minute:=dt.Minute;
  278. Second:=dt.Second;
  279. Sec100:=dt.Hundredths;
  280. end;
  281. procedure SetTime (Hour, Minute, Second, Sec100: word);
  282. var
  283. DT: TDateTime;
  284. begin
  285. DosGetDateTime (DT);
  286. DT.Hour := byte (Hour);
  287. DT.Minute := byte (Minute);
  288. DT.Second := byte (Second);
  289. DT.Sec100 := byte (Sec100);
  290. DosSetDateTime (DT);
  291. end;
  292. function DiskFree (Drive: byte): int64;
  293. var FI: TFSinfo;
  294. RC: cardinal;
  295. begin
  296. {In OS/2, we use the filesystem information.}
  297. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  298. if RC = 0 then
  299. DiskFree := int64 (FI.Free_Clusters) *
  300. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  301. else
  302. DiskFree := -1;
  303. end;
  304. function DiskSize (Drive: byte): int64;
  305. var FI: TFSinfo;
  306. RC: cardinal;
  307. begin
  308. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  309. if RC = 0 then
  310. DiskSize := int64 (FI.Total_Clusters) *
  311. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  312. else
  313. DiskSize := -1;
  314. end;
  315. procedure DosSearchRec2SearchRec (var F: SearchRec);
  316. type
  317. TRec = record
  318. T, D: word;
  319. end;
  320. begin
  321. with F do
  322. begin
  323. Name := FStat^.Name;
  324. Size := FStat^.FileSize;
  325. Attr := byte(FStat^.AttrFile and $FF);
  326. TRec (Time).T := FStat^.TimeLastWrite;
  327. TRec (Time).D := FStat^.DateLastWrite;
  328. end;
  329. end;
  330. procedure FindFirst (const Path: PathStr; Attr: word; var F: SearchRec);
  331. var Count: cardinal;
  332. begin
  333. {No error.}
  334. DosError := 0;
  335. New (F.FStat);
  336. F.Handle := THandle ($FFFFFFFF);
  337. Count := 1;
  338. DosError := integer (DosFindFirst (Path, F.Handle,
  339. Attr and FindResvdMask, F.FStat, SizeOf (F.FStat^),
  340. Count, ilStandard));
  341. if (DosError = 0) and (Count = 0) then DosError := 18;
  342. DosSearchRec2SearchRec (F);
  343. end;
  344. procedure FindNext (var F: SearchRec);
  345. var
  346. Count: cardinal;
  347. begin
  348. {No error}
  349. DosError := 0;
  350. Count := 1;
  351. DosError := integer (DosFindNext (F.Handle, F.FStat, SizeOf (F.FStat^),
  352. Count));
  353. if (DosError = 0) and (Count = 0) then DosError := 18;
  354. DosSearchRec2SearchRec (F);
  355. end;
  356. procedure FindClose (var F: SearchRec);
  357. begin
  358. if F.Handle <> THandle ($FFFFFFFF) then DosError := DosFindClose (F.Handle);
  359. Dispose (F.FStat);
  360. end;
  361. function envcount:longint;
  362. begin
  363. envcount:=envc;
  364. end;
  365. function envstr (index : longint) : string;
  366. var hp:Pchar;
  367. begin
  368. if (index<=0) or (index>envcount) then
  369. begin
  370. envstr:='';
  371. exit;
  372. end;
  373. hp:=EnvP[index-1];
  374. envstr:=strpas(hp);
  375. end;
  376. function GetEnvPChar (EnvVar: string): PChar;
  377. (* The assembler version is more than three times as fast as Pascal. *)
  378. var
  379. P: PChar;
  380. begin
  381. EnvVar := UpCase (EnvVar);
  382. {$ASMMODE INTEL}
  383. asm
  384. cld
  385. mov edi, Environment
  386. lea esi, EnvVar
  387. xor eax, eax
  388. lodsb
  389. @NewVar:
  390. cmp byte ptr [edi], 0
  391. jz @Stop
  392. push eax { eax contains length of searched variable name }
  393. push esi { esi points to the beginning of the variable name }
  394. mov ecx, -1 { our character ('=' - see below) _must_ be found }
  395. mov edx, edi { pointer to beginning of variable name saved in edx }
  396. mov al, '=' { searching until '=' (end of variable name) }
  397. repne
  398. scasb { scan until '=' not found }
  399. neg ecx { what was the name length? }
  400. dec ecx { corrected }
  401. dec ecx { exclude the '=' character }
  402. pop esi { restore pointer to beginning of variable name }
  403. pop eax { restore length of searched variable name }
  404. push eax { and save both of them again for later use }
  405. push esi
  406. cmp ecx, eax { compare length of searched variable name with name }
  407. jnz @NotEqual { ... of currently found variable, jump if different }
  408. xchg edx, edi { pointer to current variable name restored in edi }
  409. repe
  410. cmpsb { compare till the end of variable name }
  411. xchg edx, edi { pointer to beginning of variable contents in edi }
  412. jz @Equal { finish if they're equal }
  413. @NotEqual:
  414. xor eax, eax { look for 00h }
  415. mov ecx, -1 { it _must_ be found }
  416. repne
  417. scasb { scan until found }
  418. pop esi { restore pointer to beginning of variable name }
  419. pop eax { restore length of searched variable name }
  420. jmp @NewVar { ... or continue with new variable otherwise }
  421. @Stop:
  422. xor eax, eax
  423. mov P, eax { Not found - return nil }
  424. jmp @End
  425. @Equal:
  426. pop esi { restore the stack position }
  427. pop eax
  428. mov P, edi { place pointer to variable contents in P }
  429. @End:
  430. end ['eax','ecx','edx','esi','edi'];
  431. GetEnvPChar := P;
  432. end;
  433. {$ASMMODE ATT}
  434. Function GetEnv(envvar: string): string;
  435. (* The assembler version is more than three times as fast as Pascal. *)
  436. begin
  437. GetEnv := StrPas (GetEnvPChar (EnvVar));
  438. end;
  439. procedure GetFAttr (var F; var Attr: word);
  440. var
  441. PathInfo: TFileStatus3;
  442. RC: cardinal;
  443. begin
  444. Attr := 0;
  445. RC := DosQueryPathInfo (@FileRec (F).Name, ilStandard,
  446. @PathInfo, SizeOf (PathInfo));
  447. DosError := integer (RC);
  448. if RC = 0 then
  449. Attr := PathInfo.AttrFile;
  450. end;
  451. procedure SetFAttr (var F; Attr: word);
  452. var
  453. PathInfo: TFileStatus3;
  454. RC: cardinal;
  455. begin
  456. RC := DosQueryPathInfo (@FileRec (F).Name, ilStandard,
  457. @PathInfo, SizeOf (PathInfo));
  458. if RC = 0 then
  459. begin
  460. PathInfo.AttrFile := Attr;
  461. RC := DosSetPathInfo (@FileRec (F).Name, ilStandard, @PathInfo,
  462. SizeOf (PathInfo), doWriteThru);
  463. end;
  464. DosError := integer (RC);
  465. end;
  466. {function GetShortName(var p : String) : boolean;
  467. begin
  468. GetShortName:=true;}
  469. {$WARNING EA .shortname support (see FAT32 driver) should be probably added here!}
  470. {end;
  471. function GetLongName(var p : String) : boolean;
  472. begin
  473. GetLongName:=true;}
  474. {$WARNING EA .longname support should be probably added here!}
  475. {end;}
  476. begin
  477. LastDosExitCode := 0;
  478. LastDosErrorModuleName := '';
  479. ExecFlags := 0;
  480. ExecCounter := 0;
  481. end.