dos.pp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 by Olle Raab and
  4. members of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Unit Dos;
  12. Interface
  13. Uses
  14. macostp;
  15. Const
  16. FileNameLen = 255;
  17. Type
  18. SearchRec = packed record
  19. Attr: Byte; {attribute of found file}
  20. Time: LongInt; {last modify date of found file}
  21. Size: LongInt; {file size of found file}
  22. Reserved: Word; {future use}
  23. Name: string[FileNameLen]; {name of foundfile}
  24. SearchSpec: string[FileNameLen]; {search pattern}
  25. NamePos: Word; {end of path,start of name position}
  26. {MacOS specific params, private, do not use:}
  27. paramBlock: CInfoPBRec;
  28. searchFSSpec: FSSpec;
  29. searchAttr: Byte; {attribute we are searching for}
  30. exactMatch: Boolean;
  31. end;
  32. {$DEFINE HAS_FILENAMELEN}
  33. {$I dosh.inc}
  34. Implementation
  35. {TODO Obtain disk size and disk free values for volumes > 2 GB.
  36. For this, PBXGetVolInfoSync can be used. However, this function
  37. is not available on older versions of Mac OS, so the function has
  38. to be weak linked. An alternative is to directly look into the VCB
  39. (Volume Control Block), but since this is on low leveel it is a
  40. compatibility risque.}
  41. {TODO Perhaps make SearchRec.paramBlock opaque, so that uses macostp;
  42. is not needed in the interface part.}
  43. {TODO Perhaps add some kind of "Procedure AddDisk" for accessing other
  44. volumes. At lest accessing the possible disk drives with
  45. drive number 1 and 2 should be easy.}
  46. {TODO Perhaps use LongDateTime for time functions. But the function
  47. calls must then be weak linked.}
  48. Uses
  49. macutils,
  50. unixutil {for FNMatch};
  51. {$UNDEF USE_FEXPAND_INC}
  52. //{$DEFINE USE_FEXPAND_INC}
  53. {$IFNDEF USE_FEXPAND_INC}
  54. {$DEFINE HAS_FEXPAND}
  55. {Own implemetation of fexpand.inc}
  56. {$I dos.inc}
  57. {$ELSE}
  58. {$DEFINE FPC_FEXPAND_VOLUMES}
  59. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  60. {$DEFINE FPC_FEXPAND_DRIVESEP_IS_ROOT}
  61. {$DEFINE FPC_FEXPAND_NO_DOTS_UPDIR}
  62. {$DEFINE FPC_FEXPAND_NO_CURDIR}
  63. { NOTE: If HAS_FEXPAND is not defined, fexpand.inc is included in dos.inc. }
  64. { TODO A lot of issues before this works}
  65. {$I dos.inc}
  66. {$UNDEF FPC_FEXPAND_VOLUMES}
  67. {$UNDEF FPC_FEXPAND_NO_DEFAULT_PATHS}
  68. {$UNDEF FPC_FEXPAND_DRIVESEP_IS_ROOT}
  69. {$UNDEF FPC_FEXPAND_NO_DOTS_UPDIR}
  70. {$UNDEF FPC_FEXPAND_NO_CURDIR}
  71. {$ENDIF}
  72. function MacTimeToDosPackedTime(macfiletime: UInt32): Longint;
  73. var
  74. mdt: DateTimeRec; {Mac OS datastructure}
  75. ddt: Datetime; {Dos OS datastructure}
  76. dospackedtime: Longint;
  77. begin
  78. SecondsToDate(macfiletime, mdt);
  79. with ddt do
  80. begin
  81. year := mdt.year;
  82. month := mdt.month;
  83. day := mdt.day;
  84. hour := mdt.hour;
  85. min := mdt.minute;
  86. sec := mdt.second;
  87. end;
  88. Packtime(ddt, dospackedtime);
  89. MacTimeToDosPackedTime:= dospackedtime;
  90. end;
  91. {******************************************************************************
  92. --- Info / Date / Time ---
  93. ******************************************************************************}
  94. function DosVersion:Word;
  95. begin
  96. DosVersion:=
  97. (macosSystemVersion and $FF00) or
  98. ((macosSystemVersion and $00F0) shr 4);
  99. end;
  100. procedure GetDate (var year, month, mday, wday: word);
  101. var
  102. d: DateTimeRec;
  103. begin
  104. Macostp.GetTime(d);
  105. year := d.year;
  106. month := d.month;
  107. mday := d.day;
  108. wday := d.dayOfWeek - 1; {1-based on mac}
  109. end;
  110. procedure GetTime (var hour, minute, second, sec100: word);
  111. var
  112. d: DateTimeRec;
  113. begin
  114. Macostp.GetTime(d);
  115. hour := d.hour;
  116. minute := d.minute;
  117. second := d.second;
  118. sec100 := 0;
  119. end;
  120. Procedure SetDate(Year, Month, Day: Word);
  121. var
  122. d: DateTimeRec;
  123. Begin
  124. Macostp.GetTime(d);
  125. d.year := year;
  126. d.month := month;
  127. d.day := day;
  128. Macostp.SetTime(d)
  129. End;
  130. Procedure SetTime(Hour, Minute, Second, Sec100: Word);
  131. var
  132. d: DateTimeRec;
  133. Begin
  134. Macostp.GetTime(d);
  135. d.hour := hour;
  136. d.minute := minute;
  137. d.second := second;
  138. Macostp.SetTime(d)
  139. End;
  140. {******************************************************************************
  141. --- Exec ---
  142. ******************************************************************************}
  143. { Create a DoScript AppleEvent that targets the given application with text as the direct object. }
  144. function CreateDoScriptEvent (applCreator: OSType; scriptText: PChar; var theEvent: AppleEvent): OSErr;
  145. var
  146. err: OSErr;
  147. targetAddress: AEDesc;
  148. s: signedByte;
  149. begin
  150. err := AECreateDesc(FourCharCodeToLongword(typeApplSignature), @applCreator, sizeof(applCreator), targetAddress);
  151. if err = noErr then
  152. begin
  153. err := AECreateAppleEvent(FourCharCodeToLongword('misc'), FourCharCodeToLongword('dosc'),
  154. targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  155. if err = noErr then
  156. { Add script text as the direct object parameter. }
  157. err := AEPutParamPtr(theEvent, FourCharCodeToLongword('----'),
  158. FourCharCodeToLongword('TEXT'), scriptText, Length(scriptText));
  159. if err <> noErr then
  160. AEDisposeDesc(theEvent);
  161. AEDisposeDesc(targetAddress);
  162. end;
  163. CreateDoScriptEvent := err;
  164. end;
  165. Procedure Fpc_WriteBuffer(var f:Text;const b;len:longint);[external name 'FPC_WRITEBUFFER'];
  166. {declared in text.inc}
  167. procedure WriteAEDescTypeCharToFile(desc: AEDesc; var f: Text);
  168. begin
  169. if desc.descriptorType = FourCharCodeToLongword(typeChar) then
  170. begin
  171. HLock(desc.dataHandle);
  172. Fpc_WriteBuffer(f, PChar(desc.dataHandle^)^, GetHandleSize(desc.dataHandle));
  173. Flush(f);
  174. HUnLock(desc.dataHandle);
  175. end;
  176. end;
  177. function ExecuteToolserverScript(scriptText: PChar; var statusCode: Longint): OSErr;
  178. var
  179. err: OSErr;
  180. err2: OSErr; {Non serious error}
  181. theEvent: AppleEvent;
  182. reply: AppleEvent;
  183. result: AEDesc;
  184. applFileSpec: FSSpec;
  185. p: SignedByte;
  186. const
  187. applCreator = 'MPSX'; {Toolserver}
  188. begin
  189. statusCode:= 3; //3 according to MPW.
  190. err:= CreateDoScriptEvent (FourCharCodeToLongword(applCreator), scriptText, theEvent);
  191. if err = noErr then
  192. begin
  193. err := AESend(theEvent, reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeOut, nil, nil);
  194. if err = connectionInvalid then { Toolserver not available }
  195. begin
  196. err := FindApplication(FourCharCodeToLongword(applCreator), applFileSpec);
  197. if err = noErr then
  198. err := LaunchFSSpec(false, applFileSpec);
  199. if err = noErr then
  200. err := AESend(theEvent, reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeOut, nil, nil);
  201. end;
  202. if err = noErr then
  203. begin
  204. err:= AEGetParamDesc(reply, FourCharCodeToLongword('stat'),
  205. FourCharCodeToLongword(typeLongInteger), result);
  206. if err = noErr then
  207. if result.descriptorType = FourCharCodeToLongword(typeLongInteger) then
  208. statusCode:= LongintPtr(result.dataHandle^)^;
  209. {If there is no output below, we get a non zero error code}
  210. err2:= AEGetParamDesc(reply, FourCharCodeToLongword('----'),
  211. FourCharCodeToLongword(typeChar), result);
  212. if err2 = noErr then
  213. WriteAEDescTypeCharToFile(result, stdout);
  214. err2:= AEGetParamDesc(reply, FourCharCodeToLongword('diag'),
  215. FourCharCodeToLongword(typeChar), result);
  216. if err2 = noErr then
  217. WriteAEDescTypeCharToFile(result, stderr);
  218. AEDisposeDesc(reply);
  219. {$IFDEF TARGET_API_MAC_CARBON }
  220. {$ERROR FIXME AEDesc data is not allowed to be directly accessed}
  221. {$ENDIF}
  222. end;
  223. AEDisposeDesc(theEvent);
  224. end;
  225. ExecuteToolserverScript:= err;
  226. end;
  227. Procedure Exec (Const Path: PathStr; Const ComLine: ComStr);
  228. var
  229. s: AnsiString;
  230. err: OSErr;
  231. wdpath: AnsiString;
  232. Begin
  233. {Make ToolServers working directory in sync with our working directory}
  234. PathArgToFullPath(':', wdpath);
  235. wdpath:= 'Directory ''' + wdpath + '''';
  236. err:= ExecuteToolserverScript(PChar(wdpath), LastDosExitCode);
  237. {TODO Only change path when actually needed. But this requires some
  238. change counter to be incremented each time wd is changed. }
  239. s:= path + ' ' + comline;
  240. err:= ExecuteToolserverScript(PChar(s), LastDosExitCode);
  241. if err = afpItemNotFound then
  242. DosError := 900
  243. else
  244. DosError := MacOSErr2RTEerr(err);
  245. //TODO Better dos error codes
  246. End;
  247. {******************************************************************************
  248. --- Disk ---
  249. ******************************************************************************}
  250. {If drive is 0 the free space on the volume of the working directory is returned.
  251. If drive is 1 or 2, the free space on the first or second floppy disk is returned.
  252. If drive is 3 the free space on the boot volume is returned.
  253. If the free space is > 2 GB, then 2 GB is reported.}
  254. Function DiskFree(drive: Byte): Int64;
  255. var
  256. myHPB: HParamBlockRec;
  257. myErr: OSErr;
  258. begin
  259. myHPB.ioNamePtr := NIL;
  260. myHPB.ioVolIndex := 0;
  261. case drive of
  262. 0: myHPB.ioVRefNum := GetWorkingDirectoryVRefNum;
  263. 1: myHPB.ioVRefNum := 1;
  264. 2: myHPB.ioVRefNum := 2;
  265. 3: myHPB.ioVRefNum := macosBootVolumeVRefNum;
  266. else
  267. begin
  268. Diskfree:= -1;
  269. Exit;
  270. end;
  271. end;
  272. myErr := PBHGetVInfoSync(@myHPB);
  273. if myErr = noErr then
  274. Diskfree := myHPB.ioVAlBlkSiz * myHPB.ioVFrBlk
  275. else
  276. Diskfree:= -1;
  277. End;
  278. {If drive is 0 the size of the volume of the working directory is returned.
  279. If drive is 1 or 2, the size of the first or second floppy disk is returned.
  280. If drive is 3 the size of the boot volume is returned.
  281. If the actual size is > 2 GB, then 2 GB is reported.}
  282. Function DiskSize(drive: Byte): Int64;
  283. var
  284. myHPB: HParamBlockRec;
  285. myErr: OSErr;
  286. Begin
  287. myHPB.ioNamePtr := NIL;
  288. myHPB.ioVolIndex := 0;
  289. case drive of
  290. 0: myHPB.ioVRefNum := GetWorkingDirectoryVRefNum;
  291. 1: myHPB.ioVRefNum := 1;
  292. 2: myHPB.ioVRefNum := 2;
  293. 3: myHPB.ioVRefNum := macosBootVolumeVRefNum;
  294. else
  295. begin
  296. DiskSize:= -1;
  297. Exit;
  298. end;
  299. end;
  300. myErr := PBHGetVInfoSync(@myHPB);
  301. if myErr = noErr then
  302. DiskSize := myHPB.ioVAlBlkSiz * myHPB.ioVNmAlBlks
  303. else
  304. DiskSize:=-1;
  305. End;
  306. {******************************************************************************
  307. --- Findfirst FindNext ---
  308. ******************************************************************************}
  309. (*
  310. {The one defined in Unixutils.pp is used instead}
  311. function FNMatch (const Pattern, Name: string): Boolean;
  312. var
  313. LenPat, LenName: longint;
  314. function DoFNMatch (i, j: longint): Boolean;
  315. var
  316. Found: boolean;
  317. begin
  318. Found := true;
  319. while Found and (i <= LenPat) do
  320. begin
  321. case Pattern[i] of
  322. '?':
  323. Found := (j <= LenName);
  324. '*':
  325. begin
  326. {find the next character in pattern, different of ? and *}
  327. while Found and (i < LenPat) do
  328. begin
  329. i := i + 1;
  330. case Pattern[i] of
  331. '*':
  332. ;
  333. '?':
  334. begin
  335. j := j + 1;
  336. Found := (j <= LenName);
  337. end;
  338. otherwise
  339. Found := false;
  340. end;
  341. end;
  342. {Now, find in name the character which i points to, if the * or ?}
  343. {wasn 't the last character in the pattern, else, use up all the}
  344. {chars in name }
  345. Found := true;
  346. if (i <= LenPat) then
  347. begin
  348. repeat
  349. {find a letter (not only first !) which maches pattern[i]}
  350. while (j <= LenName) and (name[j] <> pattern[i]) do
  351. j := j + 1;
  352. if (j < LenName) then
  353. begin
  354. if DoFnMatch(i + 1, j + 1) then
  355. begin
  356. i := LenPat;
  357. j := LenName;{we can stop}
  358. Found := true;
  359. end
  360. else
  361. j := j + 1;{We didn't find one, need to look further}
  362. end;
  363. until (j >= LenName);
  364. end
  365. else
  366. j := LenName;{we can stop}
  367. end;
  368. otherwise {not a wildcard character in pattern}
  369. Found := (j <= LenName) and (pattern[i] = name[j]);
  370. end;
  371. i := i + 1;
  372. j := j + 1;
  373. end;
  374. DoFnMatch := Found and (j > LenName);
  375. end;
  376. begin {start FNMatch}
  377. LenPat := Length(Pattern);
  378. LenName := Length(Name);
  379. FNMatch := DoFNMatch(1, 1);
  380. end;
  381. *)
  382. function GetFileAttrFromPB (var paramBlock: CInfoPBRec): Word;
  383. var
  384. isLocked, isInvisible, isDirectory, isNameLocked: Boolean;
  385. attr: Word;
  386. {NOTE "nameLocked" was in pre-System 7 called "isSystem".
  387. It is used for files whose name and icon cannot be changed by the user,
  388. that is essentially system files. However in System 9 the folder
  389. "Applications (Mac OS 9)" also has this attribute, and since this is
  390. not a system file in traditional meaning, we will not use this attribute
  391. as the "sysfile" attribute.}
  392. begin
  393. with paramBlock do
  394. begin
  395. attr := 0;
  396. isDirectory := (ioFlAttrib and $10) <> 0;
  397. if isDirectory then
  398. attr := (attr or directory);
  399. isLocked := (ioFlAttrib and $01) <> 0;
  400. if isLocked then
  401. attr := (attr or readonly);
  402. if not isDirectory then
  403. begin
  404. isInvisible := (ioFlFndrInfo.fdFlags and 16384) <> 0;
  405. (* isNameLocked := (ioFlFndrInfo.fdFlags and 4096) <> 0; *)
  406. end
  407. else
  408. begin
  409. isInvisible := (ioDrUsrWds.frFlags and 16384) <> 0;
  410. (* isNameLocked := (ioDrUsrWds.frFlags and 4096) <> 0; *)
  411. end;
  412. if isInvisible then
  413. attr := (attr or hidden);
  414. (*
  415. if isNameLocked then
  416. attr := (attr or sysfile);
  417. *)
  418. GetFileAttrFromPB := attr;
  419. end;
  420. end;
  421. procedure SetPBFromFileAttr (var paramBlock: CInfoPBRec; attr: Word);
  422. begin
  423. with paramBlock do
  424. begin
  425. (*
  426. {Doesn't seem to work, despite the documentation.}
  427. {Can instead be set by FSpSetFLock/FSpRstFLock}
  428. if (attr and readonly) <> 0 then
  429. ioFlAttrib := (ioFlAttrib or $01)
  430. else
  431. ioFlAttrib := (ioFlAttrib and not($01));
  432. *)
  433. if (attr and hidden) <> 0 then
  434. ioFlFndrInfo.fdFlags := (ioFlFndrInfo.fdFlags or 16384)
  435. else
  436. ioFlFndrInfo.fdFlags := (ioFlFndrInfo.fdFlags and not(16384))
  437. end;
  438. end;
  439. function GetFileSizeFromPB (var paramBlock: CInfoPBRec): Longint;
  440. begin
  441. with paramBlock do
  442. if ((ioFlAttrib and $10) <> 0) then {if directory}
  443. GetFileSizeFromPB := 0
  444. else
  445. GetFileSizeFromPB := ioFlLgLen + ioFlRLgLen; {Add length of both forks}
  446. end;
  447. function DoFindOne (var spec: FSSpec; var paramBlock: CInfoPBRec): Integer;
  448. var
  449. err: OSErr;
  450. begin
  451. with paramBlock do
  452. begin
  453. ioVRefNum := spec.vRefNum;
  454. ioDirID := spec.parID;
  455. ioNamePtr := @spec.name;
  456. ioFDirIndex := 0;
  457. err := PBGetCatInfoSync(@paramBlock);
  458. DoFindOne := MacOSErr2RTEerr(err);
  459. end;
  460. end;
  461. {To be used after a call to DoFindOne, with the same spec and paramBlock.}
  462. {Change those parameters in paramBlock, which is to be changed.}
  463. function DoSetOne (var spec: FSSpec; var paramBlock: CInfoPBRec): Integer;
  464. var
  465. err: OSErr;
  466. begin
  467. with paramBlock do
  468. begin
  469. ioVRefNum := spec.vRefNum;
  470. ioDirID := spec.parID;
  471. ioNamePtr := @spec.name;
  472. err := PBSetCatInfoSync(@paramBlock);
  473. DoSetOne := MacOSErr2RTEerr(err);
  474. end;
  475. end;
  476. procedure DoFind (var F: SearchRec; firstTime: Boolean);
  477. var
  478. err: OSErr;
  479. s: Str255;
  480. begin
  481. with F, paramBlock do
  482. begin
  483. ioVRefNum := searchFSSpec.vRefNum;
  484. if firstTime then
  485. ioFDirIndex := 0;
  486. while true do
  487. begin
  488. s := '';
  489. ioDirID := searchFSSpec.parID;
  490. ioFDirIndex := ioFDirIndex + 1;
  491. ioNamePtr := @s;
  492. err := PBGetCatInfoSync(@paramBlock);
  493. if err <> noErr then
  494. begin
  495. if err = fnfErr then
  496. DosError := 18
  497. else
  498. DosError := MacOSErr2RTEerr(err);
  499. break;
  500. end;
  501. attr := GetFileAttrFromPB(f.paramBlock);
  502. if ((Attr and not(searchAttr)) = 0) then
  503. begin
  504. name := s;
  505. UpperString(s, true);
  506. if FNMatch(F.searchFSSpec.name, s) then
  507. begin
  508. size := GetFileSizeFromPB(paramBlock);
  509. time := MacTimeToDosPackedTime(ioFlMdDat);
  510. DosError := 0;
  511. break;
  512. end;
  513. end;
  514. end;
  515. end;
  516. end;
  517. procedure FindFirst (const path: pathstr; Attr: Word; var F: SearchRec);
  518. var
  519. s: Str255;
  520. begin
  521. fillchar(f, sizeof(f), 0);
  522. if path = '' then
  523. begin
  524. DosError := 3;
  525. Exit;
  526. end;
  527. {We always also search for readonly and archive, regardless of Attr.}
  528. F.searchAttr := (Attr or (archive or readonly));
  529. DosError := PathArgToFSSpec(path, F.searchFSSpec);
  530. with F do
  531. if (DosError = 0) or (DosError = 2) then
  532. begin
  533. SearchSpec := path;
  534. NamePos := Length(path) - Length(searchFSSpec.name);
  535. if (Pos('?', searchFSSpec.name) = 0) and (Pos('*', searchFSSpec.name) = 0) then {No wildcards}
  536. begin {If exact match, we don't have to scan the directory}
  537. exactMatch := true;
  538. DosError := DoFindOne(searchFSSpec, paramBlock);
  539. if DosError = 0 then
  540. begin
  541. Attr := GetFileAttrFromPB(paramBlock);
  542. if ((Attr and not(searchAttr)) = 0) then
  543. begin
  544. name := searchFSSpec.name;
  545. size := GetFileSizeFromPB(paramBlock);
  546. time := MacTimeToDosPackedTime(paramBlock.ioFlMdDat);
  547. end
  548. else
  549. DosError := 18;
  550. end
  551. else if DosError = 2 then
  552. DosError := 18;
  553. end
  554. else
  555. begin
  556. exactMatch := false;
  557. s := searchFSSpec.name;
  558. UpperString(s, true);
  559. F.searchFSSpec.name := s;
  560. DoFind(F, true);
  561. end;
  562. end;
  563. end;
  564. procedure FindNext (var f: searchRec);
  565. begin
  566. if F.exactMatch then
  567. DosError := 18
  568. else
  569. DoFind(F, false);
  570. end;
  571. procedure FindClose (var f: searchRec);
  572. {Note: Even if this routine is empty, this doesn't mean it will}
  573. {be empty in the future. Please use it.}
  574. begin
  575. end;
  576. {******************************************************************************
  577. --- File ---
  578. ******************************************************************************}
  579. function FSearch (path: pathstr; dirlist: string): pathstr;
  580. {Searches for a file 'path' in the working directory and then in the list of }
  581. {directories in 'dirlist' . Returns a valid (possibly relative) path or an }
  582. {empty string if not found . Wildcards are NOT allowed }
  583. {The dirlist can be separated with ; or , but not :}
  584. var
  585. NewDir: string[255];
  586. p1: Longint;
  587. spec: FSSpec;
  588. fpcerr: Integer;
  589. begin
  590. FSearch := '';
  591. if (Length(path) = 0) then
  592. Exit;
  593. {Check for Wild Cards}
  594. if (Pos('?', Path) <> 0) or (Pos('*', Path) <> 0) then
  595. Exit;
  596. if pathTranslation then
  597. path := TranslatePathToMac(path, false);
  598. {Search in working directory, or as full path}
  599. fpcerr := PathArgToFSSpec(path, spec);
  600. if (fpcerr = 0) and not IsDirectory(spec) then
  601. begin
  602. FSearch := path;
  603. Exit;
  604. end
  605. else if not IsMacFullPath(path) then {If full path, we do not need to continue.}
  606. begin
  607. {Replace ';' with native mac PathSeparator (',').}
  608. {Note: we cannot support unix style ':', because it is used as dir separator in MacOS}
  609. for p1 := 1 to length(dirlist) do
  610. if dirlist[p1] = ';' then
  611. dirlist[p1] := PathSeparator;
  612. repeat
  613. p1 := Pos(PathSeparator, DirList);
  614. if p1 = 0 then
  615. p1 := 255;
  616. if pathTranslation then
  617. NewDir := TranslatePathToMac(Copy(DirList, 1, P1 - 1), false)
  618. else
  619. NewDir := Copy(DirList, 1, P1 - 1);
  620. NewDir := ConcatMacPath(NewDir, Path);
  621. Delete(DirList, 1, p1);
  622. fpcerr := PathArgToFSSpec(NewDir, spec);
  623. if fpcerr = 0 then
  624. begin
  625. if IsDirectory(spec) then
  626. NewDir := '';
  627. end
  628. else
  629. NewDir := '';
  630. until (DirList = '') or (Length(NewDir) > 0);
  631. FSearch := NewDir;
  632. end;
  633. end;
  634. {$IFNDEF USE_FEXPAND_INC}
  635. { TODO nonexisting dirs in path's doesnt work (nonexisting files do work)
  636. example: Writeln('FExpand on :nisse:kalle : ', FExpand(':nisse:kalle')); }
  637. function FExpand (const path: pathstr): pathstr;
  638. var
  639. fullpath: AnsiString;
  640. begin
  641. DosError:= PathArgToFullPath(path, fullpath);
  642. FExpand:= fullpath;
  643. end;
  644. {$ENDIF USE_FEXPAND_INC}
  645. procedure GetFTime (var f ; var time: longint);
  646. var
  647. spec: FSSpec;
  648. paramBlock: CInfoPBRec;
  649. begin
  650. DosError := PathArgToFSSpec(StrPas(filerec(f).name), spec);
  651. if (DosError = 0) or (DosError = 2) then
  652. begin
  653. DosError := DoFindOne(spec, paramBlock);
  654. if DosError = 0 then
  655. time := MacTimeToDosPackedTime(paramBlock.ioFlMdDat);
  656. end;
  657. end;
  658. procedure SetFTime (var f ; time: longint);
  659. var
  660. spec: FSSpec;
  661. paramBlock: CInfoPBRec;
  662. d: DateTimeRec; {Mac OS datastructure}
  663. t: datetime;
  664. macfiletime: UInt32;
  665. begin
  666. DosError := PathArgToFSSpec(StrPas(filerec(f).name), spec);
  667. if (DosError = 0) or (DosError = 2) then
  668. begin
  669. DosError := DoFindOne(spec, paramBlock);
  670. if DosError = 0 then
  671. begin
  672. Unpacktime(time, t);
  673. with t do
  674. begin
  675. d.year := year;
  676. d.month := month;
  677. d.day := day;
  678. d.hour := hour;
  679. d.minute := min;
  680. d.second := sec;
  681. end;
  682. DateToSeconds(d, macfiletime);
  683. paramBlock.ioFlMdDat := macfiletime;
  684. DosError := DoSetOne(spec, paramBlock);
  685. end;
  686. end;
  687. end;
  688. procedure GetFAttr (var f ; var attr: word);
  689. var
  690. spec: FSSpec;
  691. paramBlock: CInfoPBRec;
  692. begin
  693. DosError := PathArgToFSSpec(StrPas(filerec(f).name), spec);
  694. if (DosError = 0) or (DosError = 2) then
  695. begin
  696. DosError := DoFindOne(spec, paramBlock);
  697. if DosError = 0 then
  698. attr := GetFileAttrFromPB(paramBlock);
  699. end;
  700. end;
  701. procedure SetFAttr (var f ; attr: word);
  702. var
  703. spec: FSSpec;
  704. paramBlock: CInfoPBRec;
  705. begin
  706. if (attr and VolumeID) <> 0 then
  707. begin
  708. Doserror := 5;
  709. Exit;
  710. end;
  711. DosError := PathArgToFSSpec(StrPas(filerec(f).name), spec);
  712. if (DosError = 0) or (DosError = 2) then
  713. begin
  714. DosError := DoFindOne(spec, paramBlock);
  715. if DosError = 0 then
  716. begin
  717. SetPBFromFileAttr(paramBlock, attr);
  718. DosError := DoSetOne(spec, paramBlock);
  719. if (paramBlock.ioFlAttrib and $10) = 0 then {check not directory}
  720. if DosError = 0 then
  721. if (attr and readonly) <> 0 then
  722. DosError := MacOSErr2RTEerr(FSpSetFLock(spec))
  723. else
  724. DosError := MacOSErr2RTEerr(FSpRstFLock(spec));
  725. end;
  726. end;
  727. end;
  728. {******************************************************************************
  729. --- Environment ---
  730. ******************************************************************************}
  731. Function EnvCount: Longint;
  732. var
  733. envcnt : longint;
  734. p : ppchar;
  735. Begin
  736. envcnt:=0;
  737. p:=envp; {defined in system}
  738. while (p^<>nil) do
  739. begin
  740. inc(envcnt);
  741. inc(p);
  742. end;
  743. EnvCount := envcnt
  744. End;
  745. Function EnvStr (Index: longint): String;
  746. Var
  747. i : longint;
  748. p : ppchar;
  749. Begin
  750. if Index <= 0 then
  751. envstr:=''
  752. else
  753. begin
  754. p:=envp; {defined in system}
  755. i:=1;
  756. while (i<Index) and (p^<>nil) do
  757. begin
  758. inc(i);
  759. inc(p);
  760. end;
  761. if p=nil then
  762. envstr:=''
  763. else
  764. envstr:=strpas(p^) + '=' + strpas(p^+strlen(p^)+1);
  765. end;
  766. end;
  767. function c_getenv(varname: PChar): PChar; {TODO perhaps move to a separate inc file.}
  768. external 'StdCLib' name 'getenv';
  769. Function GetEnv(EnvVar: String): String;
  770. var
  771. p: PChar;
  772. name: String;
  773. Begin
  774. name:= EnvVar+#0;
  775. p:= c_getenv(@name[1]);
  776. if p=nil then
  777. GetEnv:=''
  778. else
  779. GetEnv:=StrPas(p);
  780. End;
  781. {
  782. Procedure GetCBreak(Var BreakValue: Boolean);
  783. Begin
  784. -- Might be implemented in future on MacOS to handle Cmd-. (period) key press
  785. End;
  786. Procedure SetCBreak(BreakValue: Boolean);
  787. Begin
  788. -- Might be implemented in future on MacOS to handle Cmd-. (period) key press
  789. End;
  790. Procedure GetVerify(Var Verify: Boolean);
  791. Begin
  792. -- Might be implemented in future on MacOS
  793. End;
  794. Procedure SetVerify(Verify: Boolean);
  795. Begin
  796. -- Might be implemented in future on MacOS
  797. End;
  798. }
  799. {******************************************************************************
  800. --- Initialization ---
  801. ******************************************************************************}
  802. End.