unix.pp 72 KB

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