linux.pp 84 KB

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