2
0

dos.pas 21 KB

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