sysutils.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2004 by the Free Pascal development team.
  4. Sysutils unit for netware (libc)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit sysutils;
  12. interface
  13. {$MODE objfpc}
  14. {$MODESWITCH OUT}
  15. { force ansistrings }
  16. {$H+}
  17. {$modeswitch typehelpers}
  18. {$modeswitch advancedrecords}
  19. uses Libc,DOS;
  20. TYPE
  21. TNetwareLibcFindData =
  22. RECORD
  23. DirP : Pdirent; { used for opendir }
  24. EntryP: Pdirent; { and readdir }
  25. Magic : longint; { to avoid abends with uninitialized TSearchRec }
  26. _mask : RawByteString; { search mask i.e. *.* }
  27. _dir : RawByteString; { directory where to search }
  28. _attr : longint; { specified attribute }
  29. fname : string; { full pathname of found file }
  30. END;
  31. {$DEFINE HAS_SLEEP}
  32. {$DEFINE HAS_OSERROR}
  33. { Include platform independent interface part }
  34. {$i sysutilh.inc}
  35. { additional NetWare file flags}
  36. CONST
  37. faSHARE = M_A_SHARE shr 16; // Sharable file
  38. //faNO_SUBALLOC = $00000800; // Don't sub alloc. this file
  39. faTRANS = M_A_TRANS shr 16; // Transactional file (TTS usable)
  40. //faREADAUD = $00004000; // clib only: Read audit
  41. //faWRITAUD = $00008000; // clib only: Write audit
  42. faIMMPURG = M_A_IMMPURG shr 16; // Immediate purge
  43. faNORENAM = M_A_NORENAM shr 16; // Rename inhibit
  44. faNODELET = M_A_NODELET shr 16; // Delete inhibit
  45. faNOCOPY = M_A_NOCOPY shr 16; // Copy inhibit
  46. //faFILE_MIGRATED = $00400000; // clib only: File has been migrated
  47. //faDONT_MIGRATE = $00800000; // clib only: Don't migrate this file
  48. faIMMEDIATE_COMPRESS = M_A_IMMCOMPRESS shr 16; // Compress this file immediately
  49. faFILE_COMPRESSED = M_A_FILE_COMPRESSED shr 16; // File is compressed
  50. faDONT_COMPRESS = M_A_DONT_COMPRESS shr 16; // Don't compress this file
  51. faCANT_COMPRESS = M_A_CANT_COMPRESS shr 16; // Can't compress this file
  52. //faATTR_ARCHIVE = $40000000; // clib only: Entry has had an EA modified,
  53. // an ownerID changed, or trustee
  54. // info changed, etc.
  55. faSetNetwareAttrs = M_A_BITS_SIGNIFICANT; // if this is set, netware flags are changed also
  56. implementation
  57. uses
  58. sysconst;
  59. {$DEFINE FPC_FEXPAND_DRIVES}
  60. {$DEFINE FPC_FEXPAND_VOLUMES}
  61. {$DEFINE FPC_FEXPAND_NO_DEFAULT_PATHS}
  62. { used OS file system APIs use ansistring }
  63. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  64. { OS has an ansistring/single byte environment variable API }
  65. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  66. { used OS file system APIs use ansistring }
  67. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  68. { Include platform independent implementation part }
  69. {$i sysutils.inc}
  70. {****************************************************************************
  71. File Functions
  72. ****************************************************************************}
  73. Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
  74. VAR NWOpenFlags : longint;
  75. SystemFileName: RawByteString;
  76. begin
  77. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  78. NWOpenFlags:=0;
  79. Case (Mode and 3) of
  80. 0 : NWOpenFlags:=NWOpenFlags or O_RDONLY;
  81. 1 : NWOpenFlags:=NWOpenFlags or O_WRONLY;
  82. 2 : NWOpenFlags:=NWOpenFlags or O_RDWR;
  83. end;
  84. FileOpen := Fpopen (pchar(SystemFileName),NWOpenFlags);
  85. //!! We need to set locking based on Mode !!
  86. end;
  87. Function FileCreate (Const FileName : RawByteString) : THandle;
  88. var SystemFileName: RawByteString;
  89. begin
  90. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  91. FileCreate:=Fpopen(Pchar(SystemFileName),O_RdWr or O_Creat or O_Trunc or O_Binary);
  92. if FileCreate >= 0 then
  93. FileSetAttr (Filename, 0); // dont know why but open always sets ReadOnly flag
  94. end;
  95. Function FileCreate (Const FileName : RawByteString; rights:longint) : THandle;
  96. begin
  97. FileCreate:=FileCreate (FileName);
  98. end;
  99. Function FileCreate (Const FileName : RawByteString; ShareMode:longint; rights : longint) : THandle;
  100. begin
  101. FileCreate:=FileCreate (FileName);
  102. end;
  103. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
  104. begin
  105. FileRead:=libc.fpread (Handle,@Buffer,Count);
  106. end;
  107. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  108. begin
  109. FileWrite:=libc.fpwrite (Handle,@Buffer,Count);
  110. end;
  111. Function FileSeek (Handle : THandle; FOffset,Origin : Longint) : Longint;
  112. begin
  113. FileSeek:=libc.fplseek (Handle,FOffset,Origin);
  114. end;
  115. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  116. begin
  117. FileSeek:=libc.fplseek64 (Handle,FOffset,Origin);
  118. end;
  119. Procedure FileClose (Handle : THandle);
  120. begin
  121. libc.fpclose(Handle);
  122. end;
  123. Function FileTruncate (Handle : THandle; Size: Int64) : boolean;
  124. begin
  125. if Size > high (longint) then
  126. FileTruncate := false
  127. {$WARNING Possible support for 64-bit FS to be checked!}
  128. else
  129. FileTruncate:=(libc.fpchsize(Handle,Size) = 0);
  130. end;
  131. Function FileLock (Handle : THandle; FOffset,FLen : Longint) : Longint;
  132. begin
  133. {$warning FileLock not implemented}
  134. //FileLock := _lock (Handle,FOffset,FLen);
  135. FileLock := -1;
  136. end;
  137. Function FileLock (Handle : THandle; FOffset,FLen : Int64) : Longint;
  138. begin
  139. {$warning need to add 64bit FileLock call }
  140. //FileLock := FileLock (Handle, longint(FOffset),longint(FLen));
  141. FileLock := -1;
  142. end;
  143. Function FileUnlock (Handle : THandle; FOffset,FLen : Longint) : Longint;
  144. begin
  145. //FileUnlock := _unlock (Handle,FOffset,FLen);
  146. {$warning FileUnLock not implemented}
  147. FileUnlock := -1;
  148. end;
  149. Function FileUnlock (Handle : THandle; FOffset,FLen : Int64) : Longint;
  150. begin
  151. {$warning need to add 64bit FileUnlock call }
  152. //FileUnlock := FileUnlock (Handle, longint(FOffset),longint(FLen));
  153. FileUnlock := -1;
  154. end;
  155. Function FileAge (Const FileName : RawByteString): Longint;
  156. var Info : TStat;
  157. TM : TTM;
  158. SystemFileName: RawByteString;
  159. begin
  160. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  161. If Fpstat (pchar(SystemFileName),Info) <> 0 then
  162. exit(-1)
  163. else
  164. begin
  165. localtime_r (Info.st_mtim.tv_sec,tm);
  166. with TM do
  167. result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  168. end;
  169. end;
  170. Function FileExists (Const FileName : RawByteString) : Boolean;
  171. VAR Info : TStat;
  172. SystemFileName: RawByteString;
  173. begin
  174. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  175. FileExists:=(Fpstat(pchar(SystemFileName),Info) = 0);
  176. end;
  177. Function UnixToWinAge(UnixAge : time_t): Longint;
  178. Var tm : TTm;
  179. begin
  180. libc.localtime_r (UnixAge, tm);
  181. with tm do
  182. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  183. end;
  184. {returns true if attributes match}
  185. function find_setfields (var f : TsearchRec; var AttrsOk : boolean; var Name : RawByteString) : longint;
  186. var
  187. StatBuf : TStat;
  188. fname : RawByteString;
  189. begin
  190. result := 0;
  191. with F do
  192. begin
  193. if FindData.Magic = $AD02 then
  194. begin
  195. attr := (Pdirent(FindData.EntryP)^.d_mode shr 16) and $ffff;
  196. size := Pdirent(FindData.EntryP)^.d_size;
  197. name := Pdirent(FindData.EntryP)^.d_name;
  198. SetCodePage(name, DefaultFileSystemCodePage, False);
  199. fname := FindData._dir + name;
  200. if Fpstat (pchar(fname),StatBuf) = 0 then
  201. time := UnixToWinAge (StatBuf.st_mtim.tv_sec)
  202. else
  203. time := 0;
  204. AttrsOk := false;
  205. if (f.FindData._attr and faHidden) = 0 then
  206. if attr and faHidden > 0 then exit;
  207. if (f.FindData._attr and faDirectory) = 0 then
  208. if attr and faDirectory > 0 then exit;
  209. if (f.FindData._attr and faSysFile) = 0 then
  210. if attr and faSysFile > 0 then exit;
  211. AttrsOk := true;
  212. end else
  213. begin
  214. name :='';
  215. result := 18;
  216. end;
  217. end;
  218. end;
  219. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  220. var
  221. SystemEncodedPath: RawByteString;
  222. path0 : string;
  223. p : longint;
  224. begin
  225. IF path = '' then
  226. begin
  227. result := 18;
  228. exit;
  229. end;
  230. SystemEncodedPath := ToSingleByteEncodedFileName(Path);
  231. Rslt.FindData._attr := attr;
  232. p := length (SystemEncodedPath);
  233. while (p > 0) and (not (SystemEncodedPath[p] in AllowDirectorySeparators)) do
  234. dec (p);
  235. if p > 0 then
  236. begin
  237. Rslt.FindData._mask := copy (SystemEncodedPath,p+1,high (longint));
  238. Rslt.FindData._dir := copy (SystemEncodedPath,1,p);
  239. end else
  240. begin
  241. Rslt.FindData._mask := SystemEncodedPath;
  242. Rslt.FindData._dir := GetCurrentDir;
  243. if (Rslt.FindData._dir[length(Rslt.FindData._dir)] <> '/') and
  244. (Rslt.FindData._dir[length(Rslt.FindData._dir)] <> '\') then
  245. Rslt.FindData._dir := Rslt.FindData._dir + '/';
  246. end;
  247. if Rslt.FindData._mask = '*' then Rslt.FindData._mask := '';
  248. if Rslt.FindData._mask = '*.*' then Rslt.FindData._mask := '';
  249. //writeln (stderr,'mask: "',Rslt._mask,'" dir:"',path0,'"');
  250. Pdirent(Rslt.FindData.DirP) := opendir (pchar(Rslt.FindData._dir));
  251. if Rslt.FindData.DirP = nil then
  252. result := 18
  253. else begin
  254. Rslt.FindData.Magic := $AD02;
  255. result := findnext (Rslt);
  256. end;
  257. end;
  258. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  259. var attrsOk : boolean;
  260. begin
  261. if Rslt.FindData.Magic <> $AD02 then
  262. begin
  263. result := 18;
  264. exit;
  265. end;
  266. result:=0;
  267. repeat
  268. Pdirent(Rslt.FindData.EntryP) := readdir (Pdirent(Rslt.FindData.DirP));
  269. if Rslt.FindData.EntryP = nil then
  270. result := 18
  271. else
  272. result := find_setfields (Rslt,attrsOk,Name);
  273. if (result = 0) and (attrsOk) then
  274. begin
  275. if Rslt.FindData._mask = #0 then exit;
  276. if fnmatch(@Rslt.FindData._mask[1],Pdirent(Rslt.FindData.EntryP)^.d_name,FNM_CASEFOLD) = 0 then
  277. exit;
  278. end;
  279. until result <> 0;
  280. end;
  281. Procedure InternalFindClose (var Handle: THandle; var FindData: TFindData);
  282. begin
  283. if FindData.Magic <> $AD02 then exit;
  284. doserror:=0;
  285. closedir (Pdirent(FindData.DirP));
  286. FillChar (FindData,sizeof(FindData),0);
  287. end;
  288. Function FileGetDate (Handle : THandle) : Longint;
  289. Var Info : TStat;
  290. _PTM : PTM;
  291. begin
  292. If Fpfstat(Handle,Info) <> 0 then
  293. Result:=-1
  294. else
  295. begin
  296. _PTM := localtime (Info.st_mtim.tv_sec);
  297. IF _PTM = NIL THEN
  298. exit(-1)
  299. else
  300. with _PTM^ do
  301. Result:=DateTimeToFileDate(EncodeDate(tm_year+1900,tm_mon+1,tm_mday)+EncodeTime(tm_hour,tm_min,tm_sec,0));
  302. end;
  303. end;
  304. Function FileSetDate (Handle : THandle; Age : Longint) : Longint;
  305. Begin
  306. {dont know how to do that, utime needs filename}
  307. result := -1;
  308. end;
  309. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  310. Var Info : TStat;
  311. SystemFileName: RawByteString;
  312. begin
  313. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  314. If Fpstat (pchar(SystemFileName),Info) <> 0 then
  315. Result:=-1
  316. Else
  317. Result := (Info.st_mode shr 16) and $ffff;
  318. end;
  319. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  320. var
  321. StatBuf : TStat;
  322. newMode : longint;
  323. SystemFileName: RawByteString;
  324. begin
  325. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  326. if Fpstat (pchar(SystemFilename),StatBuf) = 0 then
  327. begin
  328. {what should i do here ?
  329. only support sysutils-standard attributes or also support the extensions defined
  330. only for netware libc ?
  331. For now i allow the complete attributes if the bit faSetNetwareAttrs is set. Otherwise
  332. only the standard attributes can be modified}
  333. if attr and faSetNetwareAttrs > 0 then
  334. begin
  335. newmode := ((attr shl 16) and $ffff0000) or M_A_BITS_SIGNIFICANT;
  336. end else
  337. begin
  338. attr := (attr and $2f) shl 16;
  339. newmode := StatBuf.st_mode and ($ffff0000-M_A_RDONLY-M_A_HIDDEN- M_A_SYSTEM-M_A_SUBDIR-M_A_ARCH);
  340. newmode := newmode or (attr shl 16) or M_A_BITS_SIGNIFICANT;
  341. end;
  342. if Fpchmod (pchar(SystemFilename),newMode) < 0 then
  343. result := ___errno^ else
  344. result := 0;
  345. end else
  346. result := ___errno^;
  347. end;
  348. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  349. var
  350. SystemFileName: RawByteString;
  351. begin
  352. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  353. Result:= (libc.UnLink (pchar(SystemFileName)) = 0);
  354. end;
  355. Function RenameFile (Const OldName, NewName : String) : Boolean;
  356. var
  357. OldSystemFileName, NewSystemFileName: RawByteString;
  358. begin
  359. OldSystemFileName:=ToSingleByteFileSystemEncodedFileName(OldName);
  360. NewSystemFileName:=ToSingleByteFileSystemEncodedFileName(NewName);
  361. RenameFile:=(libc.rename(pchar(OldSystemFileName),pchar(NewSystemFileName)) = 0);
  362. end;
  363. {****************************************************************************
  364. Disk Functions
  365. ****************************************************************************}
  366. {
  367. The Diskfree and Disksize functions need a file on the specified drive, since this
  368. is required for the statfs system call.
  369. These filenames are set in drivestr[0..26], and have been preset to :
  370. 0 - '.' (default drive - hence current dir is ok.)
  371. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  372. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  373. 3 - '/' (C: equivalent of dos is the root partition)
  374. 4..26 (can be set by you're own applications)
  375. ! Use AddDisk() to Add new drives !
  376. They both return -1 when a failure occurs.
  377. }
  378. Const
  379. FixDriveStr : array[0..3] of pchar=(
  380. '.',
  381. 'a:.',
  382. 'b:.',
  383. 'sys:/'
  384. );
  385. var
  386. Drives : byte;
  387. DriveStr : array[4..26] of pchar;
  388. Procedure AddDisk(const path:string);
  389. begin
  390. if not (DriveStr[Drives]=nil) then
  391. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  392. GetMem(DriveStr[Drives],length(Path)+1);
  393. StrPCopy(DriveStr[Drives],path);
  394. inc(Drives);
  395. if Drives>26 then
  396. Drives:=4;
  397. end;
  398. Function DiskFree(Drive: Byte): int64;
  399. //var fs : Tstatfs;
  400. Begin
  401. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  402. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  403. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  404. else
  405. Diskfree:=-1;}
  406. DiskFree := -1;
  407. ConsolePrintf ('warning: fpc sysutils.diskfree not implemented'#13#10);
  408. {$warning DiskFree not implemented (does it make sense ?) }
  409. End;
  410. Function DiskSize(Drive: Byte): int64;
  411. //var fs : statfs;
  412. Begin
  413. { if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  414. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  415. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  416. else
  417. DiskSize:=-1;}
  418. DiskSize := -1;
  419. ConsolePrintf ('warning: fpc sysutils.disksize not implemented'#13#10);
  420. {$warning DiskSize not implemented (does it make sense ?) }
  421. End;
  422. function DirectoryExists (const Directory: RawByteString): boolean;
  423. var
  424. Info : TStat;
  425. SystemFileName: RawByteString;
  426. begin
  427. SystemFileName:=ToSingleByteFileSystemEncodedFileName(Directory);
  428. If Fpstat (pchar(SystemFileName),Info) <> 0 then
  429. exit(false)
  430. else
  431. Exit ((Info.st_mode and M_A_SUBDIR) <> 0);
  432. end;
  433. {****************************************************************************
  434. Misc Functions
  435. ****************************************************************************}
  436. procedure SysBeep;
  437. begin
  438. RingBell;
  439. end;
  440. {****************************************************************************
  441. Locale Functions
  442. ****************************************************************************}
  443. Procedure GetLocalTime(var SystemTime: TSystemTime);
  444. var t : TTime_t;
  445. tm: Ttm;
  446. begin
  447. libc.time(t);
  448. libc.localtime_r(t,tm);
  449. with SystemTime do
  450. begin
  451. Hour := tm.tm_hour;
  452. Minute := tm.tm_min;
  453. Second := tm.tm_sec;
  454. MilliSecond := 0;
  455. Day := tm.tm_mday;
  456. Month := tm.tm_mon+1;
  457. Year := tm.tm_year+1900;
  458. end;
  459. end;
  460. Procedure InitAnsi;
  461. Var i : longint;
  462. begin
  463. { Fill table entries 0 to 127 }
  464. for i := 0 to 96 do
  465. UpperCaseTable[i] := chr(i);
  466. for i := 97 to 122 do
  467. UpperCaseTable[i] := chr(i - 32);
  468. for i := 123 to 191 do
  469. UpperCaseTable[i] := chr(i);
  470. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  471. for i := 0 to 64 do
  472. LowerCaseTable[i] := chr(i);
  473. for i := 65 to 90 do
  474. LowerCaseTable[i] := chr(i + 32);
  475. for i := 91 to 191 do
  476. LowerCaseTable[i] := chr(i);
  477. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  478. end;
  479. Procedure InitInternational;
  480. begin
  481. InitInternationalGeneric;
  482. InitAnsi;
  483. end;
  484. function SysErrorMessage(ErrorCode: Integer): String;
  485. begin
  486. Result:=''; // only found perror that prints the message
  487. end;
  488. {****************************************************************************
  489. OS utility functions
  490. ****************************************************************************}
  491. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  492. begin
  493. Result:=libc.getenv(PChar(EnvVar));
  494. end;
  495. Function GetEnvironmentVariableCount : Integer;
  496. begin
  497. Result:=FPCCountEnvVar(EnvP);
  498. end;
  499. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  500. begin
  501. Result:=FPCGetEnvStrFromP(Envp,Index);
  502. end;
  503. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  504. var
  505. params:array of AnsiString;
  506. count,i: longint;
  507. Buf : pchar;
  508. p : pchar;
  509. CLine: AnsiString;
  510. begin
  511. cLine := ComLine;
  512. buf:=pchar(CLine);
  513. count:=0;
  514. while(buf^<>#0) do
  515. begin
  516. while (buf^ in [' ',#9,#10]) do
  517. inc(buf);
  518. inc(count);
  519. while not (buf^ in [' ',#0,#9,#10]) do
  520. inc(buf);
  521. end;
  522. i := 0;
  523. setlength(params,count);
  524. buf:=pchar(CLine);
  525. while(buf^<>#0) do
  526. begin
  527. while (buf^ in [' ',#9,#10]) do
  528. inc(buf);
  529. p := buf;
  530. while not (buf^ in [' ',#0,#9,#10]) do
  531. inc(buf);
  532. if buf^ <> #0 then
  533. begin
  534. buf^ := #0;
  535. inc(buf);
  536. end;
  537. params[i]:=p;
  538. inc(i);
  539. end;
  540. result := ExecuteProcess (Path, params);
  541. end;
  542. Function GetLastOSError : Integer;
  543. begin
  544. Result:=Integer(GetLastError);
  545. end;
  546. {******************************************************************************
  547. --- Exec ---
  548. ******************************************************************************}
  549. const maxargs=256;
  550. function ExecuteProcess (const Path: AnsiString;
  551. const ComLine: array of AnsiString;Flags:TExecuteFlags=[]): integer;
  552. var c : comstr;
  553. i : integer;
  554. args : array[0..maxargs+1] of pchar;
  555. arg0 : string;
  556. numargs,wstat : integer;
  557. Wiring : TWiring;
  558. newPath : string;
  559. e : EOSError;
  560. begin
  561. if pos ('.',path) = 0 then
  562. arg0 := fexpand(path+'.nlm')
  563. else
  564. arg0 := fexpand (path);
  565. args[0] := pchar(arg0);
  566. numargs := 0;
  567. for I := 0 to High (ComLine) do
  568. if numargs < maxargs then
  569. begin
  570. inc(numargs);
  571. args[numargs] := pchar(ComLine[i]);
  572. end;
  573. args[numargs+1] := nil;
  574. Wiring.infd := StdInputHandle; //textrec(Stdin).Handle;
  575. Wiring.outfd:= textrec(stdout).Handle;
  576. Wiring.errfd:= textrec(stderr).Handle;
  577. i := procve(args[0],
  578. PROC_CURRENT_SPACE+PROC_INHERIT_CWD,
  579. envP, // const char * env[] If passed as NULL, the child process inherits the parent.s environment at the time of the call.
  580. @Wiring, // wiring_t *wiring, Pass NULL to inherit system defaults for wiring.
  581. nil, // struct fd_set *fds, Not currently implemented. Pass in NULL.
  582. nil, // void *appdata, Not currently implemented. Pass in NULL.
  583. 0, // size_t appdata_size, Not currently implemented. Pass in 0
  584. nil, // void *reserved, Reserved. Pass NULL.
  585. @args); // const char *argv[]
  586. if i <> -1 then
  587. begin
  588. Fpwaitpid(i,@wstat,0);
  589. result := wstat;
  590. end else
  591. begin
  592. e:=EOSError.CreateFmt(SExecuteProcessFailed,[arg0,___errno^]);
  593. e.ErrorCode:=___errno^;
  594. raise e;
  595. end;
  596. end;
  597. procedure Sleep(milliseconds: Cardinal);
  598. begin
  599. libc._delay (milliseconds);
  600. end;
  601. {****************************************************************************
  602. Initialization code
  603. ****************************************************************************}
  604. Initialization
  605. InitExceptions; { Initialize exceptions. OS independent }
  606. InitInternational; { Initialize internationalization settings }
  607. Finalization
  608. DoneExceptions;
  609. end.