sysutils.pp 37 KB

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