sysutils.pp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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; Mode: integer): THandle;
  441. begin
  442. FileCreate := FileCreate(FileName);
  443. end;
  444. function FileRead (Handle: THandle; Out Buffer; Count: longint): longint;
  445. Var
  446. T: cardinal;
  447. begin
  448. DosRead(Handle, Buffer, Count, T);
  449. FileRead := longint (T);
  450. end;
  451. function FileWrite (Handle: THandle; const Buffer; Count: longint): longint;
  452. Var
  453. T: cardinal;
  454. begin
  455. DosWrite (Handle, @Buffer, Count, T);
  456. FileWrite := longint (T);
  457. end;
  458. function FileSeek (Handle: THandle; FOffset, Origin: longint): longint;
  459. var
  460. NPos: int64;
  461. begin
  462. if (Sys_DosSetFilePtrL (Handle, FOffset, Origin, NPos) = 0)
  463. and (NPos < high (longint)) then
  464. FileSeek:= longint (NPos)
  465. else
  466. FileSeek:=-1;
  467. end;
  468. function FileSeek (Handle: THandle; FOffset: Int64; Origin: Longint): Int64;
  469. var
  470. NPos: int64;
  471. begin
  472. if Sys_DosSetFilePtrL (Handle, FOffset, Origin, NPos) = 0 then
  473. FileSeek:= NPos
  474. else
  475. FileSeek:=-1;
  476. end;
  477. procedure FileClose (Handle: THandle);
  478. begin
  479. DosClose(Handle);
  480. end;
  481. function FileTruncate (Handle: THandle; Size: Int64): boolean;
  482. begin
  483. FileTruncate:=Sys_DosSetFileSizeL(Handle, Size)=0;
  484. FileSeek(Handle, 0, 2);
  485. end;
  486. function FileAge (const FileName: string): longint;
  487. var Handle: longint;
  488. begin
  489. Handle := FileOpen (FileName, 0);
  490. if Handle <> -1 then
  491. begin
  492. Result := FileGetDate (Handle);
  493. FileClose (Handle);
  494. end
  495. else
  496. Result := -1;
  497. end;
  498. function FileExists (const FileName: string): boolean;
  499. var
  500. SR: TSearchRec;
  501. RC: longint;
  502. begin
  503. FileExists:=False;
  504. if FindFirst (FileName, faAnyFile and not (faDirectory), SR) = 0
  505. then FileExists := True;
  506. FindClose(SR);
  507. end;
  508. type TRec = record
  509. T, D: word;
  510. end;
  511. PSearchRec = ^SearchRec;
  512. function FindFirst (const Path: string; Attr: longint; out Rslt: TSearchRec): longint;
  513. var SR: PSearchRec;
  514. FStat: PFileFindBuf3;
  515. Count: cardinal;
  516. Err: cardinal;
  517. I: cardinal;
  518. begin
  519. New (FStat);
  520. Rslt.FindHandle := THandle ($FFFFFFFF);
  521. Count := 1;
  522. Err := DosFindFirst (PChar (Path), Rslt.FindHandle,
  523. Attr and FindResvdMask, FStat, SizeOf (FStat^), Count, ilStandard);
  524. if (Err = 0) and (Count = 0) then Err := 18;
  525. FindFirst := -Err;
  526. if Err = 0 then
  527. begin
  528. Rslt.Name := FStat^.Name;
  529. Rslt.Size := FStat^.FileSize;
  530. Rslt.Attr := FStat^.AttrFile;
  531. Rslt.ExcludeAttr := 0;
  532. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  533. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  534. end else if (Rslt.Findhandle<>0) then
  535. begin
  536. FindClose(Rslt);
  537. end;
  538. Dispose (FStat);
  539. end;
  540. function FindNext (var Rslt: TSearchRec): longint;
  541. var
  542. SR: PSearchRec;
  543. FStat: PFileFindBuf3;
  544. Count: cardinal;
  545. Err: cardinal;
  546. begin
  547. New (FStat);
  548. Count := 1;
  549. Err := DosFindNext (Rslt.FindHandle, FStat, SizeOf (FStat^),
  550. Count);
  551. if (Err = 0) and (Count = 0) then Err := 18;
  552. FindNext := -Err;
  553. if Err = 0 then
  554. begin
  555. Rslt.Name := FStat^.Name;
  556. Rslt.Size := FStat^.FileSize;
  557. Rslt.Attr := FStat^.AttrFile;
  558. Rslt.ExcludeAttr := 0;
  559. TRec (Rslt.Time).T := FStat^.TimeLastWrite;
  560. TRec (Rslt.Time).D := FStat^.DateLastWrite;
  561. end;
  562. Dispose (FStat);
  563. end;
  564. procedure FindClose (var F: TSearchrec);
  565. var
  566. SR: PSearchRec;
  567. begin
  568. DosFindClose (F.FindHandle);
  569. F.FindHandle := 0;
  570. end;
  571. function FileGetDate (Handle: THandle): longint;
  572. var
  573. FStat: TFileStatus3;
  574. Time: Longint;
  575. begin
  576. DosError := DosQueryFileInfo(Handle, ilStandard, @FStat, SizeOf(FStat));
  577. if DosError=0 then
  578. begin
  579. Time := FStat.TimeLastWrite + longint (FStat.DateLastWrite) shl 16;
  580. if Time = 0 then
  581. Time := FStat.TimeCreation + longint (FStat.DateCreation) shl 16;
  582. end else
  583. Time:=0;
  584. FileGetDate:=Time;
  585. end;
  586. function FileSetDate (Handle: THandle; Age: longint): longint;
  587. var
  588. FStat: PFileStatus3;
  589. RC: cardinal;
  590. begin
  591. New (FStat);
  592. RC := DosQueryFileInfo (Handle, ilStandard, FStat, SizeOf (FStat^));
  593. if RC <> 0 then
  594. FileSetDate := -1
  595. else
  596. begin
  597. FStat^.DateLastAccess := Hi (Age);
  598. FStat^.DateLastWrite := Hi (Age);
  599. FStat^.TimeLastAccess := Lo (Age);
  600. FStat^.TimeLastWrite := Lo (Age);
  601. RC := DosSetFileInfo (Handle, ilStandard, FStat, SizeOf (FStat^));
  602. if RC <> 0 then
  603. FileSetDate := -1
  604. else
  605. FileSetDate := 0;
  606. end;
  607. Dispose (FStat);
  608. end;
  609. function FileGetAttr (const FileName: string): longint;
  610. var
  611. FS: PFileStatus3;
  612. begin
  613. New(FS);
  614. Result:=-DosQueryPathInfo(PChar (FileName), ilStandard, FS, SizeOf(FS^));
  615. If Result=0 Then Result:=FS^.attrFile;
  616. Dispose(FS);
  617. end;
  618. function FileSetAttr (const Filename: string; Attr: longint): longint;
  619. Var
  620. FS: PFileStatus3;
  621. Begin
  622. New(FS);
  623. FillChar(FS, SizeOf(FS^), 0);
  624. FS^.AttrFile:=Attr;
  625. Result:=-DosSetPathInfo(PChar (FileName), ilStandard, FS, SizeOf(FS^), 0);
  626. Dispose(FS);
  627. end;
  628. function DeleteFile (const FileName: string): boolean;
  629. Begin
  630. Result:=(DosDelete(PChar (FileName))=0);
  631. End;
  632. function RenameFile (const OldName, NewName: string): boolean;
  633. Begin
  634. Result:=(DosMove(PChar (OldName), PChar (NewName))=0);
  635. End;
  636. {****************************************************************************
  637. Disk Functions
  638. ****************************************************************************}
  639. function DiskFree (Drive: byte): int64;
  640. var FI: TFSinfo;
  641. RC: cardinal;
  642. begin
  643. {In OS/2, we use the filesystem information.}
  644. RC := DosQueryFSInfo (Drive, 1, FI, SizeOf (FI));
  645. if RC = 0 then
  646. DiskFree := int64 (FI.Free_Clusters) *
  647. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  648. else
  649. DiskFree := -1;
  650. end;
  651. function DiskSize (Drive: byte): int64;
  652. var FI: TFSinfo;
  653. RC: cardinal;
  654. begin
  655. {In OS/2, we use the filesystem information.}
  656. RC := DosQueryFSinfo (Drive, 1, FI, SizeOf (FI));
  657. if RC = 0 then
  658. DiskSize := int64 (FI.Total_Clusters) *
  659. int64 (FI.Sectors_Per_Cluster) * int64 (FI.Bytes_Per_Sector)
  660. else
  661. DiskSize := -1;
  662. end;
  663. function GetCurrentDir: string;
  664. begin
  665. GetDir (0, Result);
  666. end;
  667. function SetCurrentDir (const NewDir: string): boolean;
  668. begin
  669. {$I-}
  670. {$WARNING Should be rewritten to avoid unit dos dependency!}
  671. ChDir (NewDir);
  672. Result := (IOResult = 0);
  673. {$I+}
  674. end;
  675. function CreateDir (const NewDir: string): boolean;
  676. begin
  677. {$I-}
  678. {$WARNING Should be rewritten to avoid unit dos dependency!}
  679. MkDir (NewDir);
  680. Result := (IOResult = 0);
  681. {$I+}
  682. end;
  683. function RemoveDir (const Dir: string): boolean;
  684. begin
  685. {$I-}
  686. {$WARNING Should be rewritten to avoid unit dos dependency!}
  687. RmDir (Dir);
  688. Result := (IOResult = 0);
  689. {$I+}
  690. end;
  691. function DirectoryExists (const Directory: string): boolean;
  692. var
  693. SR: TSearchRec;
  694. begin
  695. DirectoryExists := (FindFirst (Directory, faAnyFile, SR) = 0) and
  696. (SR.Attr and faDirectory <> 0);
  697. FindClose(SR);
  698. end;
  699. {****************************************************************************
  700. Time Functions
  701. ****************************************************************************}
  702. procedure GetLocalTime (var SystemTime: TSystemTime);
  703. var
  704. DT: TDT;
  705. begin
  706. DosGetDateTime(DT);
  707. with SystemTime do
  708. begin
  709. Year:=DT.Year;
  710. Month:=DT.Month;
  711. Day:=DT.Day;
  712. Hour:=DT.Hour;
  713. Minute:=DT.Minute;
  714. Second:=DT.Second;
  715. MilliSecond:=DT.Sec100;
  716. end;
  717. end;
  718. {****************************************************************************
  719. Misc Functions
  720. ****************************************************************************}
  721. procedure sysbeep;
  722. begin
  723. // Maybe implement later on ?
  724. end;
  725. {****************************************************************************
  726. Locale Functions
  727. ****************************************************************************}
  728. procedure InitAnsi;
  729. var I: byte;
  730. Country: TCountryCode;
  731. begin
  732. for I := 0 to 255 do
  733. UpperCaseTable [I] := Chr (I);
  734. Move (UpperCaseTable, LowerCaseTable, SizeOf (UpperCaseTable));
  735. FillChar (Country, SizeOf (Country), 0);
  736. DosMapCase (SizeOf (UpperCaseTable), Country, @UpperCaseTable);
  737. for I := 0 to 255 do
  738. if UpperCaseTable [I] <> Chr (I) then
  739. LowerCaseTable [Ord (UpperCaseTable [I])] := Chr (I);
  740. end;
  741. procedure InitInternational;
  742. var Country: TCountryCode;
  743. CtryInfo: TCountryInfo;
  744. Size: cardinal;
  745. RC: cardinal;
  746. begin
  747. Size := 0;
  748. FillChar (Country, SizeOf (Country), 0);
  749. FillChar (CtryInfo, SizeOf (CtryInfo), 0);
  750. RC := DosQueryCtryInfo (SizeOf (CtryInfo), Country, CtryInfo, Size);
  751. if RC = 0 then
  752. begin
  753. DateSeparator := CtryInfo.DateSeparator;
  754. case CtryInfo.DateFormat of
  755. 1: begin
  756. ShortDateFormat := 'd/m/y';
  757. LongDateFormat := 'dd" "mmmm" "yyyy';
  758. end;
  759. 2: begin
  760. ShortDateFormat := 'y/m/d';
  761. LongDateFormat := 'yyyy" "mmmm" "dd';
  762. end;
  763. 3: begin
  764. ShortDateFormat := 'm/d/y';
  765. LongDateFormat := 'mmmm" "dd" "yyyy';
  766. end;
  767. end;
  768. TimeSeparator := CtryInfo.TimeSeparator;
  769. DecimalSeparator := CtryInfo.DecimalSeparator;
  770. ThousandSeparator := CtryInfo.ThousandSeparator;
  771. CurrencyFormat := CtryInfo.CurrencyFormat;
  772. CurrencyString := PChar (CtryInfo.CurrencyUnit);
  773. end;
  774. InitAnsi;
  775. InitInternationalGeneric;
  776. end;
  777. function SysErrorMessage(ErrorCode: Integer): String;
  778. begin
  779. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  780. end;
  781. {****************************************************************************
  782. OS Utils
  783. ****************************************************************************}
  784. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  785. begin
  786. GetEnvironmentVariable := StrPas (GetEnvPChar (EnvVar));
  787. end;
  788. Function GetEnvironmentVariableCount : Integer;
  789. begin
  790. (* Result:=FPCCountEnvVar(EnvP); - the amount is already known... *)
  791. GetEnvironmentVariableCount := EnvC;
  792. end;
  793. Function GetEnvironmentString(Index : Integer) : String;
  794. begin
  795. Result:=FPCGetEnvStrFromP (EnvP, Index);
  796. end;
  797. procedure Sleep (Milliseconds: cardinal);
  798. begin
  799. DosSleep (Milliseconds);
  800. end;
  801. function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString;Flags:TExecuteFlags=[]):
  802. integer;
  803. var
  804. HQ: THandle;
  805. SPID, STID, QName: shortstring;
  806. SD: TStartData;
  807. SID, PID: cardinal;
  808. RD: TRequestData;
  809. PCI: PChildInfo;
  810. CISize: cardinal;
  811. Prio: byte;
  812. E: EOSError;
  813. CommandLine: ansistring;
  814. Args0, Args: PByteArray;
  815. ObjNameBuf: PChar;
  816. ArgSize: word;
  817. Res: TResultCodes;
  818. ObjName: shortstring;
  819. const
  820. MaxArgsSize = 3072; (* Amount of memory reserved for arguments in bytes. *)
  821. ObjBufSize = 512;
  822. begin
  823. ObjName := '';
  824. GetMem (ObjNameBuf, ObjBufSize);
  825. FillChar (ObjNameBuf^, ObjBufSize, 0);
  826. if ComLine = '' then
  827. begin
  828. Args0 := nil;
  829. Args := nil;
  830. end
  831. else
  832. begin
  833. GetMem (Args0, MaxArgsSize);
  834. Args := Args0;
  835. (* Work around a bug in OS/2 - argument to DosExecPgm *)
  836. (* should not cross 64K boundary. *)
  837. if ((PtrUInt (Args) + 1024) and $FFFF) < 1024 then
  838. Inc (pointer (Args), 1024);
  839. ArgSize := 0;
  840. Move (Path [1], Args^ [ArgSize], Length (Path));
  841. Inc (ArgSize, Length (Path));
  842. Args^ [ArgSize] := 0;
  843. Inc (ArgSize);
  844. {Now do the real arguments.}
  845. Move (ComLine [1], Args^ [ArgSize], Length (ComLine));
  846. Inc (ArgSize, Length (ComLine));
  847. Args^ [ArgSize] := 0;
  848. Inc (ArgSize);
  849. Args^ [ArgSize] := 0;
  850. end;
  851. Result := DosExecPgm (ObjNameBuf, ObjBufSize, 0, Args, nil, Res, PChar (Path));
  852. if Args0 <> nil then
  853. FreeMem (Args0, MaxArgsSize);
  854. if Result = 0 then
  855. begin
  856. Result := Res.ExitCode;
  857. FreeMem (ObjNameBuf, ObjBufSize);
  858. end
  859. else
  860. begin
  861. if (Result = 190) or (Result = 191) then
  862. begin
  863. FillChar (SD, SizeOf (SD), 0);
  864. SD.Length := 24;
  865. SD.Related := ssf_Related_Child;
  866. CommandLine := FExpand (Path); (* Needed for other session types... *)
  867. SD.PgmName := PChar (CommandLine);
  868. if ComLine <> '' then
  869. SD.PgmInputs := PChar (ComLine);
  870. SD.InheritOpt := ssf_InhertOpt_Parent;
  871. Str (GetProcessID, SPID);
  872. Str (ThreadID, STID);
  873. QName := '\QUEUES\FPC_ExecuteProcess_p' + SPID + 't' + STID + '.QUE'#0;
  874. SD.TermQ := @QName [1];
  875. Result := DosCreateQueue (HQ, quFIFO or quConvert_Address, @QName [1]);
  876. if Result = 0 then
  877. begin
  878. Result := DosStartSession (SD, SID, PID);
  879. if (Result = 0) or (Result = 457) then
  880. begin
  881. Result := DosReadQueue (HQ, RD, CISize, PCI, 0, 0, Prio, 0);
  882. if Result = 0 then
  883. begin
  884. Result := PCI^.Return;
  885. DosCloseQueue (HQ);
  886. DosFreeMem (PCI);
  887. Exit;
  888. end;
  889. end;
  890. DosCloseQueue (HQ);
  891. end;
  892. end
  893. else
  894. ObjName := StrPas (ObjNameBuf);
  895. FreeMem (ObjNameBuf, ObjBufSize);
  896. if ComLine = '' then
  897. CommandLine := Path
  898. else
  899. CommandLine := Path + ' ' + ComLine;
  900. if ObjName = '' then
  901. E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, Result])
  902. else
  903. E := EOSError.CreateFmt (SExecuteProcessFailed + '(' + ObjName + ')', [CommandLine, Result]);
  904. E.ErrorCode := Result;
  905. raise E;
  906. end;
  907. end;
  908. function ExecuteProcess (const Path: AnsiString;
  909. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  910. var
  911. CommandLine: AnsiString;
  912. I: integer;
  913. begin
  914. Commandline := '';
  915. for I := 0 to High (ComLine) do
  916. if Pos (' ', ComLine [I]) <> 0 then
  917. CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
  918. else
  919. CommandLine := CommandLine + ' ' + Comline [I];
  920. ExecuteProcess := ExecuteProcess (Path, CommandLine);
  921. end;
  922. {****************************************************************************
  923. Initialization code
  924. ****************************************************************************}
  925. Initialization
  926. InitExceptions; { Initialize exceptions. OS independent }
  927. InitInternational; { Initialize internationalization settings }
  928. OnBeep:=@SysBeep;
  929. Finalization
  930. DoneExceptions;
  931. end.