sysutils.pp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 OS/2
  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:cardinal; {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. TFileFindBuf3L=object(TFileStatus)
  91. NextEntryOffset: cardinal; {Offset of next entry}
  92. DateCreation, {Date of file creation.}
  93. TimeCreation, {Time of file creation.}
  94. DateLastAccess, {Date of last access to file.}
  95. TimeLastAccess, {Time of last access to file.}
  96. DateLastWrite, {Date of last modification of file.}
  97. TimeLastWrite:word; {Time of last modification of file.}
  98. FileSize, {Size of file.}
  99. FileAlloc:int64; {Amount of space the file really
  100. occupies on disk.}
  101. AttrFile:cardinal; {Attributes of file.}
  102. Name:shortstring; {Also possible to use as ASCIIZ.
  103. The byte following the last string
  104. character is always zero.}
  105. end;
  106. PFileFindBuf3L=^TFileFindBuf3L;
  107. TFileFindBuf4L=object(TFileStatus)
  108. NextEntryOffset: cardinal; {Offset of next entry}
  109. DateCreation, {Date of file creation.}
  110. TimeCreation, {Time of file creation.}
  111. DateLastAccess, {Date of last access to file.}
  112. TimeLastAccess, {Time of last access to file.}
  113. DateLastWrite, {Date of last modification of file.}
  114. TimeLastWrite:word; {Time of last modification of file.}
  115. FileSize, {Size of file.}
  116. FileAlloc:int64; {Amount of space the file really
  117. occupies on disk.}
  118. AttrFile:cardinal; {Attributes of file.}
  119. cbList:cardinal; {Size of the file's extended attributes.}
  120. Name:shortstring; {Also possible to use as ASCIIZ.
  121. The byte following the last string
  122. character is always zero.}
  123. end;
  124. PFileFindBuf4L=^TFileFindBuf4L;
  125. TFSInfo = record
  126. case word of
  127. 1:
  128. (File_Sys_ID,
  129. Sectors_Per_Cluster,
  130. Total_Clusters,
  131. Free_Clusters: cardinal;
  132. Bytes_Per_Sector: word);
  133. 2: {For date/time description,
  134. see file searching realted
  135. routines.}
  136. (Label_Date, {Date when volume label was created.}
  137. Label_Time: word; {Time when volume label was created.}
  138. VolumeLabel: ShortString); {Volume label. Can also be used
  139. as ASCIIZ, because the byte
  140. following the last character of
  141. the string is always zero.}
  142. end;
  143. PFSInfo = ^TFSInfo;
  144. TCountryCode=record
  145. Country, {Country to query info about (0=current).}
  146. CodePage: cardinal; {Code page to query info about (0=current).}
  147. end;
  148. PCountryCode=^TCountryCode;
  149. TTimeFmt = (Clock12, Clock24);
  150. TCountryInfo=record
  151. Country, CodePage: cardinal; {Country and codepage requested.}
  152. case byte of
  153. 0:
  154. (DateFormat: cardinal; {1=ddmmyy 2=yymmdd 3=mmddyy}
  155. CurrencyUnit: array [0..4] of char;
  156. ThousandSeparator: char; {Thousands separator.}
  157. Zero1: byte; {Always zero.}
  158. DecimalSeparator: char; {Decimals separator,}
  159. Zero2: byte;
  160. DateSeparator: char; {Date separator.}
  161. Zero3: byte;
  162. TimeSeparator: char; {Time separator.}
  163. Zero4: byte;
  164. CurrencyFormat, {Bit field:
  165. Bit 0: 0=indicator before value
  166. 1=indicator after value
  167. Bit 1: 1=insert space after
  168. indicator.
  169. Bit 2: 1=Ignore bit 0&1, replace
  170. decimal separator with
  171. indicator.}
  172. DecimalPlace: byte; {Number of decimal places used in
  173. currency indication.}
  174. TimeFormat: TTimeFmt; {12/24 hour.}
  175. Reserve1: array [0..1] of word;
  176. DataSeparator: char; {Data list separator}
  177. Zero5: byte;
  178. Reserve2: array [0..4] of word);
  179. 1:
  180. (fsDateFmt: cardinal; {1=ddmmyy 2=yymmdd 3=mmddyy}
  181. szCurrency: array [0..4] of char;
  182. {null terminated currency symbol}
  183. szThousandsSeparator: array [0..1] of char;
  184. {Thousands separator + #0}
  185. szDecimal: array [0..1] of char;
  186. {Decimals separator + #0}
  187. szDateSeparator: array [0..1] of char;
  188. {Date separator + #0}
  189. szTimeSeparator: array [0..1] of char;
  190. {Time separator + #0}
  191. fsCurrencyFmt, {Bit field:
  192. Bit 0: 0=indicator before value
  193. 1=indicator after value
  194. Bit 1: 1=insert space after
  195. indicator.
  196. Bit 2: 1=Ignore bit 0&1, replace
  197. decimal separator with
  198. indicator}
  199. cDecimalPlace: byte; {Number of decimal places used in
  200. currency indication}
  201. fsTimeFmt: byte; {0=12,1=24 hours}
  202. abReserved1: array [0..1] of word;
  203. szDataSeparator: array [0..1] of char;
  204. {Data list separator + #0}
  205. abReserved2: array [0..4] of word);
  206. end;
  207. PCountryInfo=^TCountryInfo;
  208. TRequestData=record
  209. PID, {ID of process that wrote element.}
  210. Data: cardinal; {Information from process writing the data.}
  211. end;
  212. PRequestData=^TRequestData;
  213. {Queue data structure for synchronously started sessions.}
  214. TChildInfo = record
  215. case boolean of
  216. false:
  217. (SessionID,
  218. Return: word); {Return code from the child process.}
  219. true:
  220. (usSessionID,
  221. usReturn: word); {Return code from the child process.}
  222. end;
  223. PChildInfo = ^TChildInfo;
  224. TStartData=record
  225. {Note: to omit some fields, use a length smaller than SizeOf(TStartData).}
  226. Length:word; {Length, in bytes, of datastructure
  227. (24/30/32/50/60).}
  228. Related:word; {Independent/child session (0/1).}
  229. FgBg:word; {Foreground/background (0/1).}
  230. TraceOpt:word; {No trace/trace this/trace all (0/1/2).}
  231. PgmTitle:PChar; {Program title.}
  232. PgmName:PChar; {Filename to program.}
  233. PgmInputs:PChar; {Command parameters (nil allowed).}
  234. TermQ:PChar; {System queue. (nil allowed).}
  235. Environment:PChar; {Environment to pass (nil allowed).}
  236. InheritOpt:word; {Inherit enviroment from shell/
  237. inherit environment from parent (0/1).}
  238. SessionType:word; {Auto/full screen/window/presentation
  239. manager/full screen Dos/windowed Dos
  240. (0/1/2/3/4/5/6/7).}
  241. Iconfile:PChar; {Icon file to use (nil allowed).}
  242. PgmHandle:cardinal; {0 or the program handle.}
  243. PgmControl:word; {Bitfield describing initial state
  244. of windowed sessions.}
  245. InitXPos,InitYPos:word; {Initial top coordinates.}
  246. InitXSize,InitYSize:word; {Initial size.}
  247. Reserved:word;
  248. ObjectBuffer:PChar; {If a module cannot be loaded, its
  249. name will be returned here.}
  250. ObjectBuffLen:cardinal; {Size of your buffer.}
  251. end;
  252. PStartData=^TStartData;
  253. TResultCodes=record
  254. TerminateReason, {0 = Normal termionation.
  255. 1 = Critical error.
  256. 2 = Trapped. (GPE, etc.)
  257. 3 = Killed by DosKillProcess.}
  258. ExitCode:cardinal; {Exit code of child.}
  259. end;
  260. const
  261. ilStandard = 1;
  262. ilQueryEAsize = 2;
  263. ilQueryEAs = 3;
  264. ilQueryFullName = 5;
  265. quFIFO = 0;
  266. quLIFO = 1;
  267. quPriority = 2;
  268. quNoConvert_Address = 0;
  269. quConvert_Address = 4;
  270. {Start the new session independent or as a child.}
  271. ssf_Related_Independent = 0; {Start new session independent
  272. of the calling session.}
  273. ssf_Related_Child = 1; {Start new session as a child
  274. session to the calling session.}
  275. {Start the new session in the foreground or in the background.}
  276. ssf_FgBg_Fore = 0; {Start new session in foreground.}
  277. ssf_FgBg_Back = 1; {Start new session in background.}
  278. {Should the program started in the new session
  279. be executed under conditions for tracing?}
  280. ssf_TraceOpt_None = 0; {No trace.}
  281. ssf_TraceOpt_Trace = 1; {Trace with no notification
  282. of descendants.}
  283. ssf_TraceOpt_TraceAll = 2; {Trace all descendant sessions.
  284. A termination queue must be
  285. supplied and Related must be
  286. ssf_Related_Child (=1).}
  287. {Will the new session inherit open file handles
  288. and environment from the calling process.}
  289. ssf_InhertOpt_Shell = 0; {Inherit from the shell.}
  290. ssf_InhertOpt_Parent = 1; {Inherit from the calling process.}
  291. {Specifies the type of session to start.}
  292. ssf_Type_Default = 0; {Use program's type.}
  293. ssf_Type_FullScreen = 1; {OS/2 full screen.}
  294. ssf_Type_WindowableVIO = 2; {OS/2 window.}
  295. ssf_Type_PM = 3; {Presentation Manager.}
  296. ssf_Type_VDM = 4; {DOS full screen.}
  297. ssf_Type_WindowedVDM = 7; {DOS window.}
  298. {Additional values for Windows programs}
  299. Prog_31_StdSeamlessVDM = 15; {Windows 3.1 program in its
  300. own windowed session.}
  301. Prog_31_StdSeamlessCommon = 16; {Windows 3.1 program in a
  302. common windowed session.}
  303. Prog_31_EnhSeamlessVDM = 17; {Windows 3.1 program in enhanced
  304. compatibility mode in its own
  305. windowed session.}
  306. Prog_31_EnhSeamlessCommon = 18; {Windows 3.1 program in enhanced
  307. compatibility mode in a common
  308. windowed session.}
  309. Prog_31_Enh = 19; {Windows 3.1 program in enhanced
  310. compatibility mode in a full
  311. screen session.}
  312. Prog_31_Std = 20; {Windows 3.1 program in a full
  313. screen session.}
  314. {Specifies the initial attributes for a OS/2 window or DOS window session.}
  315. ssf_Control_Visible = 0; {Window is visible.}
  316. ssf_Control_Invisible = 1; {Window is invisible.}
  317. ssf_Control_Maximize = 2; {Window is maximized.}
  318. ssf_Control_Minimize = 4; {Window is minimized.}
  319. ssf_Control_NoAutoClose = 8; {Window will not close after
  320. the program has ended.}
  321. ssf_Control_SetPos = 32768; {Use InitXPos, InitYPos,
  322. InitXSize, and InitYSize for
  323. the size and placement.}
  324. function DosSetFileInfo (Handle: THandle; InfoLevel: cardinal; AFileStatus: PFileStatus;
  325. FileStatusLen: cardinal): cardinal; cdecl; external 'DOSCALLS' index 218;
  326. function DosQueryFSInfo (DiskNum, InfoLevel: cardinal; var Buffer: TFSInfo;
  327. BufLen: cardinal): cardinal; cdecl; external 'DOSCALLS' index 278;
  328. function DosQueryFileInfo (Handle: THandle; InfoLevel: cardinal;
  329. AFileStatus: PFileStatus; FileStatusLen: cardinal): cardinal; cdecl;
  330. external 'DOSCALLS' index 279;
  331. function DosScanEnv (Name: PChar; var Value: PChar): cardinal; cdecl;
  332. external 'DOSCALLS' index 227;
  333. function DosFindFirst (FileMask: PChar; var Handle: THandle; Attrib: cardinal;
  334. AFileStatus: PFileStatus; FileStatusLen: cardinal;
  335. var Count: cardinal; InfoLevel: cardinal): cardinal; cdecl;
  336. external 'DOSCALLS' index 264;
  337. function DosFindNext (Handle: THandle; AFileStatus: PFileStatus;
  338. FileStatusLen: cardinal; var Count: cardinal): cardinal; cdecl;
  339. external 'DOSCALLS' index 265;
  340. function DosFindClose (Handle: THandle): cardinal; cdecl;
  341. external 'DOSCALLS' index 263;
  342. function DosQueryCtryInfo (Size: cardinal; var Country: TCountryCode;
  343. var Res: TCountryInfo; var ActualSize: cardinal): cardinal; cdecl;
  344. external 'NLS' index 5;
  345. function DosMapCase (Size: cardinal; var Country: TCountryCode;
  346. AString: PChar): cardinal; cdecl; external 'NLS' index 7;
  347. function DosDelete(FileName:PChar): cardinal; cdecl;
  348. external 'DOSCALLS' index 259;
  349. function DosMove(OldFile, NewFile:PChar): cardinal; cdecl;
  350. external 'DOSCALLS' index 271;
  351. function DosQueryPathInfo(FileName:PChar;InfoLevel:cardinal;
  352. AFileStatus:PFileStatus;FileStatusLen:cardinal): cardinal; cdecl;
  353. external 'DOSCALLS' index 223;
  354. function DosSetPathInfo(FileName:PChar;InfoLevel:cardinal;
  355. AFileStatus:PFileStatus;FileStatusLen,
  356. Options:cardinal):cardinal; cdecl;
  357. external 'DOSCALLS' index 219;
  358. function DosClose(Handle: THandle): cardinal; cdecl;
  359. external 'DOSCALLS' index 257;
  360. function DosRead(Handle:THandle; var Buffer; Count: cardinal;
  361. var ActCount: cardinal): cardinal; cdecl;
  362. external 'DOSCALLS' index 281;
  363. function DosWrite(Handle: THandle; Buffer: pointer; Count: cardinal;
  364. var ActCount: cardinal): cardinal; cdecl;
  365. external 'DOSCALLS' index 282;
  366. procedure DosSleep (MSec: cardinal); cdecl; external 'DOSCALLS' index 229;
  367. function DosCreateQueue (var Handle: THandle; Priority:longint;
  368. Name: PChar): cardinal; cdecl;
  369. external 'QUECALLS' index 16;
  370. function DosReadQueue (Handle: THandle; var ReqBuffer: TRequestData;
  371. var DataLen: cardinal; var DataPtr: pointer;
  372. Element, Wait: cardinal; var Priority: byte;
  373. ASem: THandle): cardinal; cdecl;
  374. external 'QUECALLS' index 9;
  375. function DosCloseQueue (Handle: THandle): cardinal; cdecl;
  376. external 'QUECALLS' index 11;
  377. function DosStartSession (var AStartData: TStartData;
  378. var SesID, PID: cardinal): cardinal; cdecl;
  379. external 'SESMGR' index 37;
  380. function DosFreeMem(P:pointer):cardinal; cdecl; external 'DOSCALLS' index 304;
  381. function DosExecPgm (ObjName: PChar; ObjLen: longint; ExecFlag: cardinal;
  382. Args, Env: PByteArray; var Res: TResultCodes;
  383. FileName:PChar): cardinal; cdecl;
  384. external 'DOSCALLS' index 283;
  385. type
  386. TDT=packed record
  387. Hour,
  388. Minute,
  389. Second,
  390. Sec100,
  391. Day,
  392. Month: byte;
  393. Year: word;
  394. TimeZone: smallint;
  395. WeekDay: byte;
  396. end;
  397. function DosGetDateTime(var Buf: TDT): cardinal; cdecl;
  398. external 'DOSCALLS' index 230;
  399. {****************************************************************************
  400. File Functions
  401. ****************************************************************************}
  402. const
  403. ofRead = $0000; {Open for reading}
  404. ofWrite = $0001; {Open for writing}
  405. ofReadWrite = $0002; {Open for reading/writing}
  406. doDenyRW = $0010; {DenyAll (no sharing)}
  407. faCreateNew = $00010000; {Create if file does not exist}
  408. faOpenReplace = $00040000; {Truncate if file exists}
  409. faCreate = $00050000; {Create if file does not exist, truncate otherwise}
  410. FindResvdMask = $00003737; {Allowed bits in attribute
  411. specification for DosFindFirst call.}
  412. function FileOpen (const FileName: string; Mode: integer): THandle;
  413. Var
  414. Handle: THandle;
  415. Rc, Action: cardinal;
  416. begin
  417. (* DenyNone if sharing not specified. *)
  418. if Mode and 112 = 0 then Mode:=Mode or 64;
  419. Rc:=Sys_DosOpenL(PChar (FileName), Handle, Action, 0, 0, 1, Mode, nil);
  420. If Rc=0 then
  421. FileOpen:=Handle
  422. else
  423. FileOpen:=feInvalidHandle; //FileOpen:=-RC;
  424. //should return feInvalidHandle(=-1) if fail, other negative returned value are no more errors
  425. end;
  426. function FileCreate (const FileName: string): THandle;
  427. Const
  428. Mode = ofReadWrite or faCreate or doDenyRW; (* Sharing to DenyAll *)
  429. Var
  430. Handle: THandle;
  431. RC, Action: cardinal;
  432. Begin
  433. RC:=Sys_DosOpenL(PChar (FileName), Handle, Action, 0, 0, $12, Mode, Nil);
  434. If RC=0 then
  435. FileCreate:=Handle
  436. else
  437. FileCreate:=feInvalidHandle;
  438. End;
  439. function FileCreate (const FileName: string; Mode: integer): THandle;
  440. begin
  441. FileCreate := FileCreate(FileName);
  442. end;
  443. function FileRead (Handle: THandle; var Buffer; Count: longint): longint;
  444. Var
  445. T: cardinal;
  446. begin
  447. DosRead(Handle, Buffer, Count, T);
  448. FileRead := longint (T);
  449. end;
  450. function FileWrite (Handle: THandle; const Buffer; Count: longint): longint;
  451. Var
  452. T: cardinal;
  453. begin
  454. DosWrite (Handle, @Buffer, Count, T);
  455. FileWrite := longint (T);
  456. end;
  457. function FileSeek (Handle: THandle; FOffset, Origin: longint): longint;
  458. var
  459. NPos: int64;
  460. begin
  461. if (Sys_DosSetFilePtrL (Handle, FOffset, Origin, NPos) = 0)
  462. and (NPos < high (longint)) then
  463. FileSeek:= longint (NPos)
  464. else
  465. FileSeek:=-1;
  466. end;
  467. function FileSeek (Handle: THandle; FOffset: Int64; Origin: Longint): Int64;
  468. var
  469. NPos: int64;
  470. begin
  471. if Sys_DosSetFilePtrL (Handle, FOffset, Origin, NPos) = 0 then
  472. FileSeek:= NPos
  473. else
  474. FileSeek:=-1;
  475. end;
  476. procedure FileClose (Handle: THandle);
  477. begin
  478. DosClose(Handle);
  479. end;
  480. function FileTruncate (Handle: THandle; Size: longint): boolean;
  481. begin
  482. FileTruncate:=Sys_DosSetFileSizeL(Handle, Size)=0;
  483. FileSeek(Handle, 0, 2);
  484. end;
  485. function FileAge (const FileName: string): longint;
  486. var Handle: longint;
  487. begin
  488. Handle := FileOpen (FileName, 0);
  489. if Handle <> -1 then
  490. begin
  491. Result := FileGetDate (Handle);
  492. FileClose (Handle);
  493. end
  494. else
  495. Result := -1;
  496. end;
  497. function FileExists (const FileName: string): boolean;
  498. var
  499. SR: TSearchRec;
  500. RC: longint;
  501. begin
  502. FileExists:=False;
  503. if FindFirst (FileName, faAnyFile and not (faDirectory), SR) = 0
  504. then FileExists := True;
  505. FindClose(SR);
  506. end;
  507. type TRec = record
  508. T, D: word;
  509. end;
  510. PSearchRec = ^SearchRec;
  511. function FindFirst (const Path: string; Attr: longint; out Rslt: TSearchRec): longint;
  512. var SR: PSearchRec;
  513. FStat: PFileFindBuf3;
  514. Count: cardinal;
  515. Err: cardinal;
  516. I: cardinal;
  517. begin
  518. New (FStat);
  519. Rslt.FindHandle := THandle ($FFFFFFFF);
  520. Count := 1;
  521. Err := DosFindFirst (PChar (Path), Rslt.FindHandle,
  522. Attr and FindResvdMask, FStat, SizeOf (FStat^), Count, ilStandard);
  523. if (Err = 0) and (Count = 0) then Err := 18;
  524. FindFirst := -Err;
  525. if Err = 0 then
  526. begin
  527. Rslt.Name := FStat^.Name;
  528. Rslt.Size := FStat^.FileSize;
  529. Rslt.Attr := FStat^.AttrFile;
  530. Rslt.ExcludeAttr := 0;
  531. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  532. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  533. end;
  534. Dispose (FStat);
  535. end;
  536. function FindNext (var Rslt: TSearchRec): longint;
  537. var
  538. SR: PSearchRec;
  539. FStat: PFileFindBuf3;
  540. Count: cardinal;
  541. Err: cardinal;
  542. begin
  543. New (FStat);
  544. Count := 1;
  545. Err := DosFindNext (Rslt.FindHandle, FStat, SizeOf (FStat^),
  546. Count);
  547. if (Err = 0) and (Count = 0) then Err := 18;
  548. FindNext := -Err;
  549. if Err = 0 then
  550. begin
  551. Rslt.Name := FStat^.Name;
  552. Rslt.Size := FStat^.FileSize;
  553. Rslt.Attr := FStat^.AttrFile;
  554. Rslt.ExcludeAttr := 0;
  555. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  556. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  557. end;
  558. Dispose (FStat);
  559. end;
  560. procedure FindClose (var F: TSearchrec);
  561. var
  562. SR: PSearchRec;
  563. begin
  564. DosFindClose (F.FindHandle);
  565. F.FindHandle := 0;
  566. end;
  567. function FileGetDate (Handle: THandle): longint;
  568. var
  569. FStat: TFileStatus3;
  570. Time: Longint;
  571. begin
  572. DosError := DosQueryFileInfo(Handle, ilStandard, @FStat, SizeOf(FStat));
  573. if DosError=0 then
  574. begin
  575. Time := FStat.TimeLastWrite + longint (FStat.DateLastWrite) shl 16;
  576. if Time = 0 then
  577. Time := FStat.TimeCreation + longint (FStat.DateCreation) shl 16;
  578. end else
  579. Time:=0;
  580. FileGetDate:=Time;
  581. end;
  582. function FileSetDate (Handle: THandle; Age: longint): longint;
  583. var
  584. FStat: PFileStatus3;
  585. RC: cardinal;
  586. begin
  587. New (FStat);
  588. RC := DosQueryFileInfo (Handle, ilStandard, FStat, SizeOf (FStat^));
  589. if RC <> 0 then
  590. FileSetDate := -1
  591. else
  592. begin
  593. FStat^.DateLastAccess := Hi (Age);
  594. FStat^.DateLastWrite := Hi (Age);
  595. FStat^.TimeLastAccess := Lo (Age);
  596. FStat^.TimeLastWrite := Lo (Age);
  597. RC := DosSetFileInfo (Handle, ilStandard, FStat, SizeOf (FStat^));
  598. if RC <> 0 then
  599. FileSetDate := -1
  600. else
  601. FileSetDate := 0;
  602. end;
  603. Dispose (FStat);
  604. end;
  605. function FileGetAttr (const FileName: string): longint;
  606. var
  607. FS: PFileStatus3;
  608. begin
  609. New(FS);
  610. Result:=-DosQueryPathInfo(PChar (FileName), ilStandard, FS, SizeOf(FS^));
  611. If Result=0 Then Result:=FS^.attrFile;
  612. Dispose(FS);
  613. end;
  614. function FileSetAttr (const Filename: string; Attr: longint): longint;
  615. Var
  616. FS: PFileStatus3;
  617. Begin
  618. New(FS);
  619. FillChar(FS, SizeOf(FS^), 0);
  620. FS^.AttrFile:=Attr;
  621. Result:=-DosSetPathInfo(PChar (FileName), ilStandard, FS, SizeOf(FS^), 0);
  622. Dispose(FS);
  623. end;
  624. function DeleteFile (const FileName: string): boolean;
  625. Begin
  626. Result:=(DosDelete(PChar (FileName))=0);
  627. End;
  628. function RenameFile (const OldName, NewName: string): boolean;
  629. Begin
  630. Result:=(DosMove(PChar (OldName), PChar (NewName))=0);
  631. End;
  632. {****************************************************************************
  633. Disk Functions
  634. ****************************************************************************}
  635. function DiskFree (Drive: byte): int64;
  636. var FI: TFSinfo;
  637. RC: cardinal;
  638. begin
  639. {In OS/2, we use the filesystem information.}
  640. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  641. if RC = 0 then
  642. DiskFree := int64 (FI.Free_Clusters) *
  643. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  644. else
  645. DiskFree := -1;
  646. end;
  647. function DiskSize (Drive: byte): int64;
  648. var FI: TFSinfo;
  649. RC: cardinal;
  650. begin
  651. {In OS/2, we use the filesystem information.}
  652. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  653. if RC = 0 then
  654. DiskSize := int64 (FI.Total_Clusters) *
  655. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  656. else
  657. DiskSize := -1;
  658. end;
  659. function GetCurrentDir: string;
  660. begin
  661. GetDir (0, Result);
  662. end;
  663. function SetCurrentDir (const NewDir: string): boolean;
  664. begin
  665. {$I-}
  666. {$WARNING Should be rewritten to avoid unit dos dependancy!}
  667. ChDir (NewDir);
  668. Result := (IOResult = 0);
  669. {$I+}
  670. end;
  671. function CreateDir (const NewDir: string): boolean;
  672. begin
  673. {$I-}
  674. {$WARNING Should be rewritten to avoid unit dos dependancy!}
  675. MkDir (NewDir);
  676. Result := (IOResult = 0);
  677. {$I+}
  678. end;
  679. function RemoveDir (const Dir: string): boolean;
  680. begin
  681. {$I-}
  682. {$WARNING Should be rewritten to avoid unit dos dependancy!}
  683. RmDir (Dir);
  684. Result := (IOResult = 0);
  685. {$I+}
  686. end;
  687. function DirectoryExists (const Directory: string): boolean;
  688. var
  689. SR: TSearchRec;
  690. begin
  691. DirectoryExists := (FindFirst (Directory, faAnyFile, SR) = 0) and
  692. (SR.Attr and faDirectory <> 0);
  693. FindClose(SR);
  694. end;
  695. {****************************************************************************
  696. Time Functions
  697. ****************************************************************************}
  698. procedure GetLocalTime (var SystemTime: TSystemTime);
  699. var
  700. DT: TDT;
  701. begin
  702. DosGetDateTime(DT);
  703. with SystemTime do
  704. begin
  705. Year:=DT.Year;
  706. Month:=DT.Month;
  707. Day:=DT.Day;
  708. Hour:=DT.Hour;
  709. Minute:=DT.Minute;
  710. Second:=DT.Second;
  711. MilliSecond:=DT.Sec100;
  712. end;
  713. end;
  714. {****************************************************************************
  715. Misc Functions
  716. ****************************************************************************}
  717. procedure Beep;
  718. begin
  719. end;
  720. {****************************************************************************
  721. Locale Functions
  722. ****************************************************************************}
  723. procedure InitAnsi;
  724. var I: byte;
  725. Country: TCountryCode;
  726. begin
  727. for I := 0 to 255 do
  728. UpperCaseTable [I] := Chr (I);
  729. Move (UpperCaseTable, LowerCaseTable, SizeOf (UpperCaseTable));
  730. FillChar (Country, SizeOf (Country), 0);
  731. DosMapCase (SizeOf (UpperCaseTable), Country, @UpperCaseTable);
  732. for I := 0 to 255 do
  733. if UpperCaseTable [I] <> Chr (I) then
  734. LowerCaseTable [Ord (UpperCaseTable [I])] := Chr (I);
  735. end;
  736. procedure InitInternational;
  737. var Country: TCountryCode;
  738. CtryInfo: TCountryInfo;
  739. Size: cardinal;
  740. RC: cardinal;
  741. begin
  742. Size := 0;
  743. FillChar (Country, SizeOf (Country), 0);
  744. FillChar (CtryInfo, SizeOf (CtryInfo), 0);
  745. RC := DosQueryCtryInfo (SizeOf (CtryInfo), Country, CtryInfo, Size);
  746. if RC = 0 then
  747. begin
  748. DateSeparator := CtryInfo.DateSeparator;
  749. case CtryInfo.DateFormat of
  750. 1: begin
  751. ShortDateFormat := 'd/m/y';
  752. LongDateFormat := 'dd" "mmmm" "yyyy';
  753. end;
  754. 2: begin
  755. ShortDateFormat := 'y/m/d';
  756. LongDateFormat := 'yyyy" "mmmm" "dd';
  757. end;
  758. 3: begin
  759. ShortDateFormat := 'm/d/y';
  760. LongDateFormat := 'mmmm" "dd" "yyyy';
  761. end;
  762. end;
  763. TimeSeparator := CtryInfo.TimeSeparator;
  764. DecimalSeparator := CtryInfo.DecimalSeparator;
  765. ThousandSeparator := CtryInfo.ThousandSeparator;
  766. CurrencyFormat := CtryInfo.CurrencyFormat;
  767. CurrencyString := PChar (CtryInfo.CurrencyUnit);
  768. end;
  769. InitAnsi;
  770. InitInternationalGeneric;
  771. end;
  772. function SysErrorMessage(ErrorCode: Integer): String;
  773. begin
  774. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  775. end;
  776. {****************************************************************************
  777. OS Utils
  778. ****************************************************************************}
  779. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  780. begin
  781. GetEnvironmentVariable := StrPas (GetEnvPChar (EnvVar));
  782. end;
  783. Function GetEnvironmentVariableCount : Integer;
  784. begin
  785. (* Result:=FPCCountEnvVar(EnvP); - the amount is already known... *)
  786. GetEnvironmentVariableCount := EnvC;
  787. end;
  788. Function GetEnvironmentString(Index : Integer) : String;
  789. begin
  790. Result:=FPCGetEnvStrFromP (EnvP, Index);
  791. end;
  792. procedure Sleep (Milliseconds: cardinal);
  793. begin
  794. DosSleep (Milliseconds);
  795. end;
  796. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
  797. integer;
  798. var
  799. HQ: THandle;
  800. SPID, STID, QName: shortstring;
  801. SD: TStartData;
  802. SID, PID: cardinal;
  803. RD: TRequestData;
  804. PCI: PChildInfo;
  805. CISize: cardinal;
  806. Prio: byte;
  807. E: EOSError;
  808. CommandLine: ansistring;
  809. Args0, Args: PByteArray;
  810. ObjNameBuf: PChar;
  811. ArgSize: word;
  812. Res: TResultCodes;
  813. ObjName: shortstring;
  814. const
  815. MaxArgsSize = 3072; (* Amount of memory reserved for arguments in bytes. *)
  816. ObjBufSize = 512;
  817. begin
  818. ObjName := '';
  819. GetMem (ObjNameBuf, ObjBufSize);
  820. FillChar (ObjNameBuf^, ObjBufSize, 0);
  821. if ComLine = '' then
  822. begin
  823. Args0 := nil;
  824. Args := nil;
  825. end
  826. else
  827. begin
  828. GetMem (Args0, MaxArgsSize);
  829. Args := Args0;
  830. (* Work around a bug in OS/2 - argument to DosExecPgm *)
  831. (* should not cross 64K boundary. *)
  832. if ((PtrUInt (Args) + 1024) and $FFFF) < 1024 then
  833. Inc (pointer (Args), 1024);
  834. ArgSize := 0;
  835. Move (Path [1], Args^ [ArgSize], Length (Path));
  836. Inc (ArgSize, Length (Path));
  837. Args^ [ArgSize] := 0;
  838. Inc (ArgSize);
  839. {Now do the real arguments.}
  840. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  841. Inc (ArgSize, Length (ComLine));
  842. Args^ [ArgSize] := 0;
  843. Inc (ArgSize);
  844. Args^ [ArgSize] := 0;
  845. end;
  846. Result := DosExecPgm (ObjNameBuf, ObjBufSize, 0, Args, nil, Res, PChar (Path));
  847. if Args0 <> nil then
  848. FreeMem (Args0, MaxArgsSize);
  849. if Result = 0 then
  850. begin
  851. Result := Res.ExitCode;
  852. FreeMem (ObjNameBuf, ObjBufSize);
  853. end
  854. else
  855. begin
  856. if (Result = 190) or (Result = 191) then
  857. begin
  858. FillChar (SD, SizeOf (SD), 0);
  859. SD.Length := 24;
  860. SD.Related := ssf_Related_Child;
  861. CommandLine := FExpand (Path); (* Needed for other session types... *)
  862. SD.PgmName := PChar (CommandLine);
  863. if ComLine <> '' then
  864. SD.PgmInputs := PChar (ComLine);
  865. SD.InheritOpt := ssf_InhertOpt_Parent;
  866. Str (GetProcessID, SPID);
  867. Str (ThreadID, STID);
  868. QName := '\QUEUES\FPC_ExecuteProcess_p' + SPID + 't' + STID + '.QUE'#0;
  869. SD.TermQ := @QName [1];
  870. Result := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  871. if Result = 0 then
  872. begin
  873. Result := DosStartSession (SD, SID, PID);
  874. if (Result = 0) or (Result = 457) then
  875. begin
  876. Result := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  877. if Result = 0 then
  878. begin
  879. Result := PCI^.Return;
  880. DosCloseQueue (HQ);
  881. DosFreeMem (PCI);
  882. Exit;
  883. end;
  884. end;
  885. DosCloseQueue (HQ);
  886. end;
  887. end
  888. else
  889. ObjName := StrPas (ObjNameBuf);
  890. FreeMem (ObjNameBuf, ObjBufSize);
  891. if ComLine = '' then
  892. CommandLine := Path
  893. else
  894. CommandLine := Path + ' ' + ComLine;
  895. if ObjName = '' then
  896. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, Result])
  897. else
  898. E := EOSError.CreateFmt (SExecuteProcessFailed + '(' + ObjName + ')', [CommandLine, Result]);
  899. E.ErrorCode := Result;
  900. raise E;
  901. end;
  902. end;
  903. function ExecuteProcess (const Path: AnsiString;
  904. const ComLine: array of AnsiString): integer;
  905. var
  906. CommandLine: AnsiString;
  907. I: integer;
  908. begin
  909. Commandline := '';
  910. for I := 0 to High (ComLine) do
  911. if Pos (' ', ComLine [I]) <> 0 then
  912. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  913. else
  914. CommandLine := CommandLine + ' ' + Comline [I];
  915. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  916. end;
  917. {****************************************************************************
  918. Initialization code
  919. ****************************************************************************}
  920. Initialization
  921. InitExceptions; { Initialize exceptions. OS independent }
  922. InitInternational; { Initialize internationalization settings }
  923. Finalization
  924. DoneExceptions;
  925. end.