dos.pas 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. {****************************************************************************
  2. $Id$
  3. Free Pascal Runtime-Library
  4. DOS unit for OS/2
  5. Copyright (c) 1997,1999-2000 by Daniel Mantione,
  6. member of the Free Pascal development team
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. ****************************************************************************}
  13. unit dos;
  14. {$ASMMODE ATT}
  15. {***************************************************************************}
  16. interface
  17. {***************************************************************************}
  18. {$PACKRECORDS 1}
  19. uses Strings, DosCalls;
  20. const {Bit masks for CPU flags.}
  21. fcarry = $0001;
  22. fparity = $0004;
  23. fauxiliary = $0010;
  24. fzero = $0040;
  25. fsign = $0080;
  26. foverflow = $0800;
  27. {Bit masks for file attributes.}
  28. readonly = $01;
  29. hidden = $02;
  30. sysfile = $04;
  31. volumeid = $08;
  32. directory = $10;
  33. archive = $20;
  34. anyfile = $3F;
  35. fmclosed = $D7B0;
  36. fminput = $D7B1;
  37. fmoutput = $D7B2;
  38. fminout = $D7B3;
  39. type {Some string types:}
  40. comstr=string; {Filenames can be long in OS/2.}
  41. pathstr=string; {String for pathnames.}
  42. dirstr=string; {String for a directory}
  43. namestr=string; {String for a filename.}
  44. extstr=string[40]; {String for an extension. Can be 253
  45. characters long, in theory, but let's
  46. say fourty will be enough.}
  47. {Search record which is used by findfirst and findnext:}
  48. searchrec=record
  49. case boolean of
  50. false: (handle:longint; {Used in os_OS2 mode}
  51. FStat:PFileFindBuf3;
  52. fill2:array[1..21-SizeOf(longint)-SizeOf(pointer)] of byte;
  53. attr2:byte;
  54. time2:longint;
  55. size2:longint;
  56. name2:string); {Filenames can be long in OS/2!}
  57. true: (fill:array[1..21] of byte;
  58. attr:byte;
  59. time:longint;
  60. size:longint;
  61. name:string); {Filenames can be long in OS/2!}
  62. end;
  63. {$i filerec.inc}
  64. {$i textrec.inc}
  65. {Data structure for the registers needed by msdos and intr:}
  66. registers=packed record
  67. case i:integer of
  68. 0:(ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,
  69. f8,flags,fs,gs:word);
  70. 1:(al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh:byte);
  71. 2:(eax,ebx,ecx,edx,ebp,esi,edi:longint);
  72. end;
  73. {Record for date and time:}
  74. datetime=record
  75. year,month,day,hour,min,sec:word;
  76. end;
  77. {Flags for the exec procedure:
  78. Starting the program:
  79. efwait: Wait until program terminates.
  80. efno_wait: Don't wait until the program terminates. Does not work
  81. in dos, as DOS cannot multitask.
  82. efoverlay: Terminate this program, then execute the requested
  83. program. WARNING: Exit-procedures are not called!
  84. efdebug: Debug program. Details are unknown.
  85. efsession: Do not execute as child of this program. Use a seperate
  86. session instead.
  87. efdetach: Detached. Function unknown. Info wanted!
  88. efpm: Run as presentation manager program.
  89. Determining the window state of the program:
  90. efdefault: Run the pm program in it's default situation.
  91. efminimize: Run the pm program minimized.
  92. efmaximize: Run the pm program maximized.
  93. effullscreen: Run the non-pm program fullscreen.
  94. efwindowed: Run the non-pm program in a window.
  95. Other options are not implemented defined because lack of
  96. knowledge about what they do.}
  97. type execrunflags=(efwait,efno_wait,efoverlay,efdebug,efsession,
  98. efdetach,efpm);
  99. execwinflags=(efdefault,efminimize,efmaximize,effullscreen,
  100. efwindowed);
  101. const
  102. (* For compatibility with VP/2, used for runflags in Exec procedure. *)
  103. ExecFlags: cardinal = ord (efwait);
  104. var doserror:integer;
  105. dosexitcode:word;
  106. procedure getdate(var year,month,day,dayofweek:word);
  107. procedure gettime(var hour,minute,second,sec100:word);
  108. function dosversion:word;
  109. procedure setdate(year,month,day:word);
  110. procedure settime(hour,minute,second,sec100:word);
  111. procedure getcbreak(var breakvalue:boolean);
  112. procedure setcbreak(breakvalue:boolean);
  113. procedure getverify(var verify:boolean);
  114. procedure setverify(verify : boolean);
  115. function DiskFree (Drive: byte) : int64;
  116. function DiskSize (Drive: byte) : int64;
  117. procedure findfirst(const path:pathstr;attr:word;var f:searchRec);
  118. procedure findnext(var f:searchRec);
  119. procedure findclose(var f:searchRec);
  120. {Is a dummy:}
  121. procedure swapvectors;
  122. {Not supported:
  123. procedure getintvec(intno:byte;var vector:pointer);
  124. procedure setintvec(intno:byte;vector:pointer);
  125. procedure keep(exitcode:word);
  126. }
  127. procedure msdos(var regs:registers);
  128. procedure intr(intno : byte;var regs:registers);
  129. procedure getfattr(var f;var attr:word);
  130. procedure setfattr(var f;attr:word);
  131. function fsearch(path:pathstr;dirlist:string):pathstr;
  132. procedure getftime(var f;var time:longint);
  133. procedure setftime(var f;time:longint);
  134. procedure packtime (var d:datetime; var time:longint);
  135. procedure unpacktime (time:longint; var d:datetime);
  136. function fexpand(const path:pathstr):pathstr;
  137. procedure fsplit(path:pathstr;var dir:dirstr;var name:namestr;
  138. var ext:extstr);
  139. procedure exec(const path:pathstr;const comline:comstr);
  140. function exec(path:pathstr;runflags:execrunflags;winflags:execwinflags;
  141. const comline:comstr):longint;
  142. function envcount:longint;
  143. function envstr(index:longint) : string;
  144. function getenv(const envvar:string): string;
  145. implementation
  146. var LastSR: SearchRec;
  147. envc: longint; external name '_envc';
  148. EnvP: ppchar; external name '_environ';
  149. type TBA = array [1..SizeOf (SearchRec)] of byte;
  150. PBA = ^TBA;
  151. {Import syscall to call it nicely from assembler procedures.}
  152. procedure syscall;external name '___SYSCALL';
  153. function fsearch(path:pathstr;dirlist:string):pathstr;
  154. var i,p1:longint;
  155. newdir:pathstr;
  156. {$ASMMODE INTEL}
  157. function CheckFile (FN: ShortString):boolean; assembler;
  158. asm
  159. mov ax, 4300h
  160. mov edx, FN { get pointer to string }
  161. inc edx { avoid length byte }
  162. call syscall
  163. mov ax, 0
  164. jc @LCFstop
  165. test cx, 18h
  166. jnz @LCFstop
  167. inc ax
  168. @LCFstop:
  169. end;
  170. {$ASMMODE ATT}
  171. begin
  172. { check if the file specified exists }
  173. if CheckFile (Path + #0) then
  174. FSearch := Path
  175. else
  176. begin
  177. {No wildcards allowed in these things:}
  178. if (pos('?',path)<>0) or (pos('*',path)<>0) then
  179. fsearch:=''
  180. else
  181. begin
  182. { allow slash as backslash }
  183. for i:=1 to length(dirlist) do
  184. if dirlist[i]='/' then dirlist[i]:='\';
  185. repeat
  186. p1:=pos(';',dirlist);
  187. if p1<>0 then
  188. begin
  189. newdir:=copy(dirlist,1,p1-1);
  190. delete(dirlist,1,p1);
  191. end
  192. else
  193. begin
  194. newdir:=dirlist;
  195. dirlist:='';
  196. end;
  197. if (newdir<>'') and
  198. not (newdir[length(newdir)] in ['\',':']) then
  199. newdir:=newdir+'\';
  200. if CheckFile (NewDir + Path + #0) then
  201. NewDir := NewDir + Path
  202. else
  203. NewDir := '';
  204. until (DirList = '') or (NewDir <> '');
  205. FSearch := NewDir;
  206. end;
  207. end;
  208. end;
  209. procedure getftime(var f;var time:longint);
  210. begin
  211. asm
  212. {Load handle}
  213. movl f,%ebx
  214. movl (%ebx),%ebx
  215. {Get date}
  216. movw $0x5700,%ax
  217. call syscall
  218. shll $16,%edx
  219. movw %cx,%dx
  220. movl time,%ebx
  221. movl %edx,(%ebx)
  222. xorb %ah,%ah
  223. movw %ax,doserror
  224. end;
  225. end;
  226. procedure SetFTime (var F; Time: longint);
  227. var FStat: PFileStatus3;
  228. RC: longint;
  229. begin
  230. if os_mode = osOS2 then
  231. begin
  232. New (FStat);
  233. RC := DosQueryFileInfo (FileRec (F).Handle, ilStandard, FStat,
  234. SizeOf (FStat^));
  235. if RC = 0 then
  236. begin
  237. FStat^.DateLastAccess := Hi (Time);
  238. FStat^.DateLastWrite := Hi (Time);
  239. FStat^.TimeLastAccess := Lo (Time);
  240. FStat^.TimeLastWrite := Lo (Time);
  241. RC := DosSetFileInfo (FileRec (F).Handle, ilStandard,
  242. FStat, SizeOf (FStat^));
  243. end;
  244. DosError := integer(RC);
  245. Dispose (FStat);
  246. end
  247. else
  248. asm
  249. {Load handle}
  250. movl f,%ebx
  251. movw (%ebx),%ebx
  252. movl time,%ecx
  253. shldl $16,%ecx,%edx
  254. {Set date}
  255. movw $0x5701,%ax
  256. call syscall
  257. xorb %ah,%ah
  258. movw %ax,doserror
  259. end;
  260. end;
  261. procedure msdos(var regs:registers);
  262. {Not recommended for EMX. Only works in DOS mode, not in OS/2 mode.}
  263. begin
  264. if os_mode in [osDPMI,osDOS] then
  265. intr($21,regs);
  266. end;
  267. procedure intr(intno:byte;var regs:registers);
  268. {Not recommended for EMX. Only works in DOS mode, not in OS/2 mode.}
  269. begin
  270. if os_mode = osos2 then exit;
  271. asm
  272. jmp .Lstart
  273. { .data}
  274. .Lint86:
  275. .byte 0xcd
  276. .Lint86_vec:
  277. .byte 0x03
  278. jmp .Lint86_retjmp
  279. { .text}
  280. .Lstart:
  281. movb intno,%al
  282. movb %al,.Lint86_vec
  283. {
  284. movl 10(%ebp),%eax
  285. incl %eax
  286. incl %eax
  287. }
  288. movl regs,%eax
  289. {Do not use first int}
  290. movl 4(%eax),%ebx
  291. movl 8(%eax),%ecx
  292. movl 12(%eax),%edx
  293. movl 16(%eax),%ebp
  294. movl 20(%eax),%esi
  295. movl 24(%eax),%edi
  296. movl (%eax),%eax
  297. jmp .Lint86
  298. .Lint86_retjmp:
  299. pushf
  300. pushl %ebp
  301. pushl %eax
  302. movl %esp,%ebp
  303. {Calc EBP new}
  304. addl $12,%ebp
  305. {
  306. movl 10(%ebp),%eax
  307. incl %eax
  308. incl %eax
  309. }
  310. {Do not use first int}
  311. movl regs,%eax
  312. popl (%eax)
  313. movl %ebx,4(%eax)
  314. movl %ecx,8(%eax)
  315. movl %edx,12(%eax)
  316. {Restore EBP}
  317. popl %edx
  318. movl %edx,16(%eax)
  319. movl %esi,20(%eax)
  320. movl %edi,24(%eax)
  321. {Ignore ES and DS}
  322. popl %ebx {Flags.}
  323. movl %ebx,32(%eax)
  324. {FS and GS too}
  325. end;
  326. end;
  327. procedure exec(const path:pathstr;const comline:comstr);
  328. {Execute a program.}
  329. begin
  330. dosexitcode:=word(exec(path,execrunflags(ExecFlags),efdefault,comline));
  331. end;
  332. function exec(path:pathstr;runflags:execrunflags;winflags:execwinflags;
  333. const comline:comstr):longint;
  334. {Execute a program. More suitable for OS/2 than the exec above.}
  335. {512 bytes should be enough to contain the command-line.}
  336. type bytearray=array[0..8191] of byte;
  337. Pbytearray=^bytearray;
  338. execstruc=packed record
  339. argofs : pointer; { pointer to arguments (offset) }
  340. envofs : pointer; { pointer to environment (offset) }
  341. nameofs: pointer; { pointer to file name (offset) }
  342. argseg : word; { pointer to arguments (selector) }
  343. envseg : word; { pointer to environment (selector}
  344. nameseg: word; { pointer to file name (selector) }
  345. numarg : word; { number of arguments }
  346. sizearg : word; { size of arguments }
  347. numenv : word; { number of env strings }
  348. sizeenv:word; { size of environment }
  349. mode1,mode2:byte; { mode byte }
  350. end;
  351. var args:Pbytearray;
  352. env:Pbytearray;
  353. i,argsize:word;
  354. es:execstruc;
  355. esadr:pointer;
  356. d:dirstr;
  357. n:namestr;
  358. e:extstr;
  359. p : ppchar;
  360. j : integer;
  361. begin
  362. getmem(args,512);
  363. GetMem(env, envc*sizeof(pchar)+16384);
  364. {Now setup the arguments. The first argument should be the program
  365. name without directory and extension.}
  366. fsplit(path,d,n,e);
  367. es.numarg:=1;
  368. args^[0]:=$80;
  369. argsize:=1;
  370. for i:=1 to length(n) do
  371. begin
  372. args^[argsize]:=byte(n[i]);
  373. inc(argsize);
  374. end;
  375. args^[argsize]:=0;
  376. inc(argsize);
  377. {Now do the real arguments.}
  378. i:=1;
  379. while i<=length(comline) do
  380. begin
  381. if comline[i]<>' ' then
  382. begin
  383. {Commandline argument found. Copy it.}
  384. inc(es.numarg);
  385. args^[argsize]:=$80;
  386. inc(argsize);
  387. while (i<=length(comline)) and (comline[i]<>' ') do
  388. begin
  389. args^[argsize]:=byte(comline[i]);
  390. inc(argsize);
  391. inc(i);
  392. end;
  393. args^[argsize]:=0;
  394. inc(argsize);
  395. end;
  396. inc(i);
  397. end;
  398. args^[argsize]:=0;
  399. inc(argsize);
  400. {Commandline ready, now build the environment.
  401. Oh boy, I always had the opinion that executing a program under Dos
  402. was a hard job!}
  403. asm
  404. movl env,%edi {Setup destination pointer.}
  405. movl envc,%ecx {Load number of arguments in edx.}
  406. movl envp,%esi {Load env. strings.}
  407. xorl %edx,%edx {Count environment size.}
  408. .Lexa1:
  409. lodsl {Load a Pchar.}
  410. xchgl %eax,%ebx
  411. .Lexa2:
  412. movb (%ebx),%al {Load a byte.}
  413. incl %ebx {Point to next byte.}
  414. stosb {Store it.}
  415. incl %edx {Increase counter.}
  416. cmpb $0,%al {Ready ?.}
  417. jne .Lexa2
  418. loop .Lexa1 {Next argument.}
  419. stosb {Store an extra 0 to finish. (AL is now 0).}
  420. incl %edx
  421. movw %dx,ES.SizeEnv {Store environment size.}
  422. end;
  423. {Environment ready, now set-up exec structure.}
  424. es.argofs:=args;
  425. es.envofs:=env;
  426. es.numenv:=envc;
  427. { set an error - path is too long }
  428. { since we must add a zero to the }
  429. { end. }
  430. if length(path) > 254 then
  431. begin
  432. exec := 8;
  433. exit;
  434. end;
  435. path[length(path)+1] := #0;
  436. es.nameofs:=pointer(longint(@path)+1);
  437. asm
  438. movw %ss,es.argseg
  439. movw %ss,es.envseg
  440. movw %ss,es.nameseg
  441. end;
  442. es.sizearg:=argsize;
  443. {Typecasting of sets in FPC is a bit hard.}
  444. es.mode1:=byte(runflags);
  445. es.mode2:=byte(winflags);
  446. {Now exec the program.}
  447. asm
  448. leal es,%edx
  449. movw $0x7f06,%ax
  450. call syscall
  451. movl $0,%edi
  452. jnc .Lexprg1
  453. xchgl %eax,%edi
  454. xorl %eax,%eax
  455. decl %eax
  456. .Lexprg1:
  457. movw %di,doserror
  458. movl %eax,__RESULT
  459. end;
  460. freemem(args,512);
  461. FreeMem(env, envc*sizeof(pchar)+16384);
  462. {Phew! That's it. This was the most sophisticated procedure to call
  463. a system function I ever wrote!}
  464. end;
  465. function dosversion:word;assembler;
  466. {Returns DOS version in DOS and OS/2 version in OS/2}
  467. asm
  468. movb $0x30,%ah
  469. call syscall
  470. end;
  471. procedure GetDate (var Year, Month, Day, DayOfWeek: word);
  472. begin
  473. asm
  474. movb $0x2a, %ah
  475. call syscall
  476. xorb %ah, %ah
  477. movl DayOfWeek, %edi
  478. stosw
  479. movl Day, %edi
  480. movb %dl, %al
  481. stosw
  482. movl Month, %edi
  483. movb %dh, %al
  484. stosw
  485. movl Year, %edi
  486. xchgw %ecx, %eax
  487. stosw
  488. end;
  489. end;
  490. {$asmmode intel}
  491. procedure SetDate (Year, Month, Day: word);
  492. var DT: TDateTime;
  493. begin
  494. if os_mode = osOS2 then
  495. begin
  496. DosGetDateTime (DT);
  497. DT.Year := Year;
  498. DT.Month := byte (Month);
  499. DT.Day := byte (Day);
  500. DosSetDateTime (DT);
  501. end
  502. else
  503. asm
  504. mov cx, Year
  505. mov dh, byte ptr Month
  506. mov dl, byte ptr Day
  507. mov ah, 2Bh
  508. call syscall
  509. end;
  510. end;
  511. {$asmmode att}
  512. procedure GetTime (var Hour, Minute, Second, Sec100: word); assembler;
  513. asm
  514. movb $0x2c, %ah
  515. call syscall
  516. xorb %ah, %ah
  517. movl Sec100, %edi
  518. movb %dl, %al
  519. stosw
  520. movl Second, %edi
  521. movb %dh,%al
  522. stosw
  523. movl Minute, %edi
  524. movb %cl,%al
  525. stosw
  526. movl Hour, %edi
  527. movb %ch,%al
  528. stosw
  529. end;
  530. {$asmmode intel}
  531. procedure SetTime (Hour, Minute, Second, Sec100: word);
  532. var DT: TDateTime;
  533. begin
  534. if os_mode = osOS2 then
  535. begin
  536. DosGetDateTime (DT);
  537. DT.Hour := byte (Hour);
  538. DT.Minute := byte (Minute);
  539. DT.Second := byte (Second);
  540. DT.Sec100 := byte (Sec100);
  541. DosSetDateTime (DT);
  542. end
  543. else
  544. asm
  545. mov ch, byte ptr Hour
  546. mov cl, byte ptr Minute
  547. mov dh, byte ptr Second
  548. mov dl, byte ptr Sec100
  549. mov ah, 2Dh
  550. call syscall
  551. end;
  552. end;
  553. {$asmmode att}
  554. procedure getcbreak(var breakvalue:boolean);
  555. begin
  556. DosError := 0;
  557. {! Do not use in OS/2. Also not recommended in DOS. Use
  558. signal handling instead.
  559. asm
  560. movw $0x3300,%ax
  561. call syscall
  562. movl BreakValue,%eax
  563. movb %dl,(%eax)
  564. end;
  565. }
  566. end;
  567. procedure setcbreak(breakvalue:boolean);
  568. begin
  569. DosError := 0;
  570. {! Do not use in OS/2. Also not recommended in DOS. Use
  571. signal handling instead.
  572. asm
  573. movb 8(%ebp),%dl
  574. movw $0x3301,%ax
  575. call syscall
  576. end;
  577. }
  578. end;
  579. procedure getverify(var verify:boolean);
  580. begin
  581. DosError := 0;
  582. {! Do not use in OS/2.}
  583. if os_mode in [osDOS,osDPMI] then
  584. asm
  585. movb $0x54,%ah
  586. call syscall
  587. movl verify,%edi
  588. stosb
  589. end;
  590. end;
  591. procedure setverify(verify:boolean);
  592. begin
  593. DosError := 0;
  594. {! Do not use in OS/2!}
  595. if os_mode in [osDOS,osDPMI] then
  596. asm
  597. movb verify,%al
  598. movb $0x2e,%ah
  599. call syscall
  600. end;
  601. end;
  602. function DiskFree (Drive: byte): int64;
  603. var FI: TFSinfo;
  604. RC: longint;
  605. begin
  606. if (os_mode = osDOS) or (os_mode = osDPMI) then
  607. {Function 36 is not supported in OS/2.}
  608. asm
  609. Drive,%dl
  610. movb $0x36,%ah
  611. call syscall
  612. cmpw $-1,%ax
  613. je .LDISKFREE1
  614. mulw %cx
  615. mulw %bx
  616. shll $16,%edx
  617. movw %ax,%dx
  618. movl $0,%eax
  619. xchgl %edx,%eax
  620. leave
  621. ret
  622. .LDISKFREE1:
  623. cltd
  624. leave
  625. ret
  626. end
  627. else
  628. {In OS/2, we use the filesystem information.}
  629. begin
  630. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  631. if RC = 0 then
  632. DiskFree := int64 (FI.Free_Clusters) *
  633. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  634. else
  635. DiskFree := -1;
  636. end;
  637. end;
  638. function DiskSize (Drive: byte): int64;
  639. var FI: TFSinfo;
  640. RC: longint;
  641. begin
  642. if (os_mode = osDOS) or (os_mode = osDPMI) then
  643. {Function 36 is not supported in OS/2.}
  644. asm
  645. movb Drive,%dl
  646. movb $0x36,%ah
  647. call syscall
  648. movw %dx,%bx
  649. cmpw $-1,%ax
  650. je .LDISKSIZE1
  651. mulw %cx
  652. mulw %bx
  653. shll $16,%edx
  654. movw %ax,%dx
  655. movl $0,%eax
  656. xchgl %edx,%eax
  657. leave
  658. ret
  659. .LDISKSIZE1:
  660. cltd
  661. leave
  662. ret
  663. end
  664. else
  665. {In OS/2, we use the filesystem information.}
  666. begin
  667. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  668. if RC = 0 then
  669. DiskSize := int64 (FI.Total_Clusters) *
  670. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  671. else
  672. DiskSize := -1;
  673. end;
  674. end;
  675. procedure SearchRec2DosSearchRec (var F: SearchRec);
  676. const NameSize = 255;
  677. var L, I: longint;
  678. begin
  679. if os_mode <> osOS2 then
  680. begin
  681. I := 1;
  682. while (I <= SizeOf (LastSR))
  683. and (PBA (@F)^ [I] = PBA (@LastSR)^ [I]) do Inc (I);
  684. { Raise "Invalid file handle" RTE if nested FindFirst calls were used. }
  685. if I <= SizeOf (LastSR) then RunError (6);
  686. l:=length(f.name);
  687. for i:=1 to namesize do
  688. f.name[i-1]:=f.name[i];
  689. f.name[l]:=#0;
  690. end;
  691. end;
  692. procedure DosSearchRec2SearchRec (var F: SearchRec);
  693. const NameSize=255;
  694. var L, I: longint;
  695. type TRec = record
  696. T, D: word;
  697. end;
  698. begin
  699. if os_mode = osOS2 then with F do
  700. begin
  701. Name := FStat^.Name;
  702. Size := FStat^.FileSize;
  703. Attr := FStat^.AttrFile;
  704. TRec (Time).T := FStat^.TimeLastWrite;
  705. TRec (Time).D := FStat^.DateLastWrite;
  706. end else
  707. begin
  708. for i:=0 to namesize do
  709. if f.name[i]=#0 then
  710. begin
  711. l:=i;
  712. break;
  713. end;
  714. for i:=namesize-1 downto 0 do
  715. f.name[i+1]:=f.name[i];
  716. f.name[0]:=char(l);
  717. Move (F, LastSR, SizeOf (LastSR));
  718. end;
  719. end;
  720. procedure _findfirst(path:pchar;attr:word;var f:searchrec);
  721. begin
  722. asm
  723. movl path,%edx
  724. movw attr,%cx
  725. {No need to set DTA in EMX. Just give a pointer in ESI.}
  726. movl f,%esi
  727. movb $0x4e,%ah
  728. call syscall
  729. jnc .LFF
  730. movw %ax,doserror
  731. .LFF:
  732. end;
  733. end;
  734. procedure FindFirst (const Path: PathStr; Attr: word; var F: SearchRec);
  735. var path0: array[0..255] of char;
  736. Count: longint;
  737. begin
  738. {No error.}
  739. DosError := 0;
  740. if os_mode = osOS2 then
  741. begin
  742. New (F.FStat);
  743. F.Handle := $FFFFFFFF;
  744. Count := 1;
  745. DosError := DosFindFirst (Path, F.Handle, Attr, F.FStat,
  746. SizeOf (F.FStat^), Count, ilStandard);
  747. if (DosError = 0) and (Count = 0) then DosError := 18;
  748. end else
  749. begin
  750. strPcopy(path0,path);
  751. _findfirst(path0,attr,f);
  752. end;
  753. DosSearchRec2SearchRec (F);
  754. end;
  755. procedure _findnext(var f : searchrec);
  756. begin
  757. asm
  758. movl f,%esi
  759. movb $0x4f,%ah
  760. call syscall
  761. jnc .LFN
  762. movw %ax,doserror
  763. .LFN:
  764. end;
  765. end;
  766. procedure FindNext (var F: SearchRec);
  767. var Count: longint;
  768. begin
  769. {No error}
  770. DosError := 0;
  771. SearchRec2DosSearchRec (F);
  772. if os_mode = osOS2 then
  773. begin
  774. Count := 1;
  775. DosError := DosFindNext (F.Handle, F.FStat, SizeOf (F.FStat^), Count);
  776. if (DosError = 0) and (Count = 0) then DosError := 18;
  777. end else _findnext (F);
  778. DosSearchRec2SearchRec (F);
  779. end;
  780. procedure FindClose (var F: SearchRec);
  781. begin
  782. if os_mode = osOS2 then
  783. begin
  784. if F.Handle <> $FFFFFFFF then DosError := DosFindClose (F.Handle);
  785. Dispose (F.FStat);
  786. end;
  787. end;
  788. procedure swapvectors;
  789. {For TP compatibility, this exists.}
  790. begin
  791. end;
  792. type PPchar=^Pchar;
  793. function envs:PPchar;assembler;
  794. asm
  795. movl envp,%eax
  796. end ['EAX'];
  797. function envcount:longint;assembler;
  798. asm
  799. movl envc,%eax
  800. end ['EAX'];
  801. function envstr(index : longint) : string;
  802. var hp:Pchar;
  803. begin
  804. if (index<=0) or (index>envcount) then
  805. begin
  806. envstr:='';
  807. exit;
  808. end;
  809. hp:=envs[index-1];
  810. envstr:=strpas(hp);
  811. end;
  812. function getenv(const envvar : string) : string;
  813. var hs,_envvar : string;
  814. eqpos,i : longint;
  815. begin
  816. _envvar:=upcase(envvar);
  817. getenv:='';
  818. for i:=1 to envcount do
  819. begin
  820. hs:=envstr(i);
  821. eqpos:=pos('=',hs);
  822. if copy(hs,1,eqpos-1)=_envvar then
  823. begin
  824. getenv:=copy(hs,eqpos+1,length(hs)-eqpos);
  825. exit;
  826. end;
  827. end;
  828. end;
  829. procedure fsplit(path:pathstr;var dir:dirstr;var name:namestr;
  830. var ext:extstr);
  831. var p1,i : longint;
  832. begin
  833. {Get drive name}
  834. p1:=pos(':',path);
  835. if p1>0 then
  836. begin
  837. dir:=path[1]+':';
  838. delete(path,1,p1);
  839. end
  840. else
  841. dir:='';
  842. { split the path and the name, there are no more path informtions }
  843. { if path contains no backslashes }
  844. while true do
  845. begin
  846. p1:=pos('\',path);
  847. if p1=0 then
  848. p1:=pos('/',path);
  849. if p1=0 then
  850. break;
  851. dir:=dir+copy(path,1,p1);
  852. delete(path,1,p1);
  853. end;
  854. {Try to find an extension.}
  855. ext:='';
  856. for i:=length(path) downto 1 do
  857. if path[i]='.' then
  858. begin
  859. ext:=copy(path,i,high(extstr));
  860. delete(path,i,length(path)-i+1);
  861. break;
  862. end;
  863. name:=path;
  864. end;
  865. (*
  866. function FExpand (const Path: PathStr): PathStr;
  867. - declared in fexpand.inc
  868. *)
  869. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  870. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  871. const
  872. LFNSupport = true;
  873. {$I fexpand.inc}
  874. {$UNDEF FPC_FEXPAND_DRIVES}
  875. {$UNDEF FPC_FEXPAND_UNC}
  876. procedure packtime(var d:datetime;var time:longint);
  877. var zs:longint;
  878. begin
  879. time:=-1980;
  880. time:=time+d.year and 127;
  881. time:=time shl 4;
  882. time:=time+d.month;
  883. time:=time shl 5;
  884. time:=time+d.day;
  885. time:=time shl 16;
  886. zs:=d.hour;
  887. zs:=zs shl 6;
  888. zs:=zs+d.min;
  889. zs:=zs shl 5;
  890. zs:=zs+d.sec div 2;
  891. time:=time+(zs and $ffff);
  892. end;
  893. procedure unpacktime (time:longint;var d:datetime);
  894. begin
  895. d.sec:=(time and 31) * 2;
  896. time:=time shr 5;
  897. d.min:=time and 63;
  898. time:=time shr 6;
  899. d.hour:=time and 31;
  900. time:=time shr 5;
  901. d.day:=time and 31;
  902. time:=time shr 5;
  903. d.month:=time and 15;
  904. time:=time shr 4;
  905. d.year:=time+1980;
  906. end;
  907. procedure getfattr(var f;var attr : word);
  908. { Under EMX, this routine requires }
  909. { the expanded path specification }
  910. { otherwise it will not function }
  911. { properly (CEC) }
  912. var
  913. path: pathstr;
  914. buffer:array[0..255] of char;
  915. begin
  916. DosError := 0;
  917. path:='';
  918. path := StrPas(filerec(f).Name);
  919. { Takes care of slash and backslash support }
  920. path:=FExPand(path);
  921. move(path[1],buffer,length(path));
  922. buffer[length(path)]:=#0;
  923. asm
  924. movw $0x4300,%ax
  925. leal buffer,%edx
  926. call syscall
  927. jnc .Lnoerror { is there an error ? }
  928. movw %ax,doserror
  929. .Lnoerror:
  930. movl attr,%ebx
  931. movw %cx,(%ebx)
  932. end;
  933. end;
  934. procedure setfattr(var f;attr : word);
  935. { Under EMX, this routine requires }
  936. { the expanded path specification }
  937. { otherwise it will not function }
  938. { properly (CEC) }
  939. var
  940. path: pathstr;
  941. buffer:array[0..255] of char;
  942. begin
  943. path:='';
  944. DosError := 0;
  945. path := StrPas(filerec(f).Name);
  946. { Takes care of slash and backslash support }
  947. path:=FExPand(path);
  948. move(path[1],buffer,length(path));
  949. buffer[length(path)]:=#0;
  950. asm
  951. movw $0x4301,%ax
  952. leal buffer,%edx
  953. movw attr,%cx
  954. call syscall
  955. jnc .Lnoerror
  956. movw %ax,doserror
  957. .Lnoerror:
  958. end;
  959. end;
  960. procedure InitEnvironment;
  961. var
  962. cnt : integer;
  963. ptr : pchar;
  964. base : pchar;
  965. i: integer;
  966. tib : pprocessinfoblock;
  967. begin
  968. { We need to setup the environment }
  969. { only in the case of OS/2 }
  970. { otherwise everything is in the stack }
  971. if os_Mode in [OsDOS,osDPMI] then
  972. exit;
  973. cnt := 0;
  974. { count number of environment pointers }
  975. dosgetinfoblocks(nil,@tib);
  976. ptr := pchar(tib^.env);
  977. { stringz,stringz...,#0 }
  978. i := 0;
  979. repeat
  980. repeat
  981. (inc(i));
  982. until (ptr[i] = #0);
  983. inc(i);
  984. { here, it may be a double null, end of environment }
  985. if ptr[i] <> #0 then
  986. inc(cnt);
  987. until (ptr[i] = #0);
  988. { save environment count }
  989. envc := cnt;
  990. { got count of environment strings }
  991. GetMem(envp, cnt*sizeof(pchar)+16384);
  992. cnt := 0;
  993. ptr := pchar(tib^.env);
  994. i:=0;
  995. repeat
  996. envp[cnt] := ptr;
  997. Inc(cnt);
  998. { go to next string ... }
  999. repeat
  1000. inc(ptr);
  1001. until (ptr^ = #0);
  1002. inc(ptr);
  1003. until ptr^ = #0;
  1004. envp[cnt] := #0;
  1005. end;
  1006. procedure DoneEnvironment;
  1007. begin
  1008. { it is allocated on the stack for DOS/DPMI }
  1009. if os_mode = osOs2 then
  1010. FreeMem(envp, envc*sizeof(pchar)+16384);
  1011. end;
  1012. var
  1013. oldexit : pointer;
  1014. begin
  1015. oldexit:=exitproc;
  1016. exitproc:=@doneenvironment;
  1017. InitEnvironment;
  1018. end.
  1019. {
  1020. $Log$
  1021. Revision 1.11 2001-05-20 18:55:48 hajny
  1022. * merging Carl's fixes from the fixes branch
  1023. Revision 1.10 2001/04/10 18:49:40 hajny
  1024. * better check for FindClose
  1025. Revision 1.9 2001/03/11 18:58:42 hajny
  1026. * another Find* problem :-(
  1027. Revision 1.8 2001/03/10 09:57:51 hajny
  1028. * FExpand without IOResult change, remaining direct asm removed
  1029. Revision 1.7 2001/02/04 01:57:52 hajny
  1030. * direct asm removing
  1031. Revision 1.6 2000/11/06 20:35:05 hajny
  1032. * common FExpand introduced
  1033. Revision 1.5 2000/11/05 22:21:47 hajny
  1034. * more FExpand fixes
  1035. Revision 1.4 2000/10/28 16:58:34 hajny
  1036. * many FExpand fixes
  1037. Revision 1.3 2000/09/29 21:49:41 jonas
  1038. * removed warnings
  1039. Revision 1.2 2000/07/14 10:33:10 michael
  1040. + Conditionals fixed
  1041. }