sysutils.pp 37 KB

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