dos.pp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by the Free Pascal development team.
  5. Dos unit for BP7 compatible RTL
  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. {$I os.inc}
  14. interface
  15. { Include Win32 Consts,Types }
  16. {$I win32.inc}
  17. Const
  18. Max_Path = 255;
  19. {Bitmasks for CPU Flags}
  20. fcarry = $0001;
  21. fparity = $0004;
  22. fauxiliary = $0010;
  23. fzero = $0040;
  24. fsign = $0080;
  25. foverflow = $0800;
  26. {Bitmasks for file attribute}
  27. readonly = $01;
  28. hidden = $02;
  29. sysfile = $04;
  30. volumeid = $08;
  31. directory = $10;
  32. archive = $20;
  33. anyfile = $3F;
  34. {File Status}
  35. fmclosed = $D7B0;
  36. fminput = $D7B1;
  37. fmoutput = $D7B2;
  38. fminout = $D7B3;
  39. Type
  40. { Needed for Win95 LFN Support }
  41. ComStr = String[255];
  42. PathStr = String[255];
  43. DirStr = String[255];
  44. NameStr = String[255];
  45. ExtStr = String[255];
  46. {
  47. filerec.inc contains the definition of the filerec.
  48. textrec.inc contains the definition of the textrec.
  49. It is in a separate file to make it available in other units without
  50. having to use the DOS unit for it.
  51. }
  52. {$i filerec.inc}
  53. {$i textrec.inc}
  54. DateTime = packed record
  55. Year,
  56. Month,
  57. Day,
  58. Hour,
  59. Min,
  60. Sec : word;
  61. End;
  62. PWin32FindData = ^TWin32FindData;
  63. TWin32FindData = packed record
  64. dwFileAttributes: Cardinal;
  65. ftCreationTime: TFileTime;
  66. ftLastAccessTime: TFileTime;
  67. ftLastWriteTime: TFileTime;
  68. nFileSizeHigh: Cardinal;
  69. nFileSizeLow: Cardinal;
  70. dwReserved0: Cardinal;
  71. dwReserved1: Cardinal;
  72. cFileName: array[0..MAX_PATH - 1] of Char;
  73. cAlternateFileName: array[0..13] of Char;
  74. end;
  75. Searchrec = Packed Record
  76. FindHandle : THandle;
  77. W32FindData : TWin32FindData;
  78. time : longint;
  79. size : longint;
  80. attr : longint;
  81. name : string;
  82. end;
  83. registers = packed record
  84. case i : integer of
  85. 0 : (ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,f8,flags,fs,gs : word);
  86. 1 : (al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh : byte);
  87. 2 : (eax, ebx, ecx, edx, ebp, esi, edi : longint);
  88. end;
  89. Var
  90. DosError : integer;
  91. {Interrupt}
  92. Procedure Intr(intno: byte; var regs: registers);
  93. Procedure MSDos(var regs: registers);
  94. {Info/Date/Time}
  95. Function DosVersion: Word;
  96. Procedure GetDate(var year, month, mday, wday: word);
  97. Procedure GetTime(var hour, minute, second, sec100: word);
  98. procedure SetDate(year,month,day: word);
  99. Procedure SetTime(hour,minute,second,sec100: word);
  100. Procedure UnpackTime(p: longint; var t: datetime);
  101. Procedure PackTime(var t: datetime; var p: longint);
  102. {Exec}
  103. Procedure Exec(const path: pathstr; const comline: comstr);
  104. Function DosExitCode: word;
  105. {Disk}
  106. Function DiskFree(drive: byte) : longint;
  107. Function DiskSize(drive: byte) : longint;
  108. Procedure FindFirst(const path: pathstr; attr: word; var f: searchRec);
  109. Procedure FindNext(var f: searchRec);
  110. Procedure FindClose(Var f: SearchRec);
  111. {File}
  112. Procedure GetFAttr(var f; var attr: word);
  113. Procedure GetFTime(var f; var time: longint);
  114. Function FSearch(path: pathstr; dirlist: string): pathstr;
  115. Function FExpand(const path: pathstr): pathstr;
  116. Procedure FSplit(path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
  117. {Environment}
  118. Function EnvCount: longint;
  119. Function EnvStr(index: integer): string;
  120. Function GetEnv(envvar: string): string;
  121. {Misc}
  122. Procedure SetFAttr(var f; attr: word);
  123. Procedure SetFTime(var f; time: longint);
  124. Procedure GetCBreak(var breakvalue: boolean);
  125. Procedure SetCBreak(breakvalue: boolean);
  126. Procedure GetVerify(var verify: boolean);
  127. Procedure SetVerify(verify: boolean);
  128. {Do Nothing Functions}
  129. Procedure SwapVectors;
  130. Procedure GetIntVec(intno: byte; var vector: pointer);
  131. Procedure SetIntVec(intno: byte; vector: pointer);
  132. Procedure Keep(exitcode: word);
  133. implementation
  134. uses strings;
  135. {******************************************************************************
  136. --- Conversion ---
  137. ******************************************************************************}
  138. function GetLastError : DWORD;
  139. external 'kernel32' name 'GetLastError';
  140. function FileTimeToDosDateTime(const ft :TFileTime;var data,time : word) : boolean;
  141. external 'kernel32' name 'FileTimeToDosDateTime';
  142. function DosDateTimeToFileTime(date,time : word;var ft :TFileTime) : boolean;
  143. external 'kernel32' name 'DosDateTimeToFileTime';
  144. function FileTimeToLocalFileTime(const ft : TFileTime;var lft : TFileTime) : boolean;
  145. external 'kernel32' name 'FileTimeToLocalFileTime';
  146. function LocalFileTimeToFileTime(const lft : TFileTime;var ft : TFileTime) : boolean;
  147. external 'kernel32' name 'LocalFileTimeToFileTime';
  148. type
  149. Longrec=packed record
  150. lo,hi : word;
  151. end;
  152. function Last2DosError(d:dword):integer;
  153. begin
  154. Last2DosError:=d;
  155. end;
  156. Function DosToWinAttr (Const Attr : Longint) : longint;
  157. begin
  158. DosToWinAttr:=Attr;
  159. end;
  160. Function WinToDosAttr (Const Attr : Longint) : longint;
  161. begin
  162. WinToDosAttr:=Attr;
  163. end;
  164. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):boolean;
  165. var
  166. lft : TFileTime;
  167. begin
  168. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,lft) and
  169. LocalFileTimeToFileTime(lft,Wtime);
  170. end;
  171. Function WinToDosTime (Const Wtime : TFileTime;var DTime:longint):boolean;
  172. var
  173. lft : TFileTime;
  174. begin
  175. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  176. FileTimeToDosDateTime(lft,longrec(dtime).hi,longrec(dtime).lo);
  177. end;
  178. {******************************************************************************
  179. --- Dos Interrupt ---
  180. ******************************************************************************}
  181. procedure intr(intno : byte;var regs : registers);
  182. begin
  183. { !!!!!!!! }
  184. end;
  185. procedure msdos(var regs : registers);
  186. begin
  187. { !!!!!!!! }
  188. end;
  189. {******************************************************************************
  190. --- Info / Date / Time ---
  191. ******************************************************************************}
  192. function GetVersion : longint;
  193. external 'kernel32' name 'GetVersion';
  194. procedure GetLocalTime(var t : TSystemTime);
  195. external 'kernel32' name 'GetLocalTime';
  196. function SetLocalTime(const t : TSystemTime) : boolean;
  197. external 'kernel32' name 'SetLocalTime';
  198. function dosversion : word;
  199. begin
  200. dosversion:=GetVersion;
  201. end;
  202. procedure getdate(var year,month,mday,wday : word);
  203. var
  204. t : TSystemTime;
  205. begin
  206. GetLocalTime(t);
  207. year:=t.wYear;
  208. month:=t.wMonth;
  209. mday:=t.wDay;
  210. wday:=t.wDayOfWeek;
  211. end;
  212. procedure setdate(year,month,day : word);
  213. var
  214. t : TSystemTime;
  215. begin
  216. { we need the time set privilege }
  217. { so this function crash currently }
  218. {!!!!!}
  219. GetLocalTime(t);
  220. t.wYear:=year;
  221. t.wMonth:=month;
  222. t.wDay:=day;
  223. { only a quite good solution, we can loose some ms }
  224. SetLocalTime(t);
  225. end;
  226. procedure gettime(var hour,minute,second,sec100 : word);
  227. var
  228. t : TSystemTime;
  229. begin
  230. GetLocalTime(t);
  231. hour:=t.wHour;
  232. minute:=t.wMinute;
  233. second:=t.wSecond;
  234. sec100:=t.wMilliSeconds div 10;
  235. end;
  236. procedure settime(hour,minute,second,sec100 : word);
  237. var
  238. t : TSystemTime;
  239. begin
  240. { we need the time set privilege }
  241. { so this function crash currently }
  242. {!!!!!}
  243. GetLocalTime(t);
  244. t.wHour:=hour;
  245. t.wMinute:=minute;
  246. t.wSecond:=second;
  247. t.wMilliSeconds:=sec100*10;
  248. SetLocalTime(t);
  249. end;
  250. Procedure packtime(var t : datetime;var p : longint);
  251. Begin
  252. p:=(t.sec shr 1)+(t.min shl 5)+(t.hour shl 11)+(t.day shl 16)+(t.month shl 21)+((t.year-1980) shl 25);
  253. End;
  254. Procedure unpacktime(p : longint;var t : datetime);
  255. Begin
  256. with t do
  257. begin
  258. sec:=(p and 31) shl 1;
  259. min:=(p shr 5) and 63;
  260. hour:=(p shr 11) and 31;
  261. day:=(p shr 16) and 31;
  262. month:=(p shr 21) and 15;
  263. year:=(p shr 25)+1980;
  264. end;
  265. End;
  266. {******************************************************************************
  267. --- Exec ---
  268. ******************************************************************************}
  269. function CreateProcess(lpApplicationName: PChar; lpCommandLine: PChar;
  270. lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
  271. bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer;
  272. lpCurrentDirectory: PChar; const lpStartupInfo: TStartupInfo;
  273. var lpProcessInformation: TProcessInformation): boolean;
  274. external 'kernel32' name 'CreateProcessA';
  275. function getExitCodeProcess(h:THandle;var code:longint):boolean;
  276. external 'kernel32' name 'GetExitCodeProcess';
  277. function WaitForSingleObject(hHandle: THandle; dwMilliseconds: DWORD): DWORD;
  278. external 'kernel32' name 'WaitForSingleObject';
  279. function CloseHandle(h : THandle) : longint;
  280. external 'kernel32' name 'CloseHandle';
  281. var
  282. lastdosexitcode : word;
  283. procedure exec(const path : pathstr;const comline : comstr);
  284. var
  285. SI: TStartupInfo;
  286. PI: TProcessInformation;
  287. Proc : THandle;
  288. l : Longint;
  289. AppPath,
  290. AppParam : array[0..255] of char;
  291. begin
  292. FillChar(SI, SizeOf(SI), 0);
  293. SI.cb:=SizeOf(SI);
  294. SI.wShowWindow:=1;
  295. Move(Path[1],AppPath,length(Path));
  296. AppPath[Length(Path)]:=#0;
  297. AppParam[0]:='-';
  298. AppParam[1]:=' ';
  299. Move(ComLine[1],AppParam[2],length(Comline));
  300. AppParam[Length(ComLine)+2]:=#0;
  301. if not CreateProcess(PChar(@AppPath), PChar(@AppParam), Nil, Nil, False,$20, Nil, Nil, SI, PI) then
  302. begin
  303. DosError:=Last2DosError(GetLastError);
  304. exit;
  305. end;
  306. Proc:=PI.hProcess;
  307. CloseHandle(PI.hThread);
  308. if WaitForSingleObject(Proc, Infinite) <> $ffffffff then
  309. GetExitCodeProcess(Proc,l)
  310. else
  311. l:=-1;
  312. CloseHandle(Proc);
  313. LastDosExitCode:=l;
  314. end;
  315. function dosexitcode : word;
  316. begin
  317. dosexitcode:=lastdosexitcode;
  318. end;
  319. procedure getcbreak(var breakvalue : boolean);
  320. begin
  321. { !! No Win32 Function !! }
  322. end;
  323. procedure setcbreak(breakvalue : boolean);
  324. begin
  325. { !! No Win32 Function !! }
  326. end;
  327. procedure getverify(var verify : boolean);
  328. begin
  329. { !! No Win32 Function !! }
  330. end;
  331. procedure setverify(verify : boolean);
  332. begin
  333. { !! No Win32 Function !! }
  334. end;
  335. {******************************************************************************
  336. --- Disk ---
  337. ******************************************************************************}
  338. function GetDiskFreeSpace(drive:pchar;var sector_cluster,bytes_sector,
  339. freeclusters,totalclusters:longint):boolean;
  340. external 'kernel32' name 'GetDiskFreeSpaceA';
  341. function diskfree(drive : byte) : longint;
  342. var
  343. disk : array[1..4] of char;
  344. secs,bytes,
  345. free,total : longint;
  346. begin
  347. if drive=0 then
  348. begin
  349. disk[1]:='\';
  350. disk[2]:=#0;
  351. end
  352. else
  353. begin
  354. disk[1]:=chr(drive+64);
  355. disk[2]:=':';
  356. disk[3]:='\';
  357. disk[4]:=#0;
  358. end;
  359. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  360. diskfree:=free*secs*bytes
  361. else
  362. diskfree:=-1;
  363. end;
  364. function disksize(drive : byte) : longint;
  365. var
  366. disk : array[1..4] of char;
  367. secs,bytes,
  368. free,total : longint;
  369. begin
  370. if drive=0 then
  371. begin
  372. disk[1]:='\';
  373. disk[2]:=#0;
  374. end
  375. else
  376. begin
  377. disk[1]:=chr(drive+64);
  378. disk[2]:=':';
  379. disk[3]:='\';
  380. disk[4]:=#0;
  381. end;
  382. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  383. disksize:=total*secs*bytes
  384. else
  385. disksize:=-1;
  386. end;
  387. {******************************************************************************
  388. --- Findfirst FindNext ---
  389. ******************************************************************************}
  390. { Needed kernel calls }
  391. function FindFirstFile (lpFileName: PChar; var lpFindFileData: TWIN32FindData): THandle;
  392. external 'kernel32' name 'FindFirstFileA';
  393. function FindNextFile (hFindFile: THandle; var lpFindFileData: TWIN32FindData): Boolean;
  394. external 'kernel32' name 'FindNextFileA';
  395. function FindCloseFile (hFindFile: THandle): Boolean;
  396. external 'kernel32' name 'FindClose';
  397. Procedure StringToPchar (Var S : String);
  398. Var L : Longint;
  399. begin
  400. L:=ord(S[0]);
  401. Move (S[1],S[0],L);
  402. S[L]:=#0;
  403. end;
  404. procedure FindMatch(var f:searchrec);
  405. Var
  406. TheAttr : Longint;
  407. begin
  408. TheAttr:=DosToWinAttr(F.Attr);
  409. { Find file with correct attribute }
  410. While (F.W32FindData.dwFileAttributes and TheAttr)=0 do
  411. begin
  412. if not FindNextFile (F.FindHandle,F.W32FindData) then
  413. begin
  414. DosError:=Last2DosError(GetLastError);
  415. exit;
  416. end;
  417. end;
  418. { Convert some attributes back }
  419. f.size:=F.W32FindData.NFileSizeLow;
  420. f.attr:=WinToDosAttr(F.W32FindData.dwFileAttributes);
  421. WinToDosTime(F.W32FindData.ftLastWriteTime,f.Time);
  422. f.Name:=StrPas(@F.W32FindData.cFileName);
  423. end;
  424. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  425. begin
  426. { no error }
  427. doserror:=0;
  428. F.Name:=Path;
  429. F.Attr:=attr;
  430. StringToPchar(f.name);
  431. { FindFirstFile is a Win32 Call. }
  432. F.FindHandle:=FindFirstFile (pchar(@f.Name),F.W32FindData);
  433. If longint(F.FindHandle)=Invalid_Handle_value then
  434. begin
  435. DosError:=Last2DosError(GetLastError);
  436. exit;
  437. end;
  438. { Find file with correct attribute }
  439. FindMatch(f);
  440. end;
  441. procedure findnext(var f : searchRec);
  442. begin
  443. { no error }
  444. doserror:=0;
  445. if not FindNextFile (F.FindHandle,F.W32FindData) then
  446. begin
  447. DosError:=Last2DosError(GetLastError);
  448. exit;
  449. end;
  450. { Find file with correct attribute }
  451. FindMatch(f);
  452. end;
  453. procedure swapvectors;
  454. begin
  455. end;
  456. Procedure FindClose(Var f: SearchRec);
  457. begin
  458. If longint(F.FindHandle)<>Invalid_Handle_value then
  459. FindCloseFile(F.FindHandle);
  460. end;
  461. {******************************************************************************
  462. --- File ---
  463. ******************************************************************************}
  464. function GetFileTime(h : longint;creation,lastaccess,lastwrite : PFileTime) : boolean;
  465. external 'kernel32' name 'GetFileTime';
  466. function SetFileTime(h : longint;creation,lastaccess,lastwrite : PFileTime) : boolean;
  467. external 'kernel32' name 'SetFileTime';
  468. function SetFileAttributes(lpFileName : pchar;dwFileAttributes : longint) : boolean;
  469. external 'kernel32' name 'SetFileAttributesA';
  470. function GetFileAttributes(lpFileName : pchar) : longint;
  471. external 'kernel32' name 'GetFileAttributesA';
  472. procedure fsplit(path : pathstr;var dir : dirstr;var name : namestr;var ext : extstr);
  473. var
  474. p1,i : longint;
  475. begin
  476. { allow slash as backslash }
  477. for i:=1 to length(path) do
  478. if path[i]='/' then path[i]:='\';
  479. { get drive name }
  480. p1:=pos(':',path);
  481. if p1>0 then
  482. begin
  483. dir:=path[1]+':';
  484. delete(path,1,p1);
  485. end
  486. else
  487. dir:='';
  488. { split the path and the name, there are no more path informtions }
  489. { if path contains no backslashes }
  490. while true do
  491. begin
  492. p1:=pos('\',path);
  493. if p1=0 then
  494. break;
  495. dir:=dir+copy(path,1,p1);
  496. delete(path,1,p1);
  497. end;
  498. { try to find out a extension }
  499. p1:=pos('.',path);
  500. if p1>0 then
  501. begin
  502. ext:=copy(path,p1,4);
  503. delete(path,p1,length(path)-p1+1);
  504. end
  505. else
  506. ext:='';
  507. name:=path;
  508. end;
  509. function fexpand(const path : pathstr) : pathstr;
  510. var
  511. s,pa : string[79];
  512. i,j : longint;
  513. begin
  514. getdir(0,s);
  515. pa:=upcase(path);
  516. { allow slash as backslash }
  517. for i:=1 to length(pa) do
  518. if pa[i]='/' then
  519. pa[i]:='\';
  520. if (length(pa)>1) and (pa[1] in ['A'..'Z']) and (pa[2]=':') then
  521. begin
  522. { we must get the right directory }
  523. getdir(ord(pa[1])-ord('A')+1,s);
  524. if (ord(pa[0])>2) and (pa[3]<>'\') then
  525. if pa[1]=s[1] then
  526. pa:=s+'\'+copy (pa,3,length(pa))
  527. else
  528. pa:=pa[1]+':\'+copy (pa,3,length(pa))
  529. end
  530. else
  531. if pa[1]='\' then
  532. pa:=s[1]+':'+pa
  533. else if s[0]=#3 then
  534. pa:=s+pa
  535. else
  536. pa:=s+'\'+pa;
  537. { Turbo Pascal gives current dir on drive if only drive given as parameter! }
  538. if length(pa) = 2 then
  539. begin
  540. getdir(byte(pa[1])-64,s);
  541. pa := s;
  542. end;
  543. {First remove all references to '\.\'}
  544. while pos ('\.\',pa)<>0 do
  545. delete (pa,pos('\.\',pa),2);
  546. {Now remove also all references to '\..\' + of course previous dirs..}
  547. repeat
  548. i:=pos('\..\',pa);
  549. if i<>0 then
  550. begin
  551. j:=i-1;
  552. while (j>1) and (pa[j]<>'\') do
  553. dec (j);
  554. if pa[j+1] = ':' then j := 3;
  555. delete (pa,j,i-j+3);
  556. end;
  557. until i=0;
  558. { Turbo Pascal gets rid of a \.. at the end of the path }
  559. { Now remove also any reference to '\..' at end of line
  560. + of course previous dir.. }
  561. i:=pos('\..',pa);
  562. if i<>0 then
  563. begin
  564. if i = length(pa) - 2 then
  565. begin
  566. j:=i-1;
  567. while (j>1) and (pa[j]<>'\') do
  568. dec (j);
  569. delete (pa,j,i-j+3);
  570. end;
  571. pa := pa + '\';
  572. end;
  573. { Remove End . and \}
  574. if (length(pa)>0) and (pa[length(pa)]='.') then
  575. dec(byte(pa[0]));
  576. { if only the drive + a '\' is left then the '\' should be left to prevtn the program
  577. accessing the current directory on the drive rather than the root!}
  578. { if the last char of path = '\' then leave it in as this is what TP does! }
  579. if ((length(pa)>3) and (pa[length(pa)]='\')) and (path[length(path)] <> '\') then
  580. dec(byte(pa[0]));
  581. { if only a drive is given in path then there should be a '\' at the
  582. end of the string given back }
  583. if length(path) = 2 then pa := pa + '\';
  584. fexpand:=pa;
  585. end;
  586. Function FSearch(path: pathstr; dirlist: string): pathstr;
  587. var
  588. i,p1 : longint;
  589. s : searchrec;
  590. newdir : pathstr;
  591. begin
  592. { No wildcards allowed in these things }
  593. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  594. fsearch:=''
  595. else
  596. begin
  597. { allow slash as backslash }
  598. for i:=1 to length(dirlist) do
  599. if dirlist[i]='/' then dirlist[i]:='\';
  600. repeat
  601. p1:=pos(';',dirlist);
  602. if p1=0 then
  603. begin
  604. newdir:=copy(dirlist,1,p1-1);
  605. delete(dirlist,1,p1);
  606. end
  607. else
  608. begin
  609. newdir:=dirlist;
  610. dirlist:='';
  611. end;
  612. if (newdir<>'') and (not (newdir[length(newdir)] in ['\',':'])) then
  613. newdir:=newdir+'\';
  614. findfirst(newdir+path,anyfile,s);
  615. if doserror=0 then
  616. newdir:=newdir+path
  617. else
  618. newdir:='';
  619. until (dirlist='') or (newdir<>'');
  620. fsearch:=newdir;
  621. end;
  622. end;
  623. procedure getftime(var f;var time : longint);
  624. var
  625. ft : TFileTime;
  626. begin
  627. if GetFileTime(filerec(f).Handle,nil,nil,@ft) and
  628. WinToDosTime(ft,time) then
  629. exit
  630. else
  631. time:=0;
  632. end;
  633. procedure setftime(var f;time : longint);
  634. var
  635. ft : TFileTime;
  636. begin
  637. if DosToWinTime(time,ft) then
  638. SetFileTime(filerec(f).Handle,nil,nil,@ft);
  639. end;
  640. procedure getfattr(var f;var attr : word);
  641. var
  642. l : longint;
  643. begin
  644. l:=GetFileAttributes(filerec(f).name);
  645. if l=$ffffffff then
  646. doserror:=getlasterror;
  647. attr:=l;
  648. end;
  649. procedure setfattr(var f;attr : word);
  650. begin
  651. doserror:=0;
  652. if not(SetFileAttributes(filerec(f).name,attr)) then
  653. doserror:=getlasterror;
  654. end;
  655. {******************************************************************************
  656. --- Environment ---
  657. ******************************************************************************}
  658. {
  659. The environment is a block of zero terminated strings
  660. terminated by a #0
  661. }
  662. function GetEnvironmentStrings : pchar;
  663. external 'kernel32' name 'GetEnvironmentStringsA';
  664. function FreeEnvironmentStrings(p : pchar) : boolean;
  665. external 'kernel32' name 'FreeEnvironmentStringsA';
  666. function envcount : longint;
  667. var
  668. hp,p : pchar;
  669. count : longint;
  670. begin
  671. p:=GetEnvironmentStrings;
  672. hp:=p;
  673. count:=0;
  674. while hp^<>#0 do
  675. begin
  676. { next string entry}
  677. hp:=hp+strlen(hp)+1;
  678. inc(count);
  679. end;
  680. FreeEnvironmentStrings(p);
  681. envcount:=count;
  682. end;
  683. Function EnvStr(index: integer): string;
  684. var
  685. hp,p : pchar;
  686. count,i : longint;
  687. begin
  688. { envcount takes some time in win32 }
  689. count:=envcount;
  690. { range checking }
  691. if (index<=0) or (index>count) then
  692. begin
  693. envstr:='';
  694. exit;
  695. end;
  696. p:=GetEnvironmentStrings;
  697. hp:=p;
  698. { retrive the string with the given index }
  699. for i:=2 to index do
  700. hp:=hp+strlen(hp)+1;
  701. envstr:=strpas(hp);
  702. FreeEnvironmentStrings(p);
  703. end;
  704. Function GetEnv(envvar: string): string;
  705. var
  706. s : string;
  707. i : longint;
  708. hp,p : pchar;
  709. begin
  710. getenv:='';
  711. p:=GetEnvironmentStrings;
  712. hp:=p;
  713. while hp^<>#0 do
  714. begin
  715. s:=strpas(hp);
  716. i:=pos('=',s);
  717. if copy(s,1,i-1)=envvar then
  718. begin
  719. getenv:=copy(s,i+1,length(s)-i);
  720. break;
  721. end;
  722. { next string entry}
  723. hp:=hp+strlen(hp)+1;
  724. end;
  725. FreeEnvironmentStrings(p);
  726. end;
  727. {******************************************************************************
  728. --- Not Supported ---
  729. ******************************************************************************}
  730. Procedure keep(exitcode : word);
  731. Begin
  732. End;
  733. Procedure getintvec(intno : byte;var vector : pointer);
  734. Begin
  735. End;
  736. Procedure setintvec(intno : byte;vector : pointer);
  737. Begin
  738. End;
  739. end.
  740. {
  741. $Log$
  742. Revision 1.8 1998-08-16 09:12:11 michael
  743. Corrected fexpand behaviour.
  744. Revision 1.7 1998/06/10 10:39:13 peter
  745. * working w32 rtl
  746. Revision 1.6 1998/06/08 23:07:45 peter
  747. * dos interface is now 100% compatible
  748. * fixed call PASCALMAIN which must be direct asm
  749. Revision 1.5 1998/05/06 12:36:50 michael
  750. + Removed log from before restored version.
  751. Revision 1.4 1998/04/27 14:01:38 florian
  752. * was uncompilable
  753. Revision 1.3 1998/04/26 22:37:02 florian
  754. + getftime, unpacktime, packtime
  755. Revision 1.2 1998/04/26 21:49:09 florian
  756. + first compiling and working version
  757. }