dos.pp 24 KB

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