dos.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. (* Note that the TP/BP compatible method for retrieval of exit codes *)
  43. (* is limited to only one (the last) execution! Including the following *)
  44. (* two variables in the interface part allows querying the status of *)
  45. (* of asynchronously started programs using DosWaitChild with dtNoWait *)
  46. (* parameter, i.e. without waiting for the final program result (as *)
  47. (* opposed to calling DosExitCode which would wait for the exit code). *)
  48. LastExecRes: TResultCodes;
  49. LastExecFlags: cardinal;
  50. {$i dosh.inc}
  51. {OS/2 specific functions}
  52. function GetEnvPChar (EnvVar: string): PChar;
  53. function DosErrorModuleName: string;
  54. (* In case of an error in Dos.Exec returns the name of the module *)
  55. (* causing the problem - e.g. name of a missing or corrupted DLL. *)
  56. (* It may also contain a queue name in case of a failed attempt *)
  57. (* to create queue for reading results of started sessions. *)
  58. implementation
  59. {$DEFINE HAS_GETMSCOUNT}
  60. {$DEFINE HAS_DOSEXITCODE}
  61. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  62. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  63. {$DEFINE FPC_FEXPAND_GETENV_PCHAR}
  64. {$I dos.inc}
  65. threadvar
  66. LastDosErrorModuleName: string;
  67. const
  68. FindResvdMask = $00003737 {Allowed bits for DosFindFirst parameter Attribute}
  69. and $000000FF; {combined with a mask for allowed attributes only}
  70. function GetMsCount: int64;
  71. var
  72. L: cardinal;
  73. begin
  74. DosQuerySysInfo (svMsCount, svMsCount, L, 4);
  75. GetMsCount := L;
  76. end;
  77. function FSearch (Path: PathStr; DirList: string): PathStr;
  78. var
  79. i,p1 : longint;
  80. s : searchrec;
  81. newdir : pathstr;
  82. begin
  83. { check if the file specified exists }
  84. findfirst(path,anyfile and not(directory),s);
  85. if doserror=0 then
  86. begin
  87. findclose(s);
  88. fsearch:=path;
  89. exit;
  90. end;
  91. { No wildcards allowed in these things }
  92. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  93. fsearch:=''
  94. else
  95. begin
  96. { allow slash as backslash }
  97. DoDirSeparators(dirlist);
  98. repeat
  99. p1:=pos(';',dirlist);
  100. if p1<>0 then
  101. begin
  102. newdir:=copy(dirlist,1,p1-1);
  103. delete(dirlist,1,p1);
  104. end
  105. else
  106. begin
  107. newdir:=dirlist;
  108. dirlist:='';
  109. end;
  110. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  111. newdir:=newdir+'\';
  112. findfirst(newdir+path,anyfile and not(directory),s);
  113. if doserror=0 then
  114. newdir:=newdir+path
  115. else
  116. newdir:='';
  117. until (dirlist='') or (newdir<>'');
  118. fsearch:=newdir;
  119. end;
  120. findclose(s);
  121. end;
  122. procedure getftime(var f;var time:longint);
  123. var
  124. FStat: TFileStatus3;
  125. begin
  126. DosError := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  127. SizeOf (FStat));
  128. if DosError=0 then
  129. begin
  130. Time := FStat.TimeLastWrite + longint (FStat.DateLastWrite) shl 16;
  131. if Time = 0 then
  132. Time := FStat.TimeCreation + longint (FStat.DateCreation) shl 16;
  133. end
  134. else
  135. begin
  136. Time:=0;
  137. OSErrorWatch (DosError);
  138. if DosError = 87 then
  139. DosError := 6; (* Align to TP/BP behaviour *)
  140. end;
  141. end;
  142. procedure SetFTime (var F; Time: longint);
  143. var FStat: TFileStatus3;
  144. RC: cardinal;
  145. begin
  146. RC := DosQueryFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  147. SizeOf (FStat));
  148. if RC = 0 then
  149. begin
  150. FStat.DateLastAccess := Hi (Time);
  151. FStat.DateLastWrite := Hi (Time);
  152. FStat.TimeLastAccess := Lo (Time);
  153. FStat.TimeLastWrite := Lo (Time);
  154. RC := DosSetFileInfo (FileRec (F).Handle, ilStandard, @FStat,
  155. SizeOf (FStat));
  156. if RC <> 0 then
  157. OSErrorWatch (RC);
  158. end
  159. else
  160. begin
  161. OSErrorWatch (RC);
  162. if RC = 87 then
  163. RC := 6;
  164. end;
  165. DosError := integer (RC);
  166. end;
  167. function DosExitCode: word;
  168. var
  169. Res: TResultCodes;
  170. PPID: cardinal;
  171. RC: cardinal;
  172. begin
  173. if (LastExecFlags = deAsyncResult) or (LastExecFlags = deAsyncResultDb) then
  174. begin
  175. RC := DosWaitChild (DCWA_PROCESS, dtWait, Res, PPID, LastExecRes.PID);
  176. if RC = 0 then
  177. (* If we succeeded, the process is finished - possible future querying
  178. of DosExitCode shall return the result immediately as with synchronous
  179. execution. *)
  180. begin
  181. LastExecFlags := deSync;
  182. LastExecRes := Res;
  183. end
  184. else
  185. begin
  186. LastExecRes.ExitCode := RC shl 16;
  187. OSErrorWatch (RC);
  188. end;
  189. end;
  190. if LastExecRes.ExitCode > high (word) then
  191. DosExitCode := high (word)
  192. else
  193. DosExitCode := LastExecRes.ExitCode and $FFFF;
  194. end;
  195. procedure Exec (const Path: PathStr; const ComLine: ComStr);
  196. {Execute a program.}
  197. var
  198. Args0, Args: PByteArray;
  199. ArgSize: word;
  200. ObjName: string;
  201. Res: TResultCodes;
  202. RC, RC2: cardinal;
  203. ExecAppType: cardinal;
  204. HQ: THandle;
  205. SPID, STID, QName: string;
  206. SID, PID: cardinal;
  207. SD: TStartData;
  208. RD: TRequestData;
  209. PCI: PChildInfo;
  210. CISize: cardinal;
  211. Prio: byte;
  212. DSS: boolean;
  213. SR: SearchRec;
  214. MaxArgsSize: PtrUInt; (* Amount of memory reserved for arguments in bytes. *)
  215. MaxArgsSizeInc: word;
  216. PathZ: array [0..255] of char;
  217. begin
  218. { LastDosExitCode := Exec (Path, ExecRunFlags (ExecFlags), efDefault, ComLine);}
  219. ObjName := '';
  220. (* FExpand should be used only for the DosStartSession part
  221. and only if the executable is in the current directory. *)
  222. FindFirst (Path, AnyFile, SR);
  223. if DosError = 0 then
  224. QName := FExpand (Path)
  225. else
  226. QName := Path;
  227. FindClose (SR);
  228. MaxArgsSize := Length (ComLine) + Length (QName) + 256; (* More than enough *)
  229. if MaxArgsSize > high (word) then
  230. begin
  231. DosError := 8; (* Not quite, but "not enough memory" is close enough *)
  232. Exit;
  233. end;
  234. if ComLine = '' then
  235. begin
  236. Args0 := nil;
  237. Args := nil;
  238. StrPCopy (PathZ, Path);
  239. RC := DosQueryAppType (@PathZ [0], ExecAppType);
  240. end
  241. else
  242. begin
  243. GetMem (Args0, MaxArgsSize);
  244. Args := Args0;
  245. (* Work around a bug in OS/2 - argument to DosExecPgm *)
  246. (* should not cross a 64K boundary. *)
  247. while ((PtrUInt (Args) + MaxArgsSize) and $FFFF) < MaxArgsSize do
  248. begin
  249. MaxArgsSizeInc := MaxArgsSize -
  250. ((PtrUInt (Args) + MaxArgsSize) and $FFFF);
  251. Inc (MaxArgsSize, MaxArgsSizeInc);
  252. if MaxArgsSize > high (word) then
  253. begin
  254. DosError := 8; (* Not quite, but "not enough memory" is close enough *)
  255. Exit;
  256. end;
  257. ReallocMem (Args0, MaxArgsSize);
  258. Inc (pointer (Args), MaxArgsSizeInc);
  259. end;
  260. ArgSize := 0;
  261. Move (QName [1], Args^ [ArgSize], Length (QName));
  262. Inc (ArgSize, Length (QName));
  263. Args^ [ArgSize] := 0;
  264. Inc (ArgSize);
  265. {Now do the real arguments.}
  266. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  267. Inc (ArgSize, Length (ComLine));
  268. Args^ [ArgSize] := 0;
  269. Inc (ArgSize);
  270. Args^ [ArgSize] := 0;
  271. RC := DosQueryAppType (PChar (Args), ExecAppType);
  272. end;
  273. if RC <> 0 then
  274. OSErrorWatch (RC)
  275. else
  276. if (ApplicationType and 3 = ExecAppType and 3) then
  277. (* DosExecPgm should work... *)
  278. begin
  279. DSS := false;
  280. Res.ExitCode := $FFFFFFFF;
  281. RC := DosExecPgm (ObjName, cardinal (ExecFlags), Args, nil, Res, Path);
  282. if RC = 0 then
  283. begin
  284. LastExecFlags := ExecFlags;
  285. LastExecRes := Res;
  286. LastDosErrorModuleName := '';
  287. end
  288. else
  289. begin
  290. if (RC = 190) or (RC = 191) then
  291. DSS := true;
  292. OSErrorWatch (RC);
  293. end;
  294. end
  295. else
  296. DSS := true;
  297. if DSS then
  298. begin
  299. Str (GetProcessID, SPID);
  300. Str (ThreadID, STID);
  301. QName := '\QUEUES\FPC_Dos_Exec_p' + SPID + 't' + STID + '.QUE'#0;
  302. FillChar (SD, SizeOf (SD), 0);
  303. SD.Length := SizeOf (SD);
  304. RC := 0;
  305. case ExecFlags of
  306. deSync:
  307. begin
  308. SD.Related := ssf_Related_Child;
  309. LastExecFlags := ExecFlags;
  310. SD.TermQ := @QName [1];
  311. RC := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  312. if RC <> 0 then
  313. OSErrorWatch (RC);
  314. end;
  315. deAsync,
  316. deAsyncResult:
  317. begin
  318. (* Current implementation of DosExitCode does not support retrieval *)
  319. (* of result codes for other session types started asynchronously. *)
  320. LastExecFlags := deAsync;
  321. SD.Related := ssf_Related_Independent;
  322. end;
  323. deBackground:
  324. begin
  325. (* Current implementation of DosExitCode does not support retrieval *)
  326. (* of result codes for other session types started asynchronously. *)
  327. LastExecFlags := ExecFlags;
  328. SD.Related := ssf_Related_Independent;
  329. SD.FgBg := ssf_FgBg_Back;
  330. end;
  331. deAsyncResultDB:
  332. begin
  333. (* Current implementation of DosExitCode does not support retrieval *)
  334. (* of result codes for other session types started asynchronously. *)
  335. LastExecFlags := ExecFlags;
  336. SD.Related := ssf_Related_Child;
  337. SD.TraceOpt := ssf_TraceOpt_Trace;
  338. end;
  339. end;
  340. if RC <> 0 then
  341. ObjName := Copy (QName, 1, Pred (Length (QName)))
  342. else
  343. begin
  344. if Args = nil then
  345. (* No parameters passed, Args not allocated for DosExecPgm, so allocate now. *)
  346. begin
  347. GetMem (Args0, MaxArgsSize);
  348. Args := Args0;
  349. Move (QName [1], Args^ [0], Length (QName));
  350. Args^ [Length (QName)] := 0;
  351. end
  352. else
  353. SD.PgmInputs := PChar (@Args^ [Length (QName) + 1]);
  354. SD.PgmName := PChar (Args);
  355. SD.InheritOpt := ssf_InhertOpt_Parent;
  356. SD.ObjectBuffer := @ObjName [1];
  357. SD.ObjectBuffLen := SizeOf (ObjName) - 1;
  358. RC := DosStartSession (SD, SID, PID);
  359. if RC <> 0 then
  360. OSErrorWatch (RC);
  361. if (RC = 0) or (RC = 457) then
  362. begin
  363. LastExecRes.PID := PID;
  364. if ExecFlags = deSync then
  365. begin
  366. RC := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  367. if RC <> 0 then
  368. OSErrorWatch (RC);
  369. if (RC = 0) and (PCI^.SessionID = SID) then
  370. begin
  371. LastExecRes.ExitCode := PCI^.Return;
  372. RC2 := DosCloseQueue (HQ);
  373. if RC2 <> 0 then
  374. OSErrorWatch (RC2);
  375. RC2 := DosFreeMem (PCI);
  376. if RC2 <> 0 then
  377. OSErrorWatch (RC2);
  378. end
  379. else
  380. begin
  381. RC2 := DosCloseQueue (HQ);
  382. if RC2 <> 0 then
  383. OSErrorWatch (RC2);
  384. end;
  385. end;
  386. end
  387. else if ExecFlags = deSync then
  388. begin
  389. RC2 := DosCloseQueue (HQ);
  390. if RC2 <> 0 then
  391. OSErrorWatch (RC2);
  392. end;
  393. end;
  394. end;
  395. if RC <> 0 then
  396. begin
  397. LastDosErrorModuleName := ObjName;
  398. LastExecFlags := deSync;
  399. LastExecRes.ExitCode := 0; (* Needed for TP/BP compatibility *)
  400. LastExecRes.TerminateReason := $FFFFFFFF;
  401. end;
  402. DosError := RC;
  403. if Args0 <> nil then
  404. FreeMem (Args0, MaxArgsSize);
  405. end;
  406. function DosErrorModuleName: string;
  407. begin
  408. DosErrorModuleName := LastDosErrorModuleName;
  409. end;
  410. function dosversion:word;
  411. {Returns OS/2 version}
  412. var
  413. Minor, Major: Cardinal;
  414. begin
  415. DosQuerySysInfo(svMajorVersion, svMajorVersion, Major, 4);
  416. DosQuerySysInfo(svMinorVersion, svMinorVersion, Minor, 4);
  417. DosVersion:=Major or Minor shl 8;
  418. end;
  419. procedure GetDate (var Year, Month, MDay, WDay: word);
  420. Var
  421. dt: TDateTime;
  422. begin
  423. DosGetDateTime(dt);
  424. Year:=dt.year;
  425. Month:=dt.month;
  426. MDay:=dt.Day;
  427. WDay:=dt.Weekday;
  428. end;
  429. procedure SetDate (Year, Month, Day: word);
  430. var
  431. DT: TDateTime;
  432. RC: cardinal;
  433. begin
  434. DosGetDateTime (DT);
  435. DT.Year := Year;
  436. DT.Month := byte (Month);
  437. DT.Day := byte (Day);
  438. RC := DosSetDateTime (DT);
  439. if RC <> 0 then
  440. OSErrorWatch (RC);
  441. end;
  442. procedure GetTime (var Hour, Minute, Second, Sec100: word);
  443. var
  444. dt: TDateTime;
  445. begin
  446. DosGetDateTime(dt);
  447. Hour:=dt.Hour;
  448. Minute:=dt.Minute;
  449. Second:=dt.Second;
  450. Sec100:=dt.Hundredths;
  451. end;
  452. procedure SetTime (Hour, Minute, Second, Sec100: word);
  453. var
  454. DT: TDateTime;
  455. RC: cardinal;
  456. begin
  457. DosGetDateTime (DT);
  458. DT.Hour := byte (Hour);
  459. DT.Minute := byte (Minute);
  460. DT.Second := byte (Second);
  461. DT.Sec100 := byte (Sec100);
  462. RC := DosSetDateTime (DT);
  463. if RC <> 0 then
  464. OSErrorWatch (RC);
  465. end;
  466. function DiskFree (Drive: byte): int64;
  467. var FI: TFSinfo;
  468. RC: cardinal;
  469. begin
  470. {In OS/2, we use the filesystem information.}
  471. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  472. if RC = 0 then
  473. DiskFree := int64 (FI.Free_Clusters) *
  474. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  475. else
  476. begin
  477. DiskFree := -1;
  478. OSErrorWatch (RC);
  479. end;
  480. end;
  481. function DiskSize (Drive: byte): int64;
  482. var FI: TFSinfo;
  483. RC: cardinal;
  484. begin
  485. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  486. if RC = 0 then
  487. DiskSize := int64 (FI.Total_Clusters) *
  488. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  489. else
  490. begin
  491. DiskSize := -1;
  492. OSErrorWatch (RC);
  493. end;
  494. end;
  495. procedure DosSearchRec2SearchRec (var F: SearchRec);
  496. type
  497. TRec = record
  498. T, D: word;
  499. end;
  500. begin
  501. with F do
  502. begin
  503. Name := FStat^.Name;
  504. Size := FStat^.FileSize;
  505. Attr := byte(FStat^.AttrFile and $FF);
  506. TRec (Time).T := FStat^.TimeLastWrite;
  507. TRec (Time).D := FStat^.DateLastWrite;
  508. end;
  509. end;
  510. procedure FindFirst (const Path: PathStr; Attr: word; var F: SearchRec);
  511. var Count: cardinal;
  512. begin
  513. {No error.}
  514. DosError := 0;
  515. New (F.FStat);
  516. F.Handle := THandle ($FFFFFFFF);
  517. Count := 1;
  518. DosError := integer (DosFindFirst (Path, F.Handle,
  519. Attr and FindResvdMask, F.FStat, SizeOf (F.FStat^),
  520. Count, ilStandard));
  521. if DosError <> 0 then
  522. OSErrorWatch (DosError)
  523. else if Count = 0 then
  524. DosError := 18;
  525. DosSearchRec2SearchRec (F);
  526. end;
  527. procedure FindNext (var F: SearchRec);
  528. var
  529. Count: cardinal;
  530. begin
  531. {No error}
  532. DosError := 0;
  533. Count := 1;
  534. DosError := integer (DosFindNext (F.Handle, F.FStat, SizeOf (F.FStat^),
  535. Count));
  536. if DosError <> 0 then
  537. OSErrorWatch (DosError)
  538. else if Count = 0 then
  539. DosError := 18;
  540. DosSearchRec2SearchRec (F);
  541. end;
  542. procedure FindClose (var F: SearchRec);
  543. begin
  544. if F.Handle <> THandle ($FFFFFFFF) then
  545. begin
  546. DosError := integer (DosFindClose (F.Handle));
  547. if DosError <> 0 then
  548. OSErrorWatch (DosError);
  549. end;
  550. Dispose (F.FStat);
  551. end;
  552. function envcount:longint;
  553. begin
  554. envcount:=envc;
  555. end;
  556. function envstr (index : longint) : string;
  557. var hp:Pchar;
  558. begin
  559. if (index<=0) or (index>envcount) then
  560. begin
  561. envstr:='';
  562. exit;
  563. end;
  564. hp:=EnvP[index-1];
  565. envstr:=strpas(hp);
  566. end;
  567. function GetEnvPChar (EnvVar: string): PChar;
  568. (* The assembler version is more than three times as fast as Pascal. *)
  569. var
  570. P: PChar;
  571. begin
  572. EnvVar := UpCase (EnvVar);
  573. {$ASMMODE INTEL}
  574. asm
  575. cld
  576. mov edi, Environment
  577. lea esi, EnvVar
  578. xor eax, eax
  579. lodsb
  580. @NewVar:
  581. cmp byte ptr [edi], 0
  582. jz @Stop
  583. push eax { eax contains length of searched variable name }
  584. push esi { esi points to the beginning of the variable name }
  585. mov ecx, -1 { our character ('=' - see below) _must_ be found }
  586. mov edx, edi { pointer to beginning of variable name saved in edx }
  587. mov al, '=' { searching until '=' (end of variable name) }
  588. repne
  589. scasb { scan until '=' not found }
  590. neg ecx { what was the name length? }
  591. dec ecx { corrected }
  592. dec ecx { exclude the '=' character }
  593. pop esi { restore pointer to beginning of variable name }
  594. pop eax { restore length of searched variable name }
  595. push eax { and save both of them again for later use }
  596. push esi
  597. cmp ecx, eax { compare length of searched variable name with name }
  598. jnz @NotEqual { ... of currently found variable, jump if different }
  599. xchg edx, edi { pointer to current variable name restored in edi }
  600. repe
  601. cmpsb { compare till the end of variable name }
  602. xchg edx, edi { pointer to beginning of variable contents in edi }
  603. jz @Equal { finish if they're equal }
  604. @NotEqual:
  605. xor eax, eax { look for 00h }
  606. mov ecx, -1 { it _must_ be found }
  607. repne
  608. scasb { scan until found }
  609. pop esi { restore pointer to beginning of variable name }
  610. pop eax { restore length of searched variable name }
  611. jmp @NewVar { ... or continue with new variable otherwise }
  612. @Stop:
  613. xor eax, eax
  614. mov P, eax { Not found - return nil }
  615. jmp @End
  616. @Equal:
  617. pop esi { restore the stack position }
  618. pop eax
  619. mov P, edi { place pointer to variable contents in P }
  620. @End:
  621. end ['eax','ecx','edx','esi','edi'];
  622. GetEnvPChar := P;
  623. end;
  624. {$ASMMODE ATT}
  625. Function GetEnv(envvar: string): string;
  626. (* The assembler version is more than three times as fast as Pascal. *)
  627. begin
  628. GetEnv := StrPas (GetEnvPChar (EnvVar));
  629. end;
  630. procedure GetFAttr (var F; var Attr: word);
  631. var
  632. PathInfo: TFileStatus3;
  633. RC: cardinal;
  634. {$ifndef FPC_ANSI_TEXTFILEREC}
  635. R: rawbytestring;
  636. {$endif not FPC_ANSI_TEXTFILEREC}
  637. P: pchar;
  638. begin
  639. Attr := 0;
  640. {$ifdef FPC_ANSI_TEXTFILEREC}
  641. P := @FileRec (F).Name;
  642. {$else FPC_ANSI_TEXTFILEREC}
  643. R := ToSingleByteFileSystemEncodedFileName (FileRec (F).Name);
  644. P := PChar (R);
  645. {$endif FPC_ANSI_TEXTFILEREC}
  646. RC := DosQueryPathInfo (P, ilStandard, @PathInfo, SizeOf (PathInfo));
  647. DosError := integer (RC);
  648. if RC = 0 then
  649. Attr := PathInfo.AttrFile
  650. else
  651. begin
  652. OSErrorWatch (RC);
  653. if FileRec (F).Name [0] = #0 then
  654. DosError := 3; (* Align the returned error value to TP/BP *)
  655. end;
  656. end;
  657. procedure SetFAttr (var F; Attr: word);
  658. var
  659. PathInfo: TFileStatus3;
  660. RC: cardinal;
  661. {$ifndef FPC_ANSI_TEXTFILEREC}
  662. R: rawbytestring;
  663. {$endif not FPC_ANSI_TEXTFILEREC}
  664. P: pchar;
  665. begin
  666. {$ifdef FPC_ANSI_TEXTFILEREC}
  667. P := @FileRec (F).Name;
  668. {$else FPC_ANSI_TEXTFILEREC}
  669. R := ToSingleByteFileSystemEncodedFileName (FileRec (F).Name);
  670. P := PChar (R);
  671. {$endif FPC_ANSI_TEXTFILEREC}
  672. RC := DosQueryPathInfo (P, ilStandard, @PathInfo, SizeOf (PathInfo));
  673. if RC = 0 then
  674. begin
  675. PathInfo.AttrFile := Attr;
  676. RC := DosSetPathInfo (P, ilStandard, @PathInfo, SizeOf (PathInfo), 0);
  677. if RC <> 0 then
  678. begin
  679. OSErrorWatch (RC);
  680. if Attr and VolumeID = VolumeID then
  681. RC := 5; (* Align the returned error value to TP/BP *)
  682. end;
  683. end
  684. else
  685. begin
  686. OSErrorWatch (RC);
  687. if FileRec (F).Name [0] = #0 then
  688. RC := 3; (* Align the returned error value to TP/BP *)
  689. end;
  690. DosError := integer (RC);
  691. end;
  692. {function GetShortName(var p : String) : boolean;
  693. begin
  694. GetShortName:=true;}
  695. {$WARNING EA .shortname support (see FAT32 driver) should be probably added here!}
  696. {end;
  697. function GetLongName(var p : String) : boolean;
  698. begin
  699. GetLongName:=true;}
  700. {$WARNING EA .longname support should be probably added here!}
  701. {end;}
  702. begin
  703. FillChar (LastExecRes, SizeOf (LastExecRes), 0);
  704. LastDosErrorModuleName := '';
  705. ExecFlags := 0;
  706. LastExecFlags := deSync;
  707. end.