dos.pas 13 KB

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