sysutils.pp 35 KB

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