unix.pp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. Uses UnixUtil,BaseUnix;
  16. {// i ostypes.inc}
  17. { Get Types and Constants }
  18. {$i sysconst.inc}
  19. {$i systypes.inc}
  20. { Get System call numbers and error-numbers}
  21. {$i sysnr.inc}
  22. {$i errno.inc}
  23. {$I signal.inc}
  24. {$i ostypes.inc}
  25. {********************
  26. Process
  27. ********************}
  28. const
  29. { For getting/setting priority }
  30. Prio_Process = 0;
  31. Prio_PGrp = 1;
  32. Prio_User = 2;
  33. {$ifdef Solaris}
  34. WNOHANG = $100;
  35. WUNTRACED = $4;
  36. {$ELSE}
  37. WNOHANG = $1;
  38. WUNTRACED = $2;
  39. __WCLONE = $80000000;
  40. {$ENDIF}
  41. {********************
  42. File
  43. ********************}
  44. Const
  45. P_IN = 1;
  46. P_OUT = 2;
  47. Const
  48. LOCK_SH = 1;
  49. LOCK_EX = 2;
  50. LOCK_UN = 8;
  51. LOCK_NB = 4;
  52. Type
  53. Tpipe = array[1..2] of longint;
  54. pglob = ^tglob;
  55. tglob = record
  56. name : pchar;
  57. next : pglob;
  58. end;
  59. const
  60. { For File control mechanism }
  61. F_GetFd = 1;
  62. F_SetFd = 2;
  63. F_GetFl = 3;
  64. F_SetFl = 4;
  65. {$ifdef Solaris}
  66. F_DupFd = 0;
  67. F_Dup2Fd = 9;
  68. F_GetOwn = 23;
  69. F_SetOwn = 24;
  70. F_GetLk = 14;
  71. F_SetLk = 6;
  72. F_SetLkW = 7;
  73. F_FreeSp = 11;
  74. {$else}
  75. F_GetLk = 5;
  76. F_SetLk = 6;
  77. F_SetLkW = 7;
  78. F_SetOwn = 8;
  79. F_GetOwn = 9;
  80. {$endif}
  81. {********************
  82. IOCtl(TermIOS)
  83. ********************}
  84. {Is too freebsd/Linux specific}
  85. {$I termios.inc}
  86. {******************************************************************************
  87. Procedure/Functions
  88. ******************************************************************************}
  89. {**************************
  90. Time/Date Handling
  91. ***************************}
  92. var
  93. tzdaylight : boolean;
  94. tzname : array[boolean] of pchar;
  95. { timezone support }
  96. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  97. procedure GetLocalTimezone(timer:longint);
  98. procedure ReadTimezoneFile(fn:string);
  99. function GetTimezoneFile:string;
  100. Function GetEpochTime: longint;
  101. procedure GetTime(var hour,min,sec,msec,usec:word);
  102. procedure GetTime(var hour,min,sec,sec100:word);
  103. procedure GetTime(var hour,min,sec:word);
  104. Procedure GetDate(Var Year,Month,Day:Word);
  105. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  106. function SetTime(Hour,Min,Sec:word) : Boolean;
  107. function SetDate(Year,Month,Day:Word) : Boolean;
  108. function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  109. {**************************
  110. Process Handling
  111. ***************************}
  112. function CreateShellArgV(const prog:string):ppchar;
  113. function CreateShellArgV(const prog:Ansistring):ppchar;
  114. //Procedure Execve(Path: pathstr;args:ppchar;ep:ppchar);
  115. //Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  116. //Procedure Execve(path: pchar;args:ppchar;ep:ppchar);
  117. Function Execv(const path:pathstr;args:ppchar):cint;
  118. Function Execv(const path: AnsiString;args:ppchar):cint;
  119. Function Execvp(Path: Pathstr;Args:ppchar;Ep:ppchar):cint;
  120. Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  121. Function Execl(const Todo: String):cint;
  122. Function Execl(const Todo: Ansistring):cint;
  123. Function Execle(Todo: String;Ep:ppchar):cint;
  124. Function Execle(Todo: AnsiString;Ep:ppchar):cint;
  125. Function Execlp(Todo: string;Ep:ppchar):cint;
  126. Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
  127. Function Shell(const Command:String):Longint;
  128. Function Shell(const Command:AnsiString):Longint;
  129. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  130. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  131. Function WaitProcess(Pid:longint):cint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  132. Function WIFSTOPPED(Status: Integer): Boolean;
  133. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  134. Function W_STOPCODE(Signal: Integer): Integer;
  135. {**************************
  136. File Handling
  137. ***************************}
  138. Function fdFlush (fd : Longint) : Boolean;
  139. Function Flock (fd,mode : longint) : boolean;
  140. Function Flock (var T : text;mode : longint) : boolean;
  141. Function Flock (var F : File;mode : longint) : boolean;
  142. Function StatFS(Path:Pathstr;Var Info:tstatfs):Boolean;
  143. Function StatFS(Fd: Longint;Var Info:tstatfs):Boolean;
  144. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  145. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  146. {**************************
  147. Directory Handling
  148. ***************************}
  149. procedure SeekDir(p:pdir;off:longint);
  150. function TellDir(p:pdir):longint;
  151. {**************************
  152. Pipe/Fifo/Stream
  153. ***************************}
  154. Function AssignPipe(var pipe_in,pipe_out:longint):cint;
  155. Function AssignPipe(var pipe_in,pipe_out:text):cint;
  156. Function AssignPipe(var pipe_in,pipe_out:file):cint;
  157. Function PClose(Var F:text) : cint;
  158. Function PClose(Var F:file) : cint;
  159. Function POpen(var F:text;const Prog:String;rw:char):cint;
  160. Function POpen(var F:file;const Prog:String;rw:char):cint;
  161. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
  162. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): cint;
  163. {$ifndef BSD}
  164. Function GetDomainName:String;
  165. Function GetHostName:String;
  166. {$endif}
  167. {**************************
  168. IOCtl/Termios Functions
  169. ***************************}
  170. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  171. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  172. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  173. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  174. Procedure CFMakeRaw(var tios:TermIOS);
  175. Function TCSendBreak(fd,duration:longint):boolean;
  176. Function TCSetPGrp(fd,id:longint):boolean;
  177. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  178. Function TCFlush(fd,qsel:longint):boolean;
  179. Function TCDrain(fd:longint):boolean;
  180. Function TCFlow(fd,act:longint):boolean;
  181. Function IsATTY(Handle:Longint):Boolean;
  182. Function IsATTY(var f:text):Boolean;
  183. function TTYname(Handle:Longint):string;
  184. function TTYname(var F:Text):string;
  185. {**************************
  186. Memory functions
  187. ***************************}
  188. const
  189. PROT_READ = $1; { page can be read }
  190. PROT_WRITE = $2; { page can be written }
  191. PROT_EXEC = $4; { page can be executed }
  192. PROT_NONE = $0; { page can not be accessed }
  193. MAP_SHARED = $1; { Share changes }
  194. // MAP_PRIVATE = $2; { Changes are private }
  195. MAP_TYPE = $f; { Mask for type of mapping }
  196. MAP_FIXED = $10; { Interpret addr exactly }
  197. // MAP_ANONYMOUS = $20; { don't use a file }
  198. MAP_GROWSDOWN = $100; { stack-like segment }
  199. MAP_DENYWRITE = $800; { ETXTBSY }
  200. MAP_EXECUTABLE = $1000; { mark it as an executable }
  201. MAP_LOCKED = $2000; { pages are locked }
  202. MAP_NORESERVE = $4000; { don't check for reservations }
  203. {**************************
  204. Utility functions
  205. ***************************}
  206. Function Octal(l:longint):longint;
  207. Function FExpand(Const Path: PathStr):PathStr;
  208. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  209. Function Glob(Const path:pathstr):pglob;
  210. Procedure Globfree(var p:pglob);
  211. {Filedescriptorsets}
  212. {Stat.Mode Types}
  213. procedure SigRaise(sig:integer);
  214. {******************************************************************************
  215. Implementation
  216. ******************************************************************************}
  217. {$i unxsysch.inc}
  218. Implementation
  219. Uses Strings;
  220. {$i syscallh.inc}
  221. {$i ossysch.inc}
  222. {$i unxsysc.inc}
  223. { Get the definitions of textrec and filerec }
  224. {$i textrec.inc}
  225. {$i filerec.inc}
  226. { Raw System calls are in Syscalls.inc}
  227. {$ifdef Linux} // Linux has more "oldlinux" compability.
  228. {$i sysc11.inc}
  229. {$else}
  230. {$i syscalls.inc}
  231. {$endif}
  232. {$i unixsysc.inc} {Syscalls only used in unit Unix/Linux}
  233. Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
  234. {******************************************************************************
  235. Process related calls
  236. ******************************************************************************}
  237. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  238. Function WaitProcess(Pid:longint):cint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  239. var ret,r,s : LongInt;
  240. begin
  241. s:=$7F00;
  242. repeat
  243. r:=fpWaitPid(Pid,@s,0);
  244. if (r=-1) and (fpgeterrno=ESysEIntr) Then
  245. r:=0;
  246. until (r<>-1);
  247. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  248. WaitProcess:=-1 // return -1 to indicate an error. fpwaitpid updated it.
  249. else
  250. begin
  251. {$ifndef Solaris}
  252. { at least correct for Linux and Darwin (JM) }
  253. if (s and $7F)=0 then // Only this is a valid returncode
  254. {$else}
  255. if (s and $FF)=0 then // Only this is a valid returncode
  256. {$endif}
  257. WaitProcess:=s shr 8
  258. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  259. WaitProcess:=-s // normal case
  260. else
  261. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  262. end;
  263. end;
  264. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  265. {
  266. Create an argv which executes a command in a shell using /bin/sh -c
  267. }
  268. const Shell = '/bin/sh'#0'-c'#0;
  269. var
  270. pp,p : ppchar;
  271. // temp : string; !! Never pass a local var back!!
  272. begin
  273. getmem(pp,4*4);
  274. p:=pp;
  275. p^:=@Shell[1];
  276. inc(p);
  277. p^:=@Shell[9];
  278. inc(p);
  279. getmem(p^,len+1);
  280. move(cmd^,p^^,len);
  281. pchar(p^)[len]:=#0;
  282. inc(p);
  283. p^:=Nil;
  284. InternalCreateShellArgV:=pp;
  285. end;
  286. function CreateShellArgV(const prog:string):ppchar;
  287. begin
  288. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  289. end;
  290. function CreateShellArgV(const prog:Ansistring):ppchar;
  291. {
  292. Create an argv which executes a command in a shell using /bin/sh -c
  293. using a AnsiString;
  294. }
  295. begin
  296. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  297. end;
  298. procedure FreeShellArgV(p:ppchar);
  299. begin
  300. if (p<>nil) then begin
  301. freemem(p[2]);
  302. freemem(p);
  303. end;
  304. end;
  305. Function Execv(const path: AnsiString;args:ppchar):cint;
  306. {
  307. Overloaded ansistring version.
  308. }
  309. begin
  310. Execv:=fpExecVe(Path,Args,envp);
  311. end;
  312. Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  313. {
  314. Overloaded ansistring version
  315. }
  316. var
  317. thepath : Ansistring;
  318. begin
  319. if path[1]<>'/' then
  320. begin
  321. Thepath:=strpas(fpgetenv('PATH'));
  322. if thepath='' then
  323. thepath:='.';
  324. Path:=FSearch(path,thepath)
  325. end
  326. else
  327. Path:='';
  328. if Path='' then
  329. Begin
  330. fpsetErrno(ESysEnoEnt);
  331. Exit(-1);
  332. end
  333. else
  334. Execvp:=fpExecve(Path,args,ep);
  335. end;
  336. Function Execv(const path:pathstr;args:ppchar):cint;
  337. {
  338. Replaces the current program by the program specified in path,
  339. arguments in args are passed to Execve.
  340. the current environment is passed on.
  341. }
  342. begin
  343. Execv:=fpExecve(path,args,envp);
  344. end;
  345. Function Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar):cint;
  346. {
  347. This does the same as Execve, only it searches the PATH environment
  348. for the place of the Executable, except when Path starts with a slash.
  349. if the PATH environment variable is unavailable, the path is set to '.'
  350. }
  351. var
  352. thepath : string;
  353. begin
  354. if path[1]<>'/' then
  355. begin
  356. Thepath:=strpas(fpgetenv('PATH'));
  357. if thepath='' then
  358. thepath:='.';
  359. Path:=FSearch(path,thepath)
  360. end
  361. else
  362. Path:='';
  363. if Path='' then
  364. Begin
  365. fpsetErrno(ESysEnoEnt);
  366. Exit(-1);
  367. end
  368. else
  369. execvp:=fpExecve(Path,args,ep);
  370. end;
  371. Function Execle(Todo:string;Ep:ppchar):cint;
  372. {
  373. This procedure takes the string 'Todo', parses it for command and
  374. command options, and Executes the command with the given options.
  375. The string 'Todo' shoud be of the form 'command options', options
  376. separated by commas.
  377. the PATH environment is not searched for 'command'.
  378. The specified environment(in 'ep') is passed on to command
  379. }
  380. var
  381. p : ppchar;
  382. begin
  383. p:=StringToPPChar(ToDo);
  384. if (p=nil) or (p^=nil) then
  385. Begin
  386. fpsetErrno(ESysEnoEnt);
  387. Exit(-1);
  388. end
  389. else
  390. execle:=fpExecVE(p^,p,EP);
  391. end;
  392. function Execle(Todo:AnsiString;Ep:ppchar):cint;
  393. {
  394. This procedure takes the string 'Todo', parses it for command and
  395. command options, and Executes the command with the given options.
  396. The string 'Todo' shoud be of the form 'command options', options
  397. separated by commas.
  398. the PATH environment is not searched for 'command'.
  399. The specified environment(in 'ep') is passed on to command
  400. }
  401. var
  402. p : ppchar;
  403. begin
  404. p:=StringToPPChar(ToDo);
  405. if (p=nil) or (p^=nil) then
  406. Begin
  407. fpsetErrno(ESysEnoEnt);
  408. Exit(-1);
  409. end;
  410. ExecLe:=fpExecVE(p^,p,EP);
  411. end;
  412. Function Execl(const Todo:string):cint;
  413. {
  414. This procedure takes the string 'Todo', parses it for command and
  415. command options, and Executes the command with the given options.
  416. The string 'Todo' shoud be of the form 'command options', options
  417. separated by commas.
  418. the PATH environment is not searched for 'command'.
  419. The current environment is passed on to command
  420. }
  421. begin
  422. Execl:=ExecLE(ToDo,EnvP);
  423. end;
  424. Function Execlp(Todo:string;Ep:ppchar):cint;
  425. {
  426. This procedure takes the string 'Todo', parses it for command and
  427. command options, and Executes the command with the given options.
  428. The string 'Todo' shoud be of the form 'command options', options
  429. separated by commas.
  430. the PATH environment is searched for 'command'.
  431. The specified environment (in 'ep') is passed on to command
  432. }
  433. var
  434. p : ppchar;
  435. begin
  436. p:=StringToPPchar(todo);
  437. if (p=nil) or (p^=nil) then
  438. Begin
  439. fpsetErrno(ESysEnoEnt);
  440. Exit(-1);
  441. end;
  442. Execlp:=ExecVP(StrPas(p^),p,EP);
  443. end;
  444. Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
  445. {
  446. Overloaded ansistring version.
  447. }
  448. var
  449. p : ppchar;
  450. begin
  451. p:=StringToPPchar(todo);
  452. if (p=nil) or (p^=nil) then
  453. Begin
  454. fpsetErrno(ESysEnoEnt);
  455. Exit(-1);
  456. end;
  457. execlp:=ExecVP(StrPas(p^),p,EP);
  458. end;
  459. Function Shell(const Command:String):cint;
  460. {
  461. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  462. The current environment is passed to the shell.
  463. It waits for the shell to exit, and returns its exit status.
  464. If the Exec call failed exit status 127 is reported.
  465. }
  466. { Changed the structure:
  467. - the previous version returns an undefinied value if fork fails
  468. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  469. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  470. - ShellArgs are now released
  471. - The Old CreateShellArg gives back pointers to a local var
  472. }
  473. var
  474. p : ppchar;
  475. pid : longint;
  476. begin
  477. p:=CreateShellArgv(command);
  478. pid:=fpfork;
  479. if pid=0 then // We are in the Child
  480. begin
  481. {This is the child.}
  482. fpExecve(p^,p,envp);
  483. fpExit(127); // was Exit(127)
  484. end
  485. else if (pid<>-1) then // Successfull started
  486. Shell:=WaitProcess(pid)
  487. else // no success
  488. Shell:=-1; // indicate an error
  489. FreeShellArgV(p);
  490. end;
  491. Function Shell(const Command:AnsiString):cint;
  492. {
  493. AnsiString version of Shell
  494. }
  495. var
  496. p : ppchar;
  497. pid : longint;
  498. begin { Changes as above }
  499. p:=CreateShellArgv(command);
  500. pid:=fpfork;
  501. if pid=0 then // We are in the Child
  502. begin
  503. fpExecve(p^,p,envp);
  504. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  505. end
  506. else if (pid<>-1) then // Successfull started
  507. Shell:=WaitProcess(pid)
  508. else // no success
  509. Shell:=-1;
  510. FreeShellArgV(p);
  511. end;
  512. Function WIFSTOPPED(Status: Integer): Boolean;
  513. begin
  514. WIFSTOPPED:=((Status and $FF)=$7F);
  515. end;
  516. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  517. begin
  518. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  519. end;
  520. Function W_STOPCODE(Signal: Integer): Integer;
  521. begin
  522. W_STOPCODE:=(Signal shl 8) or $7F;
  523. end;
  524. {******************************************************************************
  525. Date and Time related calls
  526. ******************************************************************************}
  527. Function GetEpochTime: longint;
  528. {
  529. Get the number of seconds since 00:00, January 1 1970, GMT
  530. the time NOT corrected any way
  531. }
  532. begin
  533. GetEpochTime:=fptime;
  534. end;
  535. procedure GetTime(var hour,min,sec,msec,usec:word);
  536. {
  537. Gets the current time, adjusted to local time
  538. }
  539. var
  540. year,day,month:Word;
  541. tz:timeval;
  542. begin
  543. fpgettimeofday(@tz,nil);
  544. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  545. msec:=tz.tv_usec div 1000;
  546. usec:=tz.tv_usec mod 1000;
  547. end;
  548. procedure GetTime(var hour,min,sec,sec100:word);
  549. {
  550. Gets the current time, adjusted to local time
  551. }
  552. var
  553. usec : word;
  554. begin
  555. gettime(hour,min,sec,sec100,usec);
  556. sec100:=sec100 div 10;
  557. end;
  558. Procedure GetTime(Var Hour,Min,Sec:Word);
  559. {
  560. Gets the current time, adjusted to local time
  561. }
  562. var
  563. msec,usec : Word;
  564. Begin
  565. gettime(hour,min,sec,msec,usec);
  566. End;
  567. Procedure GetDate(Var Year,Month,Day:Word);
  568. {
  569. Gets the current date, adjusted to local time
  570. }
  571. var
  572. hour,minute,second : word;
  573. Begin
  574. EpochToLocal(fptime,year,month,day,hour,minute,second);
  575. End;
  576. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  577. {
  578. Gets the current date, adjusted to local time
  579. }
  580. Begin
  581. EpochToLocal(fptime,year,month,day,hour,minute,second);
  582. End;
  583. {$ifndef BSD}
  584. {$ifdef linux}
  585. Function stime (t : longint) : Boolean;
  586. begin
  587. stime:=do_SysCall(Syscall_nr_stime,longint(@t))=0;
  588. end;
  589. {$endif}
  590. {$endif}
  591. {$ifdef BSD}
  592. Function stime (t : longint) : Boolean;
  593. begin
  594. end;
  595. {$endif}
  596. Function SetTime(Hour,Min,Sec:word) : boolean;
  597. var
  598. Year, Month, Day : Word;
  599. begin
  600. GetDate (Year, Month, Day);
  601. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  602. end;
  603. Function SetDate(Year,Month,Day:Word) : boolean;
  604. var
  605. Hour, Minute, Second, Sec100 : Word;
  606. begin
  607. GetTime ( Hour, Minute, Second, Sec100 );
  608. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  609. end;
  610. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  611. begin
  612. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  613. end;
  614. { Include timezone handling routines which use /usr/share/timezone info }
  615. {$i timezone.inc}
  616. {******************************************************************************
  617. FileSystem calls
  618. ******************************************************************************}
  619. Function Execl(const Todo:Ansistring):cint;
  620. {
  621. Overloaded AnsiString Version of ExecL.
  622. }
  623. begin
  624. Execl:=ExecLE(ToDo,EnvP);
  625. end;
  626. Function Flock (var T : text;mode : longint) : boolean;
  627. begin
  628. Flock:=Flock(TextRec(T).Handle,mode);
  629. end;
  630. Function Flock (var F : File;mode : longint) : boolean;
  631. begin
  632. Flock:=Flock(FileRec(F).Handle,mode);
  633. end;
  634. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  635. Var
  636. F:TfdSet;
  637. begin
  638. if textrec(t).mode=fmclosed then
  639. begin
  640. fpseterrno(ESysEBADF);
  641. exit(-1);
  642. end;
  643. FpFD_ZERO(f);
  644. fpFD_SET(textrec(T).handle,f);
  645. if textrec(T).mode=fminput then
  646. SelectText:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  647. else
  648. SelectText:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  649. end;
  650. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  651. var
  652. p : PTimeVal;
  653. tv : TimeVal;
  654. begin
  655. if TimeOut=-1 then
  656. p:=nil
  657. else
  658. begin
  659. tv.tv_Sec:=Timeout div 1000;
  660. tv.tv_Usec:=(Timeout mod 1000)*1000;
  661. p:=@tv;
  662. end;
  663. SelectText:=SelectText(T,p);
  664. end;
  665. {******************************************************************************
  666. Directory
  667. ******************************************************************************}
  668. procedure SeekDir(p:pdir;off:longint);
  669. begin
  670. if p=nil then
  671. begin
  672. fpseterrno(ESysEBADF);
  673. exit;
  674. end;
  675. {$ifndef bsd}
  676. p^.dd_nextoff:=fplseek(p^.dd_fd,off,seek_set);
  677. {$endif}
  678. p^.dd_size:=0;
  679. p^.dd_loc:=0;
  680. end;
  681. function TellDir(p:pdir):longint;
  682. begin
  683. if p=nil then
  684. begin
  685. fpseterrno(ESysEBADF);
  686. telldir:=-1;
  687. exit;
  688. end;
  689. telldir:=fplseek(p^.dd_fd,0,seek_cur)
  690. { We could try to use the nextoff field here, but on my 1.2.13
  691. kernel, this gives nothing... This may have to do with
  692. the readdir implementation of libc... I also didn't find any trace of
  693. the field in the kernel code itself, So I suspect it is an artifact of libc.
  694. Michael. }
  695. end;
  696. {******************************************************************************
  697. Pipes/Fifo
  698. ******************************************************************************}
  699. Procedure OpenPipe(var F:Text);
  700. begin
  701. case textrec(f).mode of
  702. fmoutput :
  703. if textrec(f).userdata[1]<>P_OUT then
  704. textrec(f).mode:=fmclosed;
  705. fminput :
  706. if textrec(f).userdata[1]<>P_IN then
  707. textrec(f).mode:=fmclosed;
  708. else
  709. textrec(f).mode:=fmclosed;
  710. end;
  711. end;
  712. Function IOPipe(var F:text):cint;
  713. begin
  714. IOPipe:=0;
  715. case textrec(f).mode of
  716. fmoutput :
  717. begin
  718. { first check if we need something to write, else we may
  719. get a SigPipe when Close() is called (PFV) }
  720. if textrec(f).bufpos>0 then
  721. IOPipe:=fpwrite(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  722. end;
  723. fminput : Begin
  724. textrec(f).bufend:=fpread(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  725. IOPipe:=textrec(f).bufend;
  726. End;
  727. end;
  728. textrec(f).bufpos:=0;
  729. end;
  730. Function FlushPipe(var F:Text):cint;
  731. begin
  732. FlushPipe:=0;
  733. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  734. FlushPipe:=IOPipe(f);
  735. textrec(f).bufpos:=0;
  736. end;
  737. Function ClosePipe(var F:text):cint;
  738. begin
  739. textrec(f).mode:=fmclosed;
  740. ClosePipe:=fpclose(textrec(f).handle);
  741. end;
  742. Function AssignPipe(var pipe_in,pipe_out:text):cint;
  743. {
  744. Sets up a pair of file variables, which act as a pipe. The first one can
  745. be read from, the second one can be written to.
  746. }
  747. var
  748. f_in,f_out : longint;
  749. begin
  750. if AssignPipe(f_in,f_out)=-1 then
  751. exit(-1);
  752. { Set up input }
  753. Assign(Pipe_in,'');
  754. Textrec(Pipe_in).Handle:=f_in;
  755. Textrec(Pipe_in).Mode:=fmInput;
  756. Textrec(Pipe_in).userdata[1]:=P_IN;
  757. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  758. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  759. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  760. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  761. { Set up output }
  762. Assign(Pipe_out,'');
  763. Textrec(Pipe_out).Handle:=f_out;
  764. Textrec(Pipe_out).Mode:=fmOutput;
  765. Textrec(Pipe_out).userdata[1]:=P_OUT;
  766. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  767. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  768. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  769. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  770. AssignPipe:=0;
  771. end;
  772. Function AssignPipe(var pipe_in,pipe_out:file):cint;
  773. {
  774. Sets up a pair of file variables, which act as a pipe. The first one can
  775. be read from, the second one can be written to.
  776. If the operation was unsuccesful,
  777. }
  778. var
  779. f_in,f_out : longint;
  780. begin
  781. if AssignPipe(f_in,f_out)=-1 then
  782. exit(-1);
  783. { Set up input }
  784. Assign(Pipe_in,'');
  785. Filerec(Pipe_in).Handle:=f_in;
  786. Filerec(Pipe_in).Mode:=fmInput;
  787. Filerec(Pipe_in).recsize:=1;
  788. Filerec(Pipe_in).userdata[1]:=P_IN;
  789. { Set up output }
  790. Assign(Pipe_out,'');
  791. Filerec(Pipe_out).Handle:=f_out;
  792. Filerec(Pipe_out).Mode:=fmoutput;
  793. Filerec(Pipe_out).recsize:=1;
  794. Filerec(Pipe_out).userdata[1]:=P_OUT;
  795. AssignPipe:=0;
  796. end;
  797. Function PCloseText(Var F:text):cint;
  798. {
  799. May not use @PClose due overloading
  800. }
  801. begin
  802. PCloseText:=PClose(f);
  803. end;
  804. function POpen(var F:text;const Prog:String;rw:char):cint;
  805. {
  806. Starts the program in 'Prog' and makes it's input or out put the
  807. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  808. F, will be read from stdin by the program in 'Prog'. The inverse is true
  809. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  810. read from 'f'.
  811. }
  812. var
  813. pipi,
  814. pipo : text;
  815. pid : pid_t;
  816. pl : ^cint;
  817. pp : ppchar;
  818. ret : cint;
  819. begin
  820. rw:=upcase(rw);
  821. if not (rw in ['R','W']) then
  822. begin
  823. FpSetErrno(ESysEnoent);
  824. exit(-1);
  825. end;
  826. if AssignPipe(pipi,pipo)=-1 Then
  827. Exit(-1);
  828. pid:=fpfork;
  829. if pid=-1 then
  830. begin
  831. close(pipi);
  832. close(pipo);
  833. exit(-1);
  834. end;
  835. if pid=0 then
  836. begin
  837. { We're in the child }
  838. if rw='W' then
  839. begin
  840. close(pipo);
  841. ret:=fpdup2(pipi,input);
  842. close(pipi);
  843. if ret=-1 then
  844. halt(127);
  845. end
  846. else
  847. begin
  848. close(pipi);
  849. ret:=fpdup2(pipo,output);
  850. close(pipo);
  851. if ret=-1 then
  852. halt(127);
  853. end;
  854. pp:=createshellargv(prog);
  855. fpExecve(pp^,pp,envp);
  856. halt(127);
  857. end
  858. else
  859. begin
  860. { We're in the parent }
  861. if rw='W' then
  862. begin
  863. close(pipi);
  864. f:=pipo;
  865. textrec(f).bufptr:=@textrec(f).buffer;
  866. end
  867. else
  868. begin
  869. close(pipo);
  870. f:=pipi;
  871. textrec(f).bufptr:=@textrec(f).buffer;
  872. end;
  873. {Save the process ID - needed when closing }
  874. pl:=@(textrec(f).userdata[2]);
  875. pl^:=pid;
  876. textrec(f).closefunc:=@PCloseText;
  877. end;
  878. ret:=0;
  879. end;
  880. Function POpen(var F:file;const Prog:String;rw:char):cint;
  881. {
  882. Starts the program in 'Prog' and makes it's input or out put the
  883. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  884. F, will be read from stdin by the program in 'Prog'. The inverse is true
  885. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  886. read from 'f'.
  887. }
  888. var
  889. pipi,
  890. pipo : file;
  891. pid : longint;
  892. pl : ^longint;
  893. p,pp : ppchar;
  894. temp : string[255];
  895. ret : cint;
  896. begin
  897. rw:=upcase(rw);
  898. if not (rw in ['R','W']) then
  899. begin
  900. FpSetErrno(ESysEnoent);
  901. exit(-1);
  902. end;
  903. ret:=AssignPipe(pipi,pipo);
  904. if ret=-1 then
  905. exit(-1);
  906. pid:=fpfork;
  907. if pid=-1 then
  908. begin
  909. close(pipi);
  910. close(pipo);
  911. exit(-1);
  912. end;
  913. if pid=0 then
  914. begin
  915. { We're in the child }
  916. if rw='W' then
  917. begin
  918. close(pipo);
  919. ret:=fpdup2(filerec(pipi).handle,stdinputhandle);
  920. close(pipi);
  921. if ret=-1 then
  922. halt(127);
  923. end
  924. else
  925. begin
  926. close(pipi);
  927. ret:=fpdup2(filerec(pipo).handle,stdoutputhandle);
  928. close(pipo);
  929. if ret=1 then
  930. halt(127);
  931. end;
  932. getmem(pp,sizeof(pchar)*4);
  933. temp:='/bin/sh'#0'-c'#0+prog+#0;
  934. p:=pp;
  935. p^:=@temp[1];
  936. inc(p);
  937. p^:=@temp[9];
  938. inc(p);
  939. p^:=@temp[12];
  940. inc(p);
  941. p^:=Nil;
  942. fpExecve(ansistring('/bin/sh'),pp,envp);
  943. halt(127);
  944. end
  945. else
  946. begin
  947. { We're in the parent }
  948. if rw='W' then
  949. begin
  950. close(pipi);
  951. f:=pipo;
  952. end
  953. else
  954. begin
  955. close(pipo);
  956. f:=pipi;
  957. end;
  958. {Save the process ID - needed when closing }
  959. pl:=@(filerec(f).userdata[2]);
  960. pl^:=pid;
  961. end;
  962. POpen:=0;
  963. end;
  964. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
  965. {
  966. Starts the program in 'Prog' and makes its input and output the
  967. other end of two pipes, which are the stdin and stdout of a program
  968. specified in 'Prog'.
  969. streamout can be used to write to the program, streamin can be used to read
  970. the output of the program. See the following diagram :
  971. Parent Child
  972. STreamout --> Input
  973. Streamin <-- Output
  974. Return value is the process ID of the process being spawned, or -1 in case of failure.
  975. }
  976. var
  977. pipi,
  978. pipo : text;
  979. pid : longint;
  980. pl : ^Longint;
  981. begin
  982. AssignStream:=-1;
  983. if AssignPipe(streamin,pipo)=-1 Then
  984. exit(-1);
  985. if AssignPipe(pipi,streamout)=-1 Then // shouldn't this close streamin and pipo?
  986. exit(-1);
  987. pid:=fpfork;
  988. if pid=-1 then
  989. begin
  990. close(pipi);
  991. close(pipo);
  992. close (streamin);
  993. close (streamout);
  994. exit;
  995. end;
  996. if pid=0 then
  997. begin
  998. { We're in the child }
  999. { Close what we don't need }
  1000. close(streamout);
  1001. close(streamin);
  1002. if fpdup2(pipi,input)=-1 Then
  1003. halt(127);
  1004. close(pipi);
  1005. If fpdup2(pipo,output)=-1 Then
  1006. halt (127);
  1007. close(pipo);
  1008. Execl(Prog);
  1009. halt(127);
  1010. end
  1011. else
  1012. begin
  1013. { we're in the parent}
  1014. close(pipo);
  1015. close(pipi);
  1016. {Save the process ID - needed when closing }
  1017. pl:=@(textrec(StreamIn).userdata[2]);
  1018. pl^:=pid;
  1019. textrec(StreamIn).closefunc:=@PCloseText;
  1020. {Save the process ID - needed when closing }
  1021. pl:=@(textrec(StreamOut).userdata[2]);
  1022. pl^:=pid;
  1023. textrec(StreamOut).closefunc:=@PCloseText;
  1024. AssignStream:=Pid;
  1025. end;
  1026. end;
  1027. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1028. {
  1029. Starts the program in 'prog' and makes its input, output and error output the
  1030. other end of three pipes, which are the stdin, stdout and stderr of a program
  1031. specified in 'prog'.
  1032. StreamOut can be used to write to the program, StreamIn can be used to read
  1033. the output of the program, StreamErr reads the error output of the program.
  1034. See the following diagram :
  1035. Parent Child
  1036. StreamOut --> StdIn (input)
  1037. StreamIn <-- StdOut (output)
  1038. StreamErr <-- StdErr (error output)
  1039. }
  1040. var
  1041. PipeIn, PipeOut, PipeErr: text;
  1042. pid: LongInt;
  1043. pl: ^LongInt;
  1044. begin
  1045. AssignStream := -1;
  1046. // Assign pipes
  1047. if AssignPipe(StreamIn, PipeOut)=-1 Then
  1048. Exit(-1);
  1049. If AssignPipe(StreamErr, PipeErr)=-1 Then
  1050. begin
  1051. Close(StreamIn);
  1052. Close(PipeOut);
  1053. exit(-1);
  1054. end;
  1055. if AssignPipe(PipeIn, StreamOut)=-1 Then
  1056. begin
  1057. Close(StreamIn);
  1058. Close(PipeOut);
  1059. Close(StreamErr);
  1060. Close(PipeErr);
  1061. exit(-1);
  1062. end;
  1063. // Fork
  1064. pid := fpFork;
  1065. if pid=-1 then begin
  1066. Close(StreamIn);
  1067. Close(PipeOut);
  1068. Close(StreamErr);
  1069. Close(PipeErr);
  1070. Close(PipeIn);
  1071. Close(StreamOut);
  1072. exit(-1);
  1073. end;
  1074. if pid = 0 then begin
  1075. // *** We are in the child ***
  1076. // Close what we don not need
  1077. Close(StreamOut);
  1078. Close(StreamIn);
  1079. Close(StreamErr);
  1080. // Connect pipes
  1081. if fpdup2(PipeIn, Input)=-1 Then
  1082. Halt(127);
  1083. Close(PipeIn);
  1084. if fpdup2(PipeOut, Output)=-1 Then
  1085. Halt(127);
  1086. Close(PipeOut);
  1087. if fpdup2(PipeErr, StdErr)=-1 Then
  1088. Halt(127);
  1089. Close(PipeErr);
  1090. // Execute program
  1091. Execl(Prog);
  1092. Halt(127);
  1093. end else begin
  1094. // *** We are in the parent ***
  1095. Close(PipeErr);
  1096. Close(PipeOut);
  1097. Close(PipeIn);
  1098. // Save the process ID - needed when closing
  1099. pl := @(TextRec(StreamIn).userdata[2]);
  1100. pl^ := pid;
  1101. TextRec(StreamIn).closefunc := @PCloseText;
  1102. // Save the process ID - needed when closing
  1103. pl := @(TextRec(StreamOut).userdata[2]);
  1104. pl^ := pid;
  1105. TextRec(StreamOut).closefunc := @PCloseText;
  1106. // Save the process ID - needed when closing
  1107. pl := @(TextRec(StreamErr).userdata[2]);
  1108. pl^ := pid;
  1109. TextRec(StreamErr).closefunc := @PCloseText;
  1110. AssignStream := pid;
  1111. end;
  1112. end;
  1113. {******************************************************************************
  1114. General information calls
  1115. ******************************************************************************}
  1116. {$ifndef BSD}
  1117. Function GetDomainName:String; { linux only!}
  1118. // domainname is a glibc extension.
  1119. {
  1120. Get machines domain name. Returns empty string if not set.
  1121. }
  1122. Var
  1123. Sysn : utsname;
  1124. begin
  1125. If fpUname(sysn)<>0 then
  1126. getdomainname:=''
  1127. else
  1128. getdomainname:=strpas(@Sysn.domain[0]);
  1129. end;
  1130. {$endif}
  1131. Function GetHostName:String;
  1132. {
  1133. Get machines name. Returns empty string if not set.
  1134. }
  1135. Var
  1136. Sysn : utsname;
  1137. begin
  1138. If fpuname(sysn)=-1 then
  1139. gethostname:=''
  1140. else
  1141. gethostname:=strpas(@Sysn.nodename[0]);
  1142. end;
  1143. {******************************************************************************
  1144. Signal handling calls
  1145. ******************************************************************************}
  1146. procedure SigRaise(sig:integer);
  1147. begin
  1148. fpKill(fpGetPid,Sig);
  1149. end;
  1150. {******************************************************************************
  1151. IOCtl and Termios calls
  1152. ******************************************************************************}
  1153. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1154. begin
  1155. {$ifndef BSD}
  1156. TCGetAttr:=fpIOCtl(fd,TCGETS,@tios)=0;
  1157. {$else}
  1158. TCGETAttr:=fpIoCtl(Fd,TIOCGETA,@tios)=0;
  1159. {$endif}
  1160. end;
  1161. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  1162. var
  1163. nr:longint;
  1164. begin
  1165. {$ifndef BSD}
  1166. case OptAct of
  1167. TCSANOW : nr:=TCSETS;
  1168. TCSADRAIN : nr:=TCSETSW;
  1169. TCSAFLUSH : nr:=TCSETSF;
  1170. {$else}
  1171. case OptAct of
  1172. TCSANOW : nr:=TIOCSETA;
  1173. TCSADRAIN : nr:=TIOCSETAW;
  1174. TCSAFLUSH : nr:=TIOCSETAF;
  1175. {$endif}
  1176. else
  1177. begin
  1178. fpsetErrNo(ESysEINVAL);
  1179. TCSetAttr:=false;
  1180. exit;
  1181. end;
  1182. end;
  1183. TCSetAttr:=fpIOCtl(fd,nr,@Tios)=0;
  1184. end;
  1185. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  1186. begin
  1187. {$ifndef BSD}
  1188. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1189. {$else}
  1190. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1191. {$endif}
  1192. end;
  1193. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  1194. begin
  1195. {$ifndef BSD}
  1196. CFSetISpeed(tios,speed);
  1197. {$else}
  1198. tios.c_ospeed:=speed;
  1199. {$endif}
  1200. end;
  1201. Procedure CFMakeRaw(var tios:TermIOS);
  1202. begin
  1203. {$ifndef BSD}
  1204. with tios do
  1205. begin
  1206. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1207. INLCR or IGNCR or ICRNL or IXON));
  1208. c_oflag:=c_oflag and (not OPOST);
  1209. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1210. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1211. end;
  1212. {$else}
  1213. with tios do
  1214. begin
  1215. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1216. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1217. IGNPAR));
  1218. c_iflag:=c_iflag OR IGNBRK;
  1219. c_oflag:=c_oflag and (not OPOST);
  1220. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1221. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1222. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1223. c_cc[VMIN]:=1;
  1224. c_cc[VTIME]:=0;
  1225. end;
  1226. {$endif}
  1227. end;
  1228. Function TCSendBreak(fd,duration:longint):boolean;
  1229. begin
  1230. {$ifndef BSD}
  1231. TCSendBreak:=fpIOCtl(fd,TCSBRK,pointer(duration))=0;
  1232. {$else}
  1233. TCSendBreak:=fpIOCtl(fd,TIOCSBRK,0)=0;
  1234. {$endif}
  1235. end;
  1236. Function TCSetPGrp(fd,id:longint):boolean;
  1237. begin
  1238. TCSetPGrp:=fpIOCtl(fd,TIOCSPGRP,pointer(id))=0;
  1239. end;
  1240. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1241. begin
  1242. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id)=0;
  1243. end;
  1244. Function TCDrain(fd:longint):boolean;
  1245. begin
  1246. {$ifndef BSD}
  1247. TCDrain:=fpIOCtl(fd,TCSBRK,pointer(1))=0;
  1248. {$else}
  1249. TCDrain:=fpIOCtl(fd,TIOCDRAIN,0)=0; {Should set timeout to 1 first?}
  1250. {$endif}
  1251. end;
  1252. Function TCFlow(fd,act:longint):boolean;
  1253. begin
  1254. {$ifndef BSD}
  1255. TCFlow:=fpIOCtl(fd,TCXONC,pointer(act))=0;
  1256. {$else}
  1257. case act OF
  1258. TCOOFF : TCFlow:=fpIoctl(fd,TIOCSTOP,0)=0;
  1259. TCOOn : TCFlow:=fpIOctl(Fd,TIOCStart,0)=0;
  1260. TCIOFF : {N/I}
  1261. end;
  1262. {$endif}
  1263. end;
  1264. Function TCFlush(fd,qsel:longint):boolean;
  1265. begin
  1266. {$ifndef BSD}
  1267. TCFlush:=fpIOCtl(fd,TCFLSH,pointer(qsel))=0;
  1268. {$else}
  1269. TCFlush:=fpIOCtl(fd,TIOCFLUSH,pointer(qsel))=0;
  1270. {$endif}
  1271. end;
  1272. Function IsATTY (Handle:Longint):Boolean;
  1273. {
  1274. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1275. }
  1276. var
  1277. t : Termios;
  1278. begin
  1279. IsAtty:=TCGetAttr(Handle,t);
  1280. end;
  1281. Function IsATTY(var f: text):Boolean;
  1282. {
  1283. Idem as previous, only now for text variables.
  1284. }
  1285. begin
  1286. IsATTY:=IsaTTY(textrec(f).handle);
  1287. end;
  1288. function TTYName(Handle:Longint):string;
  1289. {
  1290. Return the name of the current tty described by handle f.
  1291. returns empty string in case of an error.
  1292. }
  1293. var
  1294. mydev : dev_t;
  1295. myino : ino_t;
  1296. st : stat;
  1297. function mysearch(n:string): boolean;
  1298. {searches recursively for the device in the directory given by n,
  1299. returns true if found and sets the name of the device in ttyname}
  1300. var dirstream : pdir;
  1301. d : pdirent;
  1302. name : string;
  1303. st : stat;
  1304. begin
  1305. dirstream:=fpopendir(n);
  1306. if (dirstream=nil) then
  1307. exit(false);
  1308. d:=fpReaddir(dirstream^);
  1309. while (d<>nil) do
  1310. begin
  1311. name:=n+'/'+strpas(@(d^.d_name));
  1312. // fpstat(name,st);
  1313. if fpstat(name,st)=0 then
  1314. begin
  1315. if (fpS_ISDIR(st.st_mode)) and { if it is a directory }
  1316. (strpas(@(d^.d_name))<>'.') and { but not ., .. and fd subdirs }
  1317. (strpas(@(d^.d_name))<>'..') and
  1318. (strpas(@(d^.d_name))<>'') and
  1319. (strpas(@(d^.d_name))<>'fd') then
  1320. begin {we found a directory, search inside it}
  1321. if mysearch(name) then
  1322. begin {the device is here}
  1323. fpclosedir(dirstream^); {then don't continue searching}
  1324. mysearch:=true;
  1325. exit;
  1326. end;
  1327. end
  1328. else if (ino_t(d^.d_fileno)=myino) and (st.st_dev=mydev) then
  1329. begin
  1330. fpclosedir(dirstream^);
  1331. ttyname:=name;
  1332. mysearch:=true;
  1333. exit;
  1334. end;
  1335. end;
  1336. d:=fpReaddir(dirstream^);
  1337. end;
  1338. fpclosedir(dirstream^);
  1339. mysearch:=false;
  1340. end;
  1341. begin
  1342. TTYName:='';
  1343. fpfstat(handle,st);
  1344. if (fpgeterrno<>0) and isatty (handle) then
  1345. exit;
  1346. mydev:=st.st_dev;
  1347. myino:=st.st_ino;
  1348. mysearch('/dev');
  1349. end;
  1350. function TTYName(var F:Text):string;
  1351. {
  1352. Idem as previous, only now for text variables;
  1353. }
  1354. begin
  1355. TTYName:=TTYName(textrec(f).handle);
  1356. end;
  1357. {******************************************************************************
  1358. Utility calls
  1359. ******************************************************************************}
  1360. Function Octal(l:longint):longint;
  1361. {
  1362. Convert an octal specified number to decimal;
  1363. }
  1364. var
  1365. octnr,
  1366. oct : longint;
  1367. begin
  1368. octnr:=0;
  1369. oct:=0;
  1370. while (l>0) do
  1371. begin
  1372. oct:=oct or ((l mod 10) shl octnr);
  1373. l:=l div 10;
  1374. inc(octnr,3);
  1375. end;
  1376. Octal:=oct;
  1377. end;
  1378. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1379. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1380. {$I fexpand.inc}
  1381. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1382. {$UNDEF FPC_FEXPAND_TILDE}
  1383. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1384. {
  1385. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1386. returns an empty string if not found. Wildcards are NOT allowed.
  1387. If dirlist is empty, it is set to '.'
  1388. }
  1389. Var
  1390. NewDir : PathStr;
  1391. p1 : Longint;
  1392. Info : Stat;
  1393. Begin
  1394. {Replace ':' with ';'}
  1395. for p1:=1to length(dirlist) do
  1396. if dirlist[p1]=':' then
  1397. dirlist[p1]:=';';
  1398. {Check for WildCards}
  1399. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1400. FSearch:='' {No wildcards allowed in these things.}
  1401. Else
  1402. Begin
  1403. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1404. Repeat
  1405. p1:=Pos(';',DirList);
  1406. If p1=0 Then
  1407. p1:=255;
  1408. NewDir:=Copy(DirList,1,P1 - 1);
  1409. if NewDir[Length(NewDir)]<>'/' then
  1410. NewDir:=NewDir+'/';
  1411. NewDir:=NewDir+Path;
  1412. Delete(DirList,1,p1);
  1413. if FpStat(NewDir,Info)>=0 then
  1414. Begin
  1415. If Pos('./',NewDir)=1 Then
  1416. Delete(NewDir,1,2);
  1417. {DOS strips off an initial .\}
  1418. End
  1419. Else
  1420. NewDir:='';
  1421. Until (DirList='') or (Length(NewDir) > 0);
  1422. FSearch:=NewDir;
  1423. End;
  1424. End;
  1425. Procedure Globfree(var p : pglob);
  1426. {
  1427. Release memory occupied by pglob structure, and names in it.
  1428. sets p to nil.
  1429. }
  1430. var
  1431. temp : pglob;
  1432. begin
  1433. while assigned(p) do
  1434. begin
  1435. temp:=p^.next;
  1436. if assigned(p^.name) then
  1437. freemem(p^.name);
  1438. dispose(p);
  1439. p:=temp;
  1440. end;
  1441. end;
  1442. Function Glob(Const path:pathstr):pglob;
  1443. {
  1444. Fills a tglob structure with entries matching path,
  1445. and returns a pointer to it. Returns nil on error,
  1446. linuxerror is set accordingly.
  1447. }
  1448. var
  1449. temp,
  1450. temp2 : string[255];
  1451. thedir : pdir;
  1452. buffer : pdirent;
  1453. root,
  1454. current : pglob;
  1455. begin
  1456. { Get directory }
  1457. temp:=dirname(path);
  1458. if temp='' then
  1459. temp:='.';
  1460. temp:=temp+#0;
  1461. thedir:=fpopendir(@temp[1]);
  1462. if thedir=nil then
  1463. exit(nil);
  1464. temp:=basename(path,''); { get the pattern }
  1465. if thedir^.dd_fd<0 then
  1466. exit(nil);
  1467. {get the entries}
  1468. root:=nil;
  1469. current:=nil;
  1470. repeat
  1471. buffer:=fpreaddir(thedir^);
  1472. if buffer=nil then
  1473. break;
  1474. temp2:=strpas(@(buffer^.d_name[0]));
  1475. if fnmatch(temp,temp2) then
  1476. begin
  1477. if root=nil then
  1478. begin
  1479. new(root);
  1480. current:=root;
  1481. end
  1482. else
  1483. begin
  1484. new(current^.next);
  1485. current:=current^.next;
  1486. end;
  1487. if current=nil then
  1488. begin
  1489. fpseterrno(ESysENOMEM);
  1490. globfree(root);
  1491. break;
  1492. end;
  1493. current^.next:=nil;
  1494. getmem(current^.name,length(temp2)+1);
  1495. if current^.name=nil then
  1496. begin
  1497. fpseterrno(ESysENOMEM);
  1498. globfree(root);
  1499. break;
  1500. end;
  1501. move(buffer^.d_name[0],current^.name^,length(temp2)+1);
  1502. end;
  1503. until false;
  1504. fpclosedir(thedir^);
  1505. glob:=root;
  1506. end;
  1507. Function GetFS (var T:Text):longint;
  1508. {
  1509. Get File Descriptor of a text file.
  1510. }
  1511. begin
  1512. if textrec(t).mode=fmclosed then
  1513. exit(-1)
  1514. else
  1515. GETFS:=textrec(t).Handle
  1516. end;
  1517. Function GetFS(Var F:File):longint;
  1518. {
  1519. Get File Descriptor of an unTyped file.
  1520. }
  1521. begin
  1522. { Handle and mode are on the same place in textrec and filerec. }
  1523. if filerec(f).mode=fmclosed then
  1524. exit(-1)
  1525. else
  1526. GETFS:=filerec(f).Handle
  1527. end;
  1528. {--------------------------------
  1529. Stat.Mode Macro's
  1530. --------------------------------}
  1531. Initialization
  1532. InitLocalTime;
  1533. finalization
  1534. DoneLocalTime;
  1535. End.
  1536. {
  1537. $Log$
  1538. Revision 1.47 2003-11-14 17:30:14 marco
  1539. * weeehoo linuxerror is no more :-)
  1540. Revision 1.46 2003/11/14 16:44:48 marco
  1541. * stream functions converted to work without linuxerror
  1542. Revision 1.45 2003/11/13 18:44:06 marco
  1543. * small fi
  1544. Revision 1.44 2003/11/12 22:19:45 marco
  1545. * more linuxeror fixes
  1546. Revision 1.43 2003/11/03 09:42:28 marco
  1547. * Peter's Cardinal<->Longint fixes patch
  1548. Revision 1.42 2003/10/30 16:42:58 marco
  1549. * fixes for old syscall() convention removing
  1550. Revision 1.41 2003/10/12 19:40:43 marco
  1551. * ioctl fixes. IDE now starts, but
  1552. Revision 1.40 2003/09/29 14:36:06 peter
  1553. * fixed for stricter compiler
  1554. Revision 1.39 2003/09/27 12:51:33 peter
  1555. * fpISxxx macros renamed to C compliant fpS_ISxxx
  1556. Revision 1.38 2003/09/20 12:38:29 marco
  1557. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  1558. Revision 1.37 2003/09/17 19:07:44 marco
  1559. * more fixes for Unix<->unixutil
  1560. Revision 1.36 2003/09/17 17:30:46 marco
  1561. * Introduction of unixutil
  1562. Revision 1.35 2003/09/16 21:46:27 marco
  1563. * small fixes, checking things on linux
  1564. Revision 1.34 2003/09/16 20:52:24 marco
  1565. * small cleanups. Mostly killing of already commented code in unix etc
  1566. Revision 1.33 2003/09/16 16:13:56 marco
  1567. * fdset functions renamed to fp<posix name>
  1568. Revision 1.32 2003/09/15 20:08:49 marco
  1569. * small fixes. FreeBSD now cycles
  1570. Revision 1.31 2003/09/14 20:15:01 marco
  1571. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  1572. Revision 1.30 2003/07/08 21:23:24 peter
  1573. * sparc fixes
  1574. Revision 1.29 2003/05/30 19:58:40 marco
  1575. * Getting NetBSD/i386 to compile.
  1576. Revision 1.28 2003/05/29 19:16:16 marco
  1577. * fixed a small *BSD gotcha
  1578. Revision 1.27 2003/05/24 20:39:54 jonas
  1579. * fixed ExitCode translation in WaitProcess for Linux and Darwin (and
  1580. probably other BSD's as well)
  1581. Revision 1.26 2003/03/11 08:27:59 michael
  1582. * stringtoppchar should use tabs instead of backspace as delimiter
  1583. Revision 1.25 2002/12/18 16:50:39 marco
  1584. * Unix RTL generic parts. Linux working, *BSD will follow shortly
  1585. Revision 1.24 2002/09/07 16:01:28 peter
  1586. * old logs removed and tabs fixed
  1587. Revision 1.23 2002/08/06 13:30:46 sg
  1588. * replaced some Longints with Cardinals, to mach the C headers
  1589. * updated the termios record
  1590. Revision 1.22 2002/03/05 20:04:25 michael
  1591. + Patch from Sebastian for FCNTL call
  1592. Revision 1.21 2002/01/02 12:22:54 marco
  1593. * Removed ifdef arround getepoch.
  1594. }