linux.pp 72 KB

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