dos.pp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. function GetShortName(var p : String) : boolean;
  120. function GetLongName(var p : String) : boolean;
  121. {Environment}
  122. Function EnvCount: longint;
  123. Function EnvStr(index: integer): string;
  124. Function GetEnv(envvar: string): string;
  125. {Misc}
  126. Procedure SetFAttr(var f; attr: word);
  127. Procedure SetFTime(var f; time: longint);
  128. Procedure GetCBreak(var breakvalue: boolean);
  129. Procedure SetCBreak(breakvalue: boolean);
  130. Procedure GetVerify(var verify: boolean);
  131. Procedure SetVerify(verify: boolean);
  132. {Do Nothing Functions}
  133. Procedure SwapVectors;
  134. Procedure GetIntVec(intno: byte; var vector: pointer);
  135. Procedure SetIntVec(intno: byte; vector: pointer);
  136. Procedure Keep(exitcode: word);
  137. Const
  138. { allow EXEC to inherited handles from calling process,
  139. needed for FPREDIR in ide/text
  140. now set to true by default because
  141. other OS also pass open handles to childs
  142. finally reset to false after Florian's response PM }
  143. ExecInheritsHandles : BOOL = false;
  144. implementation
  145. uses strings;
  146. type
  147. OSVERSIONINFO = record
  148. dwOSVersionInfoSize : DWORD;
  149. dwMajorVersion : DWORD;
  150. dwMinorVersion : DWORD;
  151. dwBuildNumber : DWORD;
  152. dwPlatformId : DWORD;
  153. szCSDVersion : array[0..127] of char;
  154. end;
  155. LPOSVERSIONINFO = ^OSVERSIONINFO;
  156. var
  157. versioninfo : OSVERSIONINFO;
  158. kernel32dll : THandle;
  159. {******************************************************************************
  160. --- Conversion ---
  161. ******************************************************************************}
  162. function GetLastError : DWORD;
  163. external 'kernel32' name 'GetLastError';
  164. function FileTimeToDosDateTime(const ft :TFileTime;var data,time : word) : longbool;
  165. external 'kernel32' name 'FileTimeToDosDateTime';
  166. function DosDateTimeToFileTime(date,time : word;var ft :TFileTime) : longbool;
  167. external 'kernel32' name 'DosDateTimeToFileTime';
  168. function FileTimeToLocalFileTime(const ft : TFileTime;var lft : TFileTime) : longbool;
  169. external 'kernel32' name 'FileTimeToLocalFileTime';
  170. function LocalFileTimeToFileTime(const lft : TFileTime;var ft : TFileTime) : longbool;
  171. external 'kernel32' name 'LocalFileTimeToFileTime';
  172. type
  173. Longrec=packed record
  174. lo,hi : word;
  175. end;
  176. function Last2DosError(d:dword):integer;
  177. begin
  178. Last2DosError:=d;
  179. end;
  180. Function DosToWinAttr (Const Attr : Longint) : longint;
  181. begin
  182. DosToWinAttr:=Attr;
  183. end;
  184. Function WinToDosAttr (Const Attr : Longint) : longint;
  185. begin
  186. WinToDosAttr:=Attr;
  187. end;
  188. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  189. var
  190. lft : TFileTime;
  191. begin
  192. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,lft) and
  193. LocalFileTimeToFileTime(lft,Wtime);
  194. end;
  195. Function WinToDosTime (Const Wtime : TFileTime;var DTime:longint):longbool;
  196. var
  197. lft : TFileTime;
  198. begin
  199. WinToDosTime:=FileTimeToLocalFileTime(WTime,lft) and
  200. FileTimeToDosDateTime(lft,longrec(dtime).hi,longrec(dtime).lo);
  201. end;
  202. {******************************************************************************
  203. --- Dos Interrupt ---
  204. ******************************************************************************}
  205. procedure intr(intno : byte;var regs : registers);
  206. begin
  207. { !!!!!!!! }
  208. end;
  209. procedure msdos(var regs : registers);
  210. begin
  211. { !!!!!!!! }
  212. end;
  213. {******************************************************************************
  214. --- Info / Date / Time ---
  215. ******************************************************************************}
  216. function GetVersion : longint;
  217. external 'kernel32' name 'GetVersion';
  218. procedure GetLocalTime(var t : TSystemTime);
  219. external 'kernel32' name 'GetLocalTime';
  220. function SetLocalTime(const t : TSystemTime) : longbool;
  221. external 'kernel32' name 'SetLocalTime';
  222. function dosversion : word;
  223. begin
  224. dosversion:=GetVersion;
  225. end;
  226. procedure getdate(var year,month,mday,wday : word);
  227. var
  228. t : TSystemTime;
  229. begin
  230. GetLocalTime(t);
  231. year:=t.wYear;
  232. month:=t.wMonth;
  233. mday:=t.wDay;
  234. wday:=t.wDayOfWeek;
  235. end;
  236. procedure setdate(year,month,day : 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.wYear:=year;
  245. t.wMonth:=month;
  246. t.wDay:=day;
  247. { only a quite good solution, we can loose some ms }
  248. SetLocalTime(t);
  249. end;
  250. procedure gettime(var hour,minute,second,sec100 : word);
  251. var
  252. t : TSystemTime;
  253. begin
  254. GetLocalTime(t);
  255. hour:=t.wHour;
  256. minute:=t.wMinute;
  257. second:=t.wSecond;
  258. sec100:=t.wMilliSeconds div 10;
  259. end;
  260. procedure settime(hour,minute,second,sec100 : word);
  261. var
  262. t : TSystemTime;
  263. begin
  264. { we need the time set privilege }
  265. { so this function crash currently }
  266. {!!!!!}
  267. GetLocalTime(t);
  268. t.wHour:=hour;
  269. t.wMinute:=minute;
  270. t.wSecond:=second;
  271. t.wMilliSeconds:=sec100*10;
  272. SetLocalTime(t);
  273. end;
  274. Procedure packtime(var t : datetime;var p : longint);
  275. Begin
  276. 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);
  277. End;
  278. Procedure unpacktime(p : longint;var t : datetime);
  279. Begin
  280. with t do
  281. begin
  282. sec:=(p and 31) shl 1;
  283. min:=(p shr 5) and 63;
  284. hour:=(p shr 11) and 31;
  285. day:=(p shr 16) and 31;
  286. month:=(p shr 21) and 15;
  287. year:=(p shr 25)+1980;
  288. end;
  289. End;
  290. {******************************************************************************
  291. --- Exec ---
  292. ******************************************************************************}
  293. function CreateProcess(lpApplicationName: PChar; lpCommandLine: PChar;
  294. lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
  295. bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer;
  296. lpCurrentDirectory: PChar; const lpStartupInfo: TStartupInfo;
  297. var lpProcessInformation: TProcessInformation): longbool;
  298. external 'kernel32' name 'CreateProcessA';
  299. function getExitCodeProcess(h:THandle;var code:longint):longbool;
  300. external 'kernel32' name 'GetExitCodeProcess';
  301. function WaitForSingleObject(hHandle: THandle; dwMilliseconds: DWORD): DWORD;
  302. external 'kernel32' name 'WaitForSingleObject';
  303. function CloseHandle(h : THandle) : longint;
  304. external 'kernel32' name 'CloseHandle';
  305. var
  306. lastdosexitcode : longint;
  307. procedure exec(const path : pathstr;const comline : comstr);
  308. var
  309. SI: TStartupInfo;
  310. PI: TProcessInformation;
  311. Proc : THandle;
  312. l : Longint;
  313. AppPath,
  314. AppParam : array[0..255] of char;
  315. begin
  316. FillChar(SI, SizeOf(SI), 0);
  317. SI.cb:=SizeOf(SI);
  318. SI.wShowWindow:=1;
  319. Move(Path[1],AppPath,length(Path));
  320. AppPath[Length(Path)]:=#0;
  321. AppParam[0]:='-';
  322. AppParam[1]:=' ';
  323. Move(ComLine[1],AppParam[2],length(Comline));
  324. AppParam[Length(ComLine)+2]:=#0;
  325. if not CreateProcess(PChar(@AppPath), PChar(@AppParam),
  326. Nil, Nil, ExecInheritsHandles,$20, Nil, Nil, SI, PI) then
  327. begin
  328. DosError:=Last2DosError(GetLastError);
  329. exit;
  330. end;
  331. Proc:=PI.hProcess;
  332. CloseHandle(PI.hThread);
  333. if WaitForSingleObject(Proc, Infinite) <> $ffffffff then
  334. GetExitCodeProcess(Proc,l)
  335. else
  336. l:=-1;
  337. CloseHandle(Proc);
  338. LastDosExitCode:=l;
  339. end;
  340. function dosexitcode : word;
  341. begin
  342. dosexitcode:=lastdosexitcode and $ffff;
  343. end;
  344. procedure getcbreak(var breakvalue : boolean);
  345. begin
  346. { !! No Win32 Function !! }
  347. end;
  348. procedure setcbreak(breakvalue : boolean);
  349. begin
  350. { !! No Win32 Function !! }
  351. end;
  352. procedure getverify(var verify : boolean);
  353. begin
  354. { !! No Win32 Function !! }
  355. end;
  356. procedure setverify(verify : boolean);
  357. begin
  358. { !! No Win32 Function !! }
  359. end;
  360. {******************************************************************************
  361. --- Disk ---
  362. ******************************************************************************}
  363. function GetDiskFreeSpace(drive:pchar;var sector_cluster,bytes_sector,
  364. freeclusters,totalclusters:longint):longbool;
  365. external 'kernel32' name 'GetDiskFreeSpaceA';
  366. type
  367. TGetDiskFreeSpaceEx = function(drive:pchar;var availableforcaller,
  368. total,free):longbool;stdcall;
  369. var
  370. GetDiskFreeSpaceEx : TGetDiskFreeSpaceEx;
  371. function diskfree(drive : byte) : int64;
  372. var
  373. disk : array[1..4] of char;
  374. secs,bytes,
  375. free,total : longint;
  376. qwtotal,qwfree,qwcaller : int64;
  377. begin
  378. if drive=0 then
  379. begin
  380. disk[1]:='\';
  381. disk[2]:=#0;
  382. end
  383. else
  384. begin
  385. disk[1]:=chr(drive+64);
  386. disk[2]:=':';
  387. disk[3]:='\';
  388. disk[4]:=#0;
  389. end;
  390. if assigned(GetDiskFreeSpaceEx) then
  391. begin
  392. if GetDiskFreeSpaceEx(@disk,qwcaller,qwtotal,qwfree) then
  393. diskfree:=qwfree
  394. else
  395. diskfree:=-1;
  396. end
  397. else
  398. begin
  399. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  400. diskfree:=int64(free)*secs*bytes
  401. else
  402. diskfree:=-1;
  403. end;
  404. end;
  405. function disksize(drive : byte) : int64;
  406. var
  407. disk : array[1..4] of char;
  408. secs,bytes,
  409. free,total : longint;
  410. qwtotal,qwfree,qwcaller : int64;
  411. begin
  412. if drive=0 then
  413. begin
  414. disk[1]:='\';
  415. disk[2]:=#0;
  416. end
  417. else
  418. begin
  419. disk[1]:=chr(drive+64);
  420. disk[2]:=':';
  421. disk[3]:='\';
  422. disk[4]:=#0;
  423. end;
  424. if assigned(GetDiskFreeSpaceEx) then
  425. begin
  426. if GetDiskFreeSpaceEx(@disk,qwcaller,qwtotal,qwfree) then
  427. disksize:=qwtotal
  428. else
  429. disksize:=-1;
  430. end
  431. else
  432. begin
  433. if GetDiskFreeSpace(@disk,secs,bytes,free,total) then
  434. disksize:=int64(total)*secs*bytes
  435. else
  436. disksize:=-1;
  437. end;
  438. end;
  439. {******************************************************************************
  440. --- Findfirst FindNext ---
  441. ******************************************************************************}
  442. { Needed kernel calls }
  443. function FindFirstFile (lpFileName: PChar; var lpFindFileData: TWIN32FindData): THandle;
  444. external 'kernel32' name 'FindFirstFileA';
  445. function FindNextFile (hFindFile: THandle; var lpFindFileData: TWIN32FindData): LongBool;
  446. external 'kernel32' name 'FindNextFileA';
  447. function FindCloseFile (hFindFile: THandle): LongBool;
  448. external 'kernel32' name 'FindClose';
  449. Procedure StringToPchar (Var S : String);
  450. Var L : Longint;
  451. begin
  452. L:=ord(S[0]);
  453. Move (S[1],S[0],L);
  454. S[L]:=#0;
  455. end;
  456. Procedure PCharToString (Var S : String);
  457. Var L : Longint;
  458. begin
  459. L:=strlen(pchar(@S[0]));
  460. Move (S[0],S[1],L);
  461. S[0]:=char(l);
  462. end;
  463. procedure FindMatch(var f:searchrec);
  464. begin
  465. { Find file with correct attribute }
  466. While (F.W32FindData.dwFileAttributes and cardinal(F.ExcludeAttr))<>0 do
  467. begin
  468. if not FindNextFile (F.FindHandle,F.W32FindData) then
  469. begin
  470. DosError:=Last2DosError(GetLastError);
  471. exit;
  472. end;
  473. end;
  474. { Convert some attributes back }
  475. f.size:=F.W32FindData.NFileSizeLow;
  476. f.attr:=WinToDosAttr(F.W32FindData.dwFileAttributes);
  477. WinToDosTime(F.W32FindData.ftLastWriteTime,f.Time);
  478. f.Name:=StrPas(@F.W32FindData.cFileName);
  479. end;
  480. procedure findfirst(const path : pathstr;attr : word;var f : searchRec);
  481. begin
  482. { no error }
  483. doserror:=0;
  484. F.Name:=Path;
  485. F.Attr:=attr;
  486. F.ExcludeAttr:=(not Attr) and ($1e); {hidden,sys,dir,volume}
  487. StringToPchar(f.name);
  488. { FindFirstFile is a Win32 Call }
  489. F.FindHandle:=FindFirstFile (pchar(@f.Name),F.W32FindData);
  490. If longint(F.FindHandle)=Invalid_Handle_value then
  491. begin
  492. DosError:=Last2DosError(GetLastError);
  493. exit;
  494. end;
  495. { Find file with correct attribute }
  496. FindMatch(f);
  497. end;
  498. procedure findnext(var f : searchRec);
  499. begin
  500. { no error }
  501. doserror:=0;
  502. if not FindNextFile (F.FindHandle,F.W32FindData) then
  503. begin
  504. DosError:=Last2DosError(GetLastError);
  505. exit;
  506. end;
  507. { Find file with correct attribute }
  508. FindMatch(f);
  509. end;
  510. procedure swapvectors;
  511. begin
  512. end;
  513. Procedure FindClose(Var f: SearchRec);
  514. begin
  515. If longint(F.FindHandle)<>Invalid_Handle_value then
  516. FindCloseFile(F.FindHandle);
  517. end;
  518. {******************************************************************************
  519. --- File ---
  520. ******************************************************************************}
  521. function GetFileTime(h : longint;creation,lastaccess,lastwrite : PFileTime) : longbool;
  522. external 'kernel32' name 'GetFileTime';
  523. function SetFileTime(h : longint;creation,lastaccess,lastwrite : PFileTime) : longbool;
  524. external 'kernel32' name 'SetFileTime';
  525. function SetFileAttributes(lpFileName : pchar;dwFileAttributes : longint) : longbool;
  526. external 'kernel32' name 'SetFileAttributesA';
  527. function GetFileAttributes(lpFileName : pchar) : longint;
  528. external 'kernel32' name 'GetFileAttributesA';
  529. procedure fsplit(path : pathstr;var dir : dirstr;var name : namestr;var ext : extstr);
  530. var
  531. dotpos,p1,i : longint;
  532. begin
  533. { allow slash as backslash }
  534. for i:=1 to length(path) do
  535. if path[i]='/' then path[i]:='\';
  536. { get drive name }
  537. p1:=pos(':',path);
  538. if p1>0 then
  539. begin
  540. dir:=path[1]+':';
  541. delete(path,1,p1);
  542. end
  543. else
  544. dir:='';
  545. { split the path and the name, there are no more path informtions }
  546. { if path contains no backslashes }
  547. while true do
  548. begin
  549. p1:=pos('\',path);
  550. if p1=0 then
  551. break;
  552. dir:=dir+copy(path,1,p1);
  553. delete(path,1,p1);
  554. end;
  555. { try to find out a extension }
  556. Ext:='';
  557. i:=Length(Path);
  558. DotPos:=256;
  559. While (i>0) Do
  560. Begin
  561. If (Path[i]='.') Then
  562. begin
  563. DotPos:=i;
  564. break;
  565. end;
  566. Dec(i);
  567. end;
  568. Ext:=Copy(Path,DotPos,255);
  569. Name:=Copy(Path,1,DotPos - 1);
  570. end;
  571. { <immobilizer> }
  572. function GetFullPathName(lpFileName: PChar; nBufferLength: Longint; lpBuffer: PChar; var lpFilePart : PChar):DWORD;
  573. external 'kernel32' name 'GetFullPathNameA';
  574. function GetShortPathName(lpszLongPath:pchar; lpszShortPath:pchar; cchBuffer:DWORD):DWORD;
  575. external 'kernel32' name 'GetShortPathNameA';
  576. function FExpand(const path : pathstr) : pathstr;
  577. var value : Array[0..255] of char;
  578. tmp : PChar;
  579. p : string;
  580. i : Longint;
  581. begin
  582. { if path is empty then return the current dir }
  583. if path<>'' then
  584. p:=path
  585. else
  586. p:='.';
  587. { allow slash as backslash }
  588. for i:=1 to length(p) do
  589. if p[i]='/' then
  590. p[i]:='\';
  591. StringToPchar(p);
  592. tmp:=nil;
  593. fillchar(value,sizeof(value),0);
  594. GetFullPathName(@p, 255, value, tmp);
  595. FExpand := strpas(value);
  596. end;
  597. function SearchPath(lpPath : PChar; lpFileName : PChar; lpExtension : PChar; nBufferLength : Longint; lpBuffer : PChar;
  598. var lpFilePart : PChar) : Longint; external 'kernel32' name 'SearchPathA';
  599. Function FSearch(path: pathstr; dirlist: string): pathstr;
  600. var temp : PChar;
  601. value : Array [0..255] of char;
  602. i : Longint;
  603. dir,dir2 : dirstr;
  604. lastchar : char;
  605. name : namestr;
  606. ext : extstr;
  607. s : SearchRec;
  608. found : boolean;
  609. begin
  610. { check if the file specified exists }
  611. findfirst(path,anyfile,s);
  612. found:=(doserror=0);
  613. findclose(s);
  614. if found then
  615. begin
  616. fsearch:=path;
  617. exit;
  618. end;
  619. { search the path }
  620. fsearch:='';
  621. for i:=1 to length(path) do
  622. if path[i]='/' then
  623. path[i]:='\';
  624. fsplit(path,dir,name,ext);
  625. for i:=1 to length(dirlist) do
  626. if dirlist[i]='/' then
  627. dirlist[i]:='\';
  628. { allow slash as backslash }
  629. StringToPchar(name);
  630. StringToPchar(ext);
  631. StringToPchar(dir);
  632. if SearchPath(@dir, @name, @ext, 255, @value, temp)>0 then
  633. begin
  634. fsearch := strpas(value);
  635. exit;
  636. end;
  637. PCharToString(dir);
  638. repeat
  639. i:=pos(';',dirlist);
  640. while i=1 do
  641. begin
  642. delete(dirlist,1,1);
  643. i:=pos(';',dirlist);
  644. end;
  645. if i=0 then
  646. begin
  647. dir2:=dirlist;
  648. dirlist:='';
  649. end
  650. else
  651. begin
  652. dir2:=Copy(dirlist,1,i-1);
  653. dirlist:=Copy(dirlist,i+1,255);
  654. end;
  655. { don't add anything if dir2 is empty string }
  656. if dir2<>'' then
  657. lastchar:=dir2[length(dir2)]
  658. else
  659. lastchar:='\';
  660. if (lastchar<>'\') and (lastchar<>':') then
  661. dir2:=dir2+'\'+dir
  662. else
  663. dir2:=dir2+dir;
  664. StringToPchar(dir2);
  665. if SearchPath(@dir2, @name, @ext, 255, @value, temp)>0 then
  666. begin
  667. fsearch := strpas(value);
  668. exit;
  669. end;
  670. until dirlist='';
  671. end;
  672. { </immobilizer> }
  673. procedure getftime(var f;var time : longint);
  674. var
  675. ft : TFileTime;
  676. begin
  677. if GetFileTime(filerec(f).Handle,nil,nil,@ft) and
  678. WinToDosTime(ft,time) then
  679. exit
  680. else
  681. time:=0;
  682. end;
  683. procedure setftime(var f;time : longint);
  684. var
  685. ft : TFileTime;
  686. begin
  687. if DosToWinTime(time,ft) then
  688. SetFileTime(filerec(f).Handle,nil,nil,@ft);
  689. end;
  690. procedure getfattr(var f;var attr : word);
  691. var
  692. l : longint;
  693. begin
  694. doserror:=0;
  695. l:=GetFileAttributes(filerec(f).name);
  696. if l=longint($ffffffff) then
  697. begin
  698. doserror:=getlasterror;
  699. attr:=0;
  700. end
  701. else
  702. attr:=l and $ffff;
  703. end;
  704. procedure setfattr(var f;attr : word);
  705. begin
  706. doserror:=0;
  707. if not(SetFileAttributes(filerec(f).name,attr)) then
  708. doserror:=getlasterror;
  709. end;
  710. { change to short filename if successful win32 call PM }
  711. function GetShortName(var p : String) : boolean;
  712. var
  713. buffer : array[0..255] of char;
  714. ret : longint;
  715. begin
  716. {we can't mess with p, because we have to return it if call is
  717. unsuccesfully.}
  718. if Length(p)>0 then {copy p to array of char}
  719. move(p[1],buffer[0],length(p));
  720. buffer[length(p)]:=chr(0);
  721. {Should return value load loaddoserror?}
  722. ret:=GetShortPathName(@buffer,@buffer,255);
  723. if ret=0 then
  724. p:=strpas(buffer);
  725. GetShortName:=ret<>0;
  726. end;
  727. { change to long filename if successful DOS call PM }
  728. function GetLongName(var p : String) : boolean;
  729. var
  730. lfn,sfn : array[0..255] of char;
  731. filename : pchar;
  732. ret : longint;
  733. begin
  734. {contrary to shortname, SDK does not mention input buffer can be equal
  735. to output.}
  736. if Length(p)>0 then {copy p to array of char}
  737. move(p[1],sfn[0],length(p));
  738. sfn[length(p)]:=chr(0);
  739. fillchar(lfn,sizeof(lfn),#0);
  740. filename:=nil;
  741. {Should return value load loaddoserror?}
  742. ret:=GetFullPathName(@sfn,255,@lfn,filename);
  743. if ret=0 then
  744. p:=strpas(lfn); {lfn here returns full path, filename only fn}
  745. GetLongName:=ret<>0;
  746. end;
  747. {******************************************************************************
  748. --- Environment ---
  749. ******************************************************************************}
  750. {
  751. The environment is a block of zero terminated strings
  752. terminated by a #0
  753. }
  754. function GetEnvironmentStrings : pchar;
  755. external 'kernel32' name 'GetEnvironmentStringsA';
  756. function FreeEnvironmentStrings(p : pchar) : longbool;
  757. external 'kernel32' name 'FreeEnvironmentStringsA';
  758. function envcount : longint;
  759. var
  760. hp,p : pchar;
  761. count : longint;
  762. begin
  763. p:=GetEnvironmentStrings;
  764. hp:=p;
  765. count:=0;
  766. while hp^<>#0 do
  767. begin
  768. { next string entry}
  769. hp:=hp+strlen(hp)+1;
  770. inc(count);
  771. end;
  772. FreeEnvironmentStrings(p);
  773. envcount:=count;
  774. end;
  775. Function EnvStr(index: integer): string;
  776. var
  777. hp,p : pchar;
  778. count,i : longint;
  779. begin
  780. { envcount takes some time in win32 }
  781. count:=envcount;
  782. { range checking }
  783. if (index<=0) or (index>count) then
  784. begin
  785. envstr:='';
  786. exit;
  787. end;
  788. p:=GetEnvironmentStrings;
  789. hp:=p;
  790. { retrive the string with the given index }
  791. for i:=2 to index do
  792. hp:=hp+strlen(hp)+1;
  793. envstr:=strpas(hp);
  794. FreeEnvironmentStrings(p);
  795. end;
  796. Function GetEnv(envvar: string): string;
  797. var
  798. s : string;
  799. i : longint;
  800. hp,p : pchar;
  801. begin
  802. getenv:='';
  803. p:=GetEnvironmentStrings;
  804. hp:=p;
  805. while hp^<>#0 do
  806. begin
  807. s:=strpas(hp);
  808. i:=pos('=',s);
  809. if upcase(copy(s,1,i-1))=upcase(envvar) then
  810. begin
  811. getenv:=copy(s,i+1,length(s)-i);
  812. break;
  813. end;
  814. { next string entry}
  815. hp:=hp+strlen(hp)+1;
  816. end;
  817. FreeEnvironmentStrings(p);
  818. end;
  819. {******************************************************************************
  820. --- Not Supported ---
  821. ******************************************************************************}
  822. Procedure keep(exitcode : word);
  823. Begin
  824. End;
  825. Procedure getintvec(intno : byte;var vector : pointer);
  826. Begin
  827. End;
  828. Procedure setintvec(intno : byte;vector : pointer);
  829. Begin
  830. End;
  831. function FreeLibrary(hLibModule : THANDLE) : longbool;
  832. external 'kernel32' name 'FreeLibrary';
  833. function GetVersionEx(var VersionInformation:OSVERSIONINFO) : longbool;
  834. external 'kernel32' name 'GetVersionExA';
  835. function LoadLibrary(lpLibFileName : pchar):THandle;
  836. external 'kernel32' name 'LoadLibraryA';
  837. function GetProcAddress(hModule : THandle;lpProcName : pchar) : pointer;
  838. external 'kernel32' name 'GetProcAddress';
  839. var
  840. oldexitproc : pointer;
  841. procedure dosexitproc;
  842. begin
  843. exitproc:=oldexitproc;
  844. if kernel32dll<>0 then
  845. FreeLibrary(kernel32dll);
  846. end;
  847. begin
  848. oldexitproc:=exitproc;
  849. exitproc:=@dosexitproc;
  850. versioninfo.dwOSVersionInfoSize:=sizeof(versioninfo);
  851. GetVersionEx(versioninfo);
  852. kernel32dll:=0;
  853. GetDiskFreeSpaceEx:=nil;
  854. if ((versioninfo.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) and
  855. (versioninfo.dwBuildNUmber>=1000)) or
  856. (versioninfo.dwPlatformId=VER_PLATFORM_WIN32_NT) then
  857. begin
  858. kernel32dll:=LoadLibrary('kernel32');
  859. if kernel32dll<>0 then
  860. GetDiskFreeSpaceEx:=TGetDiskFreeSpaceEx(GetProcAddress(kernel32dll,'GetDiskFreeSpaceExA'));
  861. end;
  862. end.
  863. {
  864. $Log$
  865. Revision 1.7 2000-12-18 17:28:58 jonas
  866. * fixed range check errors
  867. Revision 1.6 2000/09/06 20:47:34 peter
  868. * removed previous fsplit() patch as it's not the correct behaviour for
  869. LFNs. The code showing the bug could easily be adapted (merged)
  870. Revision 1.5 2000/09/04 20:17:54 peter
  871. * fixed previous commit (merged)
  872. Revision 1.4 2000/09/04 19:38:13 peter
  873. * fsplit with .. fix from Thomas (merged)
  874. Revision 1.3 2000/08/24 19:02:36 peter
  875. * fsearch checks if file exists first (merged)
  876. Revision 1.2 2000/07/13 11:33:57 michael
  877. + removed logs
  878. }