linux.pp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  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. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY;without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. Unit Linux;
  13. Interface
  14. { Get Types and Constants }
  15. {$i sysconst.inc}
  16. {$i systypes.inc}
  17. { Get System call numbers and error-numbers}
  18. {$i sysnr.inc}
  19. {$i errno.inc}
  20. var
  21. ErrNo,
  22. LinuxError : Longint;
  23. {********************
  24. Process
  25. ********************}
  26. const
  27. { cloning flags }
  28. CSIGNAL = $000000ff; // signal mask to be sent at exit
  29. CLONE_VM = $00000100; // set if VM shared between processes
  30. CLONE_FS = $00000200; // set if fs info shared between processes
  31. CLONE_FILES = $00000400; // set if open files shared between processes
  32. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  33. CLONE_PID = $00001000; // set if pid shared
  34. type
  35. TCloneFunc=function(args:pointer):longint;cdecl;
  36. const
  37. { For getting/setting priority }
  38. Prio_Process = 0;
  39. Prio_PGrp = 1;
  40. Prio_User = 2;
  41. WNOHANG = $1;
  42. WUNTRACED = $2;
  43. __WCLONE = $80000000;
  44. {********************
  45. File
  46. ********************}
  47. Const
  48. P_IN = 1;
  49. P_OUT = 2;
  50. Const
  51. LOCK_SH = 1;
  52. LOCK_EX = 2;
  53. LOCK_UN = 8;
  54. LOCK_NB = 4;
  55. Type
  56. Tpipe = array[1..2] of longint;
  57. pglob = ^tglob;
  58. tglob = record
  59. name : pchar;
  60. next : pglob;
  61. end;
  62. ComStr = String[255];
  63. PathStr = String[255];
  64. DirStr = String[255];
  65. NameStr = String[255];
  66. ExtStr = String[255];
  67. const
  68. { For testing access rights }
  69. R_OK = 4;
  70. W_OK = 2;
  71. X_OK = 1;
  72. F_OK = 0;
  73. { For File control mechanism }
  74. F_GetFd = 1;
  75. F_SetFd = 2;
  76. F_GetFl = 3;
  77. F_SetFl = 4;
  78. F_GetLk = 5;
  79. F_SetLk = 6;
  80. F_SetLkW = 7;
  81. F_GetOwn = 8;
  82. F_SetOwn = 9;
  83. {********************
  84. Signal
  85. ********************}
  86. Const
  87. { For sending a signal }
  88. SA_NOCLDSTOP = 1;
  89. SA_SHIRQ = $04000000;
  90. SA_STACK = $08000000;
  91. SA_RESTART = $10000000;
  92. SA_INTERRUPT = $20000000;
  93. SA_NOMASK = $40000000;
  94. SA_ONESHOT = $80000000;
  95. SIG_BLOCK = 0;
  96. SIG_UNBLOCK = 1;
  97. SIG_SETMASK = 2;
  98. SIG_DFL = 0 ;
  99. SIG_IGN = 1 ;
  100. SIG_ERR = -1 ;
  101. SIGHUP = 1;
  102. SIGINT = 2;
  103. SIGQUIT = 3;
  104. SIGILL = 4;
  105. SIGTRAP = 5;
  106. SIGABRT = 6;
  107. SIGIOT = 6;
  108. SIGBUS = 7;
  109. SIGFPE = 8;
  110. SIGKILL = 9;
  111. SIGUSR1 = 10;
  112. SIGSEGV = 11;
  113. SIGUSR2 = 12;
  114. SIGPIPE = 13;
  115. SIGALRM = 14;
  116. SIGTerm = 15;
  117. SIGSTKFLT = 16;
  118. SIGCHLD = 17;
  119. SIGCONT = 18;
  120. SIGSTOP = 19;
  121. SIGTSTP = 20;
  122. SIGTTIN = 21;
  123. SIGTTOU = 22;
  124. SIGURG = 23;
  125. SIGXCPU = 24;
  126. SIGXFSZ = 25;
  127. SIGVTALRM = 26;
  128. SIGPROF = 27;
  129. SIGWINCH = 28;
  130. SIGIO = 29;
  131. SIGPOLL = SIGIO;
  132. SIGPWR = 30;
  133. SIGUNUSED = 31;
  134. Type
  135. SignalHandler = Procedure(Sig : LongInt);cdecl;
  136. PSignalHandler = ^SignalHandler;
  137. SignalRestorer = Procedure;cdecl;
  138. PSignalRestorer = ^SignalRestorer;
  139. SigSet = Longint;
  140. PSigSet = ^SigSet;
  141. SigActionRec = packed record
  142. Sa_Handler : SignalHandler;
  143. Sa_Mask : SigSet;
  144. Sa_Flags : Longint;
  145. Sa_restorer : SignalRestorer; { Obsolete - Don't use }
  146. end;
  147. PSigActionRec = ^SigActionRec;
  148. {********************
  149. IOCtl(TermIOS)
  150. ********************}
  151. Const
  152. { Amount of Control Chars }
  153. NCCS = 19;
  154. NCC = 8;
  155. { For Terminal handling }
  156. TCGETS = $5401;
  157. TCSETS = $5402;
  158. TCSETSW = $5403;
  159. TCSETSF = $5404;
  160. TCGETA = $5405;
  161. TCSETA = $5406;
  162. TCSETAW = $5407;
  163. TCSETAF = $5408;
  164. TCSBRK = $5409;
  165. TCXONC = $540A;
  166. TCFLSH = $540B;
  167. TIOCEXCL = $540C;
  168. TIOCNXCL = $540D;
  169. TIOCSCTTY = $540E;
  170. TIOCGPGRP = $540F;
  171. TIOCSPGRP = $5410;
  172. TIOCOUTQ = $5411;
  173. TIOCSTI = $5412;
  174. TIOCGWINSZ = $5413;
  175. TIOCSWINSZ = $5414;
  176. TIOCMGET = $5415;
  177. TIOCMBIS = $5416;
  178. TIOCMBIC = $5417;
  179. TIOCMSET = $5418;
  180. TIOCGSOFTCAR = $5419;
  181. TIOCSSOFTCAR = $541A;
  182. FIONREAD = $541B;
  183. TIOCINQ = FIONREAD;
  184. TIOCLINUX = $541C;
  185. TIOCCONS = $541D;
  186. TIOCGSERIAL = $541E;
  187. TIOCSSERIAL = $541F;
  188. TIOCPKT = $5420;
  189. FIONBIO = $5421;
  190. TIOCNOTTY = $5422;
  191. TIOCSETD = $5423;
  192. TIOCGETD = $5424;
  193. TCSBRKP = $5425;
  194. TIOCTTYGSTRUCT = $5426;
  195. FIONCLEX = $5450;
  196. FIOCLEX = $5451;
  197. FIOASYNC = $5452;
  198. TIOCSERCONFIG = $5453;
  199. TIOCSERGWILD = $5454;
  200. TIOCSERSWILD = $5455;
  201. TIOCGLCKTRMIOS = $5456;
  202. TIOCSLCKTRMIOS = $5457;
  203. TIOCSERGSTRUCT = $5458;
  204. TIOCSERGETLSR = $5459;
  205. TIOCSERGETMULTI = $545A;
  206. TIOCSERSETMULTI = $545B;
  207. TIOCMIWAIT = $545C;
  208. TIOCGICOUNT = $545D;
  209. TIOCPKT_DATA = 0;
  210. TIOCPKT_FLUSHREAD = 1;
  211. TIOCPKT_FLUSHWRITE = 2;
  212. TIOCPKT_STOP = 4;
  213. TIOCPKT_START = 8;
  214. TIOCPKT_NOSTOP = 16;
  215. TIOCPKT_DOSTOP = 32;
  216. Type
  217. winsize = packed record
  218. ws_row,
  219. ws_col,
  220. ws_xpixel,
  221. ws_ypixel : word;
  222. end;
  223. TWinSize=winsize;
  224. Termio = packed record
  225. c_iflag, { input mode flags }
  226. c_oflag, { output mode flags }
  227. c_cflag, { control mode flags }
  228. c_lflag : Word; { local mode flags }
  229. c_line : Word; { line discipline - careful, only High byte in use}
  230. c_cc : array [0..NCC-1] of char;{ control characters }
  231. end;
  232. TTermio=Termio;
  233. Termios = packed record
  234. c_iflag,
  235. c_oflag,
  236. c_cflag,
  237. c_lflag : longint;
  238. c_line : char;
  239. c_cc : array[0..NCCS-1] of byte;
  240. end;
  241. TTermios=Termios;
  242. const
  243. InitCC:array[0..NCCS-1] of byte=(3,34,177,25,4,0,1,0,21,23,32,0,22,17,27,26,0,0,0);
  244. const
  245. {c_cc characters}
  246. VINTR = 0;
  247. VQUIT = 1;
  248. VERASE = 2;
  249. VKILL = 3;
  250. VEOF = 4;
  251. VTIME = 5;
  252. VMIN = 6;
  253. VSWTC = 7;
  254. VSTART = 8;
  255. VSTOP = 9;
  256. VSUSP = 10;
  257. VEOL = 11;
  258. VREPRINT = 12;
  259. VDISCARD = 13;
  260. VWERASE = 14;
  261. VLNEXT = 15;
  262. VEOL2 = 16;
  263. {c_iflag bits}
  264. IGNBRK = $0000001;
  265. BRKINT = $0000002;
  266. IGNPAR = $0000004;
  267. PARMRK = $0000008;
  268. INPCK = $0000010;
  269. ISTRIP = $0000020;
  270. INLCR = $0000040;
  271. IGNCR = $0000080;
  272. ICRNL = $0000100;
  273. IUCLC = $0000200;
  274. IXON = $0000400;
  275. IXANY = $0000800;
  276. IXOFF = $0001000;
  277. IMAXBEL = $0002000;
  278. {c_oflag bits}
  279. OPOST = $0000001;
  280. OLCUC = $0000002;
  281. ONLCR = $0000004;
  282. OCRNL = $0000008;
  283. ONOCR = $0000010;
  284. ONLRET = $0000020;
  285. OFILL = $0000040;
  286. OFDEL = $0000080;
  287. NLDLY = $0000100;
  288. NL0 = $0000000;
  289. NL1 = $0000100;
  290. CRDLY = $0000600;
  291. CR0 = $0000000;
  292. CR1 = $0000200;
  293. CR2 = $0000400;
  294. CR3 = $0000600;
  295. TABDLY = $0001800;
  296. TAB0 = $0000000;
  297. TAB1 = $0000800;
  298. TAB2 = $0001000;
  299. TAB3 = $0001800;
  300. XTABS = $0001800;
  301. BSDLY = $0002000;
  302. BS0 = $0000000;
  303. BS1 = $0002000;
  304. VTDLY = $0004000;
  305. VT0 = $0000000;
  306. VT1 = $0004000;
  307. FFDLY = $0008000;
  308. FF0 = $0000000;
  309. FF1 = $0008000;
  310. {c_cflag bits}
  311. CBAUD = $000100F;
  312. B0 = $0000000;
  313. B50 = $0000001;
  314. B75 = $0000002;
  315. B110 = $0000003;
  316. B134 = $0000004;
  317. B150 = $0000005;
  318. B200 = $0000006;
  319. B300 = $0000007;
  320. B600 = $0000008;
  321. B1200 = $0000009;
  322. B1800 = $000000A;
  323. B2400 = $000000B;
  324. B4800 = $000000C;
  325. B9600 = $000000D;
  326. B19200 = $000000E;
  327. B38400 = $000000F;
  328. EXTA = B19200;
  329. EXTB = B38400;
  330. CSIZE = $0000030;
  331. CS5 = $0000000;
  332. CS6 = $0000010;
  333. CS7 = $0000020;
  334. CS8 = $0000030;
  335. CSTOPB = $0000040;
  336. CREAD = $0000080;
  337. PARENB = $0000100;
  338. PARODD = $0000200;
  339. HUPCL = $0000400;
  340. CLOCAL = $0000800;
  341. CBAUDEX = $0001000;
  342. B57600 = $0001001;
  343. B115200 = $0001002;
  344. B230400 = $0001003;
  345. B460800 = $0001004;
  346. CIBAUD = $100F0000;
  347. CMSPAR = $40000000;
  348. CRTSCTS = $80000000;
  349. {c_lflag bits}
  350. ISIG = $0000001;
  351. ICANON = $0000002;
  352. XCASE = $0000004;
  353. ECHO = $0000008;
  354. ECHOE = $0000010;
  355. ECHOK = $0000020;
  356. ECHONL = $0000040;
  357. NOFLSH = $0000080;
  358. TOSTOP = $0000100;
  359. ECHOCTL = $0000200;
  360. ECHOPRT = $0000400;
  361. ECHOKE = $0000800;
  362. FLUSHO = $0001000;
  363. PENDIN = $0004000;
  364. IEXTEN = $0008000;
  365. {c_line bits}
  366. TIOCM_LE = $001;
  367. TIOCM_DTR = $002;
  368. TIOCM_RTS = $004;
  369. TIOCM_ST = $008;
  370. TIOCM_SR = $010;
  371. TIOCM_CTS = $020;
  372. TIOCM_CAR = $040;
  373. TIOCM_RNG = $080;
  374. TIOCM_DSR = $100;
  375. TIOCM_CD = TIOCM_CAR;
  376. TIOCM_RI = TIOCM_RNG;
  377. TIOCM_OUT1 = $2000;
  378. TIOCM_OUT2 = $4000;
  379. {TCSetAttr}
  380. TCSANOW = 0;
  381. TCSADRAIN = 1;
  382. TCSAFLUSH = 2;
  383. {TCFlow}
  384. TCOOFF = 0;
  385. TCOON = 1;
  386. TCIOFF = 2;
  387. TCION = 3;
  388. {TCFlush}
  389. TCIFLUSH = 0;
  390. TCOFLUSH = 1;
  391. TCIOFLUSH = 2;
  392. {********************
  393. Info
  394. ********************}
  395. Type
  396. UTimBuf = packed record
  397. actime,modtime : Longint;
  398. end;
  399. UTimeBuf=UTimBuf;
  400. TUTimeBuf=UTimeBuf;
  401. PUTimeBuf=^UTimeBuf;
  402. TSysinfo = packed record
  403. uptime : longint;
  404. loads : array[1..3] of longint;
  405. totalram,
  406. freeram,
  407. sharedram,
  408. bufferram,
  409. totalswap,
  410. freeswap : longint;
  411. procs : integer;
  412. s : string[18];
  413. end;
  414. PSysInfo = ^TSysInfo;
  415. {******************************************************************************
  416. Procedure/Functions
  417. ******************************************************************************}
  418. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  419. {**************************
  420. Time/Date Handling
  421. ***************************}
  422. var
  423. tzdaylight : boolean;
  424. tzseconds : longint;
  425. tzname : array[boolean] of pchar;
  426. { timezone support }
  427. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  428. procedure GetLocalTimezone(timer:longint);
  429. procedure ReadTimezoneFile(fn:string);
  430. function GetTimezoneFile:string;
  431. Procedure GetTimeOfDay(var tv:timeval);
  432. Function GetTimeOfDay:longint;
  433. Function GetEpochTime: longint;
  434. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  435. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  436. procedure GetTime(var hour,min,sec,msec,usec:word);
  437. procedure GetTime(var hour,min,sec,sec100:word);
  438. procedure GetTime(var hour,min,sec:word);
  439. Procedure GetDate(Var Year,Month,Day:Word);
  440. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  441. {**************************
  442. Process Handling
  443. ***************************}
  444. function CreateShellArgV(const prog:string):ppchar;
  445. function CreateShellArgV(const prog:Ansistring):ppchar;
  446. Procedure Execve(Path:pathstr;args:ppchar;ep:ppchar);
  447. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  448. Procedure Execv(const path:pathstr;args:ppchar);
  449. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  450. Procedure Execl(const Todo:string);
  451. Procedure Execle(Todo:string;Ep:ppchar);
  452. Procedure Execlp(Todo:string;Ep:ppchar);
  453. Function Shell(const Command:String):Longint;
  454. Function Shell(const Command:AnsiString):Longint;
  455. Function Fork:longint;
  456. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  457. Procedure ExitProcess(val:longint);
  458. Function WaitPid(Pid:longint;Status:pointer;Options:Integer):Longint;
  459. Procedure Nice(N:integer);
  460. Function GetPriority(Which,Who:Integer):integer;
  461. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  462. Function GetPid:LongInt;
  463. Function GetPPid:LongInt;
  464. Function GetUid:Longint;
  465. Function GetEUid:Longint;
  466. Function GetGid:Longint;
  467. Function GetEGid:Longint;
  468. {**************************
  469. File Handling
  470. ***************************}
  471. Function fdOpen(pathname:string;flags:longint):longint;
  472. Function fdOpen(pathname:string;flags,mode:longint):longint;
  473. Function fdOpen(pathname:pchar;flags:longint):longint;
  474. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  475. Function fdClose(fd:longint):boolean;
  476. Function fdRead(fd:longint;var buf;size:longint):longint;
  477. Function fdWrite(fd:longint;var buf;size:longint):longint;
  478. Function fdTruncate(fd,size:longint):boolean;
  479. Function fdSeek (fd,pos,seektype :longint): longint;
  480. Function fdFlush (fd : Longint) : Boolean;
  481. Function Link(OldPath,NewPath:pathstr):boolean;
  482. Function SymLink(OldPath,NewPath:pathstr):boolean;
  483. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  484. Function ReadLink(name:pathstr):pathstr;
  485. Function UnLink(Path:pathstr):boolean;
  486. Function UnLink(Path:pchar):Boolean;
  487. Function FReName (OldName,NewName : Pchar) : Boolean;
  488. Function FReName (OldName,NewName : String) : Boolean;
  489. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  490. Function Chmod(path:pathstr;Newmode:longint):boolean;
  491. Function Utime(path:pathstr;utim:utimebuf):boolean;
  492. Function Access(Path:Pathstr ;mode:integer):boolean;
  493. Function Umask(Mask:Integer):integer;
  494. Function Flock (fd,mode : longint) : boolean;
  495. Function Flock (var T : text;mode : longint) : boolean;
  496. Function Flock (var F : File;mode : longint) : boolean;
  497. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  498. Function FStat(Fd:longint;Var Info:stat):Boolean;
  499. Function FStat(var F:Text;Var Info:stat):Boolean;
  500. Function FStat(var F:File;Var Info:stat):Boolean;
  501. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  502. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  503. Function FSStat(Fd: Longint;Var Info:statfs):Boolean;
  504. Function Fcntl(Fd:longint;Cmd:Integer):integer;
  505. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  506. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  507. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  508. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  509. Function Dup(var oldfile,newfile:text):Boolean;
  510. Function Dup(var oldfile,newfile:file):Boolean;
  511. Function Dup2(oldfile,newfile:longint):Boolean;
  512. Function Dup2(var oldfile,newfile:text):Boolean;
  513. Function Dup2(var oldfile,newfile:file):Boolean;
  514. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  515. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  516. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  517. {**************************
  518. Directory Handling
  519. ***************************}
  520. Function OpenDir(f:pchar):pdir;
  521. Function OpenDir(f: String):pdir;
  522. function CloseDir(p:pdir):integer;
  523. Function ReadDir(p:pdir):pdirent;
  524. procedure SeekDir(p:pdir;off:longint);
  525. function TellDir(p:pdir):longint;
  526. {**************************
  527. Pipe/Fifo/Stream
  528. ***************************}
  529. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  530. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  531. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  532. Function PClose(Var F:text) : longint;
  533. Function PClose(Var F:file) : longint;
  534. Procedure POpen(var F:text;const Prog:String;rw:char);
  535. Procedure POpen(var F:file;const Prog:String;rw:char);
  536. Function mkFifo(pathname:string;mode:longint):boolean;
  537. Procedure AssignStream(Var StreamIn,Streamout:text;Const Prog:String);
  538. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  539. {**************************
  540. General information
  541. ***************************}
  542. Function GetDomainName:String;
  543. Function GetHostName:String;
  544. Function GetEnv(P:string):Pchar;
  545. Function Sysinfo(var Info:TSysinfo):Boolean;
  546. Function Uname(var unamerec:utsname):Boolean;
  547. {**************************
  548. Signal
  549. ***************************}
  550. Procedure SigAction(Signum:Integer;Var Act,OldAct:PSigActionRec );
  551. Procedure SigProcMask (How:Integer;SSet,OldSSet:PSigSet);
  552. Function SigPending:SigSet;
  553. Procedure SigSuspend(Mask:Sigset);
  554. Function Signal(Signum:Integer;Handler:SignalHandler):SignalHandler;
  555. Function Kill(Pid:longint;Sig:integer):integer;
  556. Procedure SigRaise(Sig:integer);
  557. Function Alarm(Sec : Longint) : longint;
  558. Procedure Pause;
  559. {**************************
  560. IOCtl/Termios Functions
  561. ***************************}
  562. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  563. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  564. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  565. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  566. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  567. Procedure CFMakeRaw(var tios:TermIOS);
  568. Function TCSendBreak(fd,duration:longint):boolean;
  569. Function TCSetPGrp(fd,id:longint):boolean;
  570. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  571. Function TCFlush(fd,qsel:longint):boolean;
  572. Function TCDrain(fd:longint):boolean;
  573. Function TCFlow(fd,act:longint):boolean;
  574. Function IsATTY(Handle:Longint):Boolean;
  575. Function IsATTY(f:text):Boolean;
  576. function TTYname(Handle:Longint):string;
  577. function TTYname(var F:Text):string;
  578. {**************************
  579. Memory functions
  580. ***************************}
  581. const
  582. PROT_READ = $1; { page can be read }
  583. PROT_WRITE = $2; { page can be written }
  584. PROT_EXEC = $4; { page can be executed }
  585. PROT_NONE = $0; { page can not be accessed }
  586. MAP_SHARED = $1; { Share changes }
  587. MAP_PRIVATE = $2; { Changes are private }
  588. MAP_TYPE = $f; { Mask for type of mapping }
  589. MAP_FIXED = $10; { Interpret addr exactly }
  590. MAP_ANONYMOUS = $20; { don't use a file }
  591. MAP_GROWSDOWN = $100; { stack-like segment }
  592. MAP_DENYWRITE = $800; { ETXTBSY }
  593. MAP_EXECUTABLE = $1000; { mark it as an executable }
  594. MAP_LOCKED = $2000; { pages are locked }
  595. MAP_NORESERVE = $4000; { don't check for reservations }
  596. type
  597. tmmapargs=record
  598. address : longint;
  599. size : longint;
  600. prot : longint;
  601. flags : longint;
  602. fd : longint;
  603. offset : longint;
  604. end;
  605. function MMap(const m:tmmapargs):longint;
  606. {**************************
  607. Port IO functions
  608. ***************************}
  609. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  610. {$IFDEF I386}
  611. Procedure WritePort (Port : Longint; Value : Byte);
  612. Procedure WritePort (Port : Longint; Value : Word);
  613. Procedure WritePort (Port : Longint; Value : Longint);
  614. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  615. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  616. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  617. Procedure ReadPort (Port : Longint; Var Value : Byte);
  618. Procedure ReadPort (Port : Longint; Var Value : Word);
  619. Procedure ReadPort (Port : Longint; Var Value : Longint);
  620. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  621. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  622. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  623. {$ENDIF}
  624. {**************************
  625. Utility functions
  626. ***************************}
  627. Function Octal(l:longint):longint;
  628. Function FExpand(Const Path: PathStr):PathStr;
  629. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  630. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  631. Function Dirname(Const path:pathstr):pathstr;
  632. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  633. Function FNMatch(const Pattern,Name:string):Boolean;
  634. Function Glob(Const path:pathstr):pglob;
  635. Procedure Globfree(var p:pglob);
  636. Function StringToPPChar(Var S:STring):ppchar;
  637. Function GetFS(var T:Text):longint;
  638. Function GetFS(Var F:File):longint;
  639. {Filedescriptorsets}
  640. Procedure FD_Zero(var fds:fdSet);
  641. Procedure FD_Clr(fd:longint;var fds:fdSet);
  642. Procedure FD_Set(fd:longint;var fds:fdSet);
  643. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  644. {Stat.Mode Types}
  645. Function S_ISLNK(m:word):boolean;
  646. Function S_ISREG(m:word):boolean;
  647. Function S_ISDIR(m:word):boolean;
  648. Function S_ISCHR(m:word):boolean;
  649. Function S_ISBLK(m:word):boolean;
  650. Function S_ISFIFO(m:word):boolean;
  651. Function S_ISSOCK(m:word):boolean;
  652. {******************************************************************************
  653. Implementation
  654. ******************************************************************************}
  655. Implementation
  656. Uses Strings;
  657. { Get the definitions of textrec and filerec }
  658. {$i textrec.inc}
  659. {$i filerec.inc}
  660. { Raw System calls are in Syscalls.inc}
  661. {$i syscalls.inc}
  662. {******************************************************************************
  663. Process related calls
  664. ******************************************************************************}
  665. function CreateShellArgV(const prog:string):ppchar;
  666. {
  667. Create an argv which executes a command in a shell using /bin/sh -c
  668. }
  669. var
  670. pp,p : ppchar;
  671. temp : string;
  672. begin
  673. getmem(pp,4*4);
  674. temp:='/bin/sh'#0'-c'#0+prog+#0;
  675. p:=pp;
  676. p^:=@temp[1];
  677. inc(p);
  678. p^:=@temp[9];
  679. inc(p);
  680. p^:=@temp[12];
  681. inc(p);
  682. p^:=Nil;
  683. CreateShellArgV:=pp;
  684. end;
  685. function CreateShellArgV(const prog:Ansistring):ppchar;
  686. {
  687. Create an argv which executes a command in a shell using /bin/sh -c
  688. using a AnsiString;
  689. }
  690. var
  691. pp,p : ppchar;
  692. temp : AnsiString;
  693. begin
  694. getmem(pp,4*4);
  695. temp:='/bin/sh'#0'-c'#0+prog+#0;
  696. p:=pp;
  697. GetMem(p^,Length(Temp));
  698. Move(@Temp[1],p^^,Length(Temp));
  699. inc(p);
  700. p^:=@pp[0][8];
  701. inc(p);
  702. p^:=@pp[0][11];
  703. inc(p);
  704. p^:=Nil;
  705. CreateShellArgV:=pp;
  706. end;
  707. Function Fork:longint;
  708. {
  709. This function issues the 'fork' System call. the program is duplicated in memory
  710. and Execution continues in parent and child process.
  711. In the parent process, fork returns the PID of the child. In the child process,
  712. zero is returned.
  713. A negative value indicates that an error has occurred, the error is returned in
  714. LinuxError.
  715. }
  716. var
  717. regs:SysCallregs;
  718. begin
  719. Fork:=SysCall(SysCall_nr_fork,regs);
  720. LinuxError:=Errno;
  721. End;
  722. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  723. begin
  724. if (pointer(func)=nil) or (sp=nil) then
  725. begin
  726. LinuxError:=Sys_EInval;
  727. exit;
  728. end;
  729. asm
  730. { Insert the argument onto the new stack. }
  731. movl sp,%ecx
  732. subl $8,%ecx
  733. movl args,%eax
  734. movl %eax,4(%ecx)
  735. { Save the function pointer as the zeroth argument.
  736. It will be popped off in the child in the ebx frobbing below. }
  737. movl func,%eax
  738. movl %eax,0(%ecx)
  739. { Do the system call }
  740. pushl %ebx
  741. movl flags,%ebx
  742. movl SysCall_nr_clone,%eax
  743. int $0x80
  744. popl %ebx
  745. test %eax,%eax
  746. jnz .Lclone_end
  747. { We're in the new thread }
  748. subl %ebp,%ebp { terminate the stack frame }
  749. call *%ebx
  750. { exit process }
  751. movl %eax,%ebx
  752. movl $1,%eax
  753. int $0x80
  754. .Lclone_end:
  755. movl %eax,__RESULT
  756. end;
  757. end;
  758. Procedure Execve(path:pathstr;args:ppchar;ep:ppchar);
  759. {
  760. Replaces the current program by the program specified in path,
  761. arguments in args are passed to Execve.
  762. environment specified in ep is passed on.
  763. }
  764. var
  765. regs:SysCallregs;
  766. begin
  767. path:=path+#0;
  768. regs.reg2:=longint(@path[1]);
  769. regs.reg3:=longint(args);
  770. regs.reg4:=longint(ep);
  771. SysCall(SysCall_nr_Execve,regs);
  772. { This only gets set when the call fails, otherwise we don't get here ! }
  773. Linuxerror:=errno;
  774. end;
  775. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  776. {
  777. Replaces the current program by the program specified in path,
  778. arguments in args are passed to Execve.
  779. environment specified in ep is passed on.
  780. }
  781. var
  782. regs:SysCallregs;
  783. begin
  784. regs.reg2:=longint(path);
  785. regs.reg3:=longint(args);
  786. regs.reg4:=longint(ep);
  787. SysCall(SysCall_nr_Execve,regs);
  788. { This only gets set when the call fails, otherwise we don't get here ! }
  789. Linuxerror:=errno;
  790. end;
  791. Procedure Execv(const path:pathstr;args:ppchar);
  792. {
  793. Replaces the current program by the program specified in path,
  794. arguments in args are passed to Execve.
  795. the current environment is passed on.
  796. }
  797. begin
  798. Execve(path,args,envp); {On error linuxerror will get set there}
  799. end;
  800. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  801. {
  802. This does the same as Execve, only it searches the PATH environment
  803. for the place of the Executable, except when Path starts with a slash.
  804. if the PATH environment variable is unavailable, the path is set to '.'
  805. }
  806. var
  807. thepath : string;
  808. begin
  809. if path[1]<>'/' then
  810. begin
  811. Thepath:=strpas(getenv('PATH'));
  812. if thepath='' then
  813. thepath:='.';
  814. Path:=FSearch(path,thepath)
  815. end
  816. else
  817. Path:='';
  818. if Path='' then
  819. linuxerror:=Sys_enoent
  820. else
  821. Execve(Path,args,ep);{On error linuxerror will get set there}
  822. end;
  823. Procedure Execle(Todo:string;Ep:ppchar);
  824. {
  825. This procedure takes the string 'Todo', parses it for command and
  826. command options, and Executes the command with the given options.
  827. The string 'Todo' shoud be of the form 'command options', options
  828. separated by commas.
  829. the PATH environment is not searched for 'command'.
  830. The specified environment(in 'ep') is passed on to command
  831. }
  832. var
  833. p : ppchar;
  834. begin
  835. p:=StringToPPChar(ToDo);
  836. if (p=nil) or (p^=nil) then
  837. exit;
  838. ExecVE(p^,p,EP);
  839. end;
  840. Procedure Execl(const Todo:string);
  841. {
  842. This procedure takes the string 'Todo', parses it for command and
  843. command options, and Executes the command with the given options.
  844. The string 'Todo' shoud be of the form 'command options', options
  845. separated by commas.
  846. the PATH environment is not searched for 'command'.
  847. The current environment is passed on to command
  848. }
  849. begin
  850. ExecLE(ToDo,EnvP);
  851. end;
  852. Procedure Execlp(Todo:string;Ep:ppchar);
  853. {
  854. This procedure takes the string 'Todo', parses it for command and
  855. command options, and Executes the command with the given options.
  856. The string 'Todo' shoud be of the form 'command options', options
  857. separated by commas.
  858. the PATH environment is searched for 'command'.
  859. The specified environment (in 'ep') is passed on to command
  860. }
  861. var
  862. p : ppchar;
  863. begin
  864. p:=StringToPPchar(todo);
  865. if (p=nil) or (p^=nil) then
  866. exit;
  867. ExecVP(StrPas(p^),p,EP);
  868. end;
  869. Procedure ExitProcess(val:longint);
  870. var
  871. regs : SysCallregs;
  872. begin
  873. regs.reg2:=val;
  874. SysCall(SysCall_nr_exit,regs);
  875. end;
  876. Function WaitPid(Pid:longint;Status:pointer;Options:Integer):Longint;
  877. {
  878. Waits until a child with PID Pid exits, or returns if it is exited already.
  879. Any resources used by the child are freed.
  880. The exit status is reported in the adress referred to by Status. It should
  881. be a longint.
  882. }
  883. var
  884. regs : SysCallregs;
  885. begin
  886. regs.reg2:=pid;
  887. regs.reg3:=longint(status);
  888. regs.reg4:=options;
  889. WaitPid:=SysCall(SysCall_nr_waitpid,regs);
  890. LinuxError:=errno;
  891. end;
  892. Function Shell(const Command:String):Longint;
  893. {
  894. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  895. The current environment is passed to the shell.
  896. It waits for the shell to exit, and returns its exit status.
  897. If the Exec call failed exit status 127 is reported.
  898. }
  899. var
  900. p : ppchar;
  901. temp,pid : longint;
  902. begin
  903. pid:=fork;
  904. if pid=-1 then
  905. exit; {Linuxerror already set in Fork}
  906. if pid=0 then
  907. begin
  908. {This is the child.}
  909. p:=CreateShellArgv(command);
  910. Execve(p^,p,envp);
  911. exit(127);
  912. end;
  913. temp:=0;
  914. WaitPid(pid,@temp,0);{Linuxerror is set there}
  915. Shell:=temp;{ Return exit status }
  916. end;
  917. Function Shell(const Command:AnsiString):Longint;
  918. {
  919. AnsiString version of Shell
  920. }
  921. var
  922. p : ppchar;
  923. temp,pid : longint;
  924. begin
  925. pid:=fork;
  926. if pid=-1 then
  927. exit; {Linuxerror already set in Fork}
  928. if pid=0 then
  929. begin
  930. {This is the child.}
  931. p:=CreateShellArgv(command);
  932. Execve(p^,p,envp);
  933. exit(127);
  934. end;
  935. temp:=0;
  936. WaitPid(pid,@temp,0);{Linuxerror is set there}
  937. Shell:=temp;{ Return exit status }
  938. end;
  939. Function GetPriority(Which,Who:Integer):integer;
  940. {
  941. Get Priority of process, process group, or user.
  942. Which : selects what kind of priority is used.
  943. can be one of the following predefined Constants :
  944. Prio_User.
  945. Prio_PGrp.
  946. Prio_Process.
  947. Who : depending on which, this is , respectively :
  948. Uid
  949. Pid
  950. Process Group id
  951. Errors are reported in linuxerror _only_. (priority can be negative)
  952. }
  953. var
  954. sr : Syscallregs;
  955. begin
  956. errno:=0;
  957. if (which<prio_process) or (which>prio_user) then
  958. begin
  959. { We can save an interrupt here }
  960. getpriority:=0;
  961. linuxerror:=Sys_einval;
  962. end
  963. else
  964. begin
  965. sr.reg2:=which;
  966. sr.reg3:=who;
  967. getpriority:=SysCall(Syscall_nr_getpriority,sr);
  968. linuxerror:=errno;
  969. end;
  970. end;
  971. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  972. {
  973. Set Priority of process, process group, or user.
  974. Which : selects what kind of priority is used.
  975. can be one of the following predefined Constants :
  976. Prio_User.
  977. Prio_PGrp.
  978. Prio_Process.
  979. Who : depending on value of which, this is, respectively :
  980. Uid
  981. Pid
  982. Process Group id
  983. what : A number between -20 and 20. -20 is most favorable, 20 least.
  984. 0 is the default.
  985. }
  986. var
  987. sr : Syscallregs;
  988. begin
  989. errno:=0;
  990. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  991. linuxerror:=Sys_einval { We can save an interrupt here }
  992. else
  993. begin
  994. sr.reg2:=which;
  995. sr.reg3:=who;
  996. sr.reg4:=what;
  997. SysCall(Syscall_nr_setpriority,sr);
  998. linuxerror:=errno;
  999. end;
  1000. end;
  1001. Procedure Nice(N:integer);
  1002. {
  1003. Set process priority. A positive N means a lower priority.
  1004. A negative N decreases priority.
  1005. }
  1006. var
  1007. sr : Syscallregs;
  1008. begin
  1009. sr.reg2:=n;
  1010. SysCall(Syscall_nr_nice,sr);
  1011. linuxerror:=errno;
  1012. end;
  1013. Function GetPid:LongInt;
  1014. {
  1015. Get Process ID.
  1016. }
  1017. var
  1018. regs : SysCallregs;
  1019. begin
  1020. GetPid:=SysCall(SysCall_nr_getpid,regs);
  1021. linuxerror:=errno;
  1022. end;
  1023. Function GetPPid:LongInt;
  1024. {
  1025. Get Process ID of parent process.
  1026. }
  1027. var
  1028. regs : SysCallregs;
  1029. begin
  1030. GetPpid:=SysCall(SysCall_nr_getppid,regs);
  1031. linuxerror:=errno;
  1032. end;
  1033. Function GetUid:Longint;
  1034. {
  1035. Get User ID.
  1036. }
  1037. var
  1038. regs : SysCallregs;
  1039. begin
  1040. GetUid:=SysCall(SysCall_nr_getuid,regs);
  1041. Linuxerror:=errno;
  1042. end;
  1043. Function GetEUid:Longint;
  1044. {
  1045. Get _effective_ User ID.
  1046. }
  1047. var
  1048. regs : SysCallregs;
  1049. begin
  1050. GetEuid:=SysCall(SysCall_nr_geteuid,regs);
  1051. Linuxerror:=errno;
  1052. end;
  1053. Function GetGid:Longint;
  1054. {
  1055. Get Group ID.
  1056. }
  1057. var
  1058. regs : SysCallregs;
  1059. begin
  1060. Getgid:=SysCall(SysCall_nr_getgid,regs);
  1061. Linuxerror:=errno;
  1062. end;
  1063. Function GetEGid:Longint;
  1064. {
  1065. Get _effective_ Group ID.
  1066. }
  1067. var
  1068. regs : SysCallregs;
  1069. begin
  1070. GetEgid:=SysCall(SysCall_nr_getegid,regs);
  1071. Linuxerror:=errno;
  1072. end;
  1073. {******************************************************************************
  1074. Date and Time related calls
  1075. ******************************************************************************}
  1076. Const
  1077. {Date Translation}
  1078. C1970=2440588;
  1079. D0 = 1461;
  1080. D1 = 146097;
  1081. D2 =1721119;
  1082. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  1083. Var
  1084. Century,XYear: LongInt;
  1085. Begin
  1086. If Month<=2 Then
  1087. Begin
  1088. Dec(Year);
  1089. Inc(Month,12);
  1090. End;
  1091. Dec(Month,3);
  1092. Century:=(longint(Year Div 100)*D1) shr 2;
  1093. XYear:=(longint(Year Mod 100)*D0) shr 2;
  1094. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  1095. End;
  1096. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  1097. Var
  1098. YYear,XYear,Temp,TempMonth : LongInt;
  1099. Begin
  1100. Temp:=((JulianDN-D2) shl 2)-1;
  1101. JulianDN:=Temp Div D1;
  1102. XYear:=(Temp Mod D1) or 3;
  1103. YYear:=(XYear Div D0);
  1104. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  1105. Day:=((Temp Mod 153)+5) Div 5;
  1106. TempMonth:=Temp Div 153;
  1107. If TempMonth>=10 Then
  1108. Begin
  1109. inc(YYear);
  1110. dec(TempMonth,12);
  1111. End;
  1112. inc(TempMonth,3);
  1113. Month := TempMonth;
  1114. Year:=YYear+(JulianDN*100);
  1115. end;
  1116. Procedure GetTimeOfDay(var tv:timeval);
  1117. {
  1118. Get the number of seconds since 00:00, January 1 1970, GMT
  1119. the time NOT corrected any way
  1120. }
  1121. var
  1122. regs : SysCallregs;
  1123. begin
  1124. regs.reg2:=longint(@tv);
  1125. regs.reg3:=0;
  1126. SysCall(SysCall_nr_gettimeofday,regs);
  1127. LinuxError:=Errno;
  1128. end;
  1129. Function GetTimeOfDay: longint;
  1130. {
  1131. Get the number of seconds since 00:00, January 1 1970, GMT
  1132. the time NOT corrected any way
  1133. }
  1134. var
  1135. regs : SysCallregs;
  1136. tv : timeval;
  1137. begin
  1138. regs.reg2:=longint(@tv);
  1139. regs.reg3:=0;
  1140. SysCall(SysCall_nr_gettimeofday,regs);
  1141. LinuxError:=Errno;
  1142. GetTimeOfDay:=tv.sec;
  1143. end;
  1144. Function GetEpochTime: longint;
  1145. {
  1146. Get the number of seconds since 00:00, January 1 1970, GMT
  1147. the time NOT corrected any way
  1148. }
  1149. begin
  1150. GetEpochTime:=GetTimeOfDay;
  1151. end;
  1152. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  1153. {
  1154. Transforms Epoch time into local time (hour, minute,seconds)
  1155. }
  1156. Var
  1157. DateNum: LongInt;
  1158. Begin
  1159. inc(Epoch,TZSeconds);
  1160. Datenum:=(Epoch Div 86400) + c1970;
  1161. JulianToGregorian(DateNum,Year,Month,day);
  1162. Epoch:=Epoch Mod 86400;
  1163. Hour:=Epoch Div 3600;
  1164. Epoch:=Epoch Mod 3600;
  1165. Minute:=Epoch Div 60;
  1166. Second:=Epoch Mod 60;
  1167. End;
  1168. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  1169. {
  1170. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  1171. (seconds since 00:00, january 1 1970, corrected for local time zone)
  1172. }
  1173. Begin
  1174. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  1175. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  1176. End;
  1177. procedure GetTime(var hour,min,sec,msec,usec:word);
  1178. {
  1179. Gets the current time, adjusted to local time
  1180. }
  1181. var
  1182. year,day,month:Word;
  1183. t : timeval;
  1184. begin
  1185. gettimeofday(t);
  1186. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  1187. msec:=t.usec div 1000;
  1188. usec:=t.usec mod 1000;
  1189. end;
  1190. procedure GetTime(var hour,min,sec,sec100:word);
  1191. {
  1192. Gets the current time, adjusted to local time
  1193. }
  1194. var
  1195. usec : word;
  1196. begin
  1197. gettime(hour,min,sec,sec100,usec);
  1198. sec100:=sec100 div 10;
  1199. end;
  1200. Procedure GetTime(Var Hour,Min,Sec:Word);
  1201. {
  1202. Gets the current time, adjusted to local time
  1203. }
  1204. var
  1205. msec,usec : Word;
  1206. Begin
  1207. gettime(hour,min,sec,msec,usec);
  1208. End;
  1209. Procedure GetDate(Var Year,Month,Day:Word);
  1210. {
  1211. Gets the current date, adjusted to local time
  1212. }
  1213. var
  1214. hour,minute,second : word;
  1215. Begin
  1216. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  1217. End;
  1218. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  1219. {
  1220. Gets the current date, adjusted to local time
  1221. }
  1222. Begin
  1223. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  1224. End;
  1225. { Include timezone handling routines which use /usr/share/timezone info }
  1226. {$i timezone.inc}
  1227. {******************************************************************************
  1228. FileSystem calls
  1229. ******************************************************************************}
  1230. Function fdOpen(pathname:string;flags:longint):longint;
  1231. begin
  1232. pathname:=pathname+#0;
  1233. fdOpen:=Sys_Open(@pathname[1],flags,438);
  1234. LinuxError:=Errno;
  1235. end;
  1236. Function fdOpen(pathname:string;flags,mode:longint):longint;
  1237. begin
  1238. pathname:=pathname+#0;
  1239. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  1240. LinuxError:=Errno;
  1241. end;
  1242. Function fdOpen(pathname:pchar;flags:longint):longint;
  1243. begin
  1244. fdOpen:=Sys_Open(pathname,flags,0);
  1245. LinuxError:=Errno;
  1246. end;
  1247. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  1248. begin
  1249. fdOpen:=Sys_Open(pathname,flags,mode);
  1250. LinuxError:=Errno;
  1251. end;
  1252. Function fdClose(fd:longint):boolean;
  1253. begin
  1254. fdClose:=(Sys_Close(fd)=0);
  1255. LinuxError:=Errno;
  1256. end;
  1257. Function fdRead(fd:longint;var buf;size:longint):longint;
  1258. begin
  1259. fdRead:=Sys_Read(fd,pchar(@buf),size);
  1260. LinuxError:=Errno;
  1261. end;
  1262. Function fdWrite(fd:longint;var buf;size:longint):longint;
  1263. begin
  1264. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  1265. LinuxError:=Errno;
  1266. end;
  1267. Function fdTruncate(fd,size:longint):boolean;
  1268. var
  1269. Regs : SysCallRegs;
  1270. begin
  1271. Regs.reg2:=fd;
  1272. Regs.reg3:=size;
  1273. fdTruncate:=(SysCall(Syscall_nr_ftruncate,regs)=0);
  1274. LinuxError:=Errno;
  1275. end;
  1276. Function fdSeek (fd,pos,seektype :longint): longint;
  1277. {
  1278. Do a Seek on a file descriptor fd to position pos, starting from seektype
  1279. }
  1280. begin
  1281. fdseek:=Sys_LSeek (fd,pos,seektype);
  1282. LinuxError:=Errno;
  1283. end;
  1284. Function fdFlush (fd : Longint) : Boolean;
  1285. var
  1286. SR: SysCallRegs;
  1287. begin
  1288. SR.reg2 := fd;
  1289. fdFlush := (SysCall(syscall_nr_fsync, SR)=0);
  1290. LinuxError:=Errno;
  1291. end;
  1292. Function Fcntl(Fd:longint;Cmd:integer):integer;
  1293. {
  1294. Read or manipulate a file.(See also fcntl (2) )
  1295. Possible values for Cmd are :
  1296. F_GetFd,F_GetFl,F_GetOwn
  1297. Errors are reported in Linuxerror;
  1298. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  1299. }
  1300. var
  1301. sr : Syscallregs;
  1302. begin
  1303. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  1304. begin
  1305. sr.reg2:=Fd;
  1306. sr.reg3:=cmd;
  1307. Linuxerror:=SysCall(Syscall_nr_fcntl,sr);
  1308. if linuxerror=-1 then
  1309. begin
  1310. linuxerror:=errno;
  1311. fcntl:=0;
  1312. end
  1313. else
  1314. begin
  1315. fcntl:=linuxerror;
  1316. linuxerror:=0;
  1317. end;
  1318. end
  1319. else
  1320. begin
  1321. linuxerror:=Sys_einval;
  1322. Fcntl:=0;
  1323. end;
  1324. end;
  1325. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  1326. {
  1327. Read or manipulate a file. (See also fcntl (2) )
  1328. Possible values for Cmd are :
  1329. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  1330. Errors are reported in Linuxerror;
  1331. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  1332. F_DupFD is not allowed, due to the structure of Files in Pascal.
  1333. }
  1334. var
  1335. sr : Syscallregs;
  1336. begin
  1337. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  1338. begin
  1339. sr.reg2:=Fd;
  1340. sr.reg3:=cmd;
  1341. sr.reg4:=arg;
  1342. SysCall(Syscall_nr_fcntl,sr);
  1343. linuxerror:=errno;
  1344. end
  1345. else
  1346. linuxerror:=Sys_einval;
  1347. end;
  1348. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  1349. begin
  1350. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  1351. end;
  1352. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  1353. begin
  1354. Fcntl(textrec(Fd).handle, Cmd, Arg);
  1355. end;
  1356. Function Chmod(path:pathstr;Newmode:longint):Boolean;
  1357. {
  1358. Changes the permissions of a file.
  1359. }
  1360. var
  1361. sr : Syscallregs;
  1362. begin
  1363. path:=path+#0;
  1364. sr.reg2:=longint(@(path[1]));
  1365. sr.reg3:=newmode;
  1366. Chmod:=(SysCall(Syscall_nr_chmod,sr)=0);
  1367. linuxerror:=errno;
  1368. end;
  1369. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  1370. {
  1371. Change the owner and group of a file.
  1372. A user can only change the group to a group of which he is a member.
  1373. The super-user can change uid and gid of any file.
  1374. }
  1375. var
  1376. sr : Syscallregs;
  1377. begin
  1378. path:=path+#0;
  1379. sr.reg2:=longint(@(path[1]));
  1380. sr.reg3:=newuid;
  1381. sr.reg4:=newgid;
  1382. ChOwn:=(Syscall(Syscall_nr_chown,sr)=0);
  1383. linuxerror:=errno;
  1384. end;
  1385. Function Utime(path:pathstr;utim:utimebuf):boolean;
  1386. var
  1387. sr : Syscallregs;
  1388. begin
  1389. path:=path+#0;
  1390. sr.reg2:=longint(@(path[1]));
  1391. sr.reg3:=longint(@utim);
  1392. Utime:=SysCall(Syscall_nr_utime,sr)=0;
  1393. linuxerror:=errno;
  1394. end;
  1395. Function Flock (fd,mode : longint) : boolean;
  1396. var
  1397. sr : Syscallregs;
  1398. begin
  1399. sr.reg2:=fd;
  1400. sr.reg3:=mode;
  1401. flock:=Syscall(Syscall_nr_flock,sr)=0;
  1402. LinuxError:=errno;
  1403. end;
  1404. Function Flock (var T : text;mode : longint) : boolean;
  1405. begin
  1406. Flock:=Flock(TextRec(T).Handle,mode);
  1407. end;
  1408. Function Flock (var F : File;mode : longint) : boolean;
  1409. begin
  1410. Flock:=Flock(FileRec(F).Handle,mode);
  1411. end;
  1412. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  1413. {
  1414. Get all information on a file, and return it in Info.
  1415. }
  1416. begin
  1417. path:=path+#0;
  1418. FStat:=(Sys_stat(@(path[1]),Info)=0);
  1419. LinuxError:=errno;
  1420. end;
  1421. Function Fstat(Fd:Longint;var Info:stat):Boolean;
  1422. {
  1423. Get all information on a file descriptor, and return it in info.
  1424. }
  1425. var
  1426. regs : SysCallregs;
  1427. begin
  1428. regs.reg2:=Fd;
  1429. regs.reg3:=longint(@Info);
  1430. FStat:=(SysCall(SysCall_nr_fstat,regs)=0);
  1431. LinuxError:=Errno;
  1432. end;
  1433. Function FStat(var F:Text;Var Info:stat):Boolean;
  1434. {
  1435. Get all information on a text file, and return it in info.
  1436. }
  1437. begin
  1438. FStat:=Fstat(TextRec(F).Handle,INfo);
  1439. end;
  1440. Function FStat(var F:File;Var Info:stat):Boolean;
  1441. {
  1442. Get all information on a untyped file, and return it in info.
  1443. }
  1444. begin
  1445. FStat:=Fstat(FileRec(F).Handle,Info);
  1446. end;
  1447. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  1448. {
  1449. Get all information on a link (the link itself), and return it in info.
  1450. }
  1451. var
  1452. regs : SysCallregs;
  1453. begin
  1454. FileName:=FileName+#0;
  1455. regs.reg2:=longint(@filename[1]);
  1456. regs.reg3:=longint(@Info);
  1457. LStat:=(SysCall(SysCall_nr_lstat,regs)=0);
  1458. LinuxError:=Errno;
  1459. end;
  1460. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  1461. {
  1462. Get all information on a fileSystem, and return it in Info.
  1463. Path is the name of a file/directory on the fileSystem you wish to
  1464. investigate.
  1465. }
  1466. var
  1467. regs : SysCallregs;
  1468. begin
  1469. path:=path+#0;
  1470. regs.reg2:=longint(@path[1]);
  1471. regs.reg3:=longint(@Info);
  1472. FSStat:=(SysCall(SysCall_nr_statfs,regs)=0);
  1473. LinuxError:=errno;
  1474. end;
  1475. Function FSStat(Fd:Longint;Var Info:statfs):Boolean;
  1476. {
  1477. Get all information on a fileSystem, and return it in Info.
  1478. Fd is the file descriptor of a file/directory on the fileSystem
  1479. you wish to investigate.
  1480. }
  1481. var
  1482. regs : SysCallregs;
  1483. begin
  1484. regs.reg2:=Fd;
  1485. regs.reg3:=longint(@Info);
  1486. FSStat:=(SysCall(SysCall_nr_fstatfs,regs)=0);
  1487. LinuxError:=errno;
  1488. end;
  1489. Function Link(OldPath,NewPath:pathstr):boolean;
  1490. {
  1491. Proceduces a hard link from new to old.
  1492. In effect, new will be the same file as old.
  1493. }
  1494. var
  1495. regs : SysCallregs;
  1496. begin
  1497. oldpath:=oldpath+#0;
  1498. newpath:=newpath+#0;
  1499. regs.reg2:=longint(@oldpath[1]);
  1500. regs.reg3:=longint(@newpath[1]);
  1501. Link:=SysCall(SysCall_nr_link,regs)=0;
  1502. linuxerror:=errno;
  1503. end;
  1504. Function SymLink(OldPath,newPath:pathstr):boolean;
  1505. {
  1506. Proceduces a soft link from new to old.
  1507. }
  1508. begin
  1509. oldpath:=oldpath+#0;
  1510. newpath:=newpath+#0;
  1511. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  1512. linuxerror:=errno;
  1513. end;
  1514. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  1515. {
  1516. Read a link (where it points to)
  1517. }
  1518. begin
  1519. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  1520. linuxerror:=errno;
  1521. end;
  1522. Function ReadLink(Name:pathstr):pathstr;
  1523. {
  1524. Read a link (where it points to)
  1525. }
  1526. var
  1527. LinkName : pathstr;
  1528. i : longint;
  1529. begin
  1530. Name:=Name+#0;
  1531. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  1532. if i>0 then
  1533. begin
  1534. linkname[0]:=chr(i);
  1535. ReadLink:=LinkName;
  1536. end
  1537. else
  1538. ReadLink:='';
  1539. end;
  1540. Function UnLink(Path:pathstr):boolean;
  1541. {
  1542. Removes the file in 'Path' (that is, it decreases the link count with one.
  1543. if the link count is zero, the file is removed from the disk.
  1544. }
  1545. begin
  1546. path:=path+#0;
  1547. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  1548. linuxerror:=errno;
  1549. end;
  1550. Function UnLink(Path:pchar):Boolean;
  1551. {
  1552. Removes the file in 'Path' (that is, it decreases the link count with one.
  1553. if the link count is zero, the file is removed from the disk.
  1554. }
  1555. begin
  1556. Unlink:=(Sys_unlink(path)=0);
  1557. linuxerror:=errno;
  1558. end;
  1559. Function FRename (OldName,NewName : Pchar) : Boolean;
  1560. begin
  1561. FRename:=Sys_rename(OldName,NewName)=0;
  1562. LinuxError:=Errno;
  1563. end;
  1564. Function FRename (OldName,NewName : String) : Boolean;
  1565. begin
  1566. OldName:=OldName+#0;
  1567. NewName:=NewName+#0;
  1568. FRename:=FRename (@OldName[1],@NewName[1]);
  1569. end;
  1570. Function Umask(Mask:Integer):integer;
  1571. {
  1572. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  1573. previous value.
  1574. }
  1575. var
  1576. sr : Syscallregs;
  1577. begin
  1578. sr.reg2:=mask;
  1579. Umask:=SysCall(Syscall_nr_umask,sr);
  1580. linuxerror:=0;
  1581. end;
  1582. Function Access(Path:Pathstr ;mode:integer):boolean;
  1583. {
  1584. Test users access rights on the specified file.
  1585. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  1586. R,W,X stand for read,write and Execute access, simultaneously.
  1587. F_OK checks whether the test would be allowed on the file.
  1588. i.e. It checks the search permissions in all directory components
  1589. of the path.
  1590. The test is done with the real user-ID, instead of the effective.
  1591. If access is denied, or an error occurred, false is returned.
  1592. If access is granted, true is returned.
  1593. Errors other than no access,are reported in linuxerror.
  1594. }
  1595. var
  1596. sr : Syscallregs;
  1597. begin
  1598. path:=path+#0;
  1599. sr.reg2:=longint(@(path[1]));
  1600. sr.reg3:=mode;
  1601. access:=(SysCall(Syscall_nr_access,sr)=0);
  1602. linuxerror:=errno;
  1603. end;
  1604. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  1605. {
  1606. Copies the filedescriptor oldfile to newfile
  1607. }
  1608. var
  1609. sr : Syscallregs;
  1610. begin
  1611. sr.reg2:=oldfile;
  1612. newfile:=Syscall(Syscall_nr_dup,sr);
  1613. linuxerror:=errno;
  1614. Dup:=(LinuxError=0);
  1615. end;
  1616. Function Dup(var oldfile,newfile:text):Boolean;
  1617. {
  1618. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1619. oldfile.
  1620. After which the two textfiles are, in effect, the same, except
  1621. that they don't share the same buffer, and don't share the same
  1622. close_on_exit flag.
  1623. }
  1624. begin
  1625. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1626. textrec(newfile):=textrec(oldfile);
  1627. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1628. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  1629. end;
  1630. Function Dup(var oldfile,newfile:file):Boolean;
  1631. {
  1632. Copies the filedescriptor oldfile to newfile
  1633. }
  1634. begin
  1635. filerec(newfile):=filerec(oldfile);
  1636. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  1637. end;
  1638. Function Dup2(oldfile,newfile:longint):Boolean;
  1639. {
  1640. Copies the filedescriptor oldfile to newfile
  1641. }
  1642. var
  1643. sr : Syscallregs;
  1644. begin
  1645. sr.reg2:=oldfile;
  1646. sr.reg3:=newfile;
  1647. SysCall(Syscall_nr_dup2,sr);
  1648. linuxerror:=errno;
  1649. Dup2:=(LinuxError=0);
  1650. end;
  1651. Function Dup2(var oldfile,newfile:text):Boolean;
  1652. {
  1653. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1654. oldfile. It closes newfile if it was still open.
  1655. After which the two textfiles are, in effect, the same, except
  1656. that they don't share the same buffer, and don't share the same
  1657. close_on_exit flag.
  1658. }
  1659. var
  1660. tmphandle : word;
  1661. begin
  1662. case TextRec(oldfile).mode of
  1663. fmOutput, fmInOut, fmAppend :
  1664. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1665. end;
  1666. case TextRec(newfile).mode of
  1667. fmOutput, fmInOut, fmAppend :
  1668. flush(newfile);
  1669. end;
  1670. tmphandle:=textrec(newfile).handle;
  1671. textrec(newfile):=textrec(oldfile);
  1672. textrec(newfile).handle:=tmphandle;
  1673. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1674. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  1675. end;
  1676. Function Dup2(var oldfile,newfile:file):Boolean;
  1677. {
  1678. Copies the filedescriptor oldfile to newfile
  1679. }
  1680. begin
  1681. filerec(newfile):=filerec(oldfile);
  1682. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  1683. end;
  1684. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  1685. {
  1686. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1687. have changed.
  1688. }
  1689. Var
  1690. SelectArray : Array[1..5] of longint;
  1691. Sr : Syscallregs;
  1692. begin
  1693. SelectArray[1]:=n;
  1694. SelectArray[2]:=longint(Readfds);
  1695. Selectarray[3]:=longint(Writefds);
  1696. selectarray[4]:=longint(exceptfds);
  1697. Selectarray[5]:=longint(TimeOut);
  1698. sr.reg2:=longint(@selectarray);
  1699. Select:=SysCall(Syscall_nr_select,sr);
  1700. LinuxError:=Errno;
  1701. end;
  1702. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1703. {
  1704. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1705. have changed.
  1706. This function allows specification of a timeout as a longint.
  1707. }
  1708. var
  1709. p : PTimeVal;
  1710. tv : TimeVal;
  1711. begin
  1712. if TimeOut=-1 then
  1713. p:=nil
  1714. else
  1715. begin
  1716. tv.Sec:=Timeout div 1000;
  1717. tv.Usec:=(Timeout mod 1000)*1000;
  1718. p:=@tv;
  1719. end;
  1720. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  1721. end;
  1722. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1723. Var
  1724. F:FDSet;
  1725. begin
  1726. if textrec(t).mode=fmclosed then
  1727. begin
  1728. LinuxError:=Sys_EBADF;
  1729. exit(-1);
  1730. end;
  1731. FD_Zero(f);
  1732. FD_Set(textrec(T).handle,f);
  1733. if textrec(T).mode=fminput then
  1734. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1735. else
  1736. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1737. end;
  1738. {******************************************************************************
  1739. Directory
  1740. ******************************************************************************}
  1741. Function OpenDir(F:String):PDir;
  1742. begin
  1743. F:=F+#0;
  1744. OpenDir:=OpenDir(@F[1]);
  1745. end;
  1746. procedure SeekDir(p:pdir;off:longint);
  1747. begin
  1748. if p=nil then
  1749. begin
  1750. errno:=Sys_EBADF;
  1751. exit;
  1752. end;
  1753. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1754. p^.size:=0;
  1755. p^.loc:=0;
  1756. end;
  1757. function TellDir(p:pdir):longint;
  1758. begin
  1759. if p=nil then
  1760. begin
  1761. errno:=Sys_EBADF;
  1762. telldir:=-1;
  1763. exit;
  1764. end;
  1765. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1766. { We could try to use the nextoff field here, but on my 1.2.13
  1767. kernel, this gives nothing... This may have to do with
  1768. the readdir implementation of libc... I also didn't find any trace of
  1769. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1770. Michael. }
  1771. end;
  1772. Function ReadDir(P:pdir):pdirent;
  1773. begin
  1774. ReadDir:=Sys_ReadDir(p);
  1775. LinuxError:=Errno;
  1776. end;
  1777. {******************************************************************************
  1778. Pipes/Fifo
  1779. ******************************************************************************}
  1780. Procedure OpenPipe(var F:Text);
  1781. begin
  1782. case textrec(f).mode of
  1783. fmoutput :
  1784. if textrec(f).userdata[1]<>P_OUT then
  1785. textrec(f).mode:=fmclosed;
  1786. fminput :
  1787. if textrec(f).userdata[1]<>P_IN then
  1788. textrec(f).mode:=fmclosed;
  1789. else
  1790. textrec(f).mode:=fmclosed;
  1791. end;
  1792. end;
  1793. Procedure IOPipe(var F:text);
  1794. begin
  1795. case textrec(f).mode of
  1796. fmoutput :
  1797. begin
  1798. { first check if we need something to write, else we may
  1799. get a SigPipe when Close() is called (PFV) }
  1800. if textrec(f).bufpos>0 then
  1801. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1802. end;
  1803. fminput :
  1804. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1805. end;
  1806. textrec(f).bufpos:=0;
  1807. end;
  1808. Procedure FlushPipe(var F:Text);
  1809. begin
  1810. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1811. IOPipe(f);
  1812. textrec(f).bufpos:=0;
  1813. end;
  1814. Procedure ClosePipe(var F:text);
  1815. begin
  1816. textrec(f).mode:=fmclosed;
  1817. Sys_close(textrec(f).handle);
  1818. end;
  1819. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  1820. {
  1821. Sets up a pair of file variables, which act as a pipe. The first one can
  1822. be read from, the second one can be written to.
  1823. If the operation was unsuccesful, linuxerror is set.
  1824. }
  1825. var
  1826. pip : tpipe;
  1827. regs : SysCallregs;
  1828. begin
  1829. regs.reg2:=longint(@pip);
  1830. SysCall(SysCall_nr_pipe,regs);
  1831. pipe_in:=pip[1];
  1832. pipe_out:=pip[2];
  1833. linuxerror:=errno;
  1834. AssignPipe:=(LinuxError=0);
  1835. end;
  1836. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1837. {
  1838. Sets up a pair of file variables, which act as a pipe. The first one can
  1839. be read from, the second one can be written to.
  1840. If the operation was unsuccesful, linuxerror is set.
  1841. }
  1842. var
  1843. f_in,f_out : longint;
  1844. begin
  1845. if not AssignPipe(f_in,f_out) then
  1846. begin
  1847. AssignPipe:=false;
  1848. exit;
  1849. end;
  1850. { Set up input }
  1851. Assign(Pipe_in,'');
  1852. Textrec(Pipe_in).Handle:=f_in;
  1853. Textrec(Pipe_in).Mode:=fmInput;
  1854. Textrec(Pipe_in).userdata[1]:=P_IN;
  1855. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1856. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1857. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1858. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1859. { Set up output }
  1860. Assign(Pipe_out,'');
  1861. Textrec(Pipe_out).Handle:=f_out;
  1862. Textrec(Pipe_out).Mode:=fmOutput;
  1863. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1864. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1865. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1866. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1867. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1868. AssignPipe:=true;
  1869. end;
  1870. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1871. {
  1872. Sets up a pair of file variables, which act as a pipe. The first one can
  1873. be read from, the second one can be written to.
  1874. If the operation was unsuccesful, linuxerror is set.
  1875. }
  1876. var
  1877. f_in,f_out : longint;
  1878. begin
  1879. if not AssignPipe(f_in,f_out) then
  1880. begin
  1881. AssignPipe:=false;
  1882. exit;
  1883. end;
  1884. { Set up input }
  1885. Assign(Pipe_in,'');
  1886. Filerec(Pipe_in).Handle:=f_in;
  1887. Filerec(Pipe_in).Mode:=fmInput;
  1888. Filerec(Pipe_in).recsize:=1;
  1889. Filerec(Pipe_in).userdata[1]:=P_IN;
  1890. { Set up output }
  1891. Assign(Pipe_out,'');
  1892. Filerec(Pipe_out).Handle:=f_out;
  1893. Filerec(Pipe_out).Mode:=fmoutput;
  1894. Filerec(Pipe_out).recsize:=1;
  1895. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1896. AssignPipe:=true;
  1897. end;
  1898. Function PClose(Var F:text) :longint;
  1899. var
  1900. sr : syscallregs;
  1901. pl : ^longint;
  1902. res : longint;
  1903. begin
  1904. sr.reg2:=Textrec(F).Handle;
  1905. SysCall (syscall_nr_close,sr);
  1906. { closed our side, Now wait for the other - this appears to be needed ?? }
  1907. pl:=@(textrec(f).userdata[2]);
  1908. waitpid(pl^,@res,0);
  1909. pclose:=res shr 8;
  1910. end;
  1911. Function PClose(Var F:file) : longint;
  1912. var
  1913. sr : syscallregs;
  1914. pl : ^longint;
  1915. res : longint;
  1916. begin
  1917. sr.reg2:=FileRec(F).Handle;
  1918. SysCall (Syscall_nr_close,sr);
  1919. { closed our side, Now wait for the other - this appears to be needed ?? }
  1920. pl:=@(filerec(f).userdata[2]);
  1921. waitpid(pl^,@res,0);
  1922. pclose:=res shr 8;
  1923. end;
  1924. Procedure PCloseText(Var F:text);
  1925. {
  1926. May not use @PClose due overloading
  1927. }
  1928. begin
  1929. PClose(f);
  1930. end;
  1931. Procedure POpen(var F:text;const Prog:String;rw:char);
  1932. {
  1933. Starts the program in 'Prog' and makes it's input or out put the
  1934. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1935. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1936. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1937. read from 'f'.
  1938. }
  1939. var
  1940. pipi,
  1941. pipo : text;
  1942. pid : longint;
  1943. pl : ^longint;
  1944. pp : ppchar;
  1945. begin
  1946. LinuxError:=0;
  1947. rw:=upcase(rw);
  1948. if not (rw in ['R','W']) then
  1949. begin
  1950. LinuxError:=Sys_enoent;
  1951. exit;
  1952. end;
  1953. AssignPipe(pipi,pipo);
  1954. if Linuxerror<>0 then
  1955. exit;
  1956. pid:=fork;
  1957. if linuxerror<>0 then
  1958. begin
  1959. close(pipi);
  1960. close(pipo);
  1961. exit;
  1962. end;
  1963. if pid=0 then
  1964. begin
  1965. { We're in the child }
  1966. if rw='W' then
  1967. begin
  1968. close(pipo);
  1969. dup2(pipi,input);
  1970. close(pipi);
  1971. if linuxerror<>0 then
  1972. halt(127);
  1973. end
  1974. else
  1975. begin
  1976. close(pipi);
  1977. dup2(pipo,output);
  1978. close(pipo);
  1979. if linuxerror<>0 then
  1980. halt(127);
  1981. end;
  1982. pp:=createshellargv(prog);
  1983. Execve(pp^,pp,envp);
  1984. halt(127);
  1985. end
  1986. else
  1987. begin
  1988. { We're in the parent }
  1989. if rw='W' then
  1990. begin
  1991. close(pipi);
  1992. f:=pipo;
  1993. textrec(f).bufptr:=@textrec(f).buffer;
  1994. end
  1995. else
  1996. begin
  1997. close(pipo);
  1998. f:=pipi;
  1999. textrec(f).bufptr:=@textrec(f).buffer;
  2000. end;
  2001. {Save the process ID - needed when closing }
  2002. pl:=@(textrec(f).userdata[2]);
  2003. pl^:=pid;
  2004. textrec(f).closefunc:=@PCloseText;
  2005. end;
  2006. end;
  2007. Procedure POpen(var F:file;const Prog:String;rw:char);
  2008. {
  2009. Starts the program in 'Prog' and makes it's input or out put the
  2010. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  2011. F, will be read from stdin by the program in 'Prog'. The inverse is true
  2012. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  2013. read from 'f'.
  2014. }
  2015. var
  2016. pipi,
  2017. pipo : file;
  2018. pid : longint;
  2019. pl : ^longint;
  2020. p,pp : ppchar;
  2021. temp : string[255];
  2022. begin
  2023. LinuxError:=0;
  2024. rw:=upcase(rw);
  2025. if not (rw in ['R','W']) then
  2026. begin
  2027. LinuxError:=Sys_enoent;
  2028. exit;
  2029. end;
  2030. AssignPipe(pipi,pipo);
  2031. if Linuxerror<>0 then
  2032. exit;
  2033. pid:=fork;
  2034. if linuxerror<>0 then
  2035. begin
  2036. close(pipi);
  2037. close(pipo);
  2038. exit;
  2039. end;
  2040. if pid=0 then
  2041. begin
  2042. { We're in the child }
  2043. if rw='W' then
  2044. begin
  2045. close(pipo);
  2046. dup2(filerec(pipi).handle,stdinputhandle);
  2047. close(pipi);
  2048. if linuxerror<>0 then
  2049. halt(127);
  2050. end
  2051. else
  2052. begin
  2053. close(pipi);
  2054. dup2(filerec(pipo).handle,stdoutputhandle);
  2055. close(pipo);
  2056. if linuxerror<>0 then
  2057. halt(127);
  2058. end;
  2059. getmem(pp,sizeof(pchar)*4);
  2060. temp:='/bin/sh'#0'-c'#0+prog+#0;
  2061. p:=pp;
  2062. p^:=@temp[1];
  2063. inc(p);
  2064. p^:=@temp[9];
  2065. inc(p);
  2066. p^:=@temp[12];
  2067. inc(p);
  2068. p^:=Nil;
  2069. Execve('/bin/sh',pp,envp);
  2070. halt(127);
  2071. end
  2072. else
  2073. begin
  2074. { We're in the parent }
  2075. if rw='W' then
  2076. begin
  2077. close(pipi);
  2078. f:=pipo;
  2079. end
  2080. else
  2081. begin
  2082. close(pipo);
  2083. f:=pipi;
  2084. end;
  2085. {Save the process ID - needed when closing }
  2086. pl:=@(filerec(f).userdata[2]);
  2087. pl^:=pid;
  2088. end;
  2089. end;
  2090. Function mkFifo(pathname:string;mode:longint):boolean;
  2091. var
  2092. regs : SysCallRegs;
  2093. begin
  2094. pathname:=pathname+#0;
  2095. regs.reg2:=longint(@pathname[1]);
  2096. regs.reg3:=mode or STAT_IFIFO;
  2097. regs.reg4:=0;
  2098. mkFifo:=(SysCall(syscall_nr_mknod,regs)=0);
  2099. end;
  2100. Procedure AssignStream(Var StreamIn,Streamout:text;Const Prog:String);
  2101. {
  2102. Starts the program in 'Prog' and makes its input and output the
  2103. other end of two pipes, which are the stdin and stdout of a program
  2104. specified in 'Prog'.
  2105. streamout can be used to write to the program, streamin can be used to read
  2106. the output of the program. See the following diagram :
  2107. Parent Child
  2108. STreamout --> Input
  2109. Streamin <-- Output
  2110. }
  2111. var
  2112. pipi,
  2113. pipo : text;
  2114. pid : longint;
  2115. pl : ^Longint;
  2116. begin
  2117. LinuxError:=0;
  2118. AssignPipe(streamin,pipo);
  2119. if Linuxerror<>0 then
  2120. exit;
  2121. AssignPipe(pipi,streamout);
  2122. if Linuxerror<>0 then
  2123. exit;
  2124. pid:=fork;
  2125. if linuxerror<>0 then
  2126. begin
  2127. close(pipi);
  2128. close(pipo);
  2129. close (streamin);
  2130. close (streamout);
  2131. exit;
  2132. end;
  2133. if pid=0 then
  2134. begin
  2135. { We're in the child }
  2136. { Close what we don't need }
  2137. close(streamout);
  2138. close(streamin);
  2139. dup2(pipi,input);
  2140. if linuxerror<>0 then
  2141. halt(127);
  2142. close(pipi);
  2143. dup2(pipo,output);
  2144. if linuxerror<>0 then
  2145. halt (127);
  2146. close(pipo);
  2147. Execl(Prog);
  2148. halt(127);
  2149. end
  2150. else
  2151. begin
  2152. { we're in the parent}
  2153. close(pipo);
  2154. close(pipi);
  2155. {Save the process ID - needed when closing }
  2156. pl:=@(textrec(StreamIn).userdata[2]);
  2157. pl^:=pid;
  2158. textrec(StreamIn).closefunc:=@PCloseText;
  2159. {Save the process ID - needed when closing }
  2160. pl:=@(textrec(StreamOut).userdata[2]);
  2161. pl^:=pid;
  2162. textrec(StreamOut).closefunc:=@PCloseText;
  2163. end;
  2164. end;
  2165. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  2166. {
  2167. Starts the program in 'prog' and makes its input, output and error output the
  2168. other end of three pipes, which are the stdin, stdout and stderr of a program
  2169. specified in 'prog'.
  2170. StreamOut can be used to write to the program, StreamIn can be used to read
  2171. the output of the program, StreamErr reads the error output of the program.
  2172. See the following diagram :
  2173. Parent Child
  2174. StreamOut --> StdIn (input)
  2175. StreamIn <-- StdOut (output)
  2176. StreamErr <-- StdErr (error output)
  2177. }
  2178. var
  2179. PipeIn, PipeOut, PipeErr: text;
  2180. pid: LongInt;
  2181. pl: ^LongInt;
  2182. begin
  2183. LinuxError := 0;
  2184. AssignStream := -1;
  2185. // Assign pipes
  2186. AssignPipe(StreamIn, PipeOut);
  2187. if LinuxError <> 0 then exit;
  2188. AssignPipe(StreamErr, PipeErr);
  2189. if LinuxError <> 0 then begin
  2190. Close(StreamIn);
  2191. Close(PipeOut);
  2192. exit;
  2193. end;
  2194. AssignPipe(PipeIn, StreamOut);
  2195. if LinuxError <> 0 then begin
  2196. Close(StreamIn);
  2197. Close(PipeOut);
  2198. Close(StreamErr);
  2199. Close(PipeErr);
  2200. exit;
  2201. end;
  2202. // Fork
  2203. pid := Fork;
  2204. if LinuxError <> 0 then begin
  2205. Close(StreamIn);
  2206. Close(PipeOut);
  2207. Close(StreamErr);
  2208. Close(PipeErr);
  2209. Close(PipeIn);
  2210. Close(StreamOut);
  2211. exit;
  2212. end;
  2213. if pid = 0 then begin
  2214. // *** We are in the child ***
  2215. // Close what we don not need
  2216. Close(StreamOut);
  2217. Close(StreamIn);
  2218. Close(StreamErr);
  2219. // Connect pipes
  2220. dup2(PipeIn, Input);
  2221. if LinuxError <> 0 then Halt(127);
  2222. Close(PipeIn);
  2223. dup2(PipeOut, Output);
  2224. if LinuxError <> 0 then Halt(127);
  2225. Close(PipeOut);
  2226. dup2(PipeErr, StdErr);
  2227. if LinuxError <> 0 then Halt(127);
  2228. Close(PipeErr);
  2229. // Execute program
  2230. Execl(Prog);
  2231. Halt(127);
  2232. end else begin
  2233. // *** We are in the parent ***
  2234. Close(PipeErr);
  2235. Close(PipeOut);
  2236. Close(PipeIn);
  2237. // Save the process ID - needed when closing
  2238. pl := @(TextRec(StreamIn).userdata[2]);
  2239. pl^ := pid;
  2240. TextRec(StreamIn).closefunc := @PCloseText;
  2241. // Save the process ID - needed when closing
  2242. pl := @(TextRec(StreamOut).userdata[2]);
  2243. pl^ := pid;
  2244. TextRec(StreamOut).closefunc := @PCloseText;
  2245. // Save the process ID - needed when closing
  2246. pl := @(TextRec(StreamErr).userdata[2]);
  2247. pl^ := pid;
  2248. TextRec(StreamErr).closefunc := @PCloseText;
  2249. AssignStream := pid;
  2250. end;
  2251. end;
  2252. {******************************************************************************
  2253. General information calls
  2254. ******************************************************************************}
  2255. Function Sysinfo(var Info:TSysinfo):Boolean;
  2256. {
  2257. Get system info
  2258. }
  2259. var
  2260. regs : SysCallregs;
  2261. Begin
  2262. regs.reg2:=longint(@info);
  2263. Sysinfo:=SysCall(SysCall_nr_Sysinfo,regs)=0;
  2264. End;
  2265. Function Uname(var unamerec:utsname):Boolean;
  2266. {
  2267. Get machine's names
  2268. }
  2269. var
  2270. regs : SysCallregs;
  2271. Begin
  2272. regs.reg2:=longint(@unamerec);
  2273. Uname:=SysCall(SysCall_nr_uname,regs)=0;
  2274. LinuxError:=Errno;
  2275. End;
  2276. Function GetEnv(P:string):Pchar;
  2277. {
  2278. Searches the environment for a string with name p and
  2279. returns a pchar to it's value.
  2280. A pchar is used to accomodate for strings of length > 255
  2281. }
  2282. var
  2283. ep : ppchar;
  2284. found : boolean;
  2285. Begin
  2286. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  2287. ep:=envp;
  2288. found:=false;
  2289. if ep<>nil then
  2290. begin
  2291. while (not found) and (ep^<>nil) do
  2292. begin
  2293. if strlcomp(@p[1],(ep^),length(p))=0 then
  2294. found:=true
  2295. else
  2296. inc(ep);
  2297. end;
  2298. end;
  2299. if found then
  2300. getenv:=ep^+length(p)
  2301. else
  2302. getenv:=nil;
  2303. end;
  2304. Function GetDomainName:String;
  2305. {
  2306. Get machines domain name. Returns empty string if not set.
  2307. }
  2308. Var
  2309. Sysn : utsname;
  2310. begin
  2311. Uname(Sysn);
  2312. linuxerror:=errno;
  2313. If linuxerror<>0 then
  2314. getdomainname:=''
  2315. else
  2316. getdomainname:=strpas(@Sysn.domainname[0]);
  2317. end;
  2318. Function GetHostName:String;
  2319. {
  2320. Get machines name. Returns empty string if not set.
  2321. }
  2322. Var
  2323. Sysn : utsname;
  2324. begin
  2325. uname(Sysn);
  2326. linuxerror:=errno;
  2327. If linuxerror<>0 then
  2328. gethostname:=''
  2329. else
  2330. gethostname:=strpas(@Sysn.nodename[0]);
  2331. end;
  2332. {******************************************************************************
  2333. Signal handling calls
  2334. ******************************************************************************}
  2335. Function Kill(Pid:longint;Sig:integer):integer;
  2336. {
  2337. Send signal 'sig' to a process, or a group of processes.
  2338. If Pid > 0 then the signal is sent to pid
  2339. pid=-1 to all processes except process 1
  2340. pid < -1 to process group -pid
  2341. Return value is zero, except for case three, where the return value
  2342. is the number of processes to which the signal was sent.
  2343. }
  2344. var
  2345. regs : Syscallregs;
  2346. begin
  2347. regs.reg2:=Pid;
  2348. regs.reg3:=Sig;
  2349. kill:=SysCall(Syscall_nr_kill,regs);
  2350. if kill<0 then
  2351. Kill:=0;
  2352. linuxerror:=errno;
  2353. end;
  2354. Procedure SigAction(Signum:Integer;Var Act,OldAct:PSigActionRec );
  2355. {
  2356. Change action of process upon receipt of a signal.
  2357. Signum specifies the signal (all except SigKill and SigStop).
  2358. If Act is non-nil, it is used to specify the new action.
  2359. If OldAct is non-nil the previous action is saved there.
  2360. }
  2361. Var
  2362. sr : Syscallregs;
  2363. begin
  2364. sr.reg2:=Signum;
  2365. sr.reg3:=Longint(act);
  2366. sr.reg4:=Longint(oldact);
  2367. SysCall(Syscall_nr_sigaction,sr);
  2368. linuxerror:=errno;
  2369. end;
  2370. Procedure SigProcMask(How:Integer;SSet,OldSSet:PSigSet);
  2371. {
  2372. Change the list of currently blocked signals.
  2373. How determines which signals will be blocked :
  2374. SigBlock : Add SSet to the current list of blocked signals
  2375. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  2376. SigSetMask : Set the list of blocked signals to SSet
  2377. if OldSSet is non-null, the old set will be saved there.
  2378. }
  2379. Var
  2380. sr : SyscallRegs;
  2381. begin
  2382. sr.reg2:=how;
  2383. sr.reg3:=longint(SSet);
  2384. sr.reg4:=longint(OldSSet);
  2385. SysCall(Syscall_nr_sigprocmask,sr);
  2386. linuxerror:=errno;
  2387. end;
  2388. Function SigPending:SigSet;
  2389. {
  2390. Allows examination of pending signals. The signal mask of pending
  2391. signals is set in SSet
  2392. }
  2393. Var
  2394. sr : SyscallRegs;
  2395. dummy : Sigset;
  2396. begin
  2397. sr.reg2:=longint(@dummy);
  2398. SysCall(Syscall_nr_sigpending,sr);
  2399. linuxerror:=errno;
  2400. Sigpending:=dummy;
  2401. end;
  2402. Procedure SigSuspend(Mask:Sigset);
  2403. {
  2404. Set the signal mask with Mask, and suspend the program until a signal
  2405. is received.
  2406. }
  2407. Var
  2408. sr : SyscallRegs;
  2409. begin
  2410. sr.reg2:=mask;
  2411. SysCall(Syscall_nr_sigsuspend,sr);
  2412. linuxerror:=errno;
  2413. end;
  2414. Function Signal(Signum:Integer;Handler:SignalHandler):SignalHandler;
  2415. {
  2416. Install a new handler for signal Signum.
  2417. The old signal handler is returned.
  2418. This call does, in fact, the same as SigAction.
  2419. }
  2420. var
  2421. sr : Syscallregs;
  2422. begin
  2423. sr.reg2:=signum;
  2424. sr.reg3:=longint(handler);
  2425. Linuxerror:=SysCall(Syscall_nr_signal,sr);
  2426. If linuxerror=Sig_Err then
  2427. begin
  2428. Signal:=nil;
  2429. Linuxerror:=errno;
  2430. end
  2431. else
  2432. begin
  2433. Signal:=signalhandler(Linuxerror);
  2434. linuxerror:=0;
  2435. end;
  2436. end;
  2437. procedure SigRaise(sig:integer);
  2438. begin
  2439. Kill(GetPid,Sig);
  2440. end;
  2441. Function Alarm(Sec : Longint) : longint;
  2442. Var Sr : Syscallregs;
  2443. begin
  2444. sr.reg2:=Sec;
  2445. Alarm:=Syscall(syscall_nr_alarm,sr);
  2446. end;
  2447. Procedure Pause;
  2448. Var Sr : Syscallregs;
  2449. begin
  2450. syscall(syscall_nr_pause,sr);
  2451. end;
  2452. {******************************************************************************
  2453. IOCtl and Termios calls
  2454. ******************************************************************************}
  2455. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  2456. {
  2457. Interface to Unix ioctl call.
  2458. Performs various operations on the filedescriptor Handle.
  2459. Ndx describes the operation to perform.
  2460. Data points to data needed for the Ndx function. The structure of this
  2461. data is function-dependent.
  2462. }
  2463. var
  2464. sr: SysCallRegs;
  2465. begin
  2466. sr.reg2:=Handle;
  2467. sr.reg3:=Ndx;
  2468. sr.reg4:=Longint(Data);
  2469. IOCtl:=(SysCall(Syscall_nr_ioctl,sr)=0);
  2470. LinuxError:=Errno;
  2471. end;
  2472. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  2473. begin
  2474. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  2475. end;
  2476. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  2477. var
  2478. nr:longint;
  2479. begin
  2480. case OptAct of
  2481. TCSANOW : nr:=TCSETS;
  2482. TCSADRAIN : nr:=TCSETSW;
  2483. TCSAFLUSH : nr:=TCSETSF;
  2484. else
  2485. begin
  2486. ErrNo:=Sys_EINVAL;
  2487. TCSetAttr:=false;
  2488. exit;
  2489. end;
  2490. end;
  2491. TCSetAttr:=IOCtl(fd,nr,@Tios);
  2492. end;
  2493. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  2494. begin
  2495. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  2496. end;
  2497. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  2498. begin
  2499. CFSetISpeed(tios,speed);
  2500. end;
  2501. Procedure CFMakeRaw(var tios:TermIOS);
  2502. begin
  2503. with tios do
  2504. begin
  2505. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  2506. INLCR or IGNCR or ICRNL or IXON));
  2507. c_oflag:=c_oflag and (not OPOST);
  2508. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  2509. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  2510. end;
  2511. end;
  2512. Function TCSendBreak(fd,duration:longint):boolean;
  2513. begin
  2514. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  2515. end;
  2516. Function TCSetPGrp(fd,id:longint):boolean;
  2517. begin
  2518. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  2519. end;
  2520. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  2521. begin
  2522. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  2523. end;
  2524. Function TCDrain(fd:longint):boolean;
  2525. begin
  2526. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  2527. end;
  2528. Function TCFlow(fd,act:longint):boolean;
  2529. begin
  2530. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  2531. end;
  2532. Function TCFlush(fd,qsel:longint):boolean;
  2533. begin
  2534. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  2535. end;
  2536. Function IsATTY(Handle:Longint):Boolean;
  2537. {
  2538. Check if the filehandle described by 'handle' is a TTY (Terminal)
  2539. }
  2540. var
  2541. t : Termios;
  2542. begin
  2543. IsAtty:=TCGetAttr(Handle,t);
  2544. end;
  2545. Function IsATTY(f: text):Boolean;
  2546. {
  2547. Idem as previous, only now for text variables.
  2548. }
  2549. begin
  2550. IsATTY:=IsaTTY(textrec(f).handle);
  2551. end;
  2552. function TTYName(Handle:Longint):string;
  2553. {
  2554. Return the name of the current tty described by handle f.
  2555. returns empty string in case of an error.
  2556. }
  2557. Const
  2558. dev='/dev';
  2559. var
  2560. name : string;
  2561. st : stat;
  2562. mydev,
  2563. myino : longint;
  2564. dirstream : pdir;
  2565. d : pdirent;
  2566. begin
  2567. TTYName:='';
  2568. fstat(handle,st);
  2569. if (errno<>0) and isatty (handle) then
  2570. exit;
  2571. mydev:=st.dev;
  2572. myino:=st.ino;
  2573. dirstream:=opendir(dev);
  2574. if (linuxerror<>0) then
  2575. exit;
  2576. d:=Readdir(dirstream);
  2577. while (d<>nil) do
  2578. begin
  2579. if (d^.ino=myino) then
  2580. begin
  2581. name:=dev+'/'+strpas(@(d^.name));
  2582. fstat(name,st);
  2583. if (linuxerror=0) and (st.dev=mydev) then
  2584. begin
  2585. closedir(dirstream);
  2586. ttyname:=name;
  2587. exit;
  2588. end;
  2589. end;
  2590. d:=Readdir(dirstream);
  2591. end;
  2592. closedir(dirstream);
  2593. end;
  2594. function TTYName(var F:Text):string;
  2595. {
  2596. Idem as previous, only now for text variables;
  2597. }
  2598. begin
  2599. TTYName:=TTYName(textrec(f).handle);
  2600. end;
  2601. {******************************************************************************
  2602. Utility calls
  2603. ******************************************************************************}
  2604. Function Octal(l:longint):longint;
  2605. {
  2606. Convert an octal specified number to decimal;
  2607. }
  2608. var
  2609. octnr,
  2610. oct : longint;
  2611. begin
  2612. octnr:=0;
  2613. oct:=0;
  2614. while (l>0) do
  2615. begin
  2616. oct:=oct or ((l mod 10) shl octnr);
  2617. l:=l div 10;
  2618. inc(octnr,3);
  2619. end;
  2620. Octal:=oct;
  2621. end;
  2622. Function StringToPPChar(Var S:STring):ppchar;
  2623. {
  2624. Create a PPChar to structure of pchars which are the arguments specified
  2625. in the string S. Especially usefull for creating an ArgV for Exec-calls
  2626. }
  2627. var
  2628. nr : longint;
  2629. Buf : ^char;
  2630. p : ppchar;
  2631. begin
  2632. s:=s+#0;
  2633. buf:=@s[1];
  2634. nr:=0;
  2635. while(buf^<>#0) do
  2636. begin
  2637. while (buf^ in [' ',#8,#10]) do
  2638. inc(buf);
  2639. inc(nr);
  2640. while not (buf^ in [' ',#0,#8,#10]) do
  2641. inc(buf);
  2642. end;
  2643. getmem(p,nr*4);
  2644. StringToPPChar:=p;
  2645. if p=nil then
  2646. begin
  2647. LinuxError:=sys_enomem;
  2648. exit;
  2649. end;
  2650. buf:=@s[1];
  2651. while (buf^<>#0) do
  2652. begin
  2653. while (buf^ in [' ',#8,#10]) do
  2654. begin
  2655. buf^:=#0;
  2656. inc(buf);
  2657. end;
  2658. p^:=buf;
  2659. inc(p);
  2660. p^:=nil;
  2661. while not (buf^ in [' ',#0,#8,#10]) do
  2662. inc(buf);
  2663. end;
  2664. end;
  2665. Function FExpand(Const Path:PathStr):PathStr;
  2666. var
  2667. temp : pathstr;
  2668. i,j : longint;
  2669. p : pchar;
  2670. Begin
  2671. {Remove eventual drive - doesn't exist in Linux}
  2672. if path[2]=':' then
  2673. i:=3
  2674. else
  2675. i:=1;
  2676. temp:='';
  2677. {Replace ~/ with $HOME}
  2678. if (path[i]='~') and ((i+1>length(path)) or (path[i+1]='/')) then
  2679. begin
  2680. p:=getenv('HOME');
  2681. if not (p=nil) then
  2682. Insert(StrPas(p),temp,i);
  2683. i:=1;
  2684. temp:=temp+Copy(Path,2,255);
  2685. end;
  2686. {Do we have an absolute path ? No - prefix the current dir}
  2687. if temp='' then
  2688. begin
  2689. if path[i]<>'/' then
  2690. begin
  2691. {$I-}
  2692. getdir(0,temp);
  2693. {$I+}
  2694. if ioresult<>0 then;
  2695. end
  2696. else
  2697. inc(i);
  2698. temp:=temp+'/'+copy(path,i,length(path)-i+1)+'/';
  2699. end;
  2700. {First remove all references to '/./'}
  2701. while pos('/./',temp)<>0 do
  2702. delete(temp,pos('/./',temp),2);
  2703. {Now remove also all references to '/../' + of course previous dirs..}
  2704. repeat
  2705. i:=pos('/../',temp);
  2706. {Find the pos of the previous dir}
  2707. if i>1 then
  2708. begin
  2709. j:=i-1;
  2710. while (j>1) and (temp[j]<>'/') do
  2711. dec (j);{temp[1] is always '/'}
  2712. delete(temp,j,i-j+3);
  2713. end
  2714. else
  2715. if i=1 then {i=1, so we have temp='/../something', just delete '/../'}
  2716. delete(temp,1,3);
  2717. until i=0;
  2718. { Remove ending /.. }
  2719. i:=pos('/..',temp);
  2720. if (i<>0) and (i =length(temp)-2) then
  2721. begin
  2722. j:=i-1;
  2723. while (j>1) and (temp[j]<>'/') do
  2724. dec (j);
  2725. delete (temp,j,i-j+3);
  2726. end;
  2727. { if last character is / then remove it - dir is also a file :-) }
  2728. if (length(temp)>0) and (temp[length(temp)]='/') then
  2729. dec(byte(temp[0]));
  2730. fexpand:=temp;
  2731. End;
  2732. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  2733. {
  2734. Searches for a file 'path' in the list of direcories in 'dirlist'.
  2735. returns an empty string if not found. Wildcards are NOT allowed.
  2736. If dirlist is empty, it is set to '.'
  2737. }
  2738. Var
  2739. NewDir : PathStr;
  2740. p1 : Longint;
  2741. Info : Stat;
  2742. Begin
  2743. {Replace ':' with ';'}
  2744. for p1:=1to length(dirlist) do
  2745. if dirlist[p1]=':' then
  2746. dirlist[p1]:=';';
  2747. {Check for WildCards}
  2748. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  2749. FSearch:='' {No wildcards allowed in these things.}
  2750. Else
  2751. Begin
  2752. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  2753. Repeat
  2754. p1:=Pos(';',DirList);
  2755. If p1=0 Then
  2756. p1:=255;
  2757. NewDir:=Copy(DirList,1,P1 - 1);
  2758. if NewDir[Length(NewDir)]<>'/' then
  2759. NewDir:=NewDir+'/';
  2760. NewDir:=NewDir+Path;
  2761. Delete(DirList,1,p1);
  2762. if FStat(NewDir,Info) then
  2763. Begin
  2764. If Pos('./',NewDir)=1 Then
  2765. Delete(NewDir,1,2);
  2766. {DOS strips off an initial .\}
  2767. End
  2768. Else
  2769. NewDir:='';
  2770. Until (DirList='') or (Length(NewDir) > 0);
  2771. FSearch:=NewDir;
  2772. End;
  2773. End;
  2774. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  2775. Var
  2776. DotPos,SlashPos,i : longint;
  2777. Begin
  2778. SlashPos:=0;
  2779. DotPos:=256;
  2780. i:=Length(Path);
  2781. While (i>0) and (SlashPos=0) Do
  2782. Begin
  2783. If (DotPos=256) and (Path[i]='.') Then
  2784. DotPos:=i;
  2785. If (Path[i]='/') Then
  2786. SlashPos:=i;
  2787. Dec(i);
  2788. End;
  2789. Ext:=Copy(Path,DotPos,255);
  2790. Dir:=Copy(Path,1,SlashPos);
  2791. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  2792. End;
  2793. Function Dirname(Const path:pathstr):pathstr;
  2794. {
  2795. This function returns the directory part of a complete path.
  2796. Unless the directory is root '/', The last character is not
  2797. a slash.
  2798. }
  2799. var
  2800. Dir : PathStr;
  2801. Name : NameStr;
  2802. Ext : ExtStr;
  2803. begin
  2804. FSplit(Path,Dir,Name,Ext);
  2805. if length(Dir)>1 then
  2806. Delete(Dir,length(Dir),1);
  2807. DirName:=Dir;
  2808. end;
  2809. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2810. {
  2811. This function returns the filename part of a complete path. If suf is
  2812. supplied, it is cut off the filename.
  2813. }
  2814. var
  2815. Dir : PathStr;
  2816. Name : NameStr;
  2817. Ext : ExtStr;
  2818. begin
  2819. FSplit(Path,Dir,Name,Ext);
  2820. if Suf<>Ext then
  2821. Name:=Name+Ext;
  2822. BaseName:=Name;
  2823. end;
  2824. Function FNMatch(const Pattern,Name:string):Boolean;
  2825. Var
  2826. LenPat,LenName : longint;
  2827. Function DoFNMatch(i,j:longint):Boolean;
  2828. Var
  2829. Found : boolean;
  2830. Begin
  2831. Found:=true;
  2832. While Found and (i<=LenPat) Do
  2833. Begin
  2834. Case Pattern[i] of
  2835. '?' : Found:=(j<=LenName);
  2836. '*' : Begin
  2837. {find the next character in pattern, different of ? and *}
  2838. while Found and (i<LenPat) do
  2839. begin
  2840. inc(i);
  2841. case Pattern[i] of
  2842. '*' : ;
  2843. '?' : begin
  2844. inc(j);
  2845. Found:=(j<=LenName);
  2846. end;
  2847. else
  2848. Found:=false;
  2849. end;
  2850. end;
  2851. {Now, find in name the character which i points to, if the * or ?
  2852. wasn't the last character in the pattern, else, use up all the
  2853. chars in name}
  2854. Found:=true;
  2855. if (i<=LenPat) then
  2856. begin
  2857. repeat
  2858. {find a letter (not only first !) which maches pattern[i]}
  2859. while (j<=LenName) and (name[j]<>pattern[i]) do
  2860. inc (j);
  2861. if (j<LenName) then
  2862. begin
  2863. if DoFnMatch(i+1,j+1) then
  2864. begin
  2865. i:=LenPat;
  2866. j:=LenName;{we can stop}
  2867. Found:=true;
  2868. end
  2869. else
  2870. inc(j);{We didn't find one, need to look further}
  2871. end;
  2872. until (j>=LenName);
  2873. end
  2874. else
  2875. j:=LenName;{we can stop}
  2876. end;
  2877. else {not a wildcard character in pattern}
  2878. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2879. end;
  2880. inc(i);
  2881. inc(j);
  2882. end;
  2883. DoFnMatch:=Found and (j>LenName);
  2884. end;
  2885. Begin {start FNMatch}
  2886. LenPat:=Length(Pattern);
  2887. LenName:=Length(Name);
  2888. FNMatch:=DoFNMatch(1,1);
  2889. End;
  2890. Procedure Globfree(var p : pglob);
  2891. {
  2892. Release memory occupied by pglob structure, and names in it.
  2893. sets p to nil.
  2894. }
  2895. var
  2896. temp : pglob;
  2897. begin
  2898. while assigned(p) do
  2899. begin
  2900. temp:=p^.next;
  2901. if assigned(p^.name) then
  2902. freemem(p^.name);
  2903. dispose(p);
  2904. p:=temp;
  2905. end;
  2906. end;
  2907. Function Glob(Const path:pathstr):pglob;
  2908. {
  2909. Fills a tglob structure with entries matching path,
  2910. and returns a pointer to it. Returns nil on error,
  2911. linuxerror is set accordingly.
  2912. }
  2913. var
  2914. temp,
  2915. temp2 : string[255];
  2916. thedir : pdir;
  2917. buffer : pdirent;
  2918. root,
  2919. current : pglob;
  2920. begin
  2921. { Get directory }
  2922. temp:=dirname(path);
  2923. if temp='' then
  2924. temp:='.';
  2925. temp:=temp+#0;
  2926. thedir:=opendir(@temp[1]);
  2927. if thedir=nil then
  2928. begin
  2929. glob:=nil;
  2930. linuxerror:=errno;
  2931. exit;
  2932. end;
  2933. temp:=basename(path,''); { get the pattern }
  2934. if thedir^.fd<0 then
  2935. begin
  2936. linuxerror:=errno;
  2937. glob:=nil;
  2938. exit;
  2939. end;
  2940. {get the entries}
  2941. root:=nil;
  2942. current:=nil;
  2943. repeat
  2944. buffer:=Sys_readdir(thedir);
  2945. if buffer=nil then
  2946. break;
  2947. temp2:=strpas(@(buffer^.name[0]));
  2948. if fnmatch(temp,temp2) then
  2949. begin
  2950. if root=nil then
  2951. begin
  2952. new(root);
  2953. current:=root;
  2954. end
  2955. else
  2956. begin
  2957. new(current^.next);
  2958. current:=current^.next;
  2959. end;
  2960. if current=nil then
  2961. begin
  2962. linuxerror:=Sys_ENOMEM;
  2963. globfree(root);
  2964. break;
  2965. end;
  2966. current^.next:=nil;
  2967. getmem(current^.name,length(temp2)+1);
  2968. if current^.name=nil then
  2969. begin
  2970. linuxerror:=Sys_ENOMEM;
  2971. globfree(root);
  2972. break;
  2973. end;
  2974. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2975. end;
  2976. until false;
  2977. closedir(thedir);
  2978. glob:=root;
  2979. end;
  2980. {--------------------------------
  2981. FiledescriptorSets
  2982. --------------------------------}
  2983. Procedure FD_Zero(var fds:fdSet);
  2984. {
  2985. Clear the set of filedescriptors
  2986. }
  2987. begin
  2988. FillChar(fds,sizeof(fdSet),0);
  2989. end;
  2990. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2991. {
  2992. Remove fd from the set of filedescriptors
  2993. }
  2994. begin
  2995. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2996. end;
  2997. Procedure FD_Set(fd:longint;var fds:fdSet);
  2998. {
  2999. Add fd to the set of filedescriptors
  3000. }
  3001. begin
  3002. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  3003. end;
  3004. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  3005. {
  3006. Test if fd is part of the set of filedescriptors
  3007. }
  3008. begin
  3009. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  3010. end;
  3011. Function GetFS (var T:Text):longint;
  3012. {
  3013. Get File Descriptor of a text file.
  3014. }
  3015. begin
  3016. if textrec(t).mode=fmclosed then
  3017. exit(-1)
  3018. else
  3019. GETFS:=textrec(t).Handle
  3020. end;
  3021. Function GetFS(Var F:File):longint;
  3022. {
  3023. Get File Descriptor of an unTyped file.
  3024. }
  3025. begin
  3026. { Handle and mode are on the same place in textrec and filerec. }
  3027. if filerec(f).mode=fmclosed then
  3028. exit(-1)
  3029. else
  3030. GETFS:=filerec(f).Handle
  3031. end;
  3032. {--------------------------------
  3033. Stat.Mode Macro's
  3034. --------------------------------}
  3035. Function S_ISLNK(m:word):boolean;
  3036. {
  3037. Check mode field of inode for link.
  3038. }
  3039. begin
  3040. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  3041. end;
  3042. Function S_ISREG(m:word):boolean;
  3043. {
  3044. Check mode field of inode for regular file.
  3045. }
  3046. begin
  3047. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  3048. end;
  3049. Function S_ISDIR(m:word):boolean;
  3050. {
  3051. Check mode field of inode for directory.
  3052. }
  3053. begin
  3054. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  3055. end;
  3056. Function S_ISCHR(m:word):boolean;
  3057. {
  3058. Check mode field of inode for character device.
  3059. }
  3060. begin
  3061. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  3062. end;
  3063. Function S_ISBLK(m:word):boolean;
  3064. {
  3065. Check mode field of inode for block device.
  3066. }
  3067. begin
  3068. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  3069. end;
  3070. Function S_ISFIFO(m:word):boolean;
  3071. {
  3072. Check mode field of inode for named pipe (FIFO).
  3073. }
  3074. begin
  3075. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  3076. end;
  3077. Function S_ISSOCK(m:word):boolean;
  3078. {
  3079. Check mode field of inode for socket.
  3080. }
  3081. begin
  3082. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  3083. end;
  3084. {--------------------------------
  3085. Memory functions
  3086. --------------------------------}
  3087. function MMap(const m:tmmapargs):longint;
  3088. Var
  3089. Sr : Syscallregs;
  3090. begin
  3091. Sr.reg2:=longint(@m);
  3092. MMap:=syscall(syscall_nr_mmap,sr);
  3093. LinuxError:=Errno;
  3094. end;
  3095. {--------------------------------
  3096. Port IO functions
  3097. --------------------------------}
  3098. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  3099. {
  3100. Set permissions on NUM ports starting with port FROM to VALUE
  3101. this works ONLY as root.
  3102. }
  3103. Var
  3104. Sr : Syscallregs;
  3105. begin
  3106. Sr.Reg2:=From;
  3107. Sr.Reg3:=Num;
  3108. Sr.Reg4:=Value;
  3109. IOPerm:=Syscall(Syscall_nr_ioperm,sr)=0;
  3110. LinuxError:=Errno;
  3111. end;
  3112. {$IFDEF I386}
  3113. {$asmmode direct}
  3114. Procedure WritePort (Port : Longint; Value : Byte);
  3115. {
  3116. Writes 'Value' to port 'Port'
  3117. }
  3118. begin
  3119. asm
  3120. movl 8(%ebp),%edx
  3121. movb 12(%ebp),%al
  3122. outb %al,%dx
  3123. end ['EAX','EDX'];
  3124. end;
  3125. Procedure WritePort (Port : Longint; Value : Word);
  3126. {
  3127. Writes 'Value' to port 'Port'
  3128. }
  3129. begin
  3130. asm
  3131. movl 8(%ebp),%edx
  3132. movw 12(%ebp),%ax
  3133. outw %ax,%dx
  3134. end ['EAX','EDX'];
  3135. end;
  3136. Procedure WritePort (Port : Longint; Value : Longint);
  3137. {
  3138. Writes 'Value' to port 'Port'
  3139. }
  3140. begin
  3141. asm
  3142. movl 8(%ebp),%edx
  3143. movl 12(%ebp),%eax
  3144. outl %eax,%dx
  3145. end ['EAX','EDX'];
  3146. end;
  3147. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  3148. {
  3149. Writes 'Count' longints from 'Buf' to Port
  3150. }
  3151. begin
  3152. asm
  3153. movl 16(%ebp),%ecx
  3154. movl 12(%ebp),%esi
  3155. movl 8(%ebp),%edx
  3156. cld
  3157. rep
  3158. outsl
  3159. end ['ECX','ESI','EDX'];
  3160. end;
  3161. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  3162. {
  3163. Writes 'Count' words from 'Buf' to Port
  3164. }
  3165. begin
  3166. asm
  3167. movl 16(%ebp),%ecx
  3168. movl 12(%ebp),%esi
  3169. movl 8(%ebp),%edx
  3170. cld
  3171. rep
  3172. outsw
  3173. end ['ECX','ESI','EDX'];
  3174. end;
  3175. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  3176. {
  3177. Writes 'Count' bytes from 'Buf' to Port
  3178. }
  3179. begin
  3180. asm
  3181. movl 16(%ebp),%ecx
  3182. movl 12(%ebp),%esi
  3183. movl 8(%ebp),%edx
  3184. cld
  3185. rep
  3186. outsb
  3187. end ['ECX','ESI','EDX'];
  3188. end;
  3189. Procedure ReadPort (Port : Longint; Var Value : Byte);
  3190. {
  3191. Reads 'Value' from port 'Port'
  3192. }
  3193. begin
  3194. asm
  3195. movl 8(%ebp),%edx
  3196. inb %dx,%al
  3197. andl $255,%eax
  3198. movl %eax,12(%ebp)
  3199. end ['EAX','EDX'];
  3200. end;
  3201. Procedure ReadPort (Port : Longint; Var Value : Word);
  3202. {
  3203. Reads 'Value' from port 'Port'
  3204. }
  3205. begin
  3206. asm
  3207. movl 8(%ebp),%edx
  3208. inw %dx,%ax
  3209. andl $65535,%eax
  3210. movl %eax,12(%ebp)
  3211. end ['EAX','EDX'];
  3212. end;
  3213. Procedure ReadPort (Port : Longint; Var Value : Longint);
  3214. {
  3215. Reads 'Value' from port 'Port'
  3216. }
  3217. begin
  3218. asm
  3219. movl 8(%ebp),%edx
  3220. inl %dx,%eax
  3221. movl %eax,12(%ebp)
  3222. end ['EAX','EDX'];
  3223. end;
  3224. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  3225. {
  3226. Reads 'Count' longints from port 'Port' to 'Buf'.
  3227. }
  3228. begin
  3229. asm
  3230. movl 16(%ebp),%ecx
  3231. movl 12(%ebp),%edi
  3232. movl 8(%ebp),%edx
  3233. cld
  3234. rep
  3235. insl
  3236. end ['ECX','EDI','EDX'];
  3237. end;
  3238. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  3239. {
  3240. Reads 'Count' words from port 'Port' to 'Buf'.
  3241. }
  3242. begin
  3243. asm
  3244. movl 16(%ebp),%ecx
  3245. movl 12(%ebp),%edi
  3246. movl 8(%ebp),%edx
  3247. cld
  3248. rep
  3249. insw
  3250. end ['ECX','EDI','EDX'];
  3251. end;
  3252. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  3253. {
  3254. Reads 'Count' bytes from port 'Port' to 'Buf'.
  3255. }
  3256. begin
  3257. asm
  3258. movl 16(%ebp),%ecx
  3259. movl 12(%ebp),%edi
  3260. movl 8(%ebp),%edx
  3261. cld
  3262. rep
  3263. insb
  3264. end ['ECX','EDI','EDX'];
  3265. end;
  3266. {$ENDIF}
  3267. Initialization
  3268. InitLocalTime;
  3269. finalization
  3270. DoneLocalTime;
  3271. End.
  3272. {
  3273. $Log$
  3274. Revision 1.63 2000-02-23 17:19:06 peter
  3275. + readded getepochtime which simply calls gettimeofday
  3276. Revision 1.62 2000/02/09 23:09:13 peter
  3277. * rewrote glob to be much simpler and cleaner, the old code did
  3278. strange complex things with pointers which was unnecessary
  3279. Revision 1.61 2000/02/09 16:59:31 peter
  3280. * truncated log
  3281. Revision 1.60 2000/02/08 12:05:58 peter
  3282. + readlink
  3283. Revision 1.59 2000/01/07 16:41:40 daniel
  3284. * copyright 2000
  3285. Revision 1.58 2000/01/07 16:32:26 daniel
  3286. * copyright 2000 added
  3287. Revision 1.57 2000/01/04 12:56:09 jonas
  3288. * fixed modified registers for port routines
  3289. Revision 1.56 1999/12/28 09:38:07 sg
  3290. * the long version of AssignStream now sets the result value to -1 in
  3291. _all_ cases when it would fail.
  3292. Revision 1.55 1999/12/08 01:03:54 peter
  3293. * overloaded gettime functions supporting hsec and msec,usec
  3294. Revision 1.54 1999/12/01 22:46:59 peter
  3295. + timezone support
  3296. Revision 1.53 1999/11/14 21:35:04 peter
  3297. * removed warnings
  3298. Revision 1.52 1999/11/14 11:11:15 michael
  3299. + Added Pause() and alarm()
  3300. Revision 1.51 1999/11/11 19:43:49 sg
  3301. * fixed severe bug: change by ? in dup2 (flushing) resulted in broken
  3302. AssignStream functions
  3303. Revision 1.50 1999/11/06 14:39:12 peter
  3304. * truncated log
  3305. Revision 1.49 1999/10/28 09:48:31 peter
  3306. + mmap
  3307. Revision 1.48 1999/10/22 10:37:44 peter
  3308. * fixed sigset
  3309. Revision 1.47 1999/10/06 17:43:58 peter
  3310. * freemem with wrong size (found with the new heapmanager)
  3311. Revision 1.46 1999/09/08 16:14:41 peter
  3312. * pointer fixes
  3313. Revision 1.45 1999/08/11 22:02:25 peter
  3314. * removed old integer versions of localtoepoch and epochtolocal, you
  3315. need to use the word versions instead else you got an overloaded bug
  3316. Revision 1.44 1999/07/31 23:55:04 michael
  3317. + FCNTL patch from Sebastian Guenther
  3318. Revision 1.43 1999/07/29 16:33:24 michael
  3319. + Yet more Fixes to assignstream with rerouting of stderr
  3320. Revision 1.42 1999/07/29 15:55:54 michael
  3321. + Fixes to assignstream with rerouting of stderr, by Sebastian Guenther
  3322. Revision 1.41 1999/07/29 15:53:55 michael
  3323. + Added assignstream with rerouting of stderr, by Sebastian Guenther
  3324. }