unix.pp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  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. { Get Types and Constants }
  16. {$i sysconst.inc}
  17. {$i systypes.inc}
  18. { Get System call numbers and error-numbers}
  19. {$i sysnr.inc}
  20. {$i errno.inc}
  21. {$I signal.inc}
  22. var
  23. ErrNo,
  24. LinuxError : Longint;
  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. ComStr = String[255];
  60. PathStr = String[255];
  61. DirStr = String[255];
  62. NameStr = String[255];
  63. ExtStr = String[255];
  64. const
  65. { For testing access rights }
  66. R_OK = 4;
  67. W_OK = 2;
  68. X_OK = 1;
  69. F_OK = 0;
  70. { For File control mechanism }
  71. F_GetFd = 1;
  72. F_SetFd = 2;
  73. F_GetFl = 3;
  74. F_SetFl = 4;
  75. {$ifdef Solaris}
  76. F_DupFd = 0;
  77. F_Dup2Fd = 9;
  78. F_GetOwn = 23;
  79. F_SetOwn = 24;
  80. F_GetLk = 14;
  81. F_SetLk = 6;
  82. F_SetLkW = 7;
  83. F_FreeSp = 11;
  84. {$else}
  85. F_GetLk = 5;
  86. F_SetLk = 6;
  87. F_SetLkW = 7;
  88. F_SetOwn = 8;
  89. F_GetOwn = 9;
  90. {$endif}
  91. {********************
  92. IOCtl(TermIOS)
  93. ********************}
  94. {Is too freebsd/Linux specific}
  95. {$I termios.inc}
  96. {********************
  97. Info
  98. ********************}
  99. Type
  100. UTimBuf = packed record{in BSD array[0..1] of timeval, but this is
  101. backwards compatible with linux version}
  102. actime,
  103. {$ifdef BSD}
  104. uactime, {BSD Micro seconds}
  105. {$endif}
  106. modtime
  107. {$ifdef BSD}
  108. ,
  109. umodtime {BSD Micro seconds}
  110. {$endif}
  111. : longint;
  112. end;
  113. UTimeBuf=UTimBuf;
  114. TUTimeBuf=UTimeBuf;
  115. PUTimeBuf=^UTimeBuf;
  116. TSysinfo = packed record
  117. uptime : longint;
  118. loads : array[1..3] of longint;
  119. totalram,
  120. freeram,
  121. sharedram,
  122. bufferram,
  123. totalswap,
  124. freeswap : longint;
  125. procs : integer;
  126. s : string[18];
  127. end;
  128. PSysInfo = ^TSysInfo;
  129. {******************************************************************************
  130. Procedure/Functions
  131. ******************************************************************************}
  132. {$ifdef bsd}
  133. function Do_SysCall(sysnr:longint):longint;
  134. function Do_Syscall(sysnr,param1:integer):longint;
  135. function Do_SysCall(sysnr,param1:LONGINT):longint;
  136. function Do_SysCall(sysnr,param1,param2:LONGINT):longint;
  137. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint;
  138. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint;
  139. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint;
  140. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6:LONGINT):longint;
  141. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint;
  142. {$else}
  143. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  144. {$endif}
  145. {**************************
  146. Time/Date Handling
  147. ***************************}
  148. var
  149. tzdaylight : boolean;
  150. tzseconds : longint;
  151. tzname : array[boolean] of pchar;
  152. { timezone support }
  153. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  154. procedure GetLocalTimezone(timer:longint);
  155. procedure ReadTimezoneFile(fn:string);
  156. function GetTimezoneFile:string;
  157. Procedure GetTimeOfDay(var tv:timeval);
  158. Function GetTimeOfDay:longint;
  159. {$ifndef bsd}
  160. Function GetEpochTime: longint;
  161. {$endif}
  162. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  163. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  164. procedure GetTime(var hour,min,sec,msec,usec:word);
  165. procedure GetTime(var hour,min,sec,sec100:word);
  166. procedure GetTime(var hour,min,sec:word);
  167. Procedure GetDate(Var Year,Month,Day:Word);
  168. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  169. function SetTime(Hour,Min,Sec:word) : Boolean;
  170. function SetDate(Year,Month,Day:Word) : Boolean;
  171. function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  172. {**************************
  173. Process Handling
  174. ***************************}
  175. function CreateShellArgV(const prog:string):ppchar;
  176. function CreateShellArgV(const prog:Ansistring):ppchar;
  177. Procedure Execve(Path:pathstr;args:ppchar;ep:ppchar);
  178. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  179. Procedure Execv(const path:pathstr;args:ppchar);
  180. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  181. Procedure Execl(const Todo:string);
  182. Procedure Execle(Todo:string;Ep:ppchar);
  183. Procedure Execlp(Todo:string;Ep:ppchar);
  184. Function Shell(const Command:String):Longint;
  185. Function Shell(const Command:AnsiString):Longint;
  186. Function Fork:longint;
  187. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  188. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  189. Procedure ExitProcess(val:longint);
  190. Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint; {=>PID (Status Valid), 0 (No Status), -1: Error, special case errno=EINTR }
  191. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  192. Procedure Nice(N:integer);
  193. function WEXITSTATUS(Status: Integer): Integer;
  194. function WTERMSIG(Status: Integer): Integer;
  195. function WSTOPSIG(Status: Integer): Integer;
  196. Function WIFEXITED(Status: Integer): Boolean;
  197. Function WIFSTOPPED(Status: Integer): Boolean;
  198. Function WIFSIGNALED(Status: Integer): Boolean;
  199. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  200. Function W_STOPCODE(Signal: Integer): Integer;
  201. {$ifdef bsd}
  202. Function GetPriority(Which,Who:longint):longint;
  203. procedure SetPriority(Which,Who,What:longint);
  204. {$else}
  205. Function GetPriority(Which,Who:Integer):integer;
  206. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  207. {$endif}
  208. Function GetPid:LongInt;
  209. Function GetPPid:LongInt;
  210. Function GetUid:Longint;
  211. Function GetEUid:Longint;
  212. Function GetGid:Longint;
  213. Function GetEGid:Longint;
  214. {**************************
  215. File Handling
  216. ***************************}
  217. Function fdOpen(pathname:string;flags:longint):longint;
  218. Function fdOpen(pathname:string;flags,mode:longint):longint;
  219. Function fdOpen(pathname:pchar;flags:longint):longint;
  220. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  221. Function fdClose(fd:longint):boolean;
  222. Function fdRead(fd:longint;var buf;size:longint):longint;
  223. Function fdWrite(fd:longint;const buf;size:longint):longint;
  224. Function fdTruncate(fd,size:longint):boolean;
  225. Function fdSeek (fd,pos,seektype :longint): longint;
  226. Function fdFlush (fd : Longint) : Boolean;
  227. Function Link(OldPath,NewPath:pathstr):boolean;
  228. Function SymLink(OldPath,NewPath:pathstr):boolean;
  229. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  230. Function ReadLink(name:pathstr):pathstr;
  231. Function UnLink(Path:pathstr):boolean;
  232. Function UnLink(Path:pchar):Boolean;
  233. Function FReName (OldName,NewName : Pchar) : Boolean;
  234. Function FReName (OldName,NewName : String) : Boolean;
  235. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  236. Function Chmod(path:pathstr;Newmode:longint):boolean;
  237. Function Utime(path:pathstr;utim:utimebuf):boolean;
  238. {$ifdef BSD}
  239. Function Access(Path:Pathstr ;mode:longint):boolean;
  240. {$else}
  241. Function Access(Path:Pathstr ;mode:integer):boolean;
  242. {$endif}
  243. Function Umask(Mask:Integer):integer;
  244. Function Flock (fd,mode : longint) : boolean;
  245. Function Flock (var T : text;mode : longint) : boolean;
  246. Function Flock (var F : File;mode : longint) : boolean;
  247. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  248. Function FStat(Fd:longint;Var Info:stat):Boolean;
  249. Function FStat(var F:Text;Var Info:stat):Boolean;
  250. Function FStat(var F:File;Var Info:stat):Boolean;
  251. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  252. Function StatFS(Path:Pathstr;Var Info:tstatfs):Boolean;
  253. Function StatFS(Fd: Longint;Var Info:tstatfs):Boolean;
  254. {$ifdef bsd}
  255. Function Fcntl(Fd:longint;Cmd:longint):longint;
  256. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  257. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  258. Procedure Fcntl(var Fd:Text;Cmd:longint;Arg:Longint);
  259. {$else}
  260. Function Fcntl(Fd:longint;Cmd:Integer):integer;
  261. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  262. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  263. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  264. {$endif}
  265. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  266. Function Dup(var oldfile,newfile:text):Boolean;
  267. Function Dup(var oldfile,newfile:file):Boolean;
  268. Function Dup2(oldfile,newfile:longint):Boolean;
  269. Function Dup2(var oldfile,newfile:text):Boolean;
  270. Function Dup2(var oldfile,newfile:file):Boolean;
  271. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  272. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  273. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  274. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  275. {**************************
  276. Directory Handling
  277. ***************************}
  278. Function OpenDir(f:pchar):pdir;
  279. Function OpenDir(f: String):pdir;
  280. function CloseDir(p:pdir):integer;
  281. Function ReadDir(p:pdir):pdirent;
  282. procedure SeekDir(p:pdir;off:longint);
  283. function TellDir(p:pdir):longint;
  284. {**************************
  285. Pipe/Fifo/Stream
  286. ***************************}
  287. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  288. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  289. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  290. Function PClose(Var F:text) : longint;
  291. Function PClose(Var F:file) : longint;
  292. Procedure POpen(var F:text;const Prog:String;rw:char);
  293. Procedure POpen(var F:file;const Prog:String;rw:char);
  294. Function mkFifo(pathname:string;mode:longint):boolean;
  295. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  296. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  297. {**************************
  298. General information
  299. ***************************}
  300. Function GetEnv(P:string):Pchar;
  301. {$ifndef BSD}
  302. Function GetDomainName:String;
  303. Function GetHostName:String;
  304. Function Sysinfo(var Info:TSysinfo):Boolean;
  305. Function Uname(var unamerec:utsname):Boolean;
  306. {$endif}
  307. {**************************
  308. Signal
  309. ***************************}
  310. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  311. Procedure SigProcMask (How:longint;SSet,OldSSet:PSigSet);
  312. Function SigPending:SigSet;
  313. Procedure SigSuspend(Mask:Sigset);
  314. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  315. Function Kill(Pid:longint;Sig:longint):integer;
  316. Procedure SigRaise(Sig:integer);
  317. {$ifndef BSD}
  318. Function Alarm(Sec : Longint) : longint;
  319. Procedure Pause;
  320. {$endif}
  321. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  322. {**************************
  323. IOCtl/Termios Functions
  324. ***************************}
  325. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  326. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  327. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  328. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  329. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  330. Procedure CFMakeRaw(var tios:TermIOS);
  331. Function TCSendBreak(fd,duration:longint):boolean;
  332. Function TCSetPGrp(fd,id:longint):boolean;
  333. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  334. Function TCFlush(fd,qsel:longint):boolean;
  335. Function TCDrain(fd:longint):boolean;
  336. Function TCFlow(fd,act:longint):boolean;
  337. Function IsATTY(Handle:Longint):Boolean;
  338. Function IsATTY(f:text):Boolean;
  339. function TTYname(Handle:Longint):string;
  340. function TTYname(var F:Text):string;
  341. {**************************
  342. Memory functions
  343. ***************************}
  344. const
  345. PROT_READ = $1; { page can be read }
  346. PROT_WRITE = $2; { page can be written }
  347. PROT_EXEC = $4; { page can be executed }
  348. PROT_NONE = $0; { page can not be accessed }
  349. MAP_SHARED = $1; { Share changes }
  350. // MAP_PRIVATE = $2; { Changes are private }
  351. MAP_TYPE = $f; { Mask for type of mapping }
  352. MAP_FIXED = $10; { Interpret addr exactly }
  353. // MAP_ANONYMOUS = $20; { don't use a file }
  354. MAP_GROWSDOWN = $100; { stack-like segment }
  355. MAP_DENYWRITE = $800; { ETXTBSY }
  356. MAP_EXECUTABLE = $1000; { mark it as an executable }
  357. MAP_LOCKED = $2000; { pages are locked }
  358. MAP_NORESERVE = $4000; { don't check for reservations }
  359. type
  360. tmmapargs=record
  361. address : longint;
  362. size : longint;
  363. prot : longint;
  364. flags : longint;
  365. fd : longint;
  366. offset : longint;
  367. end;
  368. function MMap(const m:tmmapargs):longint;
  369. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  370. {**************************
  371. Port IO functions
  372. ***************************}
  373. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  374. {$ifndef BSD}
  375. Function IoPL(Level : longint) : Boolean;
  376. {$endif}
  377. {$ifdef i386}
  378. Procedure WritePort (Port : Longint; Value : Byte);
  379. Procedure WritePort (Port : Longint; Value : Word);
  380. Procedure WritePort (Port : Longint; Value : Longint);
  381. Procedure WritePortB (Port : Longint; Value : Byte);
  382. Procedure WritePortW (Port : Longint; Value : Word);
  383. Procedure WritePortL (Port : Longint; Value : Longint);
  384. Procedure WritePortL (Port : Longint; Var Buf; Count: longint);
  385. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  386. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  387. Procedure ReadPort (Port : Longint; Var Value : Byte);
  388. Procedure ReadPort (Port : Longint; Var Value : Word);
  389. Procedure ReadPort (Port : Longint; Var Value : Longint);
  390. function ReadPortB (Port : Longint): Byte;
  391. function ReadPortW (Port : Longint): Word;
  392. function ReadPortL (Port : Longint): LongInt;
  393. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  394. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  395. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  396. {$endif}
  397. {**************************
  398. Utility functions
  399. ***************************}
  400. Function Octal(l:longint):longint;
  401. Function FExpand(Const Path: PathStr):PathStr;
  402. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  403. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  404. Function Dirname(Const path:pathstr):pathstr;
  405. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  406. Function FNMatch(const Pattern,Name:string):Boolean;
  407. Function Glob(Const path:pathstr):pglob;
  408. Procedure Globfree(var p:pglob);
  409. Function StringToPPChar(Var S:STring):ppchar;
  410. Function GetFS(var T:Text):longint;
  411. Function GetFS(Var F:File):longint;
  412. {Filedescriptorsets}
  413. Procedure FD_Zero(var fds:fdSet);
  414. Procedure FD_Clr(fd:longint;var fds:fdSet);
  415. Procedure FD_Set(fd:longint;var fds:fdSet);
  416. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  417. {Stat.Mode Types}
  418. Function S_ISLNK(m:word):boolean;
  419. Function S_ISREG(m:word):boolean;
  420. Function S_ISDIR(m:word):boolean;
  421. Function S_ISCHR(m:word):boolean;
  422. Function S_ISBLK(m:word):boolean;
  423. Function S_ISFIFO(m:word):boolean;
  424. Function S_ISSOCK(m:word):boolean;
  425. {******************************************************************************
  426. Implementation
  427. ******************************************************************************}
  428. Implementation
  429. Uses Strings;
  430. { Get the definitions of textrec and filerec }
  431. {$i textrec.inc}
  432. {$i filerec.inc}
  433. { Raw System calls are in Syscalls.inc}
  434. {$i syscalls.inc}
  435. {$i unixsysc.inc} {Syscalls only used in unit Unix/Linux}
  436. {******************************************************************************
  437. Process related calls
  438. ******************************************************************************}
  439. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  440. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  441. var r,s : LongInt;
  442. begin
  443. repeat
  444. s:=$7F00;
  445. r:=WaitPid(Pid,@s,0);
  446. until (r<>-1) or (LinuxError<>Sys_EINTR);
  447. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  448. WaitProcess:=-1 // return -1 to indicate an error
  449. else
  450. begin
  451. {$ifndef Solaris}
  452. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  453. {$else}
  454. if (s and $FF)=0 then // Only this is a valid returncode
  455. WaitProcess:=s shr 8
  456. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  457. WaitProcess:=-s // normal case
  458. else
  459. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  460. {$endif}
  461. end;
  462. end;
  463. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  464. {
  465. Create an argv which executes a command in a shell using /bin/sh -c
  466. }
  467. const Shell = '/bin/sh'#0'-c'#0;
  468. var
  469. pp,p : ppchar;
  470. // temp : string; !! Never pass a local var back!!
  471. begin
  472. getmem(pp,4*4);
  473. p:=pp;
  474. p^:=@Shell[1];
  475. inc(p);
  476. p^:=@Shell[9];
  477. inc(p);
  478. getmem(p^,len+1);
  479. move(cmd^,p^^,len);
  480. pchar(p^)[len]:=#0;
  481. inc(p);
  482. p^:=Nil;
  483. InternalCreateShellArgV:=pp;
  484. end;
  485. function CreateShellArgV(const prog:string):ppchar;
  486. begin
  487. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  488. end;
  489. function CreateShellArgV(const prog:Ansistring):ppchar;
  490. {
  491. Create an argv which executes a command in a shell using /bin/sh -c
  492. using a AnsiString;
  493. }
  494. begin
  495. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  496. end;
  497. procedure FreeShellArgV(p:ppchar);
  498. begin
  499. if (p<>nil) then begin
  500. freemem(p[2]);
  501. freemem(p);
  502. end;
  503. end;
  504. Procedure Execv(const path:pathstr;args:ppchar);
  505. {
  506. Replaces the current program by the program specified in path,
  507. arguments in args are passed to Execve.
  508. the current environment is passed on.
  509. }
  510. begin
  511. Execve(path,args,envp); {On error linuxerror will get set there}
  512. end;
  513. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  514. {
  515. This does the same as Execve, only it searches the PATH environment
  516. for the place of the Executable, except when Path starts with a slash.
  517. if the PATH environment variable is unavailable, the path is set to '.'
  518. }
  519. var
  520. thepath : string;
  521. begin
  522. if path[1]<>'/' then
  523. begin
  524. Thepath:=strpas(getenv('PATH'));
  525. if thepath='' then
  526. thepath:='.';
  527. Path:=FSearch(path,thepath)
  528. end
  529. else
  530. Path:='';
  531. if Path='' then
  532. linuxerror:=Sys_enoent
  533. else
  534. Execve(Path,args,ep);{On error linuxerror will get set there}
  535. end;
  536. Procedure Execle(Todo:string;Ep:ppchar);
  537. {
  538. This procedure takes the string 'Todo', parses it for command and
  539. command options, and Executes the command with the given options.
  540. The string 'Todo' shoud be of the form 'command options', options
  541. separated by commas.
  542. the PATH environment is not searched for 'command'.
  543. The specified environment(in 'ep') is passed on to command
  544. }
  545. var
  546. p : ppchar;
  547. begin
  548. p:=StringToPPChar(ToDo);
  549. if (p=nil) or (p^=nil) then
  550. exit;
  551. ExecVE(p^,p,EP);
  552. end;
  553. Procedure Execl(const Todo:string);
  554. {
  555. This procedure takes the string 'Todo', parses it for command and
  556. command options, and Executes the command with the given options.
  557. The string 'Todo' shoud be of the form 'command options', options
  558. separated by commas.
  559. the PATH environment is not searched for 'command'.
  560. The current environment is passed on to command
  561. }
  562. begin
  563. ExecLE(ToDo,EnvP);
  564. end;
  565. Procedure Execlp(Todo:string;Ep:ppchar);
  566. {
  567. This procedure takes the string 'Todo', parses it for command and
  568. command options, and Executes the command with the given options.
  569. The string 'Todo' shoud be of the form 'command options', options
  570. separated by commas.
  571. the PATH environment is searched for 'command'.
  572. The specified environment (in 'ep') is passed on to command
  573. }
  574. var
  575. p : ppchar;
  576. begin
  577. p:=StringToPPchar(todo);
  578. if (p=nil) or (p^=nil) then
  579. exit;
  580. ExecVP(StrPas(p^),p,EP);
  581. end;
  582. Function Shell(const Command:String):Longint;
  583. {
  584. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  585. The current environment is passed to the shell.
  586. It waits for the shell to exit, and returns its exit status.
  587. If the Exec call failed exit status 127 is reported.
  588. }
  589. { Changed the structure:
  590. - the previous version returns an undefinied value if fork fails
  591. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  592. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  593. - ShellArgs are now released
  594. - The Old CreateShellArg gives back pointers to a local var
  595. }
  596. var
  597. p : ppchar;
  598. pid : longint;
  599. begin
  600. p:=CreateShellArgv(command);
  601. pid:=fork;
  602. if pid=0 then // We are in the Child
  603. begin
  604. {This is the child.}
  605. Execve(p^,p,envp);
  606. ExitProcess(127); // was Exit(127)
  607. end
  608. else if (pid<>-1) then // Successfull started
  609. Shell:=WaitProcess(pid) {Linuxerror is set there}
  610. else // no success
  611. Shell:=-1; // indicate an error
  612. FreeShellArgV(p);
  613. end;
  614. Function Shell(const Command:AnsiString):Longint;
  615. {
  616. AnsiString version of Shell
  617. }
  618. var
  619. p : ppchar;
  620. pid : longint;
  621. begin { Changes as above }
  622. p:=CreateShellArgv(command);
  623. pid:=fork;
  624. if pid=0 then // We are in the Child
  625. begin
  626. Execve(p^,p,envp);
  627. ExitProcess(127); // was exit(127)!! We must exit the Process, not the function
  628. end
  629. else if (pid<>-1) then // Successfull started
  630. Shell:=WaitProcess(pid) {Linuxerror is set there}
  631. else // no success
  632. Shell:=-1;
  633. FreeShellArgV(p);
  634. end;
  635. function WEXITSTATUS(Status: Integer): Integer;
  636. begin
  637. WEXITSTATUS:=(Status and $FF00) shr 8;
  638. end;
  639. function WTERMSIG(Status: Integer): Integer;
  640. begin
  641. WTERMSIG:=(Status and $7F);
  642. end;
  643. function WSTOPSIG(Status: Integer): Integer;
  644. begin
  645. WSTOPSIG:=WEXITSTATUS(Status);
  646. end;
  647. Function WIFEXITED(Status: Integer): Boolean;
  648. begin
  649. WIFEXITED:=(WTERMSIG(Status)=0);
  650. end;
  651. Function WIFSTOPPED(Status: Integer): Boolean;
  652. begin
  653. WIFSTOPPED:=((Status and $FF)=$7F);
  654. end;
  655. Function WIFSIGNALED(Status: Integer): Boolean;
  656. begin
  657. WIFSIGNALED:=(not WIFSTOPPED(Status)) and
  658. (not WIFEXITED(Status));
  659. end;
  660. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  661. begin
  662. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  663. end;
  664. Function W_STOPCODE(Signal: Integer): Integer;
  665. begin
  666. W_STOPCODE:=(Signal shl 8) or $7F;
  667. end;
  668. {******************************************************************************
  669. Date and Time related calls
  670. ******************************************************************************}
  671. Const
  672. {Date Translation}
  673. C1970=2440588;
  674. D0 = 1461;
  675. D1 = 146097;
  676. D2 =1721119;
  677. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  678. Var
  679. Century,XYear: LongInt;
  680. Begin
  681. If Month<=2 Then
  682. Begin
  683. Dec(Year);
  684. Inc(Month,12);
  685. End;
  686. Dec(Month,3);
  687. Century:=(longint(Year Div 100)*D1) shr 2;
  688. XYear:=(longint(Year Mod 100)*D0) shr 2;
  689. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  690. End;
  691. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  692. Var
  693. YYear,XYear,Temp,TempMonth : LongInt;
  694. Begin
  695. Temp:=((JulianDN-D2) shl 2)-1;
  696. JulianDN:=Temp Div D1;
  697. XYear:=(Temp Mod D1) or 3;
  698. YYear:=(XYear Div D0);
  699. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  700. Day:=((Temp Mod 153)+5) Div 5;
  701. TempMonth:=Temp Div 153;
  702. If TempMonth>=10 Then
  703. Begin
  704. inc(YYear);
  705. dec(TempMonth,12);
  706. End;
  707. inc(TempMonth,3);
  708. Month := TempMonth;
  709. Year:=YYear+(JulianDN*100);
  710. end;
  711. Function GetEpochTime: longint;
  712. {
  713. Get the number of seconds since 00:00, January 1 1970, GMT
  714. the time NOT corrected any way
  715. }
  716. begin
  717. GetEpochTime:=GetTimeOfDay;
  718. end;
  719. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  720. {
  721. Transforms Epoch time into local time (hour, minute,seconds)
  722. }
  723. Var
  724. DateNum: LongInt;
  725. Begin
  726. inc(Epoch,TZSeconds);
  727. Datenum:=(Epoch Div 86400) + c1970;
  728. JulianToGregorian(DateNum,Year,Month,day);
  729. Epoch:=Abs(Epoch Mod 86400);
  730. Hour:=Epoch Div 3600;
  731. Epoch:=Epoch Mod 3600;
  732. Minute:=Epoch Div 60;
  733. Second:=Epoch Mod 60;
  734. End;
  735. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  736. {
  737. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  738. (seconds since 00:00, january 1 1970, corrected for local time zone)
  739. }
  740. Begin
  741. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  742. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  743. End;
  744. procedure GetTime(var hour,min,sec,msec,usec:word);
  745. {
  746. Gets the current time, adjusted to local time
  747. }
  748. var
  749. year,day,month:Word;
  750. t : timeval;
  751. begin
  752. gettimeofday(t);
  753. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  754. msec:=t.usec div 1000;
  755. usec:=t.usec mod 1000;
  756. end;
  757. procedure GetTime(var hour,min,sec,sec100:word);
  758. {
  759. Gets the current time, adjusted to local time
  760. }
  761. var
  762. usec : word;
  763. begin
  764. gettime(hour,min,sec,sec100,usec);
  765. sec100:=sec100 div 10;
  766. end;
  767. Procedure GetTime(Var Hour,Min,Sec:Word);
  768. {
  769. Gets the current time, adjusted to local time
  770. }
  771. var
  772. msec,usec : Word;
  773. Begin
  774. gettime(hour,min,sec,msec,usec);
  775. End;
  776. Procedure GetDate(Var Year,Month,Day:Word);
  777. {
  778. Gets the current date, adjusted to local time
  779. }
  780. var
  781. hour,minute,second : word;
  782. Begin
  783. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  784. End;
  785. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  786. {
  787. Gets the current date, adjusted to local time
  788. }
  789. Begin
  790. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  791. End;
  792. {$ifndef BSD}
  793. {$ifdef linux}
  794. Function stime (t : longint) : Boolean;
  795. var
  796. sr : Syscallregs;
  797. begin
  798. sr.reg2:=longint(@t);
  799. SysCall(Syscall_nr_stime,sr);
  800. linuxerror:=errno;
  801. stime:=linuxerror=0;
  802. end;
  803. {$endif}
  804. {$endif}
  805. {$ifdef BSD}
  806. Function stime (t : longint) : Boolean;
  807. begin
  808. end;
  809. {$endif}
  810. Function SetTime(Hour,Min,Sec:word) : boolean;
  811. var
  812. Year, Month, Day : Word;
  813. begin
  814. GetDate (Year, Month, Day);
  815. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  816. end;
  817. Function SetDate(Year,Month,Day:Word) : boolean;
  818. var
  819. Hour, Minute, Second, Sec100 : Word;
  820. begin
  821. GetTime ( Hour, Minute, Second, Sec100 );
  822. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  823. end;
  824. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  825. begin
  826. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  827. end;
  828. { Include timezone handling routines which use /usr/share/timezone info }
  829. {$i timezone.inc}
  830. {******************************************************************************
  831. FileSystem calls
  832. ******************************************************************************}
  833. Function fdOpen(pathname:string;flags:longint):longint;
  834. begin
  835. pathname:=pathname+#0;
  836. fdOpen:=Sys_Open(@pathname[1],flags,438);
  837. LinuxError:=Errno;
  838. end;
  839. Function fdOpen(pathname:string;flags,mode:longint):longint;
  840. begin
  841. pathname:=pathname+#0;
  842. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  843. LinuxError:=Errno;
  844. end;
  845. Function fdOpen(pathname:pchar;flags:longint):longint;
  846. begin
  847. fdOpen:=Sys_Open(pathname,flags,0);
  848. LinuxError:=Errno;
  849. end;
  850. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  851. begin
  852. fdOpen:=Sys_Open(pathname,flags,mode);
  853. LinuxError:=Errno;
  854. end;
  855. Function fdClose(fd:longint):boolean;
  856. begin
  857. fdClose:=(Sys_Close(fd)=0);
  858. LinuxError:=Errno;
  859. end;
  860. Function fdRead(fd:longint;var buf;size:longint):longint;
  861. begin
  862. fdRead:=Sys_Read(fd,pchar(@buf),size);
  863. LinuxError:=Errno;
  864. end;
  865. Function fdWrite(fd:longint;const buf;size:longint):longint;
  866. begin
  867. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  868. LinuxError:=Errno;
  869. end;
  870. Function fdSeek (fd,pos,seektype :longint): longint;
  871. {
  872. Do a Seek on a file descriptor fd to position pos, starting from seektype
  873. }
  874. begin
  875. fdseek:=Sys_LSeek (fd,pos,seektype);
  876. LinuxError:=Errno;
  877. end;
  878. {$ifdef BSD}
  879. Function Fcntl(Fd:longint;Cmd:longint):longint;
  880. {
  881. Read or manipulate a file.(See also fcntl (2) )
  882. Possible values for Cmd are :
  883. F_GetFd,F_GetFl,F_GetOwn
  884. Errors are reported in Linuxerror;
  885. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  886. }
  887. begin
  888. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  889. begin
  890. Linuxerror:=sys_fcntl(fd,cmd,0);
  891. if linuxerror=-1 then
  892. begin
  893. linuxerror:=errno;
  894. fcntl:=0;
  895. end
  896. else
  897. begin
  898. fcntl:=linuxerror;
  899. linuxerror:=0;
  900. end;
  901. end
  902. else
  903. begin
  904. linuxerror:=Sys_einval;
  905. Fcntl:=0;
  906. end;
  907. end;
  908. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  909. {
  910. Read or manipulate a file. (See also fcntl (2) )
  911. Possible values for Cmd are :
  912. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  913. Errors are reported in Linuxerror;
  914. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  915. F_DupFD is not allowed, due to the structure of Files in Pascal.
  916. }
  917. begin
  918. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  919. begin
  920. sys_fcntl(fd,cmd,arg);
  921. LinuxError:=ErrNo;
  922. end
  923. else
  924. linuxerror:=Sys_einval;
  925. end;
  926. {$endif}
  927. {$ifdef BSD}
  928. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  929. {$else}
  930. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  931. {$endif}
  932. begin
  933. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  934. end;
  935. {$ifdef BSD}
  936. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  937. {$else}
  938. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  939. {$endif}
  940. begin
  941. Fcntl(textrec(Fd).handle, Cmd, Arg);
  942. end;
  943. Function Flock (var T : text;mode : longint) : boolean;
  944. begin
  945. Flock:=Flock(TextRec(T).Handle,mode);
  946. end;
  947. Function Flock (var F : File;mode : longint) : boolean;
  948. begin
  949. Flock:=Flock(FileRec(F).Handle,mode);
  950. end;
  951. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  952. {
  953. Get all information on a file, and return it in Info.
  954. }
  955. begin
  956. path:=path+#0;
  957. FStat:=(Sys_stat(@(path[1]),Info)=0);
  958. LinuxError:=errno;
  959. end;
  960. Function FStat(var F:Text;Var Info:stat):Boolean;
  961. {
  962. Get all information on a text file, and return it in info.
  963. }
  964. begin
  965. FStat:=Fstat(TextRec(F).Handle,INfo);
  966. end;
  967. Function FStat(var F:File;Var Info:stat):Boolean;
  968. {
  969. Get all information on a untyped file, and return it in info.
  970. }
  971. begin
  972. FStat:=Fstat(FileRec(F).Handle,Info);
  973. end;
  974. Function SymLink(OldPath,newPath:pathstr):boolean;
  975. {
  976. Proceduces a soft link from new to old.
  977. }
  978. begin
  979. oldpath:=oldpath+#0;
  980. newpath:=newpath+#0;
  981. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  982. linuxerror:=errno;
  983. end;
  984. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  985. {
  986. Read a link (where it points to)
  987. }
  988. begin
  989. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  990. linuxerror:=errno;
  991. end;
  992. Function ReadLink(Name:pathstr):pathstr;
  993. {
  994. Read a link (where it points to)
  995. }
  996. var
  997. LinkName : pathstr;
  998. i : longint;
  999. begin
  1000. Name:=Name+#0;
  1001. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  1002. if i>0 then
  1003. begin
  1004. linkname[0]:=chr(i);
  1005. ReadLink:=LinkName;
  1006. end
  1007. else
  1008. ReadLink:='';
  1009. end;
  1010. Function UnLink(Path:pathstr):boolean;
  1011. {
  1012. Removes the file in 'Path' (that is, it decreases the link count with one.
  1013. if the link count is zero, the file is removed from the disk.
  1014. }
  1015. begin
  1016. path:=path+#0;
  1017. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  1018. linuxerror:=errno;
  1019. end;
  1020. Function UnLink(Path:pchar):Boolean;
  1021. {
  1022. Removes the file in 'Path' (that is, it decreases the link count with one.
  1023. if the link count is zero, the file is removed from the disk.
  1024. }
  1025. begin
  1026. Unlink:=(Sys_unlink(path)=0);
  1027. linuxerror:=errno;
  1028. end;
  1029. Function FRename (OldName,NewName : Pchar) : Boolean;
  1030. begin
  1031. FRename:=Sys_rename(OldName,NewName)=0;
  1032. LinuxError:=Errno;
  1033. end;
  1034. Function FRename (OldName,NewName : String) : Boolean;
  1035. begin
  1036. OldName:=OldName+#0;
  1037. NewName:=NewName+#0;
  1038. FRename:=FRename (@OldName[1],@NewName[1]);
  1039. end;
  1040. Function Dup(var oldfile,newfile:text):Boolean;
  1041. {
  1042. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1043. oldfile.
  1044. After which the two textfiles are, in effect, the same, except
  1045. that they don't share the same buffer, and don't share the same
  1046. close_on_exit flag.
  1047. }
  1048. begin
  1049. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1050. textrec(newfile):=textrec(oldfile);
  1051. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1052. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  1053. end;
  1054. Function Dup(var oldfile,newfile:file):Boolean;
  1055. {
  1056. Copies the filedescriptor oldfile to newfile
  1057. }
  1058. begin
  1059. filerec(newfile):=filerec(oldfile);
  1060. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  1061. end;
  1062. Function Dup2(var oldfile,newfile:text):Boolean;
  1063. {
  1064. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1065. oldfile. It closes newfile if it was still open.
  1066. After which the two textfiles are, in effect, the same, except
  1067. that they don't share the same buffer, and don't share the same
  1068. close_on_exit flag.
  1069. }
  1070. var
  1071. tmphandle : word;
  1072. begin
  1073. case TextRec(oldfile).mode of
  1074. fmOutput, fmInOut, fmAppend :
  1075. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1076. end;
  1077. case TextRec(newfile).mode of
  1078. fmOutput, fmInOut, fmAppend :
  1079. flush(newfile);
  1080. end;
  1081. tmphandle:=textrec(newfile).handle;
  1082. textrec(newfile):=textrec(oldfile);
  1083. textrec(newfile).handle:=tmphandle;
  1084. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1085. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  1086. end;
  1087. Function Dup2(var oldfile,newfile:file):Boolean;
  1088. {
  1089. Copies the filedescriptor oldfile to newfile
  1090. }
  1091. begin
  1092. filerec(newfile):=filerec(oldfile);
  1093. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  1094. end;
  1095. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1096. {
  1097. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1098. have changed.
  1099. This function allows specification of a timeout as a longint.
  1100. }
  1101. var
  1102. p : PTimeVal;
  1103. tv : TimeVal;
  1104. begin
  1105. if TimeOut=-1 then
  1106. p:=nil
  1107. else
  1108. begin
  1109. tv.Sec:=Timeout div 1000;
  1110. tv.Usec:=(Timeout mod 1000)*1000;
  1111. p:=@tv;
  1112. end;
  1113. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  1114. end;
  1115. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1116. Var
  1117. F:FDSet;
  1118. begin
  1119. if textrec(t).mode=fmclosed then
  1120. begin
  1121. LinuxError:=Sys_EBADF;
  1122. exit(-1);
  1123. end;
  1124. FD_Zero(f);
  1125. FD_Set(textrec(T).handle,f);
  1126. if textrec(T).mode=fminput then
  1127. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1128. else
  1129. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1130. end;
  1131. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  1132. var
  1133. p : PTimeVal;
  1134. tv : TimeVal;
  1135. begin
  1136. if TimeOut=-1 then
  1137. p:=nil
  1138. else
  1139. begin
  1140. tv.Sec:=Timeout div 1000;
  1141. tv.Usec:=(Timeout mod 1000)*1000;
  1142. p:=@tv;
  1143. end;
  1144. SelectText:=SelectText(T,p);
  1145. end;
  1146. {******************************************************************************
  1147. Directory
  1148. ******************************************************************************}
  1149. Function OpenDir(F:String):PDir;
  1150. begin
  1151. F:=F+#0;
  1152. OpenDir:=OpenDir(@F[1]);
  1153. LinuxError:=ErrNo;
  1154. end;
  1155. procedure SeekDir(p:pdir;off:longint);
  1156. begin
  1157. if p=nil then
  1158. begin
  1159. errno:=Sys_EBADF;
  1160. exit;
  1161. end;
  1162. {$ifndef bsd}
  1163. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1164. {$endif}
  1165. p^.size:=0;
  1166. p^.loc:=0;
  1167. end;
  1168. function TellDir(p:pdir):longint;
  1169. begin
  1170. if p=nil then
  1171. begin
  1172. errno:=Sys_EBADF;
  1173. telldir:=-1;
  1174. exit;
  1175. end;
  1176. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1177. { We could try to use the nextoff field here, but on my 1.2.13
  1178. kernel, this gives nothing... This may have to do with
  1179. the readdir implementation of libc... I also didn't find any trace of
  1180. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1181. Michael. }
  1182. end;
  1183. Function ReadDir(P:pdir):pdirent;
  1184. begin
  1185. ReadDir:=Sys_ReadDir(p);
  1186. LinuxError:=Errno;
  1187. end;
  1188. {******************************************************************************
  1189. Pipes/Fifo
  1190. ******************************************************************************}
  1191. Procedure OpenPipe(var F:Text);
  1192. begin
  1193. case textrec(f).mode of
  1194. fmoutput :
  1195. if textrec(f).userdata[1]<>P_OUT then
  1196. textrec(f).mode:=fmclosed;
  1197. fminput :
  1198. if textrec(f).userdata[1]<>P_IN then
  1199. textrec(f).mode:=fmclosed;
  1200. else
  1201. textrec(f).mode:=fmclosed;
  1202. end;
  1203. end;
  1204. Procedure IOPipe(var F:text);
  1205. begin
  1206. case textrec(f).mode of
  1207. fmoutput :
  1208. begin
  1209. { first check if we need something to write, else we may
  1210. get a SigPipe when Close() is called (PFV) }
  1211. if textrec(f).bufpos>0 then
  1212. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1213. end;
  1214. fminput :
  1215. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1216. end;
  1217. textrec(f).bufpos:=0;
  1218. end;
  1219. Procedure FlushPipe(var F:Text);
  1220. begin
  1221. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1222. IOPipe(f);
  1223. textrec(f).bufpos:=0;
  1224. end;
  1225. Procedure ClosePipe(var F:text);
  1226. begin
  1227. textrec(f).mode:=fmclosed;
  1228. Sys_close(textrec(f).handle);
  1229. end;
  1230. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1231. {
  1232. Sets up a pair of file variables, which act as a pipe. The first one can
  1233. be read from, the second one can be written to.
  1234. If the operation was unsuccesful, linuxerror is set.
  1235. }
  1236. var
  1237. f_in,f_out : longint;
  1238. begin
  1239. if not AssignPipe(f_in,f_out) then
  1240. begin
  1241. AssignPipe:=false;
  1242. exit;
  1243. end;
  1244. { Set up input }
  1245. Assign(Pipe_in,'');
  1246. Textrec(Pipe_in).Handle:=f_in;
  1247. Textrec(Pipe_in).Mode:=fmInput;
  1248. Textrec(Pipe_in).userdata[1]:=P_IN;
  1249. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1250. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1251. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1252. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1253. { Set up output }
  1254. Assign(Pipe_out,'');
  1255. Textrec(Pipe_out).Handle:=f_out;
  1256. Textrec(Pipe_out).Mode:=fmOutput;
  1257. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1258. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1259. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1260. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1261. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1262. AssignPipe:=true;
  1263. end;
  1264. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1265. {
  1266. Sets up a pair of file variables, which act as a pipe. The first one can
  1267. be read from, the second one can be written to.
  1268. If the operation was unsuccesful, linuxerror is set.
  1269. }
  1270. var
  1271. f_in,f_out : longint;
  1272. begin
  1273. if not AssignPipe(f_in,f_out) then
  1274. begin
  1275. AssignPipe:=false;
  1276. exit;
  1277. end;
  1278. { Set up input }
  1279. Assign(Pipe_in,'');
  1280. Filerec(Pipe_in).Handle:=f_in;
  1281. Filerec(Pipe_in).Mode:=fmInput;
  1282. Filerec(Pipe_in).recsize:=1;
  1283. Filerec(Pipe_in).userdata[1]:=P_IN;
  1284. { Set up output }
  1285. Assign(Pipe_out,'');
  1286. Filerec(Pipe_out).Handle:=f_out;
  1287. Filerec(Pipe_out).Mode:=fmoutput;
  1288. Filerec(Pipe_out).recsize:=1;
  1289. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1290. AssignPipe:=true;
  1291. end;
  1292. Procedure PCloseText(Var F:text);
  1293. {
  1294. May not use @PClose due overloading
  1295. }
  1296. begin
  1297. PClose(f);
  1298. end;
  1299. Procedure POpen(var F:text;const Prog:String;rw:char);
  1300. {
  1301. Starts the program in 'Prog' and makes it's input or out put the
  1302. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1303. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1304. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1305. read from 'f'.
  1306. }
  1307. var
  1308. pipi,
  1309. pipo : text;
  1310. pid : longint;
  1311. pl : ^longint;
  1312. pp : ppchar;
  1313. begin
  1314. LinuxError:=0;
  1315. rw:=upcase(rw);
  1316. if not (rw in ['R','W']) then
  1317. begin
  1318. LinuxError:=Sys_enoent;
  1319. exit;
  1320. end;
  1321. AssignPipe(pipi,pipo);
  1322. if Linuxerror<>0 then
  1323. exit;
  1324. pid:=fork;
  1325. if linuxerror<>0 then
  1326. begin
  1327. close(pipi);
  1328. close(pipo);
  1329. exit;
  1330. end;
  1331. if pid=0 then
  1332. begin
  1333. { We're in the child }
  1334. if rw='W' then
  1335. begin
  1336. close(pipo);
  1337. dup2(pipi,input);
  1338. close(pipi);
  1339. if linuxerror<>0 then
  1340. halt(127);
  1341. end
  1342. else
  1343. begin
  1344. close(pipi);
  1345. dup2(pipo,output);
  1346. close(pipo);
  1347. if linuxerror<>0 then
  1348. halt(127);
  1349. end;
  1350. pp:=createshellargv(prog);
  1351. Execve(pp^,pp,envp);
  1352. halt(127);
  1353. end
  1354. else
  1355. begin
  1356. { We're in the parent }
  1357. if rw='W' then
  1358. begin
  1359. close(pipi);
  1360. f:=pipo;
  1361. textrec(f).bufptr:=@textrec(f).buffer;
  1362. end
  1363. else
  1364. begin
  1365. close(pipo);
  1366. f:=pipi;
  1367. textrec(f).bufptr:=@textrec(f).buffer;
  1368. end;
  1369. {Save the process ID - needed when closing }
  1370. pl:=@(textrec(f).userdata[2]);
  1371. pl^:=pid;
  1372. textrec(f).closefunc:=@PCloseText;
  1373. end;
  1374. end;
  1375. Procedure POpen(var F:file;const Prog:String;rw:char);
  1376. {
  1377. Starts the program in 'Prog' and makes it's input or out put the
  1378. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1379. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1380. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1381. read from 'f'.
  1382. }
  1383. var
  1384. pipi,
  1385. pipo : file;
  1386. pid : longint;
  1387. pl : ^longint;
  1388. p,pp : ppchar;
  1389. temp : string[255];
  1390. begin
  1391. LinuxError:=0;
  1392. rw:=upcase(rw);
  1393. if not (rw in ['R','W']) then
  1394. begin
  1395. LinuxError:=Sys_enoent;
  1396. exit;
  1397. end;
  1398. AssignPipe(pipi,pipo);
  1399. if Linuxerror<>0 then
  1400. exit;
  1401. pid:=fork;
  1402. if linuxerror<>0 then
  1403. begin
  1404. close(pipi);
  1405. close(pipo);
  1406. exit;
  1407. end;
  1408. if pid=0 then
  1409. begin
  1410. { We're in the child }
  1411. if rw='W' then
  1412. begin
  1413. close(pipo);
  1414. dup2(filerec(pipi).handle,stdinputhandle);
  1415. close(pipi);
  1416. if linuxerror<>0 then
  1417. halt(127);
  1418. end
  1419. else
  1420. begin
  1421. close(pipi);
  1422. dup2(filerec(pipo).handle,stdoutputhandle);
  1423. close(pipo);
  1424. if linuxerror<>0 then
  1425. halt(127);
  1426. end;
  1427. getmem(pp,sizeof(pchar)*4);
  1428. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1429. p:=pp;
  1430. p^:=@temp[1];
  1431. inc(p);
  1432. p^:=@temp[9];
  1433. inc(p);
  1434. p^:=@temp[12];
  1435. inc(p);
  1436. p^:=Nil;
  1437. Execve('/bin/sh',pp,envp);
  1438. halt(127);
  1439. end
  1440. else
  1441. begin
  1442. { We're in the parent }
  1443. if rw='W' then
  1444. begin
  1445. close(pipi);
  1446. f:=pipo;
  1447. end
  1448. else
  1449. begin
  1450. close(pipo);
  1451. f:=pipi;
  1452. end;
  1453. {Save the process ID - needed when closing }
  1454. pl:=@(filerec(f).userdata[2]);
  1455. pl^:=pid;
  1456. end;
  1457. end;
  1458. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1459. {
  1460. Starts the program in 'Prog' and makes its input and output the
  1461. other end of two pipes, which are the stdin and stdout of a program
  1462. specified in 'Prog'.
  1463. streamout can be used to write to the program, streamin can be used to read
  1464. the output of the program. See the following diagram :
  1465. Parent Child
  1466. STreamout --> Input
  1467. Streamin <-- Output
  1468. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1469. }
  1470. var
  1471. pipi,
  1472. pipo : text;
  1473. pid : longint;
  1474. pl : ^Longint;
  1475. begin
  1476. LinuxError:=0;
  1477. AssignStream:=-1;
  1478. AssignPipe(streamin,pipo);
  1479. if Linuxerror<>0 then
  1480. exit;
  1481. AssignPipe(pipi,streamout);
  1482. if Linuxerror<>0 then
  1483. exit;
  1484. pid:=fork;
  1485. if linuxerror<>0 then
  1486. begin
  1487. close(pipi);
  1488. close(pipo);
  1489. close (streamin);
  1490. close (streamout);
  1491. exit;
  1492. end;
  1493. if pid=0 then
  1494. begin
  1495. { We're in the child }
  1496. { Close what we don't need }
  1497. close(streamout);
  1498. close(streamin);
  1499. dup2(pipi,input);
  1500. if linuxerror<>0 then
  1501. halt(127);
  1502. close(pipi);
  1503. dup2(pipo,output);
  1504. if linuxerror<>0 then
  1505. halt (127);
  1506. close(pipo);
  1507. Execl(Prog);
  1508. halt(127);
  1509. end
  1510. else
  1511. begin
  1512. { we're in the parent}
  1513. close(pipo);
  1514. close(pipi);
  1515. {Save the process ID - needed when closing }
  1516. pl:=@(textrec(StreamIn).userdata[2]);
  1517. pl^:=pid;
  1518. textrec(StreamIn).closefunc:=@PCloseText;
  1519. {Save the process ID - needed when closing }
  1520. pl:=@(textrec(StreamOut).userdata[2]);
  1521. pl^:=pid;
  1522. textrec(StreamOut).closefunc:=@PCloseText;
  1523. AssignStream:=Pid;
  1524. end;
  1525. end;
  1526. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1527. {
  1528. Starts the program in 'prog' and makes its input, output and error output the
  1529. other end of three pipes, which are the stdin, stdout and stderr of a program
  1530. specified in 'prog'.
  1531. StreamOut can be used to write to the program, StreamIn can be used to read
  1532. the output of the program, StreamErr reads the error output of the program.
  1533. See the following diagram :
  1534. Parent Child
  1535. StreamOut --> StdIn (input)
  1536. StreamIn <-- StdOut (output)
  1537. StreamErr <-- StdErr (error output)
  1538. }
  1539. var
  1540. PipeIn, PipeOut, PipeErr: text;
  1541. pid: LongInt;
  1542. pl: ^LongInt;
  1543. begin
  1544. LinuxError := 0;
  1545. AssignStream := -1;
  1546. // Assign pipes
  1547. AssignPipe(StreamIn, PipeOut);
  1548. if LinuxError <> 0 then exit;
  1549. AssignPipe(StreamErr, PipeErr);
  1550. if LinuxError <> 0 then begin
  1551. Close(StreamIn);
  1552. Close(PipeOut);
  1553. exit;
  1554. end;
  1555. AssignPipe(PipeIn, StreamOut);
  1556. if LinuxError <> 0 then begin
  1557. Close(StreamIn);
  1558. Close(PipeOut);
  1559. Close(StreamErr);
  1560. Close(PipeErr);
  1561. exit;
  1562. end;
  1563. // Fork
  1564. pid := Fork;
  1565. if LinuxError <> 0 then begin
  1566. Close(StreamIn);
  1567. Close(PipeOut);
  1568. Close(StreamErr);
  1569. Close(PipeErr);
  1570. Close(PipeIn);
  1571. Close(StreamOut);
  1572. exit;
  1573. end;
  1574. if pid = 0 then begin
  1575. // *** We are in the child ***
  1576. // Close what we don not need
  1577. Close(StreamOut);
  1578. Close(StreamIn);
  1579. Close(StreamErr);
  1580. // Connect pipes
  1581. dup2(PipeIn, Input);
  1582. if LinuxError <> 0 then Halt(127);
  1583. Close(PipeIn);
  1584. dup2(PipeOut, Output);
  1585. if LinuxError <> 0 then Halt(127);
  1586. Close(PipeOut);
  1587. dup2(PipeErr, StdErr);
  1588. if LinuxError <> 0 then Halt(127);
  1589. Close(PipeErr);
  1590. // Execute program
  1591. Execl(Prog);
  1592. Halt(127);
  1593. end else begin
  1594. // *** We are in the parent ***
  1595. Close(PipeErr);
  1596. Close(PipeOut);
  1597. Close(PipeIn);
  1598. // Save the process ID - needed when closing
  1599. pl := @(TextRec(StreamIn).userdata[2]);
  1600. pl^ := pid;
  1601. TextRec(StreamIn).closefunc := @PCloseText;
  1602. // Save the process ID - needed when closing
  1603. pl := @(TextRec(StreamOut).userdata[2]);
  1604. pl^ := pid;
  1605. TextRec(StreamOut).closefunc := @PCloseText;
  1606. // Save the process ID - needed when closing
  1607. pl := @(TextRec(StreamErr).userdata[2]);
  1608. pl^ := pid;
  1609. TextRec(StreamErr).closefunc := @PCloseText;
  1610. AssignStream := pid;
  1611. end;
  1612. end;
  1613. {******************************************************************************
  1614. General information calls
  1615. ******************************************************************************}
  1616. Function GetEnv(P:string):Pchar;
  1617. {
  1618. Searches the environment for a string with name p and
  1619. returns a pchar to it's value.
  1620. A pchar is used to accomodate for strings of length > 255
  1621. }
  1622. var
  1623. ep : ppchar;
  1624. found : boolean;
  1625. Begin
  1626. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1627. ep:=envp;
  1628. found:=false;
  1629. if ep<>nil then
  1630. begin
  1631. while (not found) and (ep^<>nil) do
  1632. begin
  1633. if strlcomp(@p[1],(ep^),length(p))=0 then
  1634. found:=true
  1635. else
  1636. inc(ep);
  1637. end;
  1638. end;
  1639. if found then
  1640. getenv:=ep^+length(p)
  1641. else
  1642. getenv:=nil;
  1643. end;
  1644. {$ifndef bsd}
  1645. Function GetDomainName:String;
  1646. {
  1647. Get machines domain name. Returns empty string if not set.
  1648. }
  1649. Var
  1650. Sysn : utsname;
  1651. begin
  1652. Uname(Sysn);
  1653. linuxerror:=errno;
  1654. If linuxerror<>0 then
  1655. getdomainname:=''
  1656. else
  1657. getdomainname:=strpas(@Sysn.domainname[0]);
  1658. end;
  1659. Function GetHostName:String;
  1660. {
  1661. Get machines name. Returns empty string if not set.
  1662. }
  1663. Var
  1664. Sysn : utsname;
  1665. begin
  1666. uname(Sysn);
  1667. linuxerror:=errno;
  1668. If linuxerror<>0 then
  1669. gethostname:=''
  1670. else
  1671. gethostname:=strpas(@Sysn.nodename[0]);
  1672. end;
  1673. {$endif}
  1674. {******************************************************************************
  1675. Signal handling calls
  1676. ******************************************************************************}
  1677. procedure SigRaise(sig:integer);
  1678. begin
  1679. Kill(GetPid,Sig);
  1680. end;
  1681. {******************************************************************************
  1682. IOCtl and Termios calls
  1683. ******************************************************************************}
  1684. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1685. begin
  1686. {$ifndef BSD}
  1687. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1688. {$else}
  1689. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1690. {$endif}
  1691. end;
  1692. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  1693. var
  1694. nr:longint;
  1695. begin
  1696. {$ifndef BSD}
  1697. case OptAct of
  1698. TCSANOW : nr:=TCSETS;
  1699. TCSADRAIN : nr:=TCSETSW;
  1700. TCSAFLUSH : nr:=TCSETSF;
  1701. {$else}
  1702. case OptAct of
  1703. TCSANOW : nr:=TIOCSETA;
  1704. TCSADRAIN : nr:=TIOCSETAW;
  1705. TCSAFLUSH : nr:=TIOCSETAF;
  1706. {$endif}
  1707. else
  1708. begin
  1709. ErrNo:=Sys_EINVAL;
  1710. TCSetAttr:=false;
  1711. exit;
  1712. end;
  1713. end;
  1714. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1715. end;
  1716. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1717. begin
  1718. {$ifndef BSD}
  1719. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1720. {$else}
  1721. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1722. {$endif}
  1723. end;
  1724. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1725. begin
  1726. {$ifndef BSD}
  1727. CFSetISpeed(tios,speed);
  1728. {$else}
  1729. tios.c_ospeed:=speed;
  1730. {$endif}
  1731. end;
  1732. Procedure CFMakeRaw(var tios:TermIOS);
  1733. begin
  1734. {$ifndef BSD}
  1735. with tios do
  1736. begin
  1737. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1738. INLCR or IGNCR or ICRNL or IXON));
  1739. c_oflag:=c_oflag and (not OPOST);
  1740. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1741. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1742. end;
  1743. {$else}
  1744. with tios do
  1745. begin
  1746. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1747. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1748. IGNPAR));
  1749. c_iflag:=c_iflag OR IGNBRK;
  1750. c_oflag:=c_oflag and (not OPOST);
  1751. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1752. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1753. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1754. c_cc[VMIN]:=1;
  1755. c_cc[VTIME]:=0;
  1756. end;
  1757. {$endif}
  1758. end;
  1759. Function TCSendBreak(fd,duration:longint):boolean;
  1760. begin
  1761. {$ifndef BSD}
  1762. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1763. {$else}
  1764. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1765. {$endif}
  1766. end;
  1767. Function TCSetPGrp(fd,id:longint):boolean;
  1768. begin
  1769. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1770. end;
  1771. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1772. begin
  1773. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1774. end;
  1775. Function TCDrain(fd:longint):boolean;
  1776. begin
  1777. {$ifndef BSD}
  1778. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1779. {$else}
  1780. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1781. {$endif}
  1782. end;
  1783. Function TCFlow(fd,act:longint):boolean;
  1784. begin
  1785. {$ifndef BSD}
  1786. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1787. {$else}
  1788. case act OF
  1789. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1790. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1791. TCIOFF : {N/I}
  1792. end;
  1793. {$endif}
  1794. end;
  1795. Function TCFlush(fd,qsel:longint):boolean;
  1796. begin
  1797. {$ifndef BSD}
  1798. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1799. {$else}
  1800. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1801. {$endif}
  1802. end;
  1803. Function IsATTY(Handle:Longint):Boolean;
  1804. {
  1805. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1806. }
  1807. var
  1808. t : Termios;
  1809. begin
  1810. IsAtty:=TCGetAttr(Handle,t);
  1811. end;
  1812. Function IsATTY(f: text):Boolean;
  1813. {
  1814. Idem as previous, only now for text variables.
  1815. }
  1816. begin
  1817. IsATTY:=IsaTTY(textrec(f).handle);
  1818. end;
  1819. function TTYName(Handle:Longint):string;
  1820. {
  1821. Return the name of the current tty described by handle f.
  1822. returns empty string in case of an error.
  1823. }
  1824. var
  1825. mydev,
  1826. myino : longint;
  1827. st : stat;
  1828. function mysearch(n:string): boolean;
  1829. {searches recursively for the device in the directory given by n,
  1830. returns true if found and sets the name of the device in ttyname}
  1831. var dirstream : pdir;
  1832. d : pdirent;
  1833. name : string;
  1834. st : stat;
  1835. begin
  1836. dirstream:=opendir(n);
  1837. if (linuxerror<>0) then
  1838. exit;
  1839. d:=Readdir(dirstream);
  1840. while (d<>nil) do
  1841. begin
  1842. name:=n+'/'+strpas(@(d^.name));
  1843. fstat(name,st);
  1844. if linuxerror=0 then
  1845. begin
  1846. if ((st.mode and $E000)=$4000) and { if it is a directory }
  1847. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  1848. (strpas(@(d^.name))<>'..') and
  1849. (strpas(@(d^.name))<>'') and
  1850. (strpas(@(d^.name))<>'fd') then
  1851. begin {we found a directory, search inside it}
  1852. if mysearch(name) then
  1853. begin {the device is here}
  1854. closedir(dirstream); {then don't continue searching}
  1855. mysearch:=true;
  1856. exit;
  1857. end;
  1858. end
  1859. else if (d^.ino=myino) and (st.dev=mydev) then
  1860. begin
  1861. closedir(dirstream);
  1862. ttyname:=name;
  1863. mysearch:=true;
  1864. exit;
  1865. end;
  1866. end;
  1867. d:=Readdir(dirstream);
  1868. end;
  1869. closedir(dirstream);
  1870. mysearch:=false;
  1871. end;
  1872. begin
  1873. TTYName:='';
  1874. fstat(handle,st);
  1875. if (errno<>0) and isatty (handle) then
  1876. exit;
  1877. mydev:=st.dev;
  1878. myino:=st.ino;
  1879. mysearch('/dev');
  1880. end;
  1881. function TTYName(var F:Text):string;
  1882. {
  1883. Idem as previous, only now for text variables;
  1884. }
  1885. begin
  1886. TTYName:=TTYName(textrec(f).handle);
  1887. end;
  1888. {******************************************************************************
  1889. Utility calls
  1890. ******************************************************************************}
  1891. Function Octal(l:longint):longint;
  1892. {
  1893. Convert an octal specified number to decimal;
  1894. }
  1895. var
  1896. octnr,
  1897. oct : longint;
  1898. begin
  1899. octnr:=0;
  1900. oct:=0;
  1901. while (l>0) do
  1902. begin
  1903. oct:=oct or ((l mod 10) shl octnr);
  1904. l:=l div 10;
  1905. inc(octnr,3);
  1906. end;
  1907. Octal:=oct;
  1908. end;
  1909. Function StringToPPChar(Var S:STring):ppchar;
  1910. {
  1911. Create a PPChar to structure of pchars which are the arguments specified
  1912. in the string S. Especially usefull for creating an ArgV for Exec-calls
  1913. }
  1914. var
  1915. nr : longint;
  1916. Buf : ^char;
  1917. p : ppchar;
  1918. begin
  1919. s:=s+#0;
  1920. buf:=@s[1];
  1921. nr:=0;
  1922. while(buf^<>#0) do
  1923. begin
  1924. while (buf^ in [' ',#8,#10]) do
  1925. inc(buf);
  1926. inc(nr);
  1927. while not (buf^ in [' ',#0,#8,#10]) do
  1928. inc(buf);
  1929. end;
  1930. getmem(p,nr*4);
  1931. StringToPPChar:=p;
  1932. if p=nil then
  1933. begin
  1934. LinuxError:=sys_enomem;
  1935. exit;
  1936. end;
  1937. buf:=@s[1];
  1938. while (buf^<>#0) do
  1939. begin
  1940. while (buf^ in [' ',#8,#10]) do
  1941. begin
  1942. buf^:=#0;
  1943. inc(buf);
  1944. end;
  1945. p^:=buf;
  1946. inc(p);
  1947. p^:=nil;
  1948. while not (buf^ in [' ',#0,#8,#10]) do
  1949. inc(buf);
  1950. end;
  1951. end;
  1952. {
  1953. function FExpand (const Path: PathStr): PathStr;
  1954. - declared in fexpand.inc
  1955. }
  1956. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1957. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1958. {$I fexpand.inc}
  1959. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1960. {$UNDEF FPC_FEXPAND_TILDE}
  1961. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1962. {
  1963. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1964. returns an empty string if not found. Wildcards are NOT allowed.
  1965. If dirlist is empty, it is set to '.'
  1966. }
  1967. Var
  1968. NewDir : PathStr;
  1969. p1 : Longint;
  1970. Info : Stat;
  1971. Begin
  1972. {Replace ':' with ';'}
  1973. for p1:=1to length(dirlist) do
  1974. if dirlist[p1]=':' then
  1975. dirlist[p1]:=';';
  1976. {Check for WildCards}
  1977. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1978. FSearch:='' {No wildcards allowed in these things.}
  1979. Else
  1980. Begin
  1981. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1982. Repeat
  1983. p1:=Pos(';',DirList);
  1984. If p1=0 Then
  1985. p1:=255;
  1986. NewDir:=Copy(DirList,1,P1 - 1);
  1987. if NewDir[Length(NewDir)]<>'/' then
  1988. NewDir:=NewDir+'/';
  1989. NewDir:=NewDir+Path;
  1990. Delete(DirList,1,p1);
  1991. if FStat(NewDir,Info) then
  1992. Begin
  1993. If Pos('./',NewDir)=1 Then
  1994. Delete(NewDir,1,2);
  1995. {DOS strips off an initial .\}
  1996. End
  1997. Else
  1998. NewDir:='';
  1999. Until (DirList='') or (Length(NewDir) > 0);
  2000. FSearch:=NewDir;
  2001. End;
  2002. End;
  2003. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  2004. Var
  2005. DotPos,SlashPos,i : longint;
  2006. Begin
  2007. SlashPos:=0;
  2008. DotPos:=256;
  2009. i:=Length(Path);
  2010. While (i>0) and (SlashPos=0) Do
  2011. Begin
  2012. If (DotPos=256) and (Path[i]='.') Then
  2013. begin
  2014. DotPos:=i;
  2015. end;
  2016. If (Path[i]='/') Then
  2017. SlashPos:=i;
  2018. Dec(i);
  2019. End;
  2020. Ext:=Copy(Path,DotPos,255);
  2021. Dir:=Copy(Path,1,SlashPos);
  2022. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  2023. End;
  2024. Function Dirname(Const path:pathstr):pathstr;
  2025. {
  2026. This function returns the directory part of a complete path.
  2027. Unless the directory is root '/', The last character is not
  2028. a slash.
  2029. }
  2030. var
  2031. Dir : PathStr;
  2032. Name : NameStr;
  2033. Ext : ExtStr;
  2034. begin
  2035. FSplit(Path,Dir,Name,Ext);
  2036. if length(Dir)>1 then
  2037. Delete(Dir,length(Dir),1);
  2038. DirName:=Dir;
  2039. end;
  2040. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2041. {
  2042. This function returns the filename part of a complete path. If suf is
  2043. supplied, it is cut off the filename.
  2044. }
  2045. var
  2046. Dir : PathStr;
  2047. Name : NameStr;
  2048. Ext : ExtStr;
  2049. begin
  2050. FSplit(Path,Dir,Name,Ext);
  2051. if Suf<>Ext then
  2052. Name:=Name+Ext;
  2053. BaseName:=Name;
  2054. end;
  2055. Function FNMatch(const Pattern,Name:string):Boolean;
  2056. Var
  2057. LenPat,LenName : longint;
  2058. Function DoFNMatch(i,j:longint):Boolean;
  2059. Var
  2060. Found : boolean;
  2061. Begin
  2062. Found:=true;
  2063. While Found and (i<=LenPat) Do
  2064. Begin
  2065. Case Pattern[i] of
  2066. '?' : Found:=(j<=LenName);
  2067. '*' : Begin
  2068. {find the next character in pattern, different of ? and *}
  2069. while Found and (i<LenPat) do
  2070. begin
  2071. inc(i);
  2072. case Pattern[i] of
  2073. '*' : ;
  2074. '?' : begin
  2075. inc(j);
  2076. Found:=(j<=LenName);
  2077. end;
  2078. else
  2079. Found:=false;
  2080. end;
  2081. end;
  2082. {Now, find in name the character which i points to, if the * or ?
  2083. wasn't the last character in the pattern, else, use up all the
  2084. chars in name}
  2085. Found:=true;
  2086. if (i<=LenPat) then
  2087. begin
  2088. repeat
  2089. {find a letter (not only first !) which maches pattern[i]}
  2090. while (j<=LenName) and (name[j]<>pattern[i]) do
  2091. inc (j);
  2092. if (j<LenName) then
  2093. begin
  2094. if DoFnMatch(i+1,j+1) then
  2095. begin
  2096. i:=LenPat;
  2097. j:=LenName;{we can stop}
  2098. Found:=true;
  2099. end
  2100. else
  2101. inc(j);{We didn't find one, need to look further}
  2102. end;
  2103. until (j>=LenName);
  2104. end
  2105. else
  2106. j:=LenName;{we can stop}
  2107. end;
  2108. else {not a wildcard character in pattern}
  2109. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2110. end;
  2111. inc(i);
  2112. inc(j);
  2113. end;
  2114. DoFnMatch:=Found and (j>LenName);
  2115. end;
  2116. Begin {start FNMatch}
  2117. LenPat:=Length(Pattern);
  2118. LenName:=Length(Name);
  2119. FNMatch:=DoFNMatch(1,1);
  2120. End;
  2121. Procedure Globfree(var p : pglob);
  2122. {
  2123. Release memory occupied by pglob structure, and names in it.
  2124. sets p to nil.
  2125. }
  2126. var
  2127. temp : pglob;
  2128. begin
  2129. while assigned(p) do
  2130. begin
  2131. temp:=p^.next;
  2132. if assigned(p^.name) then
  2133. freemem(p^.name);
  2134. dispose(p);
  2135. p:=temp;
  2136. end;
  2137. end;
  2138. Function Glob(Const path:pathstr):pglob;
  2139. {
  2140. Fills a tglob structure with entries matching path,
  2141. and returns a pointer to it. Returns nil on error,
  2142. linuxerror is set accordingly.
  2143. }
  2144. var
  2145. temp,
  2146. temp2 : string[255];
  2147. thedir : pdir;
  2148. buffer : pdirent;
  2149. root,
  2150. current : pglob;
  2151. begin
  2152. { Get directory }
  2153. temp:=dirname(path);
  2154. if temp='' then
  2155. temp:='.';
  2156. temp:=temp+#0;
  2157. thedir:=opendir(@temp[1]);
  2158. if thedir=nil then
  2159. begin
  2160. glob:=nil;
  2161. linuxerror:=errno;
  2162. exit;
  2163. end;
  2164. temp:=basename(path,''); { get the pattern }
  2165. if thedir^.fd<0 then
  2166. begin
  2167. linuxerror:=errno;
  2168. glob:=nil;
  2169. exit;
  2170. end;
  2171. {get the entries}
  2172. root:=nil;
  2173. current:=nil;
  2174. repeat
  2175. buffer:=Sys_readdir(thedir);
  2176. if buffer=nil then
  2177. break;
  2178. temp2:=strpas(@(buffer^.name[0]));
  2179. if fnmatch(temp,temp2) then
  2180. begin
  2181. if root=nil then
  2182. begin
  2183. new(root);
  2184. current:=root;
  2185. end
  2186. else
  2187. begin
  2188. new(current^.next);
  2189. current:=current^.next;
  2190. end;
  2191. if current=nil then
  2192. begin
  2193. linuxerror:=Sys_ENOMEM;
  2194. globfree(root);
  2195. break;
  2196. end;
  2197. current^.next:=nil;
  2198. getmem(current^.name,length(temp2)+1);
  2199. if current^.name=nil then
  2200. begin
  2201. linuxerror:=Sys_ENOMEM;
  2202. globfree(root);
  2203. break;
  2204. end;
  2205. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2206. end;
  2207. until false;
  2208. closedir(thedir);
  2209. glob:=root;
  2210. end;
  2211. {--------------------------------
  2212. FiledescriptorSets
  2213. --------------------------------}
  2214. Procedure FD_Zero(var fds:fdSet);
  2215. {
  2216. Clear the set of filedescriptors
  2217. }
  2218. begin
  2219. FillChar(fds,sizeof(fdSet),0);
  2220. end;
  2221. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2222. {
  2223. Remove fd from the set of filedescriptors
  2224. }
  2225. begin
  2226. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2227. end;
  2228. Procedure FD_Set(fd:longint;var fds:fdSet);
  2229. {
  2230. Add fd to the set of filedescriptors
  2231. }
  2232. begin
  2233. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2234. end;
  2235. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2236. {
  2237. Test if fd is part of the set of filedescriptors
  2238. }
  2239. begin
  2240. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2241. end;
  2242. Function GetFS (var T:Text):longint;
  2243. {
  2244. Get File Descriptor of a text file.
  2245. }
  2246. begin
  2247. if textrec(t).mode=fmclosed then
  2248. exit(-1)
  2249. else
  2250. GETFS:=textrec(t).Handle
  2251. end;
  2252. Function GetFS(Var F:File):longint;
  2253. {
  2254. Get File Descriptor of an unTyped file.
  2255. }
  2256. begin
  2257. { Handle and mode are on the same place in textrec and filerec. }
  2258. if filerec(f).mode=fmclosed then
  2259. exit(-1)
  2260. else
  2261. GETFS:=filerec(f).Handle
  2262. end;
  2263. {--------------------------------
  2264. Stat.Mode Macro's
  2265. --------------------------------}
  2266. Function S_ISLNK(m:word):boolean;
  2267. {
  2268. Check mode field of inode for link.
  2269. }
  2270. begin
  2271. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2272. end;
  2273. Function S_ISREG(m:word):boolean;
  2274. {
  2275. Check mode field of inode for regular file.
  2276. }
  2277. begin
  2278. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2279. end;
  2280. Function S_ISDIR(m:word):boolean;
  2281. {
  2282. Check mode field of inode for directory.
  2283. }
  2284. begin
  2285. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2286. end;
  2287. Function S_ISCHR(m:word):boolean;
  2288. {
  2289. Check mode field of inode for character device.
  2290. }
  2291. begin
  2292. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2293. end;
  2294. Function S_ISBLK(m:word):boolean;
  2295. {
  2296. Check mode field of inode for block device.
  2297. }
  2298. begin
  2299. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2300. end;
  2301. Function S_ISFIFO(m:word):boolean;
  2302. {
  2303. Check mode field of inode for named pipe (FIFO).
  2304. }
  2305. begin
  2306. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2307. end;
  2308. Function S_ISSOCK(m:word):boolean;
  2309. {
  2310. Check mode field of inode for socket.
  2311. }
  2312. begin
  2313. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2314. end;
  2315. {--------------------------------
  2316. Memory functions
  2317. --------------------------------}
  2318. {$IFDEF I386}
  2319. Procedure WritePort (Port : Longint; Value : Byte);
  2320. {
  2321. Writes 'Value' to port 'Port'
  2322. }
  2323. begin
  2324. asm
  2325. movl port,%edx
  2326. movb value,%al
  2327. outb %al,%dx
  2328. end ['EAX','EDX'];
  2329. end;
  2330. Procedure WritePort (Port : Longint; Value : Word);
  2331. {
  2332. Writes 'Value' to port 'Port'
  2333. }
  2334. begin
  2335. asm
  2336. movl port,%edx
  2337. movw value,%ax
  2338. outw %ax,%dx
  2339. end ['EAX','EDX'];
  2340. end;
  2341. Procedure WritePort (Port : Longint; Value : Longint);
  2342. {
  2343. Writes 'Value' to port 'Port'
  2344. }
  2345. begin
  2346. asm
  2347. movl port,%edx
  2348. movl value,%eax
  2349. outl %eax,%dx
  2350. end ['EAX','EDX'];
  2351. end;
  2352. Procedure WritePortB (Port : Longint; Value : Byte);
  2353. {
  2354. Writes 'Value' to port 'Port'
  2355. }
  2356. begin
  2357. asm
  2358. movl port,%edx
  2359. movb value,%al
  2360. outb %al,%dx
  2361. end ['EAX','EDX'];
  2362. end;
  2363. Procedure WritePortW (Port : Longint; Value : Word);
  2364. {
  2365. Writes 'Value' to port 'Port'
  2366. }
  2367. begin
  2368. asm
  2369. movl port,%edx
  2370. movw value,%ax
  2371. outw %ax,%dx
  2372. end ['EAX','EDX'];
  2373. end;
  2374. Procedure WritePortL (Port : Longint; Value : Longint);
  2375. {
  2376. Writes 'Value' to port 'Port'
  2377. }
  2378. begin
  2379. asm
  2380. movl port,%edx
  2381. movl value,%eax
  2382. outl %eax,%dx
  2383. end ['EAX','EDX'];
  2384. end;
  2385. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2386. {
  2387. Writes 'Count' longints from 'Buf' to Port
  2388. }
  2389. begin
  2390. asm
  2391. movl count,%ecx
  2392. movl buf,%esi
  2393. movl port,%edx
  2394. cld
  2395. rep
  2396. outsl
  2397. end ['ECX','ESI','EDX'];
  2398. end;
  2399. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2400. {
  2401. Writes 'Count' words from 'Buf' to Port
  2402. }
  2403. begin
  2404. asm
  2405. movl count,%ecx
  2406. movl buf,%esi
  2407. movl port,%edx
  2408. cld
  2409. rep
  2410. outsw
  2411. end ['ECX','ESI','EDX'];
  2412. end;
  2413. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2414. {
  2415. Writes 'Count' bytes from 'Buf' to Port
  2416. }
  2417. begin
  2418. asm
  2419. movl count,%ecx
  2420. movl buf,%esi
  2421. movl port,%edx
  2422. cld
  2423. rep
  2424. outsb
  2425. end ['ECX','ESI','EDX'];
  2426. end;
  2427. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2428. {
  2429. Reads 'Value' from port 'Port'
  2430. }
  2431. begin
  2432. asm
  2433. movl port,%edx
  2434. inb %dx,%al
  2435. movl value,%edx
  2436. movb %al,(%edx)
  2437. end ['EAX','EDX'];
  2438. end;
  2439. Procedure ReadPort (Port : Longint; Var Value : Word);
  2440. {
  2441. Reads 'Value' from port 'Port'
  2442. }
  2443. begin
  2444. asm
  2445. movl port,%edx
  2446. inw %dx,%ax
  2447. movl value,%edx
  2448. movw %ax,(%edx)
  2449. end ['EAX','EDX'];
  2450. end;
  2451. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2452. {
  2453. Reads 'Value' from port 'Port'
  2454. }
  2455. begin
  2456. asm
  2457. movl port,%edx
  2458. inl %dx,%eax
  2459. movl value,%edx
  2460. movl %eax,(%edx)
  2461. end ['EAX','EDX'];
  2462. end;
  2463. function ReadPortB (Port : Longint): Byte; assembler;
  2464. {
  2465. Reads a byte from port 'Port'
  2466. }
  2467. asm
  2468. xorl %eax,%eax
  2469. movl port,%edx
  2470. inb %dx,%al
  2471. end ['EAX','EDX'];
  2472. function ReadPortW (Port : Longint): Word; assembler;
  2473. {
  2474. Reads a word from port 'Port'
  2475. }
  2476. asm
  2477. xorl %eax,%eax
  2478. movl port,%edx
  2479. inw %dx,%ax
  2480. end ['EAX','EDX'];
  2481. function ReadPortL (Port : Longint): LongInt; assembler;
  2482. {
  2483. Reads a LongInt from port 'Port'
  2484. }
  2485. asm
  2486. movl port,%edx
  2487. inl %dx,%eax
  2488. end ['EAX','EDX'];
  2489. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  2490. {
  2491. Reads 'Count' longints from port 'Port' to 'Buf'.
  2492. }
  2493. begin
  2494. asm
  2495. movl count,%ecx
  2496. movl buf,%edi
  2497. movl port,%edx
  2498. cld
  2499. rep
  2500. insl
  2501. end ['ECX','EDI','EDX'];
  2502. end;
  2503. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  2504. {
  2505. Reads 'Count' words from port 'Port' to 'Buf'.
  2506. }
  2507. begin
  2508. asm
  2509. movl count,%ecx
  2510. movl buf,%edi
  2511. movl port,%edx
  2512. cld
  2513. rep
  2514. insw
  2515. end ['ECX','EDI','EDX'];
  2516. end;
  2517. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  2518. {
  2519. Reads 'Count' bytes from port 'Port' to 'Buf'.
  2520. }
  2521. begin
  2522. asm
  2523. movl count,%ecx
  2524. movl buf,%edi
  2525. movl port,%edx
  2526. cld
  2527. rep
  2528. insb
  2529. end ['ECX','EDI','EDX'];
  2530. end;
  2531. {$ENDIF}
  2532. Initialization
  2533. InitLocalTime;
  2534. finalization
  2535. DoneLocalTime;
  2536. End.
  2537. {
  2538. $Log$
  2539. Revision 1.19 2001-11-30 07:16:42 marco
  2540. * TTYname fix from Maarten Beekers. Apparantly accidentally not commited the first time.
  2541. Revision 1.18 2001/11/05 21:46:06 michael
  2542. + Added exit status examining functions
  2543. Revision 1.17 2001/10/14 13:33:21 peter
  2544. * start of thread support for linux
  2545. Revision 1.16 2001/09/17 21:36:31 peter
  2546. * merged fixes
  2547. Revision 1.15 2001/08/12 18:05:19 peter
  2548. * Range check fix with epoch (merged)
  2549. Revision 1.14 2001/07/12 12:42:39 marco
  2550. * Fixes to the FreeBSD compability of the datetime patches
  2551. Revision 1.13 2001/07/12 07:20:05 michael
  2552. + Added setdate/time/datetime functions
  2553. Revision 1.12 2001/06/20 15:24:47 marco
  2554. * readlink for Unix fix.
  2555. Revision 1.11 2001/06/19 17:19:50 marco
  2556. * Again uncommented readlink. Merges or typo, I dunno
  2557. Revision 1.10 2001/06/13 22:20:11 hajny
  2558. + platform specific information
  2559. Revision 1.9 2001/06/03 20:19:09 peter
  2560. * FSStat to StatFS
  2561. * StatFS structure to TStatFS
  2562. Revision 1.8 2001/06/02 00:31:31 peter
  2563. * merge unix updates from the 1.0 branch, mostly related to the
  2564. solaris target
  2565. Revision 1.7 2001/04/19 12:57:33 marco
  2566. * Readlink uncommented for FreeBSD.
  2567. Revision 1.6 2001/04/13 22:37:21 peter
  2568. * remove warning
  2569. Revision 1.5 2001/03/27 11:47:25 michael
  2570. + Fixed F_[G,S]etOwn constants. By Alexander Sychev
  2571. Revision 1.4 2001/03/17 16:04:37 hajny
  2572. * FExpand omission fixed
  2573. Revision 1.3 2001/03/16 20:09:58 hajny
  2574. * universal FExpand
  2575. Revision 1.2 2001/01/22 07:25:10 marco
  2576. * IOPERM for FreeBSD. Port routines moved from linsysca to Unix again .
  2577. Revision 1.1 2001/01/21 20:21:41 marco
  2578. * Rename fest II. Rtl OK
  2579. Revision 1.6 2000/12/28 20:42:12 peter
  2580. * ttyname fix from the mailinglist (merged)
  2581. Revision 1.5 2000/10/26 22:51:12 peter
  2582. * nano sleep (merged)
  2583. Revision 1.4 2000/10/11 13:59:16 marco
  2584. * FreeBSD TermIOS support and minor changes to some related files.
  2585. Revision 1.3 2000/10/10 12:02:35 marco
  2586. * Terminal stuff of Linux moved to separate file. I think it is too
  2587. much to do with ifdefs.
  2588. Revision 1.2 2000/09/18 13:14:50 marco
  2589. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2590. Revision 1.7 2000/09/12 08:51:43 marco
  2591. * fixed some small problems left from merging. (waitpid has now last param longint)
  2592. Revision 1.6 2000/09/11 14:05:31 marco
  2593. * FreeBSD support and removed old signalhandling
  2594. Revision 1.5 2000/09/06 20:47:34 peter
  2595. * removed previous fsplit() patch as it's not the correct behaviour for
  2596. LFNs. The code showing the bug could easily be adapted (merged)
  2597. Revision 1.4 2000/09/04 20:17:53 peter
  2598. * fixed previous commit (merged)
  2599. Revision 1.3 2000/09/04 19:38:13 peter
  2600. * fsplit with .. fix from Thomas (merged)
  2601. Revision 1.2 2000/07/13 11:33:48 michael
  2602. + removed logs
  2603. }