sysutils.pp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for EMX
  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 sysutils;
  13. interface
  14. {$MODE objfpc}
  15. { force ansistrings }
  16. {$H+}
  17. uses
  18. Dos;
  19. {$DEFINE HAS_SLEEP}
  20. { Include platform independent interface part }
  21. {$i sysutilh.inc}
  22. implementation
  23. uses
  24. sysconst;
  25. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  26. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  27. { Include platform independent implementation part }
  28. {$i sysutils.inc}
  29. {****************************************************************************
  30. System (imported) calls
  31. ****************************************************************************}
  32. (* "uses DosCalls" could not be used here due to type *)
  33. (* conflicts, so needed parts had to be redefined here). *)
  34. type
  35. TFileStatus = object
  36. end;
  37. PFileStatus = ^TFileStatus;
  38. TFileStatus3 = object (TFileStatus)
  39. DateCreation, {Date of file creation.}
  40. TimeCreation, {Time of file creation.}
  41. DateLastAccess, {Date of last access to file.}
  42. TimeLastAccess, {Time of last access to file.}
  43. DateLastWrite, {Date of last modification of file.}
  44. TimeLastWrite:word; {Time of last modification of file.}
  45. FileSize, {Size of file.}
  46. FileAlloc:cardinal; {Amount of space the file really
  47. occupies on disk.}
  48. AttrFile:cardinal; {Attributes of file.}
  49. end;
  50. PFileStatus3=^TFileStatus3;
  51. TFileStatus4=object(TFileStatus3)
  52. cbList:cardinal; {Length of entire EA set.}
  53. end;
  54. PFileStatus4=^TFileStatus4;
  55. TFileFindBuf3=object(TFileStatus)
  56. NextEntryOffset: cardinal; {Offset of next entry}
  57. DateCreation, {Date of file creation.}
  58. TimeCreation, {Time of file creation.}
  59. DateLastAccess, {Date of last access to file.}
  60. TimeLastAccess, {Time of last access to file.}
  61. DateLastWrite, {Date of last modification of file.}
  62. TimeLastWrite:word; {Time of last modification of file.}
  63. FileSize, {Size of file.}
  64. FileAlloc:cardinal; {Amount of space the file really
  65. occupies on disk.}
  66. AttrFile:cardinal; {Attributes of file.}
  67. Name:shortstring; {Also possible to use as ASCIIZ.
  68. The byte following the last string
  69. character is always zero.}
  70. end;
  71. PFileFindBuf3=^TFileFindBuf3;
  72. TFileFindBuf4=object(TFileStatus)
  73. NextEntryOffset: cardinal; {Offset of next entry}
  74. DateCreation, {Date of file creation.}
  75. TimeCreation, {Time of file creation.}
  76. DateLastAccess, {Date of last access to file.}
  77. TimeLastAccess, {Time of last access to file.}
  78. DateLastWrite, {Date of last modification of file.}
  79. TimeLastWrite:word; {Time of last modification of file.}
  80. FileSize, {Size of file.}
  81. FileAlloc:cardinal; {Amount of space the file really
  82. occupies on disk.}
  83. AttrFile:cardinal; {Attributes of file.}
  84. cbList:longint; {Size of the file's extended attributes.}
  85. Name:shortstring; {Also possible to use as ASCIIZ.
  86. The byte following the last string
  87. character is always zero.}
  88. end;
  89. PFileFindBuf4=^TFileFindBuf4;
  90. TFSInfo = record
  91. case word of
  92. 1:
  93. (File_Sys_ID,
  94. Sectors_Per_Cluster,
  95. Total_Clusters,
  96. Free_Clusters: cardinal;
  97. Bytes_Per_Sector: word);
  98. 2: {For date/time description,
  99. see file searching realted
  100. routines.}
  101. (Label_Date, {Date when volume label was created.}
  102. Label_Time: word; {Time when volume label was created.}
  103. VolumeLabel: ShortString); {Volume label. Can also be used
  104. as ASCIIZ, because the byte
  105. following the last character of
  106. the string is always zero.}
  107. end;
  108. PFSInfo = ^TFSInfo;
  109. TCountryCode=record
  110. Country, {Country to query info about (0=current).}
  111. CodePage: cardinal; {Code page to query info about (0=current).}
  112. end;
  113. PCountryCode=^TCountryCode;
  114. TTimeFmt = (Clock12, Clock24);
  115. TCountryInfo=record
  116. Country, CodePage: cardinal; {Country and codepage requested.}
  117. case byte of
  118. 0:
  119. (DateFormat: cardinal; {1=ddmmyy 2=yymmdd 3=mmddyy}
  120. CurrencyUnit: array [0..4] of char;
  121. ThousandSeparator: char; {Thousands separator.}
  122. Zero1: byte; {Always zero.}
  123. DecimalSeparator: char; {Decimals separator,}
  124. Zero2: byte;
  125. DateSeparator: char; {Date separator.}
  126. Zero3: byte;
  127. TimeSeparator: char; {Time separator.}
  128. Zero4: byte;
  129. CurrencyFormat, {Bit field:
  130. Bit 0: 0=indicator before value
  131. 1=indicator after value
  132. Bit 1: 1=insert space after
  133. indicator.
  134. Bit 2: 1=Ignore bit 0&1, replace
  135. decimal separator with
  136. indicator.}
  137. DecimalPlace: byte; {Number of decimal places used in
  138. currency indication.}
  139. TimeFormat: TTimeFmt; {12/24 hour.}
  140. Reserve1: array [0..1] of word;
  141. DataSeparator: char; {Data list separator}
  142. Zero5: byte;
  143. Reserve2: array [0..4] of word);
  144. 1:
  145. (fsDateFmt: cardinal; {1=ddmmyy 2=yymmdd 3=mmddyy}
  146. szCurrency: array [0..4] of char;
  147. {null terminated currency symbol}
  148. szThousandsSeparator: array [0..1] of char;
  149. {Thousands separator + #0}
  150. szDecimal: array [0..1] of char;
  151. {Decimals separator + #0}
  152. szDateSeparator: array [0..1] of char;
  153. {Date separator + #0}
  154. szTimeSeparator: array [0..1] of char;
  155. {Time separator + #0}
  156. fsCurrencyFmt, {Bit field:
  157. Bit 0: 0=indicator before value
  158. 1=indicator after value
  159. Bit 1: 1=insert space after
  160. indicator.
  161. Bit 2: 1=Ignore bit 0&1, replace
  162. decimal separator with
  163. indicator}
  164. cDecimalPlace: byte; {Number of decimal places used in
  165. currency indication}
  166. fsTimeFmt: byte; {0=12,1=24 hours}
  167. abReserved1: array [0..1] of word;
  168. szDataSeparator: array [0..1] of char;
  169. {Data list separator + #0}
  170. abReserved2: array [0..4] of word);
  171. end;
  172. PCountryInfo=^TCountryInfo;
  173. TRequestData=record
  174. PID, {ID of process that wrote element.}
  175. Data: cardinal; {Information from process writing the data.}
  176. end;
  177. PRequestData=^TRequestData;
  178. {Queue data structure for synchronously started sessions.}
  179. TChildInfo = record
  180. case boolean of
  181. false:
  182. (SessionID,
  183. Return: word); {Return code from the child process.}
  184. true:
  185. (usSessionID,
  186. usReturn: word); {Return code from the child process.}
  187. end;
  188. PChildInfo = ^TChildInfo;
  189. TStartData=record
  190. {Note: to omit some fields, use a length smaller than SizeOf(TStartData).}
  191. Length:word; {Length, in bytes, of datastructure
  192. (24/30/32/50/60).}
  193. Related:word; {Independent/child session (0/1).}
  194. FgBg:word; {Foreground/background (0/1).}
  195. TraceOpt:word; {No trace/trace this/trace all (0/1/2).}
  196. PgmTitle:PChar; {Program title.}
  197. PgmName:PChar; {Filename to program.}
  198. PgmInputs:PChar; {Command parameters (nil allowed).}
  199. TermQ:PChar; {System queue. (nil allowed).}
  200. Environment:PChar; {Environment to pass (nil allowed).}
  201. InheritOpt:word; {Inherit enviroment from shell/
  202. inherit environment from parent (0/1).}
  203. SessionType:word; {Auto/full screen/window/presentation
  204. manager/full screen Dos/windowed Dos
  205. (0/1/2/3/4/5/6/7).}
  206. Iconfile:PChar; {Icon file to use (nil allowed).}
  207. PgmHandle:cardinal; {0 or the program handle.}
  208. PgmControl:word; {Bitfield describing initial state
  209. of windowed sessions.}
  210. InitXPos,InitYPos:word; {Initial top coordinates.}
  211. InitXSize,InitYSize:word; {Initial size.}
  212. Reserved:word;
  213. ObjectBuffer:PChar; {If a module cannot be loaded, its
  214. name will be returned here.}
  215. ObjectBuffLen:cardinal; {Size of your buffer.}
  216. end;
  217. PStartData=^TStartData;
  218. const
  219. ilStandard = 1;
  220. ilQueryEAsize = 2;
  221. ilQueryEAs = 3;
  222. ilQueryFullName = 5;
  223. quFIFO = 0;
  224. quLIFO = 1;
  225. quPriority = 2;
  226. quNoConvert_Address = 0;
  227. quConvert_Address = 4;
  228. {Start the new session independent or as a child.}
  229. ssf_Related_Independent = 0; {Start new session independent
  230. of the calling session.}
  231. ssf_Related_Child = 1; {Start new session as a child
  232. session to the calling session.}
  233. {Start the new session in the foreground or in the background.}
  234. ssf_FgBg_Fore = 0; {Start new session in foreground.}
  235. ssf_FgBg_Back = 1; {Start new session in background.}
  236. {Should the program started in the new session
  237. be executed under conditions for tracing?}
  238. ssf_TraceOpt_None = 0; {No trace.}
  239. ssf_TraceOpt_Trace = 1; {Trace with no notification
  240. of descendants.}
  241. ssf_TraceOpt_TraceAll = 2; {Trace all descendant sessions.
  242. A termination queue must be
  243. supplied and Related must be
  244. ssf_Related_Child (=1).}
  245. {Will the new session inherit open file handles
  246. and environment from the calling process.}
  247. ssf_InhertOpt_Shell = 0; {Inherit from the shell.}
  248. ssf_InhertOpt_Parent = 1; {Inherit from the calling process.}
  249. {Specifies the type of session to start.}
  250. ssf_Type_Default = 0; {Use program's type.}
  251. ssf_Type_FullScreen = 1; {OS/2 full screen.}
  252. ssf_Type_WindowableVIO = 2; {OS/2 window.}
  253. ssf_Type_PM = 3; {Presentation Manager.}
  254. ssf_Type_VDM = 4; {DOS full screen.}
  255. ssf_Type_WindowedVDM = 7; {DOS window.}
  256. {Additional values for Windows programs}
  257. Prog_31_StdSeamlessVDM = 15; {Windows 3.1 program in its
  258. own windowed session.}
  259. Prog_31_StdSeamlessCommon = 16; {Windows 3.1 program in a
  260. common windowed session.}
  261. Prog_31_EnhSeamlessVDM = 17; {Windows 3.1 program in enhanced
  262. compatibility mode in its own
  263. windowed session.}
  264. Prog_31_EnhSeamlessCommon = 18; {Windows 3.1 program in enhanced
  265. compatibility mode in a common
  266. windowed session.}
  267. Prog_31_Enh = 19; {Windows 3.1 program in enhanced
  268. compatibility mode in a full
  269. screen session.}
  270. Prog_31_Std = 20; {Windows 3.1 program in a full
  271. screen session.}
  272. {Specifies the initial attributes for a OS/2 window or DOS window session.}
  273. ssf_Control_Visible = 0; {Window is visible.}
  274. ssf_Control_Invisible = 1; {Window is invisible.}
  275. ssf_Control_Maximize = 2; {Window is maximized.}
  276. ssf_Control_Minimize = 4; {Window is minimized.}
  277. ssf_Control_NoAutoClose = 8; {Window will not close after
  278. the program has ended.}
  279. ssf_Control_SetPos = 32768; {Use InitXPos, InitYPos,
  280. InitXSize, and InitYSize for
  281. the size and placement.}
  282. {This is the correct way to call external assembler procedures.}
  283. procedure syscall;external name '___SYSCALL';
  284. function DosSetFileInfo (Handle: THandle; InfoLevel: cardinal; AFileStatus: PFileStatus;
  285. FileStatusLen: cardinal): cardinal; cdecl; external 'DOSCALLS' index 218;
  286. function DosQueryFSInfo (DiskNum, InfoLevel: cardinal; var Buffer: TFSInfo;
  287. BufLen: cardinal): cardinal; cdecl; external 'DOSCALLS' index 278;
  288. function DosQueryFileInfo (Handle: THandle; InfoLevel: cardinal;
  289. AFileStatus: PFileStatus; FileStatusLen: cardinal): cardinal; cdecl;
  290. external 'DOSCALLS' index 279;
  291. function DosScanEnv (Name: PChar; var Value: PChar): cardinal; cdecl;
  292. external 'DOSCALLS' index 227;
  293. function DosFindFirst (FileMask: PChar; var Handle: THandle; Attrib: cardinal;
  294. AFileStatus: PFileStatus; FileStatusLen: cardinal;
  295. var Count: cardinal; InfoLevel: cardinal): cardinal; cdecl;
  296. external 'DOSCALLS' index 264;
  297. function DosFindNext (Handle: THandle; AFileStatus: PFileStatus;
  298. FileStatusLen: cardinal; var Count: cardinal): cardinal; cdecl;
  299. external 'DOSCALLS' index 265;
  300. function DosFindClose (Handle: THandle): cardinal; cdecl;
  301. external 'DOSCALLS' index 263;
  302. function DosQueryCtryInfo (Size: cardinal; var Country: TCountryCode;
  303. var Res: TCountryInfo; var ActualSize: cardinal): cardinal; cdecl;
  304. external 'NLS' index 5;
  305. function DosMapCase (Size: cardinal; var Country: TCountryCode;
  306. AString: PChar): cardinal; cdecl; external 'NLS' index 7;
  307. procedure DosSleep (MSec: cardinal); cdecl; external 'DOSCALLS' index 229;
  308. function DosCreateQueue (var Handle: THandle; Priority:longint;
  309. Name: PChar): cardinal; cdecl;
  310. external 'QUECALLS' index 16;
  311. function DosReadQueue (Handle: THandle; var ReqBuffer: TRequestData;
  312. var DataLen: cardinal; var DataPtr: pointer;
  313. Element, Wait: cardinal; var Priority: byte;
  314. ASem: THandle): cardinal; cdecl;
  315. external 'QUECALLS' index 9;
  316. function DosCloseQueue (Handle: THandle): cardinal; cdecl;
  317. external 'QUECALLS' index 11;
  318. function DosStartSession (var AStartData: TStartData;
  319. var SesID, PID: cardinal): cardinal; cdecl;
  320. external 'SESMGR' index 37;
  321. function DosFreeMem(P:pointer):cardinal; cdecl; external 'DOSCALLS' index 304;
  322. {****************************************************************************
  323. File Functions
  324. ****************************************************************************}
  325. const
  326. ofRead = $0000; {Open for reading}
  327. ofWrite = $0001; {Open for writing}
  328. ofReadWrite = $0002; {Open for reading/writing}
  329. doDenyRW = $0010; {DenyAll (no sharing)}
  330. faCreateNew = $00010000; {Create if file does not exist}
  331. faOpenReplace = $00040000; {Truncate if file exists}
  332. faCreate = $00050000; {Create if file does not exist, truncate otherwise}
  333. FindResvdMask = $00003737; {Allowed bits in attribute
  334. specification for DosFindFirst call.}
  335. {$ASMMODE INTEL}
  336. function FileOpen (const FileName: string; Mode: integer): longint; assembler;
  337. asm
  338. push ebx
  339. {$IFDEF REGCALL}
  340. mov ecx, edx
  341. mov edx, eax
  342. {$ELSE REGCALL}
  343. mov ecx, Mode
  344. mov edx, FileName
  345. {$ENDIF REGCALL}
  346. (* DenyAll if sharing not specified. *)
  347. test ecx, 112
  348. jnz @FOpen1
  349. or ecx, 16
  350. @FOpen1:
  351. mov eax, 7F2Bh
  352. call syscall
  353. (* syscall __open() returns -1 in case of error, i.e. exactly what we need *)
  354. pop ebx
  355. end {['eax', 'ebx', 'ecx', 'edx']};
  356. function FileCreate (const FileName: string): longint; assembler;
  357. asm
  358. push ebx
  359. {$IFDEF REGCALL}
  360. mov edx, eax
  361. {$ELSE REGCALL}
  362. mov edx, FileName
  363. {$ENDIF REGCALL}
  364. mov eax, 7F2Bh
  365. mov ecx, ofReadWrite or faCreate or doDenyRW (* Sharing to DenyAll *)
  366. call syscall
  367. pop ebx
  368. end {['eax', 'ebx', 'ecx', 'edx']};
  369. function FileCreate (const FileName: string; Mode: integer): longint;
  370. begin
  371. FileCreate:=FileCreate(FileName);
  372. end;
  373. function FileRead (Handle: longint; var Buffer; Count: longint): longint;
  374. assembler;
  375. asm
  376. push ebx
  377. {$IFDEF REGCALL}
  378. mov ebx, eax
  379. {$ELSE REGCALL}
  380. mov ebx, Handle
  381. mov ecx, Count
  382. mov edx, Buffer
  383. {$ENDIF REGCALL}
  384. mov eax, 3F00h
  385. call syscall
  386. jnc @FReadEnd
  387. mov eax, -1
  388. @FReadEnd:
  389. pop ebx
  390. end {['eax', 'ebx', 'ecx', 'edx']};
  391. function FileWrite (Handle: longint; const Buffer; Count: longint): longint;
  392. assembler;
  393. asm
  394. push ebx
  395. {$IFDEF REGCALL}
  396. mov ebx, eax
  397. {$ELSE REGCALL}
  398. mov ebx, Handle
  399. mov ecx, Count
  400. mov edx, Buffer
  401. {$ENDIF REGCALL}
  402. mov eax, 4000h
  403. call syscall
  404. jnc @FWriteEnd
  405. mov eax, -1
  406. @FWriteEnd:
  407. pop ebx
  408. end {['eax', 'ebx', 'ecx', 'edx']};
  409. function FileSeek (Handle, FOffset, Origin: longint): longint; assembler;
  410. asm
  411. push ebx
  412. {$IFDEF REGCALL}
  413. mov ebx, eax
  414. mov eax, ecx
  415. {$ELSE REGCALL}
  416. mov ebx, Handle
  417. mov eax, Origin
  418. mov edx, FOffset
  419. {$ENDIF REGCALL}
  420. mov ah, 42h
  421. call syscall
  422. jnc @FSeekEnd
  423. mov eax, -1
  424. @FSeekEnd:
  425. pop ebx
  426. end {['eax', 'ebx', 'edx']};
  427. function FileSeek (Handle: longint; FOffset: Int64; Origin: longint): Int64;
  428. begin
  429. {$warning need to add 64bit call }
  430. Result:=FileSeek(Handle,Longint(Foffset),Longint(Origin));
  431. end;
  432. procedure FileClose (Handle: longint);
  433. begin
  434. if (Handle > 4) or ((os_mode = osOS2) and (Handle > 2)) then
  435. asm
  436. push ebx
  437. mov eax, 3E00h
  438. mov ebx, Handle
  439. call syscall
  440. pop ebx
  441. end ['eax'];
  442. end;
  443. function FileTruncate (Handle, Size: longint): boolean; assembler;
  444. asm
  445. push ebx
  446. {$IFDEF REGCALL}
  447. mov ebx, eax
  448. {$ELSE REGCALL}
  449. mov ebx, Handle
  450. mov edx, Size
  451. {$ENDIF REGCALL}
  452. mov eax, 7F25h
  453. push ebx
  454. call syscall
  455. pop ebx
  456. jc @FTruncEnd
  457. mov eax, 4202h
  458. mov edx, 0
  459. call syscall
  460. mov eax, 0
  461. jnc @FTruncEnd
  462. dec eax
  463. @FTruncEnd:
  464. pop ebx
  465. end {['eax', 'ebx', 'ecx', 'edx']};
  466. function FileAge (const FileName: string): longint;
  467. var Handle: longint;
  468. begin
  469. Handle := FileOpen (FileName, 0);
  470. if Handle <> -1 then
  471. begin
  472. Result := FileGetDate (Handle);
  473. FileClose (Handle);
  474. end
  475. else
  476. Result := -1;
  477. end;
  478. function FileExists (const FileName: string): boolean; assembler;
  479. asm
  480. {$IFDEF REGCALL}
  481. mov edx, eax
  482. {$ELSE REGCALL}
  483. mov edx, FileName
  484. {$ENDIF REGCALL}
  485. mov ax, 4300h
  486. call syscall
  487. mov eax, 0
  488. jc @FExistsEnd
  489. test cx, 18h
  490. jnz @FExistsEnd
  491. inc eax
  492. @FExistsEnd:
  493. end {['eax', 'ecx', 'edx']};
  494. type TRec = record
  495. T, D: word;
  496. end;
  497. PSearchRec = ^SearchRec;
  498. function FindFirst (const Path: string; Attr: longint; out Rslt: TSearchRec): longint;
  499. var SR: PSearchRec;
  500. FStat: PFileFindBuf3;
  501. Count: cardinal;
  502. Err: cardinal;
  503. begin
  504. if os_mode = osOS2 then
  505. begin
  506. New (FStat);
  507. Rslt.FindHandle := THandle ($FFFFFFFF);
  508. Count := 1;
  509. Err := DosFindFirst (PChar (Path), Rslt.FindHandle,
  510. Attr and FindResvdMask, FStat, SizeOf (FStat^), Count,
  511. ilStandard);
  512. if (Err = 0) and (Count = 0) then Err := 18;
  513. FindFirst := -Err;
  514. if Err = 0 then
  515. begin
  516. Rslt.Name := FStat^.Name;
  517. Rslt.Size := FStat^.FileSize;
  518. Rslt.Attr := FStat^.AttrFile;
  519. Rslt.ExcludeAttr := 0;
  520. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  521. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  522. end;
  523. Dispose (FStat);
  524. end
  525. else
  526. begin
  527. Err := DOS.DosError;
  528. GetMem (SR, SizeOf (SearchRec));
  529. Rslt.FindHandle := longint(SR);
  530. DOS.FindFirst (Path, Attr, SR^);
  531. FindFirst := -DOS.DosError;
  532. if DosError = 0 then
  533. begin
  534. Rslt.Time := SR^.Time;
  535. Rslt.Size := SR^.Size;
  536. Rslt.Attr := SR^.Attr;
  537. Rslt.ExcludeAttr := 0;
  538. Rslt.Name := SR^.Name;
  539. end;
  540. DOS.DosError := Err;
  541. end;
  542. end;
  543. function FindNext (var Rslt: TSearchRec): longint;
  544. var SR: PSearchRec;
  545. FStat: PFileFindBuf3;
  546. Count: cardinal;
  547. Err: cardinal;
  548. begin
  549. if os_mode = osOS2 then
  550. begin
  551. New (FStat);
  552. Count := 1;
  553. Err := DosFindNext (Rslt.FindHandle, FStat, SizeOf (FStat^),
  554. Count);
  555. if (Err = 0) and (Count = 0) then Err := 18;
  556. FindNext := -Err;
  557. if Err = 0 then
  558. begin
  559. Rslt.Name := FStat^.Name;
  560. Rslt.Size := FStat^.FileSize;
  561. Rslt.Attr := FStat^.AttrFile;
  562. Rslt.ExcludeAttr := 0;
  563. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  564. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  565. end;
  566. Dispose (FStat);
  567. end
  568. else
  569. begin
  570. SR := PSearchRec (Rslt.FindHandle);
  571. if SR <> nil then
  572. begin
  573. DOS.FindNext (SR^);
  574. FindNext := -DosError;
  575. if DosError = 0 then
  576. begin
  577. Rslt.Time := SR^.Time;
  578. Rslt.Size := SR^.Size;
  579. Rslt.Attr := SR^.Attr;
  580. Rslt.ExcludeAttr := 0;
  581. Rslt.Name := SR^.Name;
  582. end;
  583. end;
  584. end;
  585. end;
  586. procedure FindClose (var F: TSearchrec);
  587. var SR: PSearchRec;
  588. begin
  589. if os_mode = osOS2 then
  590. begin
  591. DosFindClose (F.FindHandle);
  592. end
  593. else
  594. begin
  595. SR := PSearchRec (F.FindHandle);
  596. DOS.FindClose (SR^);
  597. FreeMem (SR, SizeOf (SearchRec));
  598. end;
  599. F.FindHandle := 0;
  600. end;
  601. function FileGetDate (Handle: longint): longint; assembler;
  602. asm
  603. push ebx
  604. {$IFDEF REGCALL}
  605. mov ebx, eax
  606. {$ELSE REGCALL}
  607. mov ebx, Handle
  608. {$ENDIF REGCALL}
  609. mov ax, 5700h
  610. call syscall
  611. mov eax, -1
  612. jc @FGetDateEnd
  613. mov ax, dx
  614. shld eax, ecx, 16
  615. @FGetDateEnd:
  616. pop ebx
  617. end {['eax', 'ebx', 'ecx', 'edx']};
  618. function FileSetDate (Handle, Age: longint): longint;
  619. var FStat: PFileStatus3;
  620. RC: cardinal;
  621. begin
  622. if os_mode = osOS2 then
  623. begin
  624. New (FStat);
  625. RC := DosQueryFileInfo (Handle, ilStandard, FStat,
  626. SizeOf (FStat^));
  627. if RC <> 0 then
  628. FileSetDate := -1
  629. else
  630. begin
  631. FStat^.DateLastAccess := Hi (Age);
  632. FStat^.DateLastWrite := Hi (Age);
  633. FStat^.TimeLastAccess := Lo (Age);
  634. FStat^.TimeLastWrite := Lo (Age);
  635. RC := DosSetFileInfo (Handle, ilStandard, FStat,
  636. SizeOf (FStat^));
  637. if RC <> 0 then
  638. FileSetDate := -1
  639. else
  640. FileSetDate := 0;
  641. end;
  642. Dispose (FStat);
  643. end
  644. else
  645. asm
  646. push ebx
  647. mov ax, 5701h
  648. mov ebx, Handle
  649. mov cx, word ptr [Age]
  650. mov dx, word ptr [Age + 2]
  651. call syscall
  652. jnc @FSetDateEnd
  653. mov eax, -1
  654. @FSetDateEnd:
  655. mov Result, eax
  656. pop ebx
  657. end ['eax', 'ecx', 'edx'];
  658. end;
  659. function FileGetAttr (const FileName: string): longint; assembler;
  660. asm
  661. {$IFDEF REGCALL}
  662. mov edx, eax
  663. {$ELSE REGCALL}
  664. mov edx, FileName
  665. {$ENDIF REGCALL}
  666. mov ax, 4300h
  667. call syscall
  668. jnc @FGetAttrEnd
  669. mov eax, -1
  670. @FGetAttrEnd:
  671. end {['eax', 'edx']};
  672. function FileSetAttr (const Filename: string; Attr: longint): longint; assembler;
  673. asm
  674. {$IFDEF REGCALL}
  675. mov ecx, edx
  676. mov edx, eax
  677. {$ELSE REGCALL}
  678. mov ecx, Attr
  679. mov edx, FileName
  680. {$ENDIF REGCALL}
  681. mov ax, 4301h
  682. call syscall
  683. mov eax, 0
  684. jnc @FSetAttrEnd
  685. mov eax, -1
  686. @FSetAttrEnd:
  687. end {['eax', 'ecx', 'edx']};
  688. function DeleteFile (const FileName: string): boolean; assembler;
  689. asm
  690. {$IFDEF REGCALL}
  691. mov edx, eax
  692. {$ELSE REGCALL}
  693. mov edx, FileName
  694. {$ENDIF REGCALL}
  695. mov ax, 4100h
  696. call syscall
  697. mov eax, 0
  698. jc @FDeleteEnd
  699. inc eax
  700. @FDeleteEnd:
  701. end {['eax', 'edx']};
  702. function RenameFile (const OldName, NewName: string): boolean; assembler;
  703. asm
  704. push edi
  705. {$IFDEF REGCALL}
  706. mov edx, eax
  707. mov edi, edx
  708. {$ELSE REGCALL}
  709. mov edx, OldName
  710. mov edi, NewName
  711. {$ENDIF REGCALL}
  712. mov ax, 5600h
  713. call syscall
  714. mov eax, 0
  715. jc @FRenameEnd
  716. inc eax
  717. @FRenameEnd:
  718. pop edi
  719. end {['eax', 'edx', 'edi']};
  720. {****************************************************************************
  721. Disk Functions
  722. ****************************************************************************}
  723. {$ASMMODE ATT}
  724. function DiskFree (Drive: byte): int64;
  725. var FI: TFSinfo;
  726. RC: cardinal;
  727. begin
  728. if (os_mode = osDOS) or (os_mode = osDPMI) then
  729. {Function 36 is not supported in OS/2.}
  730. asm
  731. pushl %ebx
  732. movb Drive,%dl
  733. movb $0x36,%ah
  734. call syscall
  735. cmpw $-1,%ax
  736. je .LDISKFREE1
  737. mulw %cx
  738. mulw %bx
  739. shll $16,%edx
  740. movw %ax,%dx
  741. movl $0,%eax
  742. xchgl %edx,%eax
  743. jmp .LDISKFREE2
  744. .LDISKFREE1:
  745. cltd
  746. .LDISKFREE2:
  747. popl %ebx
  748. leave
  749. ret
  750. end
  751. else
  752. {In OS/2, we use the filesystem information.}
  753. begin
  754. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  755. if RC = 0 then
  756. DiskFree := int64 (FI.Free_Clusters) *
  757. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  758. else
  759. DiskFree := -1;
  760. end;
  761. end;
  762. function DiskSize (Drive: byte): int64;
  763. var FI: TFSinfo;
  764. RC: cardinal;
  765. begin
  766. if (os_mode = osDOS) or (os_mode = osDPMI) then
  767. {Function 36 is not supported in OS/2.}
  768. asm
  769. pushl %ebx
  770. movb Drive,%dl
  771. movb $0x36,%ah
  772. call syscall
  773. movw %dx,%bx
  774. cmpw $-1,%ax
  775. je .LDISKSIZE1
  776. mulw %cx
  777. mulw %bx
  778. shll $16,%edx
  779. movw %ax,%dx
  780. movl $0,%eax
  781. xchgl %edx,%eax
  782. jmp .LDISKSIZE2
  783. .LDISKSIZE1:
  784. cltd
  785. .LDISKSIZE2:
  786. popl %ebx
  787. leave
  788. ret
  789. end
  790. else
  791. {In OS/2, we use the filesystem information.}
  792. begin
  793. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  794. if RC = 0 then
  795. DiskSize := int64 (FI.Total_Clusters) *
  796. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  797. else
  798. DiskSize := -1;
  799. end;
  800. end;
  801. function GetCurrentDir: string;
  802. begin
  803. GetDir (0, Result);
  804. end;
  805. function SetCurrentDir (const NewDir: string): boolean;
  806. begin
  807. {$I-}
  808. ChDir (NewDir);
  809. Result := (IOResult = 0);
  810. {$I+}
  811. end;
  812. function CreateDir (const NewDir: string): boolean;
  813. begin
  814. {$I-}
  815. MkDir (NewDir);
  816. Result := (IOResult = 0);
  817. {$I+}
  818. end;
  819. function RemoveDir (const Dir: string): boolean;
  820. begin
  821. {$I-}
  822. RmDir (Dir);
  823. Result := (IOResult = 0);
  824. {$I+}
  825. end;
  826. {$ASMMODE INTEL}
  827. function DirectoryExists (const Directory: string): boolean; assembler;
  828. asm
  829. {$IFDEF REGCALL}
  830. mov edx, eax
  831. {$ELSE REGCALL}
  832. mov edx, Directory
  833. {$ENDIF REGCALL}
  834. mov ax, 4300h
  835. call syscall
  836. mov eax, 0
  837. jc @FExistsEnd
  838. test cx, 10h
  839. jz @FExistsEnd
  840. inc eax
  841. @FExistsEnd:
  842. end {['eax', 'ecx', 'edx']};
  843. {****************************************************************************
  844. Time Functions
  845. ****************************************************************************}
  846. procedure GetLocalTime (var SystemTime: TSystemTime); assembler;
  847. asm
  848. (* Expects the default record alignment (word)!!! *)
  849. push edi
  850. {$IFDEF REGCALL}
  851. push eax
  852. {$ENDIF REGCALL}
  853. mov ah, 2Ah
  854. call syscall
  855. {$IFDEF REGCALL}
  856. pop eax
  857. {$ELSE REGCALL}
  858. mov edi, SystemTime
  859. {$ENDIF REGCALL}
  860. mov ax, cx
  861. stosw
  862. xor eax, eax
  863. mov al, 10
  864. mul dl
  865. shl eax, 16
  866. mov al, dh
  867. stosd
  868. push edi
  869. mov ah, 2Ch
  870. call syscall
  871. pop edi
  872. xor eax, eax
  873. mov al, cl
  874. shl eax, 16
  875. mov al, ch
  876. stosd
  877. mov al, dl
  878. shl eax, 16
  879. mov al, dh
  880. stosd
  881. pop edi
  882. end {['eax', 'ecx', 'edx', 'edi']};
  883. {$asmmode default}
  884. {****************************************************************************
  885. Misc Functions
  886. ****************************************************************************}
  887. procedure Beep;
  888. begin
  889. end;
  890. {****************************************************************************
  891. Locale Functions
  892. ****************************************************************************}
  893. procedure InitAnsi;
  894. var I: byte;
  895. Country: TCountryCode;
  896. begin
  897. for I := 0 to 255 do
  898. UpperCaseTable [I] := Chr (I);
  899. Move (UpperCaseTable, LowerCaseTable, SizeOf (UpperCaseTable));
  900. if os_mode = osOS2 then
  901. begin
  902. FillChar (Country, SizeOf (Country), 0);
  903. DosMapCase (SizeOf (UpperCaseTable), Country, @UpperCaseTable);
  904. end
  905. else
  906. begin
  907. (* !!! TODO: DOS/DPMI mode support!!! *)
  908. end;
  909. for I := 0 to 255 do
  910. if UpperCaseTable [I] <> Chr (I) then
  911. LowerCaseTable [Ord (UpperCaseTable [I])] := Chr (I);
  912. end;
  913. procedure InitInternational;
  914. var Country: TCountryCode;
  915. CtryInfo: TCountryInfo;
  916. Size: cardinal;
  917. RC: cardinal;
  918. begin
  919. Size := 0;
  920. FillChar (Country, SizeOf (Country), 0);
  921. FillChar (CtryInfo, SizeOf (CtryInfo), 0);
  922. RC := DosQueryCtryInfo (SizeOf (CtryInfo), Country, CtryInfo, Size);
  923. if RC = 0 then
  924. begin
  925. DateSeparator := CtryInfo.DateSeparator;
  926. case CtryInfo.DateFormat of
  927. 1: begin
  928. ShortDateFormat := 'd/m/y';
  929. LongDateFormat := 'dd" "mmmm" "yyyy';
  930. end;
  931. 2: begin
  932. ShortDateFormat := 'y/m/d';
  933. LongDateFormat := 'yyyy" "mmmm" "dd';
  934. end;
  935. 3: begin
  936. ShortDateFormat := 'm/d/y';
  937. LongDateFormat := 'mmmm" "dd" "yyyy';
  938. end;
  939. end;
  940. TimeSeparator := CtryInfo.TimeSeparator;
  941. DecimalSeparator := CtryInfo.DecimalSeparator;
  942. ThousandSeparator := CtryInfo.ThousandSeparator;
  943. CurrencyFormat := CtryInfo.CurrencyFormat;
  944. CurrencyString := PChar (CtryInfo.CurrencyUnit);
  945. end;
  946. InitAnsi;
  947. InitInternationalGeneric;
  948. end;
  949. function SysErrorMessage(ErrorCode: Integer): String;
  950. begin
  951. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  952. end;
  953. {****************************************************************************
  954. OS Utils
  955. ****************************************************************************}
  956. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  957. begin
  958. GetEnvironmentVariable := StrPas (GetEnvPChar (EnvVar));
  959. end;
  960. Function GetEnvironmentVariableCount : Integer;
  961. begin
  962. (* Result:=FPCCountEnvVar(EnvP); - the amount is already known... *)
  963. GetEnvironmentVariableCount := EnvC;
  964. end;
  965. Function GetEnvironmentString(Index : Integer) : String;
  966. begin
  967. Result:=FPCGetEnvStrFromP (EnvP, Index);
  968. end;
  969. {$ASMMODE INTEL}
  970. procedure Sleep (Milliseconds: cardinal);
  971. begin
  972. if os_mode = osOS2 then DosSleep (Milliseconds) else
  973. asm
  974. mov edx, Milliseconds
  975. mov eax, 7F30h
  976. call syscall
  977. end ['eax', 'edx'];
  978. end;
  979. {$ASMMODE DEFAULT}
  980. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  981. integer;
  982. var
  983. HQ: THandle;
  984. SPID, STID, QName: shortstring;
  985. SD: TStartData;
  986. SID, PID: cardinal;
  987. RD: TRequestData;
  988. PCI: PChildInfo;
  989. CISize: cardinal;
  990. Prio: byte;
  991. E: EOSError;
  992. CommandLine: ansistring;
  993. begin
  994. if os_Mode = osOS2 then
  995. begin
  996. FillChar (SD, SizeOf (SD), 0);
  997. SD.Length := 24;
  998. SD.Related := ssf_Related_Child;
  999. SD.PgmName := PChar (Path);
  1000. SD.PgmInputs := PChar (ComLine);
  1001. Str (GetProcessID, SPID);
  1002. Str (ThreadID, STID);
  1003. QName := '\QUEUES\FPC_ExecuteProcess_p' + SPID + 't' + STID + '.QUE'#0;
  1004. SD.TermQ := @QName [1];
  1005. Result := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  1006. if Result = 0 then
  1007. begin
  1008. Result := DosStartSession (SD, SID, PID);
  1009. if (Result = 0) or (Result = 457) then
  1010. begin
  1011. Result := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  1012. if Result = 0 then
  1013. begin
  1014. Result := PCI^.Return;
  1015. DosCloseQueue (HQ);
  1016. DosFreeMem (PCI);
  1017. Exit;
  1018. end;
  1019. end;
  1020. DosCloseQueue (HQ);
  1021. end;
  1022. if ComLine = '' then
  1023. CommandLine := Path
  1024. else
  1025. CommandLine := Path + ' ' + ComLine;
  1026. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, Result]);
  1027. E.ErrorCode := Result;
  1028. raise E;
  1029. end else
  1030. begin
  1031. Dos.Exec (Path, ComLine);
  1032. if DosError <> 0 then
  1033. begin
  1034. if ComLine = '' then
  1035. CommandLine := Path
  1036. else
  1037. CommandLine := Path + ' ' + ComLine;
  1038. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
  1039. E.ErrorCode := DosError;
  1040. raise E;
  1041. end;
  1042. ExecuteProcess := DosExitCode;
  1043. end;
  1044. end;
  1045. function ExecuteProcess (const Path: AnsiString;
  1046. const ComLine: array of AnsiString): integer;
  1047. var
  1048. CommandLine: AnsiString;
  1049. I: integer;
  1050. begin
  1051. Commandline := '';
  1052. for I := 0 to High (ComLine) do
  1053. if Pos (' ', ComLine [I]) <> 0 then
  1054. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  1055. else
  1056. CommandLine := CommandLine + ' ' + Comline [I];
  1057. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  1058. end;
  1059. {****************************************************************************
  1060. Initialization code
  1061. ****************************************************************************}
  1062. Initialization
  1063. InitExceptions; { Initialize exceptions. OS independent }
  1064. InitInternational; { Initialize internationalization settings }
  1065. Finalization
  1066. DoneExceptions;
  1067. end.