linuxold.inc 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916
  1. {
  2. $Id: linuxold.inc,v 1.19 2005/02/14 17:13:31 peter Exp $
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit oldlinux;
  14. Interface
  15. Const
  16. { Things for LSEEK call }
  17. Seek_set = 0;
  18. Seek_Cur = 1;
  19. Seek_End = 2;
  20. { Things for OPEN call - after linux/fcntl.h }
  21. Open_Accmode = 3;
  22. Open_RdOnly = 0;
  23. Open_WrOnly = 1;
  24. Open_RdWr = 2;
  25. Open_Creat = 1 shl 6;
  26. Open_Excl = 2 shl 6;
  27. Open_NoCtty = 4 shl 6;
  28. Open_Trunc = 1 shl 9;
  29. Open_Append = 2 shl 9;
  30. Open_NonBlock = 4 shl 9;
  31. Open_NDelay = Open_NonBlock;
  32. Open_Sync = 1 shl 12;
  33. Open_Direct = 4 shl 12;
  34. Open_LargeFile = 1 shl 15;
  35. Open_Directory = 2 shl 15;
  36. Open_NoFollow = 4 shl 15;
  37. { The waitpid uses the following options:}
  38. Wait_NoHang = 1;
  39. Wait_UnTraced = 2;
  40. Wait_Any = -1;
  41. Wait_MyPGRP = 0;
  42. Wait_Clone = $80000000;
  43. { Constants to check stat.mode }
  44. STAT_IFMT = $f000; {00170000}
  45. STAT_IFSOCK = $c000; {0140000}
  46. STAT_IFLNK = $a000; {0120000}
  47. STAT_IFREG = $8000; {0100000}
  48. STAT_IFBLK = $6000; {0060000}
  49. STAT_IFDIR = $4000; {0040000}
  50. STAT_IFCHR = $2000; {0020000}
  51. STAT_IFIFO = $1000; {0010000}
  52. STAT_ISUID = $0800; {0004000}
  53. STAT_ISGID = $0400; {0002000}
  54. STAT_ISVTX = $0200; {0001000}
  55. { Constants to check permissions }
  56. STAT_IRWXO = $7;
  57. STAT_IROTH = $4;
  58. STAT_IWOTH = $2;
  59. STAT_IXOTH = $1;
  60. STAT_IRWXG = STAT_IRWXO shl 3;
  61. STAT_IRGRP = STAT_IROTH shl 3;
  62. STAT_IWGRP = STAT_IWOTH shl 3;
  63. STAT_IXGRP = STAT_IXOTH shl 3;
  64. STAT_IRWXU = STAT_IRWXO shl 6;
  65. STAT_IRUSR = STAT_IROTH shl 6;
  66. STAT_IWUSR = STAT_IWOTH shl 6;
  67. STAT_IXUSR = STAT_IXOTH shl 6;
  68. { Constants to test the type of filesystem }
  69. fs_old_ext2 = $ef51;
  70. fs_ext2 = $ef53;
  71. fs_ext = $137d;
  72. fs_iso = $9660;
  73. fs_minix = $137f;
  74. fs_minix_30 = $138f;
  75. fs_minux_V2 = $2468;
  76. fs_msdos = $4d44;
  77. fs_nfs = $6969;
  78. fs_proc = $9fa0;
  79. fs_xia = $012FD16D;
  80. { Constansts for MMAP }
  81. MAP_PRIVATE =2;
  82. MAP_ANONYMOUS =$20;
  83. {Constansts Termios/Ioctl (used in Do_IsDevice) }
  84. IOCtl_TCGETS=$5401; // TCGETS is also in termios.inc, but the sysunix needs only this
  85. type
  86. {
  87. Linux system calls take arguments as follows :
  88. cpui386/m68k:
  89. %eax/%d0 : System call number
  90. %ebx/%d1 : first argument
  91. %ecx/%d2 : second argument
  92. %edx/%d3 : third argumens
  93. %esi/%d3 : fourth argument
  94. %edi/%d4 : fifth argument
  95. That is why we define a special type, with only these arguments
  96. To make it processor independent, we don't give any system dependent
  97. names, but the rather abstract reg1,reg2 etc;
  98. }
  99. SysCallRegs=record
  100. reg1,reg2,reg3,reg4,reg5,reg6 : longint;
  101. end;
  102. PSysCallRegs=^SysCallRegs;
  103. TSysCallRegs=SysCallRegs;
  104. { The following are records for system calls }
  105. dirent = packed record
  106. ino,
  107. off : longint;
  108. reclen : word;
  109. name : array [0..255] of char;
  110. end;
  111. pdirent =^dirent;
  112. TDirEnt = dirent;
  113. TDir = packed record
  114. fd : integer;
  115. loc : longint;
  116. size : integer;
  117. buf : pdirent;
  118. {The following are used in libc, but NOT in the linux kernel sources ??}
  119. nextoff: longint;
  120. dd_max : integer; {size of buf. Irrelevant, as buf is of type dirent}
  121. lock : pointer;
  122. end;
  123. PDir =^TDir;
  124. dev_t = word;
  125. Stat = packed record
  126. dev : dev_t;
  127. pad1 : word;
  128. ino : longint;
  129. mode,
  130. nlink,
  131. uid,
  132. gid : word;
  133. rdev : dev_t;
  134. pad2 : word;
  135. size,
  136. blksze,
  137. blocks,
  138. atime,
  139. unused1,
  140. mtime,
  141. unused2,
  142. ctime,
  143. unused3,
  144. unused4,
  145. unused5 : longint;
  146. end;
  147. PStat=^Stat;
  148. TStat=Stat;
  149. Statfs = packed record
  150. fstype, { File system type }
  151. bsize, { Optimal block trensfer size }
  152. blocks, { Data blocks in system }
  153. bfree, { free blocks in system }
  154. bavail, { Available free blocks to non-root users }
  155. files, { File nodes in system }
  156. ffree, { Free file nodes in system }
  157. fsid, { File system ID }
  158. namelen : longint; { Maximum name length in system }
  159. spare : array [0..6] of longint; { For later use }
  160. end;
  161. PStatFS=^StatFS;
  162. TStatFS=StatFS;
  163. fdSet=array[0..7] of longint;{=256 bits}
  164. pfdset=^fdset;
  165. TFDSet=fdset;
  166. timeval = packed record
  167. sec,usec:longint
  168. end;
  169. ptimeval=^timeval;
  170. TTimeVal=timeval;
  171. timespec = packed record
  172. tv_sec,tv_nsec:longint;
  173. end;
  174. timezone = packed record
  175. minuteswest,dsttime:longint;
  176. end;
  177. ptimezone =^timezone;
  178. TTimeZone = timezone;
  179. utsname = packed record
  180. sysname,
  181. nodename,
  182. release,
  183. version,
  184. machine,
  185. domainname : Array[0..64] of char;
  186. end;
  187. PUTSName=^UTSName;
  188. TUTSName=UTSName;
  189. { Get System call numbers and error-numbers}
  190. const
  191. syscall_nr_setup = 0;
  192. syscall_nr_exit = 1;
  193. syscall_nr_fork = 2;
  194. syscall_nr_read = 3;
  195. syscall_nr_write = 4;
  196. syscall_nr_open = 5;
  197. syscall_nr_close = 6;
  198. syscall_nr_waitpid = 7;
  199. syscall_nr_creat = 8;
  200. syscall_nr_link = 9;
  201. syscall_nr_unlink = 10;
  202. syscall_nr_execve = 11;
  203. syscall_nr_chdir = 12;
  204. syscall_nr_time = 13;
  205. syscall_nr_mknod = 14;
  206. syscall_nr_chmod = 15;
  207. syscall_nr_chown = 16;
  208. syscall_nr_break = 17;
  209. syscall_nr_oldstat = 18;
  210. syscall_nr_lseek = 19;
  211. syscall_nr_getpid = 20;
  212. syscall_nr_mount = 21;
  213. syscall_nr_umount = 22;
  214. syscall_nr_setuid = 23;
  215. syscall_nr_getuid = 24;
  216. syscall_nr_stime = 25;
  217. syscall_nr_ptrace = 26;
  218. syscall_nr_alarm = 27;
  219. syscall_nr_oldfstat = 28;
  220. syscall_nr_pause = 29;
  221. syscall_nr_utime = 30;
  222. syscall_nr_stty = 31;
  223. syscall_nr_gtty = 32;
  224. syscall_nr_access = 33;
  225. syscall_nr_nice = 34;
  226. syscall_nr_ftime = 35;
  227. syscall_nr_sync = 36;
  228. syscall_nr_kill = 37;
  229. syscall_nr_rename = 38;
  230. syscall_nr_mkdir = 39;
  231. syscall_nr_rmdir = 40;
  232. syscall_nr_dup = 41;
  233. syscall_nr_pipe = 42;
  234. syscall_nr_times = 43;
  235. syscall_nr_prof = 44;
  236. syscall_nr_brk = 45;
  237. syscall_nr_setgid = 46;
  238. syscall_nr_getgid = 47;
  239. syscall_nr_signal = 48;
  240. syscall_nr_geteuid = 49;
  241. syscall_nr_getegid = 50;
  242. syscall_nr_acct = 51;
  243. syscall_nr_phys = 52;
  244. syscall_nr_lock = 53;
  245. syscall_nr_ioctl = 54;
  246. syscall_nr_fcntl = 55;
  247. syscall_nr_mpx = 56;
  248. syscall_nr_setpgid = 57;
  249. syscall_nr_ulimit = 58;
  250. syscall_nr_oldolduname = 59;
  251. syscall_nr_umask = 60;
  252. syscall_nr_chroot = 61;
  253. syscall_nr_ustat = 62;
  254. syscall_nr_dup2 = 63;
  255. syscall_nr_getppid = 64;
  256. syscall_nr_getpgrp = 65;
  257. syscall_nr_setsid = 66;
  258. syscall_nr_sigaction = 67;
  259. syscall_nr_sgetmask = 68;
  260. syscall_nr_ssetmask = 69;
  261. syscall_nr_setreuid = 70;
  262. syscall_nr_setregid = 71;
  263. syscall_nr_sigsuspend = 72;
  264. syscall_nr_sigpending = 73;
  265. syscall_nr_sethostname = 74;
  266. syscall_nr_setrlimit = 75;
  267. syscall_nr_getrlimit = 76;
  268. syscall_nr_getrusage = 77;
  269. syscall_nr_gettimeofday = 78;
  270. syscall_nr_settimeofday = 79;
  271. syscall_nr_getgroups = 80;
  272. syscall_nr_setgroups = 81;
  273. syscall_nr_select = 82;
  274. syscall_nr_symlink = 83;
  275. syscall_nr_oldlstat = 84;
  276. syscall_nr_readlink = 85;
  277. syscall_nr_uselib = 86;
  278. syscall_nr_swapon = 87;
  279. syscall_nr_reboot = 88;
  280. syscall_nr_readdir = 89;
  281. syscall_nr_mmap = 90;
  282. syscall_nr_munmap = 91;
  283. syscall_nr_truncate = 92;
  284. syscall_nr_ftruncate = 93;
  285. syscall_nr_fchmod = 94;
  286. syscall_nr_fchown = 95;
  287. syscall_nr_getpriority = 96;
  288. syscall_nr_setpriority = 97;
  289. syscall_nr_profil = 98;
  290. syscall_nr_statfs = 99;
  291. syscall_nr_fstatfs = 100;
  292. syscall_nr_ioperm = 101;
  293. syscall_nr_socketcall = 102;
  294. syscall_nr_syslog = 103;
  295. syscall_nr_setitimer = 104;
  296. syscall_nr_getitimer = 105;
  297. syscall_nr_stat = 106;
  298. syscall_nr_lstat = 107;
  299. syscall_nr_fstat = 108;
  300. syscall_nr_olduname = 109;
  301. syscall_nr_iopl = 110;
  302. syscall_nr_vhangup = 111;
  303. syscall_nr_idle = 112;
  304. syscall_nr_vm86old = 113;
  305. syscall_nr_wait4 = 114;
  306. syscall_nr_swapoff = 115;
  307. syscall_nr_sysinfo = 116;
  308. syscall_nr_ipc = 117;
  309. syscall_nr_fsync = 118;
  310. syscall_nr_sigreturn = 119;
  311. syscall_nr_clone = 120;
  312. syscall_nr_setdomainname = 121;
  313. syscall_nr_uname = 122;
  314. syscall_nr_modify_ldt = 123;
  315. syscall_nr_adjtimex = 124;
  316. syscall_nr_mprotect = 125;
  317. syscall_nr_sigprocmask = 126;
  318. syscall_nr_create_module = 127;
  319. syscall_nr_init_module = 128;
  320. syscall_nr_delete_module = 129;
  321. syscall_nr_get_kernel_syms = 130;
  322. syscall_nr_quotactl = 131;
  323. syscall_nr_getpgid = 132;
  324. syscall_nr_fchdir = 133;
  325. syscall_nr_bdflush = 134;
  326. syscall_nr_sysfs = 135;
  327. syscall_nr_personality = 136;
  328. syscall_nr_afs_syscall = 137;
  329. syscall_nr_setfsuid = 138;
  330. syscall_nr_setfsgid = 139;
  331. syscall_nr__llseek = 140;
  332. syscall_nr_getdents = 141;
  333. syscall_nr__newselect = 142;
  334. syscall_nr_flock = 143;
  335. syscall_nr_msync = 144;
  336. syscall_nr_readv = 145;
  337. syscall_nr_writev = 146;
  338. syscall_nr_getsid = 147;
  339. syscall_nr_fdatasync = 148;
  340. syscall_nr__sysctl = 149;
  341. syscall_nr_mlock = 150;
  342. syscall_nr_munlock = 151;
  343. syscall_nr_mlockall = 152;
  344. syscall_nr_munlockall = 153;
  345. syscall_nr_sched_setparam = 154;
  346. syscall_nr_sched_getparam = 155;
  347. syscall_nr_sched_setscheduler = 156;
  348. syscall_nr_sched_getscheduler = 157;
  349. syscall_nr_sched_yield = 158;
  350. syscall_nr_sched_get_priority_max = 159;
  351. syscall_nr_sched_get_priority_min = 160;
  352. syscall_nr_sched_rr_get_interval = 161;
  353. syscall_nr_nanosleep = 162;
  354. syscall_nr_mremap = 163;
  355. syscall_nr_setresuid = 164;
  356. syscall_nr_getresuid = 165;
  357. syscall_nr_vm86 = 166;
  358. syscall_nr_query_module = 167;
  359. syscall_nr_poll = 168;
  360. syscall_nr_sigaltstack = 186;
  361. {$IFDEF SYSCALL_DEBUG}
  362. const
  363. Sys_nr_txt : array[0..168] of string[15]=(
  364. 'Setup', { 0 }
  365. 'Exit', { 1 }
  366. 'Fork', { 2 }
  367. 'Read', { 3 }
  368. 'Write', { 4 }
  369. 'Open', { 5 }
  370. 'Close', { 6 }
  371. 'WaitPid', { 7 }
  372. 'Create', { 8 }
  373. 'Link', { 9 }
  374. 'UnLink', { 10 }
  375. 'ExecVe', { 11 }
  376. 'ChDir', { 12 }
  377. 'Time', { 13 }
  378. 'MkNod', { 14 }
  379. 'ChMod', { 15 }
  380. 'ChOwn', { 16 }
  381. 'Break', { 17 }
  382. 'OldState', { 18 }
  383. 'LSeek', { 19 }
  384. 'GetPid', { 20 }
  385. 'Mount', { 21 }
  386. 'UMount', { 22 }
  387. 'SetUid', { 23 }
  388. 'GetUid', { 24 }
  389. 'STime', { 25 }
  390. 'PTrace', { 26 }
  391. 'Alarm', { 27 }
  392. 'OldFStat', { 28 }
  393. 'Pause', { 29 }
  394. 'UTime', { 30 }
  395. 'STTY', { 31 }
  396. 'GTTY', { 32 }
  397. 'Access', { 33 }
  398. 'Nice', { 34 }
  399. 'FTime', { 35 }
  400. 'Sync', { 36 }
  401. 'Kill', { 37 }
  402. 'Rename', { 38 }
  403. 'MkDir', { 39 }
  404. 'RmDir', { 40 }
  405. 'Dup', { 41 }
  406. 'Pipe', { 42 }
  407. 'Times', { 43 }
  408. 'Prof', { 44 }
  409. 'Break', { 45 }
  410. 'SetGid', { 46 }
  411. 'GetGid', { 47 }
  412. 'Signal', { 48 }
  413. 'GetEUid', { 49 }
  414. 'GetEGid', { 50 }
  415. 'Acct', { 51 }
  416. 'Phys', { 52 }
  417. 'Lock', { 53 }
  418. 'IOCtl', { 54 }
  419. 'FCNtl', { 55 }
  420. 'Mpx', { 56 }
  421. 'SetPGid', { 57 }
  422. 'ULimit', { 58 }
  423. 'OldOldUName', { 59 }
  424. 'UMask', { 60 }
  425. 'ChRoot', { 61 }
  426. 'UStat', { 62 }
  427. 'Dup2', { 63 }
  428. 'GetPPid', { 64 }
  429. 'GetPGrp', { 65 }
  430. 'SetSid', { 66 }
  431. 'SigAction', { 67 }
  432. 'SGetMask', { 68 }
  433. 'SSetMask', { 69 }
  434. 'SetReUid', { 70 }
  435. 'SetReGid', { 71 }
  436. 'SigSuspend', { 72 }
  437. 'SigPending', { 73 }
  438. 'SetHostName', { 74 }
  439. 'SetRLimit', { 75 }
  440. 'GetRLimit', { 76 }
  441. 'GetRUsage', { 77 }
  442. 'GetTimeOfDay', { 78 }
  443. 'SetTimeOfDay', { 79 }
  444. 'GetGroups', { 80 }
  445. 'SetGroups', { 81 }
  446. 'Select', { 82 }
  447. 'SymLink', { 83 }
  448. 'OldLStat', { 84 }
  449. 'ReadLink', { 85 }
  450. 'UseLib', { 86 }
  451. 'SwapOn', { 87 }
  452. 'Reboot', { 88 }
  453. 'ReadDir', { 89 }
  454. 'MMap', { 90 }
  455. 'MunMap', { 91 }
  456. 'Truncate', { 92 }
  457. 'FTruncate', { 93 }
  458. 'FChMod', { 94 }
  459. 'FChOwn', { 95 }
  460. 'GetPriority', { 96 }
  461. 'SetPriority', { 97 }
  462. 'Profile', { 98 }
  463. 'StatFs', { 99 }
  464. 'FStatFs', { 100 }
  465. 'IOPerm', { 101 }
  466. 'SocketCall', { 102 }
  467. 'SysLog', { 103 }
  468. 'SetITimer', { 104 }
  469. 'GetITimer', { 105 }
  470. 'Stat', { 106 }
  471. 'LStat', { 107 }
  472. 'FStat', { 108 }
  473. 'OldUName', { 109 }
  474. 'IOPl', { 110 }
  475. 'VHangup', { 111 }
  476. 'Idle', { 112 }
  477. 'VM86', { 113 }
  478. 'Wait4', { 114 }
  479. 'SwapOff', { 115 }
  480. 'SysInfo', { 116 }
  481. 'IPC', { 117 }
  482. 'FSync', { 118 }
  483. 'SigReturn', { 119 }
  484. 'Clone', { 120 }
  485. 'SetDomainName', { 121 }
  486. 'UName', { 122 }
  487. 'Modify_Ldt', { 123 }
  488. 'AdjTimeX', { 124 }
  489. 'MProtect', { 125 }
  490. 'SigProcMask', { 126 }
  491. 'Create_Module', { 127 }
  492. 'Init_Module', { 128 }
  493. 'Delete_Module', { 129 }
  494. 'Get_Kernel_Syms', { 130 }
  495. 'QuotaCtl', { 131 }
  496. 'GetPGid', { 132 }
  497. 'FChDir', { 133 }
  498. 'BDFlush', { 134 }
  499. 'SysFs', { 135 }
  500. 'Personality', { 136 }
  501. 'AFS_SysCall', { 137 }
  502. 'SetFsUid', { 138 }
  503. 'SetFsGid', { 139 }
  504. '__LLSeek', { 140 }
  505. 'GetDents', { 141 }
  506. '__NewSelect', { 142 }
  507. 'FLock', { 143 }
  508. 'MSync', { 144 }
  509. 'ReadV', { 145 }
  510. 'WriteV', { 146 }
  511. 'GetSid', { 147 }
  512. 'FDataSync', { 148 }
  513. '__SysCtl', { 149 }
  514. 'MLock', { 150 }
  515. 'MUnLock', { 151 }
  516. 'MLockAll', { 152 }
  517. 'MUnLockAll', { 153 }
  518. 'MSchdSetParam', { 154 }
  519. 'MSchdGetParam', { 155 }
  520. 'MSchdSetSchd', { 156 }
  521. 'MSchdGetSchd', { 157 }
  522. 'MSchdYield', { 158 }
  523. 'MSchdGetPriMax', { 159 }
  524. 'MSchdGetPriMin', { 160 }
  525. 'MSchdRRGetInt', { 161 }
  526. 'NanoSleep', { 162 }
  527. 'MRemap', { 163 }
  528. 'SetReSuid', { 164 }
  529. 'GetReSuid', { 165 }
  530. 'vm86', { 166 }
  531. 'QueryModule', { 167 }
  532. 'Poll'); { 168 }
  533. {$ENDIF}
  534. Const
  535. Sys_EPERM = 1; { Operation not permitted }
  536. Sys_ENOENT = 2; { No such file or directory }
  537. Sys_ESRCH = 3; { No such process }
  538. Sys_EINTR = 4; { Interrupted system call }
  539. Sys_EIO = 5; { I/O error }
  540. Sys_ENXIO = 6; { No such device or address }
  541. Sys_E2BIG = 7; { Arg list too long }
  542. Sys_ENOEXEC = 8; { Exec format error }
  543. Sys_EBADF = 9; { Bad file number }
  544. Sys_ECHILD = 10; { No child processes }
  545. Sys_EAGAIN = 11; { Try again }
  546. Sys_ENOMEM = 12; { Out of memory }
  547. Sys_EACCES = 13; { Permission denied }
  548. Sys_EFAULT = 14; { Bad address }
  549. Sys_ENOTBLK = 15; { Block device required, NOT POSIX! }
  550. Sys_EBUSY = 16; { Device or resource busy }
  551. Sys_EEXIST = 17; { File exists }
  552. Sys_EXDEV = 18; { Cross-device link }
  553. Sys_ENODEV = 19; { No such device }
  554. Sys_ENOTDIR = 20; { Not a directory }
  555. Sys_EISDIR = 21; { Is a directory }
  556. Sys_EINVAL = 22; { Invalid argument }
  557. Sys_ENFILE = 23; { File table overflow }
  558. Sys_EMFILE = 24; { Too many open files }
  559. Sys_ENOTTY = 25; { Not a typewriter }
  560. Sys_ETXTBSY = 26; { Text file busy. The new process was
  561. a pure procedure (shared text) file which was
  562. open for writing by another process, or file
  563. which was open for writing by another process,
  564. or while the pure procedure file was being
  565. executed an open(2) call requested write access
  566. requested write access.}
  567. Sys_EFBIG = 27; { File too large }
  568. Sys_ENOSPC = 28; { No space left on device }
  569. Sys_ESPIPE = 29; { Illegal seek }
  570. Sys_EROFS = 30; { Read-only file system }
  571. Sys_EMLINK = 31; { Too many links }
  572. Sys_EPIPE = 32; { Broken pipe }
  573. Sys_EDOM = 33; { Math argument out of domain of func }
  574. Sys_ERANGE = 34; { Math result not representable }
  575. Sys_EDEADLK = 35; { Resource deadlock would occur }
  576. Sys_ENAMETOOLONG= 36; { File name too long }
  577. Sys_ENOLCK = 37; { No record locks available }
  578. Sys_ENOSYS = 38; { Function not implemented }
  579. Sys_ENOTEMPTY= 39; { Directory not empty }
  580. Sys_ELOOP = 40; { Too many symbolic links encountered }
  581. Sys_EWOULDBLOCK = Sys_EAGAIN; { Operation would block }
  582. Sys_ENOMSG = 42; { No message of desired type }
  583. Sys_EIDRM = 43; { Identifier removed }
  584. Sys_ECHRNG = 44; { Channel number out of range }
  585. Sys_EL2NSYNC= 45; { Level 2 not synchronized }
  586. Sys_EL3HLT = 46; { Level 3 halted }
  587. Sys_EL3RST = 47; { Level 3 reset }
  588. Sys_ELNRNG = 48; { Link number out of range }
  589. Sys_EUNATCH = 49; { Protocol driver not attached }
  590. Sys_ENOCSI = 50; { No CSI structure available }
  591. Sys_EL2HLT = 51; { Level 2 halted }
  592. Sys_EBADE = 52; { Invalid exchange }
  593. Sys_EBADR = 53; { Invalid request descriptor }
  594. Sys_EXFULL = 54; { Exchange full }
  595. Sys_ENOANO = 55; { No anode }
  596. Sys_EBADRQC = 56; { Invalid request code }
  597. Sys_EBADSLT = 57; { Invalid slot }
  598. Sys_EDEADLOCK= 58; { File locking deadlock error }
  599. Sys_EBFONT = 59; { Bad font file format }
  600. Sys_ENOSTR = 60; { Device not a stream }
  601. Sys_ENODATA = 61; { No data available }
  602. Sys_ETIME = 62; { Timer expired }
  603. Sys_ENOSR = 63; { Out of streams resources }
  604. Sys_ENONET = 64; { Machine is not on the network }
  605. Sys_ENOPKG = 65; { Package not installed }
  606. Sys_EREMOTE = 66; { Object is remote }
  607. Sys_ENOLINK = 67; { Link has been severed }
  608. Sys_EADV = 68; { Advertise error }
  609. Sys_ESRMNT = 69; { Srmount error }
  610. Sys_ECOMM = 70; { Communication error on send }
  611. Sys_EPROTO = 71; { Protocol error }
  612. Sys_EMULTIHOP= 72; { Multihop attempted }
  613. Sys_EDOTDOT = 73; { RFS specific error }
  614. Sys_EBADMSG = 74; { Not a data message }
  615. Sys_EOVERFLOW= 75; { Value too large for defined data type }
  616. Sys_ENOTUNIQ= 76; { Name not unique on network }
  617. Sys_EBADFD = 77; { File descriptor in bad state }
  618. Sys_EREMCHG = 78; { Remote address changed }
  619. Sys_ELIBACC = 79; { Can not access a needed shared library }
  620. Sys_ELIBBAD = 80; { Accessing a corrupted shared library }
  621. Sys_ELIBSCN = 81; { .lib section in a.out corrupted }
  622. Sys_ELIBMAX = 82; { Attempting to link in too many shared libraries }
  623. Sys_ELIBEXEC= 83; { Cannot exec a shared library directly }
  624. Sys_EILSEQ = 84; { Illegal byte sequence }
  625. Sys_ERESTART= 85; { Interrupted system call should be restarted }
  626. Sys_ESTRPIPE= 86; { Streams pipe error }
  627. Sys_EUSERS = 87; { Too many users }
  628. Sys_ENOTSOCK= 88; { Socket operation on non-socket }
  629. Sys_EDESTADDRREQ= 89; { Destination address required }
  630. Sys_EMSGSIZE= 90; { Message too long }
  631. Sys_EPROTOTYPE= 91; { Protocol wrong type for socket }
  632. Sys_ENOPROTOOPT= 92; { Protocol not available }
  633. Sys_EPROTONOSUPPORT= 93; { Protocol not supported }
  634. Sys_ESOCKTNOSUPPORT= 94; { Socket type not supported }
  635. Sys_EOPNOTSUPP= 95; { Operation not supported on transport endpoint }
  636. Sys_EPFNOSUPPORT= 96; { Protocol family not supported }
  637. Sys_EAFNOSUPPORT= 97; { Address family not supported by protocol }
  638. Sys_EADDRINUSE= 98; { Address already in use }
  639. Sys_EADDRNOTAVAIL= 99; { Cannot assign requested address }
  640. Sys_ENETDOWN= 100; { Network is down }
  641. Sys_ENETUNREACH= 101; { Network is unreachable }
  642. Sys_ENETRESET= 102; { Network dropped connection because of reset }
  643. Sys_ECONNABORTED= 103; { Software caused connection abort }
  644. Sys_ECONNRESET= 104; { Connection reset by peer }
  645. Sys_ENOBUFS = 105; { No buffer space available }
  646. Sys_EISCONN = 106; { Transport endpoint is already connected }
  647. Sys_ENOTCONN= 107; { Transport endpoint is not connected }
  648. Sys_ESHUTDOWN= 108; { Cannot send after transport endpoint shutdown }
  649. Sys_ETOOMANYREFS= 109; { Too many references: cannot splice }
  650. Sys_ETIMEDOUT= 110; { Connection timed out }
  651. Sys_ECONNREFUSED= 111; { Connection refused }
  652. Sys_EHOSTDOWN= 112; { Host is down }
  653. Sys_EHOSTUNREACH= 113; { No route to host }
  654. Sys_EALREADY= 114; { Operation already in progress }
  655. Sys_EINPROGRESS= 115; { Operation now in progress }
  656. Sys_ESTALE = 116; { Stale NFS file handle }
  657. Sys_EUCLEAN = 117; { Structure needs cleaning }
  658. Sys_ENOTNAM = 118; { Not a XENIX named type file }
  659. Sys_ENAVAIL = 119; { No XENIX semaphores available }
  660. Sys_EISNAM = 120; { Is a named type file }
  661. Sys_EREMOTEIO= 121; { Remote I/O error }
  662. Sys_EDQUOT = 122; { Quota exceeded }
  663. { This value was suggested by Daniel
  664. based on infos from www.linuxassembly.org }
  665. Sys_ERROR_MAX = $fff;
  666. {$packrecords C}
  667. {********************
  668. Signal
  669. ********************}
  670. type
  671. SigSet = Longint;
  672. PSigSet = ^SigSet;
  673. Const
  674. { For sending a signal }
  675. SA_NOCLDSTOP = 1;
  676. SA_SHIRQ = $04000000;
  677. SA_STACK = $08000000;
  678. SA_RESTART = $10000000;
  679. SA_INTERRUPT = $20000000;
  680. SA_NOMASK = $40000000;
  681. SA_ONESHOT = $80000000;
  682. SA_ONSTACK = SA_STACK;
  683. SIG_BLOCK = 0;
  684. SIG_UNBLOCK = 1;
  685. SIG_SETMASK = 2;
  686. SIG_DFL = 0 ;
  687. SIG_IGN = 1 ;
  688. SIG_ERR = -1 ;
  689. SIGHUP = 1;
  690. SIGINT = 2;
  691. SIGQUIT = 3;
  692. SIGILL = 4;
  693. SIGTRAP = 5;
  694. SIGABRT = 6;
  695. SIGIOT = 6;
  696. SIGBUS = 7;
  697. SIGFPE = 8;
  698. SIGKILL = 9;
  699. SIGUSR1 = 10;
  700. SIGSEGV = 11;
  701. SIGUSR2 = 12;
  702. SIGPIPE = 13;
  703. SIGALRM = 14;
  704. SIGTerm = 15;
  705. SIGSTKFLT = 16;
  706. SIGCHLD = 17;
  707. SIGCONT = 18;
  708. SIGSTOP = 19;
  709. SIGTSTP = 20;
  710. SIGTTIN = 21;
  711. SIGTTOU = 22;
  712. SIGURG = 23;
  713. SIGXCPU = 24;
  714. SIGXFSZ = 25;
  715. SIGVTALRM = 26;
  716. SIGPROF = 27;
  717. SIGWINCH = 28;
  718. SIGIO = 29;
  719. SIGPOLL = SIGIO;
  720. SIGPWR = 30;
  721. SIGUNUSED = 31;
  722. const
  723. SI_PAD_SIZE = ((128/sizeof(longint)) - 3);
  724. type
  725. Size_T = cardinal;
  726. tfpreg = record
  727. significand: array[0..3] of word;
  728. exponent: word;
  729. end;
  730. pfpstate = ^tfpstate;
  731. tfpstate = record
  732. cw, sw, tag, ipoff, cssel, dataoff, datasel: cardinal;
  733. st: array[0..7] of tfpreg;
  734. status: cardinal;
  735. end;
  736. PSigContextRec = ^SigContextRec;
  737. SigContextRec = record
  738. gs, __gsh: word;
  739. fs, __fsh: word;
  740. es, __esh: word;
  741. ds, __dsh: word;
  742. edi: cardinal;
  743. esi: cardinal;
  744. ebp: cardinal;
  745. esp: cardinal;
  746. ebx: cardinal;
  747. edx: cardinal;
  748. ecx: cardinal;
  749. eax: cardinal;
  750. trapno: cardinal;
  751. err: cardinal;
  752. eip: cardinal;
  753. cs, __csh: word;
  754. eflags: cardinal;
  755. esp_at_signal: cardinal;
  756. ss, __ssh: word;
  757. fpstate: pfpstate;
  758. oldmask: cardinal;
  759. cr2: cardinal;
  760. end;
  761. (*
  762. PSigInfoRec = ^SigInfoRec;
  763. SigInfoRec = record
  764. si_signo: longint;
  765. si_errno: longint;
  766. si_code: longint;
  767. case longint of
  768. 0:
  769. (pad: array[SI_PAD_SIZE] of longint);
  770. 1: { kill }
  771. ( kill: record
  772. pid: longint; { sender's pid }
  773. uid : longint; { sender's uid }
  774. end );
  775. 2: { POSIX.1b timers }
  776. ( timer : record
  777. timer1 : cardinal;
  778. timer2 : cardinal;
  779. end );
  780. 3: { POSIX.1b signals }
  781. ( rt : record
  782. pid : longint; { sender's pid }
  783. uid : longint; { sender's uid }
  784. sigval : longint;
  785. end );
  786. 4: { SIGCHLD }
  787. ( sigchld : record
  788. pid : longint; { which child }
  789. uid : longint; { sender's uid }
  790. status : longint; { exit code }
  791. utime : timeval;
  792. stime : timeval;
  793. end );
  794. 5: { SIGILL, SIGFPE, SIGSEGV, SIGBUS }
  795. ( sigfault : record
  796. addr : pointer;{ faulting insn/memory ref. }
  797. end );
  798. 6:
  799. ( sigpoll : record
  800. band : longint; { POLL_IN, POLL_OUT, POLL_MSG }
  801. fd : longint;
  802. end );
  803. end;
  804. *)
  805. SignalHandler = Procedure(Sig : Longint);cdecl;
  806. PSignalHandler = ^SignalHandler;
  807. SignalRestorer = Procedure;cdecl;
  808. PSignalRestorer = ^SignalRestorer;
  809. TSigAction = procedure(Sig: Longint; SigContext: SigContextRec);cdecl;
  810. SigActionRec = packed record
  811. Handler : record
  812. case byte of
  813. 0: (Sh: SignalHandler);
  814. 1: (Sa: TSigAction);
  815. end;
  816. Sa_Mask : SigSet;
  817. Sa_Flags : Longint;
  818. Sa_restorer : SignalRestorer; { Obsolete - Don't use }
  819. end;
  820. PSigActionRec = ^SigActionRec;
  821. const
  822. SS_ONSTACK = 1;
  823. SS_DISABLE = 2;
  824. MINSIGSTKSZ = 2048;
  825. SIGSTKSZ = 8192;
  826. type
  827. SigAltStack = record
  828. ss_sp : pointer;
  829. ss_flags : longint;
  830. ss_size : size_t;
  831. end;
  832. stack_t = sigaltstack;
  833. PSigAltStack = ^SigAltStack;
  834. pstack_t = ^stack_t;
  835. var
  836. ErrNo,
  837. LinuxError : Longint;
  838. {********************
  839. Process
  840. ********************}
  841. const
  842. {Checked for BSD using Linuxthreads port}
  843. { cloning flags }
  844. CSIGNAL = $000000ff; // signal mask to be sent at exit
  845. CLONE_VM = $00000100; // set if VM shared between processes
  846. CLONE_FS = $00000200; // set if fs info shared between processes
  847. CLONE_FILES = $00000400; // set if open files shared between processes
  848. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  849. CLONE_PID = $00001000; // set if pid shared
  850. type
  851. TCloneFunc=function(args:pointer):longint;cdecl;
  852. const
  853. { For getting/setting priority }
  854. Prio_Process = 0;
  855. Prio_PGrp = 1;
  856. Prio_User = 2;
  857. {$ifdef Solaris}
  858. WNOHANG = $100;
  859. WUNTRACED = $4;
  860. {$ELSE}
  861. WNOHANG = $1;
  862. WUNTRACED = $2;
  863. __WCLONE = $80000000;
  864. {$ENDIF}
  865. {********************
  866. File
  867. ********************}
  868. Const
  869. P_IN = 1;
  870. P_OUT = 2;
  871. Const
  872. LOCK_SH = 1;
  873. LOCK_EX = 2;
  874. LOCK_UN = 8;
  875. LOCK_NB = 4;
  876. Type
  877. Tpipe = array[1..2] of longint;
  878. pglob = ^tglob;
  879. tglob = record
  880. name : pchar;
  881. next : pglob;
  882. end;
  883. ComStr = String[255];
  884. PathStr = String[255];
  885. DirStr = String[255];
  886. NameStr = String[255];
  887. ExtStr = String[255];
  888. const
  889. { For testing access rights }
  890. R_OK = 4;
  891. W_OK = 2;
  892. X_OK = 1;
  893. F_OK = 0;
  894. {$ifndef newreaddir}
  895. { For File control mechanism }
  896. F_GetFd = 1;
  897. F_SetFd = 2;
  898. F_GetFl = 3;
  899. F_SetFl = 4;
  900. {$ifdef Solaris}
  901. F_DupFd = 0;
  902. F_Dup2Fd = 9;
  903. F_GetOwn = 23;
  904. F_SetOwn = 24;
  905. F_GetLk = 14;
  906. F_SetLk = 6;
  907. F_SetLkW = 7;
  908. F_FreeSp = 11;
  909. {$else}
  910. F_GetLk = 5;
  911. F_SetLk = 6;
  912. F_SetLkW = 7;
  913. F_SetOwn = 8;
  914. F_GetOwn = 9;
  915. {$endif}
  916. {$endif}
  917. {********************
  918. IOCtl(TermIOS)
  919. ********************}
  920. {Is too freebsd/Linux specific}
  921. {********************
  922. IOCtl(TermIOS)
  923. ********************}
  924. Const
  925. { Amount of Control Chars }
  926. NCCS = 32;
  927. NCC = 8;
  928. {$Ifndef BSD}
  929. { For Terminal handling }
  930. TCGETS = $5401;
  931. TCSETS = $5402;
  932. TCSETSW = $5403;
  933. TCSETSF = $5404;
  934. TCGETA = $5405;
  935. TCSETA = $5406;
  936. TCSETAW = $5407;
  937. TCSETAF = $5408;
  938. TCSBRK = $5409;
  939. TCXONC = $540A;
  940. TCFLSH = $540B;
  941. TIOCEXCL = $540C;
  942. TIOCNXCL = $540D;
  943. TIOCSCTTY = $540E;
  944. TIOCGPGRP = $540F;
  945. TIOCSPGRP = $5410;
  946. TIOCOUTQ = $5411;
  947. TIOCSTI = $5412;
  948. TIOCGWINSZ = $5413;
  949. TIOCSWINSZ = $5414;
  950. TIOCMGET = $5415;
  951. TIOCMBIS = $5416;
  952. TIOCMBIC = $5417;
  953. TIOCMSET = $5418;
  954. TIOCGSOFTCAR = $5419;
  955. TIOCSSOFTCAR = $541A;
  956. FIONREAD = $541B;
  957. TIOCINQ = FIONREAD;
  958. TIOCLINUX = $541C;
  959. TIOCCONS = $541D;
  960. TIOCGSERIAL = $541E;
  961. TIOCSSERIAL = $541F;
  962. TIOCPKT = $5420;
  963. FIONBIO = $5421;
  964. TIOCNOTTY = $5422;
  965. TIOCSETD = $5423;
  966. TIOCGETD = $5424;
  967. TCSBRKP = $5425;
  968. TIOCTTYGSTRUCT = $5426;
  969. FIONCLEX = $5450;
  970. FIOCLEX = $5451;
  971. FIOASYNC = $5452;
  972. TIOCSERCONFIG = $5453;
  973. TIOCSERGWILD = $5454;
  974. TIOCSERSWILD = $5455;
  975. TIOCGLCKTRMIOS = $5456;
  976. TIOCSLCKTRMIOS = $5457;
  977. TIOCSERGSTRUCT = $5458;
  978. TIOCSERGETLSR = $5459;
  979. TIOCSERGETMULTI = $545A;
  980. TIOCSERSETMULTI = $545B;
  981. TIOCMIWAIT = $545C;
  982. TIOCGICOUNT = $545D;
  983. TIOCPKT_DATA = 0;
  984. TIOCPKT_FLUSHREAD = 1;
  985. TIOCPKT_FLUSHWRITE = 2;
  986. TIOCPKT_STOP = 4;
  987. TIOCPKT_START = 8;
  988. TIOCPKT_NOSTOP = 16;
  989. TIOCPKT_DOSTOP = 32;
  990. {$else}
  991. {$endif}
  992. Type
  993. winsize = packed record
  994. ws_row,
  995. ws_col,
  996. ws_xpixel,
  997. ws_ypixel : word;
  998. end;
  999. TWinSize=winsize;
  1000. Termio = packed record
  1001. c_iflag, { input mode flags }
  1002. c_oflag, { output mode flags }
  1003. c_cflag, { control mode flags }
  1004. c_lflag : Word; { local mode flags }
  1005. c_line : Word; { line discipline - careful, only High byte in use}
  1006. c_cc : array [0..NCC-1] of char;{ control characters }
  1007. end;
  1008. TTermio=Termio;
  1009. {$PACKRECORDS C}
  1010. Termios = record
  1011. c_iflag,
  1012. c_oflag,
  1013. c_cflag,
  1014. c_lflag : Cardinal;
  1015. c_line : char;
  1016. c_cc : array[0..NCCS-1] of byte;
  1017. c_ispeed,
  1018. c_ospeed : longint;
  1019. end;
  1020. TTermios=Termios;
  1021. {$PACKRECORDS Default}
  1022. {const
  1023. 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);}
  1024. const
  1025. {c_cc characters}
  1026. VINTR = 0;
  1027. VQUIT = 1;
  1028. VERASE = 2;
  1029. VKILL = 3;
  1030. VEOF = 4;
  1031. VTIME = 5;
  1032. VMIN = 6;
  1033. VSWTC = 7;
  1034. VSTART = 8;
  1035. VSTOP = 9;
  1036. VSUSP = 10;
  1037. VEOL = 11;
  1038. VREPRINT = 12;
  1039. VDISCARD = 13;
  1040. VWERASE = 14;
  1041. VLNEXT = 15;
  1042. VEOL2 = 16;
  1043. {c_iflag bits}
  1044. IGNBRK = $0000001;
  1045. BRKINT = $0000002;
  1046. IGNPAR = $0000004;
  1047. PARMRK = $0000008;
  1048. INPCK = $0000010;
  1049. ISTRIP = $0000020;
  1050. INLCR = $0000040;
  1051. IGNCR = $0000080;
  1052. ICRNL = $0000100;
  1053. IUCLC = $0000200;
  1054. IXON = $0000400;
  1055. IXANY = $0000800;
  1056. IXOFF = $0001000;
  1057. IMAXBEL = $0002000;
  1058. {c_oflag bits}
  1059. OPOST = $0000001;
  1060. OLCUC = $0000002;
  1061. ONLCR = $0000004;
  1062. OCRNL = $0000008;
  1063. ONOCR = $0000010;
  1064. ONLRET = $0000020;
  1065. OFILL = $0000040;
  1066. OFDEL = $0000080;
  1067. NLDLY = $0000100;
  1068. NL0 = $0000000;
  1069. NL1 = $0000100;
  1070. CRDLY = $0000600;
  1071. CR0 = $0000000;
  1072. CR1 = $0000200;
  1073. CR2 = $0000400;
  1074. CR3 = $0000600;
  1075. TABDLY = $0001800;
  1076. TAB0 = $0000000;
  1077. TAB1 = $0000800;
  1078. TAB2 = $0001000;
  1079. TAB3 = $0001800;
  1080. XTABS = $0001800;
  1081. BSDLY = $0002000;
  1082. BS0 = $0000000;
  1083. BS1 = $0002000;
  1084. VTDLY = $0004000;
  1085. VT0 = $0000000;
  1086. VT1 = $0004000;
  1087. FFDLY = $0008000;
  1088. FF0 = $0000000;
  1089. FF1 = $0008000;
  1090. {c_cflag bits}
  1091. CBAUD = $000100F;
  1092. B0 = $0000000;
  1093. B50 = $0000001;
  1094. B75 = $0000002;
  1095. B110 = $0000003;
  1096. B134 = $0000004;
  1097. B150 = $0000005;
  1098. B200 = $0000006;
  1099. B300 = $0000007;
  1100. B600 = $0000008;
  1101. B1200 = $0000009;
  1102. B1800 = $000000A;
  1103. B2400 = $000000B;
  1104. B4800 = $000000C;
  1105. B9600 = $000000D;
  1106. B19200 = $000000E;
  1107. B38400 = $000000F;
  1108. EXTA = B19200;
  1109. EXTB = B38400;
  1110. CSIZE = $0000030;
  1111. CS5 = $0000000;
  1112. CS6 = $0000010;
  1113. CS7 = $0000020;
  1114. CS8 = $0000030;
  1115. CSTOPB = $0000040;
  1116. CREAD = $0000080;
  1117. PARENB = $0000100;
  1118. PARODD = $0000200;
  1119. HUPCL = $0000400;
  1120. CLOCAL = $0000800;
  1121. CBAUDEX = $0001000;
  1122. B57600 = $0001001;
  1123. B115200 = $0001002;
  1124. B230400 = $0001003;
  1125. B460800 = $0001004;
  1126. CIBAUD = $100F0000;
  1127. CMSPAR = $40000000;
  1128. CRTSCTS = $80000000;
  1129. {c_lflag bits}
  1130. ISIG = $0000001;
  1131. ICANON = $0000002;
  1132. XCASE = $0000004;
  1133. ECHO = $0000008;
  1134. ECHOE = $0000010;
  1135. ECHOK = $0000020;
  1136. ECHONL = $0000040;
  1137. NOFLSH = $0000080;
  1138. TOSTOP = $0000100;
  1139. ECHOCTL = $0000200;
  1140. ECHOPRT = $0000400;
  1141. ECHOKE = $0000800;
  1142. FLUSHO = $0001000;
  1143. PENDIN = $0004000;
  1144. IEXTEN = $0008000;
  1145. {c_line bits}
  1146. TIOCM_LE = $001;
  1147. TIOCM_DTR = $002;
  1148. TIOCM_RTS = $004;
  1149. TIOCM_ST = $008;
  1150. TIOCM_SR = $010;
  1151. TIOCM_CTS = $020;
  1152. TIOCM_CAR = $040;
  1153. TIOCM_RNG = $080;
  1154. TIOCM_DSR = $100;
  1155. TIOCM_CD = TIOCM_CAR;
  1156. TIOCM_RI = TIOCM_RNG;
  1157. TIOCM_OUT1 = $2000;
  1158. TIOCM_OUT2 = $4000;
  1159. {TCSetAttr}
  1160. TCSANOW = 0;
  1161. TCSADRAIN = 1;
  1162. TCSAFLUSH = 2;
  1163. {TCFlow}
  1164. TCOOFF = 0;
  1165. TCOON = 1;
  1166. TCIOFF = 2;
  1167. TCION = 3;
  1168. {TCFlush}
  1169. TCIFLUSH = 0;
  1170. TCOFLUSH = 1;
  1171. TCIOFLUSH = 2;
  1172. {********************
  1173. Info
  1174. ********************}
  1175. Type
  1176. UTimBuf = packed record{in BSD array[0..1] of timeval, but this is
  1177. backwards compatible with linux version}
  1178. actime,
  1179. modtime
  1180. : longint;
  1181. end;
  1182. UTimeBuf=UTimBuf;
  1183. TUTimeBuf=UTimeBuf;
  1184. PUTimeBuf=^UTimeBuf;
  1185. TSysinfo = packed record
  1186. uptime : longint;
  1187. loads : array[1..3] of longint;
  1188. totalram,
  1189. freeram,
  1190. sharedram,
  1191. bufferram,
  1192. totalswap,
  1193. freeswap : longint;
  1194. procs : integer;
  1195. s : string[18];
  1196. end;
  1197. PSysInfo = ^TSysInfo;
  1198. {******************************************************************************
  1199. Procedure/Functions
  1200. ******************************************************************************}
  1201. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  1202. {**************************
  1203. Time/Date Handling
  1204. ***************************}
  1205. var
  1206. tzdaylight : boolean;
  1207. tzseconds : longint;
  1208. tzname : array[boolean] of pchar;
  1209. { timezone support }
  1210. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  1211. procedure GetLocalTimezone(timer:longint);
  1212. procedure ReadTimezoneFile(fn:string);
  1213. function GetTimezoneFile:string;
  1214. Procedure GetTimeOfDay(var tv:timeval);
  1215. Function GetTimeOfDay:longint;
  1216. Function GetEpochTime: longint;
  1217. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  1218. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  1219. procedure GetTime(var hour,min,sec,msec,usec:word);
  1220. procedure GetTime(var hour,min,sec,sec100:word);
  1221. procedure GetTime(var hour,min,sec:word);
  1222. Procedure GetDate(Var Year,Month,Day:Word);
  1223. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  1224. function SetTime(Hour,Min,Sec:word) : Boolean;
  1225. function SetDate(Year,Month,Day:Word) : Boolean;
  1226. function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  1227. {**************************
  1228. Process Handling
  1229. ***************************}
  1230. function CreateShellArgV(const prog:string):ppchar;
  1231. function CreateShellArgV(const prog:Ansistring):ppchar;
  1232. Procedure Execve(Path: pathstr;args:ppchar;ep:ppchar);
  1233. Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  1234. Procedure Execve(path: pchar;args:ppchar;ep:ppchar);
  1235. Procedure Execv(const path:pathstr;args:ppchar);
  1236. Procedure Execv(const path: AnsiString;args:ppchar);
  1237. Procedure Execvp(Path: Pathstr;Args:ppchar;Ep:ppchar);
  1238. Procedure Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar);
  1239. Procedure Execl(const Todo: String);
  1240. Procedure Execl(const Todo: Ansistring);
  1241. Procedure Execle(Todo: String;Ep:ppchar);
  1242. Procedure Execle(Todo: AnsiString;Ep:ppchar);
  1243. Procedure Execlp(Todo: string;Ep:ppchar);
  1244. Procedure Execlp(Todo: Ansistring;Ep:ppchar);
  1245. Function Shell(const Command:String):Longint;
  1246. Function Shell(const Command:AnsiString):Longint;
  1247. Function Fork:longint;
  1248. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  1249. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  1250. Procedure ExitProcess(val:longint);
  1251. Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint; {=>PID (Status Valid), 0 (No Status), -1: Error, special case errno=EINTR }
  1252. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  1253. Procedure Nice(N:integer);
  1254. Function GetPriority(Which,Who:Integer):integer;
  1255. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  1256. function WEXITSTATUS(Status: Integer): Integer;
  1257. function WTERMSIG(Status: Integer): Integer;
  1258. function WSTOPSIG(Status: Integer): Integer;
  1259. Function WIFEXITED(Status: Integer): Boolean;
  1260. Function WIFSTOPPED(Status: Integer): Boolean;
  1261. Function WIFSIGNALED(Status: Integer): Boolean;
  1262. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  1263. Function W_STOPCODE(Signal: Integer): Integer;
  1264. Function GetPid:LongInt;
  1265. Function GetPPid:LongInt;
  1266. Function GetUid:Longint;
  1267. Function GetEUid:Longint;
  1268. Function GetGid:Longint;
  1269. Function GetEGid:Longint;
  1270. {**************************
  1271. File Handling
  1272. ***************************}
  1273. Function fdOpen(pathname:string;flags:longint):longint;
  1274. Function fdOpen(pathname:string;flags,mode:longint):longint;
  1275. Function fdOpen(pathname:pchar;flags:longint):longint;
  1276. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  1277. Function fdClose(fd:longint):boolean;
  1278. Function fdRead(fd:longint;var buf;size:longint):longint;
  1279. Function fdWrite(fd:longint;const buf;size:longint):longint;
  1280. Function fdTruncate(fd,size:longint):boolean;
  1281. Function fdSeek (fd,pos,seektype :longint): longint;
  1282. Function fdFlush (fd : Longint) : Boolean;
  1283. Function Link(OldPath,NewPath:pathstr):boolean;
  1284. Function SymLink(OldPath,NewPath:pathstr):boolean;
  1285. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  1286. Function ReadLink(name:pathstr):pathstr;
  1287. Function UnLink(Path:pathstr):boolean;
  1288. Function UnLink(Path:pchar):Boolean;
  1289. Function FReName (OldName,NewName : Pchar) : Boolean;
  1290. Function FReName (OldName,NewName : String) : Boolean;
  1291. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  1292. Function Chmod(path:pathstr;Newmode:longint):boolean;
  1293. Function Utime(const path:pathstr;utim:utimebuf):boolean;
  1294. Function Access(Path:Pathstr ;mode:integer):boolean;
  1295. Function Umask(Mask:Integer):integer;
  1296. Function Flock (fd,mode : longint) : boolean;
  1297. Function Flock (var T : text;mode : longint) : boolean;
  1298. Function Flock (var F : File;mode : longint) : boolean;
  1299. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  1300. Function FStat(Fd:longint;Var Info:stat):Boolean;
  1301. Function FStat(var F:Text;Var Info:stat):Boolean;
  1302. Function FStat(var F:File;Var Info:stat):Boolean;
  1303. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  1304. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  1305. Function FSStat(Fd: Longint;Var Info:statfs):Boolean;
  1306. Function Fcntl(Fd:longint;Cmd:longint):longint;
  1307. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  1308. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  1309. Procedure Fcntl(var Fd:Text;Cmd:longint;Arg:Longint);
  1310. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  1311. Function Dup(var oldfile,newfile:text):Boolean;
  1312. Function Dup(var oldfile,newfile:file):Boolean;
  1313. Function Dup2(oldfile,newfile:longint):Boolean;
  1314. Function Dup2(var oldfile,newfile:text):Boolean;
  1315. Function Dup2(var oldfile,newfile:file):Boolean;
  1316. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  1317. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1318. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  1319. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  1320. {**************************
  1321. Directory Handling
  1322. ***************************}
  1323. {$ifndef newreaddir} {only for FreeBSD, temporary solution}
  1324. Function OpenDir(f:pchar):pdir;
  1325. Function OpenDir(f: String):pdir;
  1326. function CloseDir(p:pdir):integer;
  1327. Function ReadDir(p:pdir):pdirent;
  1328. procedure SeekDir(p:pdir;off:longint);
  1329. function TellDir(p:pdir):longint;
  1330. {$else}
  1331. Function OpenDir(name:pchar):pdir;
  1332. Function OpenDir(f: String):pdir;
  1333. function CloseDir(dirp:pdir):integer;
  1334. Function ReadDir(p:pdir):pdirent;
  1335. procedure SeekDir(dirp:pdir;loc:longint);
  1336. function TellDir(dirp:pdir):longint;
  1337. {$endif}
  1338. {**************************
  1339. Pipe/Fifo/Stream
  1340. ***************************}
  1341. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  1342. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1343. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1344. Function PClose(Var F:text) : longint;
  1345. Function PClose(Var F:file) : longint;
  1346. Procedure POpen(var F:text;const Prog:String;rw:char);
  1347. Procedure POpen(var F:file;const Prog:String;rw:char);
  1348. Function mkFifo(pathname:string;mode:longint):boolean;
  1349. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1350. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1351. {**************************
  1352. General information
  1353. ***************************}
  1354. Function GetEnv(P:string):Pchar;
  1355. Function GetDomainName:String;
  1356. Function GetHostName:String;
  1357. Function Sysinfo(var Info:TSysinfo):Boolean;
  1358. Function Uname(var unamerec:utsname):Boolean;
  1359. {**************************
  1360. Signal
  1361. ***************************}
  1362. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  1363. Procedure SigProcMask (How:longint;SSet,OldSSet:PSigSet);
  1364. Function SigPending:SigSet;
  1365. Procedure SigSuspend(Mask:Sigset);
  1366. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  1367. Function Kill(Pid:longint;Sig:longint):integer;
  1368. Procedure SigRaise(Sig:integer);
  1369. Function Alarm(Sec : Longint) : longint;
  1370. Procedure Pause;
  1371. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  1372. {**************************
  1373. IOCtl/Termios Functions
  1374. ***************************}
  1375. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  1376. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1377. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  1378. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  1379. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  1380. Procedure CFMakeRaw(var tios:TermIOS);
  1381. Function TCSendBreak(fd,duration:longint):boolean;
  1382. Function TCSetPGrp(fd,id:longint):boolean;
  1383. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1384. Function TCFlush(fd,qsel:longint):boolean;
  1385. Function TCDrain(fd:longint):boolean;
  1386. Function TCFlow(fd,act:longint):boolean;
  1387. Function IsATTY(Handle:Longint):Boolean;
  1388. Function IsATTY(var f:text):Boolean;
  1389. function TTYname(Handle:Longint):string;
  1390. function TTYname(var F:Text):string;
  1391. {**************************
  1392. Memory functions
  1393. ***************************}
  1394. const
  1395. PROT_READ = $1; { page can be read }
  1396. PROT_WRITE = $2; { page can be written }
  1397. PROT_EXEC = $4; { page can be executed }
  1398. PROT_NONE = $0; { page can not be accessed }
  1399. MAP_SHARED = $1; { Share changes }
  1400. // MAP_PRIVATE = $2; { Changes are private }
  1401. MAP_TYPE = $f; { Mask for type of mapping }
  1402. MAP_FIXED = $10; { Interpret addr exactly }
  1403. // MAP_ANONYMOUS = $20; { don't use a file }
  1404. MAP_GROWSDOWN = $100; { stack-like segment }
  1405. MAP_DENYWRITE = $800; { ETXTBSY }
  1406. MAP_EXECUTABLE = $1000; { mark it as an executable }
  1407. MAP_LOCKED = $2000; { pages are locked }
  1408. MAP_NORESERVE = $4000; { don't check for reservations }
  1409. type
  1410. tmmapargs=record
  1411. address : longint;
  1412. size : longint;
  1413. prot : longint;
  1414. flags : longint;
  1415. fd : longint;
  1416. offset : longint;
  1417. end;
  1418. function MMap(const m:tmmapargs):longint;
  1419. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  1420. {**************************
  1421. Port IO functions
  1422. ***************************}
  1423. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  1424. Function IoPL(Level : longint) : Boolean;
  1425. {$ifdef cpui386}
  1426. Procedure WritePort (Port : Longint; Value : Byte);oldfpccall;
  1427. Procedure WritePort (Port : Longint; Value : Word);oldfpccall;
  1428. Procedure WritePort (Port : Longint; Value : Longint);oldfpccall;
  1429. Procedure WritePortB (Port : Longint; Value : Byte);oldfpccall;
  1430. Procedure WritePortW (Port : Longint; Value : Word);oldfpccall;
  1431. Procedure WritePortL (Port : Longint; Value : Longint);oldfpccall;
  1432. Procedure WritePortL (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1433. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1434. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1435. Procedure ReadPort (Port : Longint; Var Value : Byte);oldfpccall;
  1436. Procedure ReadPort (Port : Longint; Var Value : Word);oldfpccall;
  1437. Procedure ReadPort (Port : Longint; Var Value : Longint);oldfpccall;
  1438. function ReadPortB (Port : Longint): Byte;oldfpccall;
  1439. function ReadPortW (Port : Longint): Word;oldfpccall;
  1440. function ReadPortL (Port : Longint): LongInt;oldfpccall;
  1441. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1442. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1443. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);oldfpccall;
  1444. {$endif}
  1445. {**************************
  1446. Utility functions
  1447. ***************************}
  1448. Function Octal(l:longint):longint;
  1449. Function FExpand(Const Path: PathStr):PathStr;
  1450. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1451. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  1452. Function Dirname(Const path:pathstr):pathstr;
  1453. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  1454. Function FNMatch(const Pattern,Name:string):Boolean;
  1455. Function Glob(Const path:pathstr):pglob;
  1456. Procedure Globfree(var p:pglob);
  1457. Function StringToPPChar(Var S:String):ppchar;
  1458. Function StringToPPChar(Var S:AnsiString):ppchar;
  1459. Function StringToPPChar(S : Pchar):ppchar;
  1460. Function GetFS(var T:Text):longint;
  1461. Function GetFS(Var F:File):longint;
  1462. {Filedescriptorsets}
  1463. Procedure FD_Zero(var fds:fdSet);
  1464. Procedure FD_Clr(fd:longint;var fds:fdSet);
  1465. Procedure FD_Set(fd:longint;var fds:fdSet);
  1466. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  1467. {Stat.Mode Types}
  1468. Function S_ISLNK(m:word):boolean;
  1469. Function S_ISREG(m:word):boolean;
  1470. Function S_ISDIR(m:word):boolean;
  1471. Function S_ISCHR(m:word):boolean;
  1472. Function S_ISBLK(m:word):boolean;
  1473. Function S_ISFIFO(m:word):boolean;
  1474. Function S_ISSOCK(m:word):boolean;
  1475. {******************************************************************************
  1476. Implementation
  1477. ******************************************************************************}
  1478. Implementation
  1479. Uses Strings;
  1480. { Get the definitions of textrec and filerec }
  1481. {$i textrec.inc}
  1482. {$i filerec.inc}
  1483. {No debugging for syslinux include !}
  1484. {$IFDEF SYS_LINUX}
  1485. {$UNDEF SYSCALL_DEBUG}
  1486. {$ENDIF SYS_LINUX}
  1487. {*****************************************************************************
  1488. --- Main:The System Call Self ---
  1489. *****************************************************************************}
  1490. {$ifdef FPC_PROFILE}
  1491. {$define PROFILE_WAS_ACTIVE}
  1492. {$profile off}
  1493. {$else}
  1494. {$undef PROFILE_WAS_ACTIVE}
  1495. {$endif}
  1496. Procedure Do_SysCall( callnr:longint;var regs : SysCallregs );oldfpccall;assembler;
  1497. {
  1498. This function puts the registers in place, does the call, and then
  1499. copies back the registers as they are after the SysCall.
  1500. }
  1501. {$ifdef cpui386}
  1502. {$ASMMODE ATT}
  1503. asm
  1504. { load the registers... }
  1505. movl 12(%ebp),%eax
  1506. movl 4(%eax),%ebx
  1507. movl 8(%eax),%ecx
  1508. movl 12(%eax),%edx
  1509. movl 16(%eax),%esi
  1510. movl 20(%eax),%edi
  1511. { set the call number }
  1512. movl 8(%ebp),%eax
  1513. { Go ! }
  1514. int $0x80
  1515. { Put back the registers... }
  1516. pushl %eax
  1517. movl 12(%ebp),%eax
  1518. movl %edi,20(%eax)
  1519. movl %esi,16(%eax)
  1520. movl %edx,12(%eax)
  1521. movl %ecx,8(%eax)
  1522. movl %ebx,4(%eax)
  1523. popl %ebx
  1524. movl %ebx,(%eax)
  1525. end;
  1526. {$ASMMODE DEFAULT}
  1527. {$else}
  1528. {$ifdef cpum68k}
  1529. asm
  1530. { load the registers... }
  1531. move.l 12(a6),a0
  1532. move.l 4(a0),d1
  1533. move.l 8(a0),d2
  1534. move.l 12(a0),d3
  1535. move.l 16(a0),d4
  1536. move.l 20(a0),d5
  1537. { set the call number }
  1538. move.l 8(a6),d0
  1539. { Go ! }
  1540. trap #0
  1541. { Put back the registers... }
  1542. move.l d0,-(sp)
  1543. move.l 12(a6),a0
  1544. move.l d5,20(a0)
  1545. move.l d4,16(a0)
  1546. move.l d3,12(a0)
  1547. move.l d2,8(a0)
  1548. move.l d1,4(a0)
  1549. move.l (sp)+,d1
  1550. move.l d1,(a0)
  1551. end;
  1552. {$else}
  1553. {$error Cannot decide which processor you have ! define cpui386 or m68k }
  1554. {$endif}
  1555. {$endif}
  1556. {$IFDEF SYSCALL_DEBUG}
  1557. Const
  1558. DoSysCallDebug : Boolean = False;
  1559. var
  1560. LastCnt,
  1561. LastEax,
  1562. LastCall : longint;
  1563. DebugTxt : string[20];
  1564. {$ENDIF}
  1565. Function SysCall( callnr:longint;var regs : SysCallregs ):longint;
  1566. {
  1567. This function serves as an interface to do_SysCall.
  1568. If the SysCall returned a negative number, it returns -1, and puts the
  1569. SysCall result in errno. Otherwise, it returns the SysCall return value
  1570. }
  1571. begin
  1572. do_SysCall(callnr,regs);
  1573. if (regs.reg1<0) and (regs.reg1>=-Sys_ERROR_MAX) then
  1574. begin
  1575. {$IFDEF SYSCALL_DEBUG}
  1576. If DoSysCallDebug then
  1577. debugtxt:=' syscall error: ';
  1578. {$endif}
  1579. ErrNo:=-regs.reg1;
  1580. SysCall:=-1;
  1581. end
  1582. else
  1583. begin
  1584. {$IFDEF SYSCALL_DEBUG}
  1585. if DoSysCallDebug then
  1586. debugtxt:=' syscall returned: ';
  1587. {$endif}
  1588. SysCall:=regs.reg1;
  1589. errno:=0
  1590. end;
  1591. {$IFDEF SYSCALL_DEBUG}
  1592. if DoSysCallDebug then
  1593. begin
  1594. inc(lastcnt);
  1595. if (callnr<>lastcall) or (regs.reg1<>lasteax) then
  1596. begin
  1597. if lastcnt>1 then
  1598. writeln(sys_nr_txt[lastcall],debugtxt,lasteax,' (',lastcnt,'x)');
  1599. lastcall:=callnr;
  1600. lasteax:=regs.reg1;
  1601. lastcnt:=0;
  1602. writeln(sys_nr_txt[lastcall],debugtxt,lasteax);
  1603. end;
  1604. end;
  1605. {$endif}
  1606. end;
  1607. {$ifdef PROFILE_WAS_ACTIVE}
  1608. {$profile on}
  1609. {$undef PROFILE_WAS_ACTIVE}
  1610. {$endif}
  1611. Function Sys_Time:longint;
  1612. var
  1613. regs : SysCallregs;
  1614. begin
  1615. regs.reg2:=0;
  1616. Sys_Time:=SysCall(SysCall_nr_time,regs);
  1617. end;
  1618. {*****************************************************************************
  1619. --- File:File handling related calls ---
  1620. *****************************************************************************}
  1621. Function Sys_Open(f:pchar;flags:longint;mode:integer):longint;
  1622. var
  1623. regs : SysCallregs;
  1624. Begin
  1625. regs.reg2:=longint(f);
  1626. regs.reg3:=flags;
  1627. regs.reg4:=mode;
  1628. Sys_Open:=SysCall(SysCall_nr_open,regs);
  1629. End;
  1630. Function Sys_Close(f:longint):longint;
  1631. var
  1632. regs : SysCallregs;
  1633. begin
  1634. regs.reg2:=f;
  1635. Sys_Close:=SysCall(SysCall_nr_close,regs);
  1636. end;
  1637. Function Sys_Lseek(F:longint;Off:longint;Whence:longint):longint;
  1638. var
  1639. regs : SysCallregs;
  1640. begin
  1641. regs.reg2:=f;
  1642. regs.reg3:=off;
  1643. regs.reg4:=Whence;
  1644. Sys_lseek:=SysCall(SysCall_nr_lseek,regs);
  1645. end;
  1646. Function Sys_Read(f:longint;buffer:pchar;count:longint):longint;
  1647. var
  1648. regs : SysCallregs;
  1649. begin
  1650. regs.reg2:=f;
  1651. regs.reg3:=longint(buffer);
  1652. regs.reg4:=count;
  1653. Sys_Read:=SysCall(SysCall_nr_read,regs);
  1654. end;
  1655. Function Sys_Write(f:longint;buffer:pchar;count:longint):longint;
  1656. var
  1657. regs : SysCallregs;
  1658. begin
  1659. regs.reg2:=f;
  1660. regs.reg3:=longint(buffer);
  1661. regs.reg4:=count;
  1662. Sys_Write:=SysCall(SysCall_nr_write,regs);
  1663. end;
  1664. Function Sys_Unlink(Filename:pchar):longint;
  1665. var
  1666. regs : SysCallregs;
  1667. begin
  1668. regs.reg2:=longint(filename);
  1669. Sys_Unlink:=SysCall(SysCall_nr_unlink,regs);
  1670. end;
  1671. Function Sys_fstat(fd : longint;var Info:stat):Longint;
  1672. var
  1673. regs : SysCallregs;
  1674. begin
  1675. regs.reg2:=fd;
  1676. regs.reg3:=longint(@Info);
  1677. Sys_fStat:=SysCall(SysCall_nr_fstat,regs);
  1678. end;
  1679. Function Sys_Rename(Oldname,Newname:pchar):longint;
  1680. var
  1681. regs : SysCallregs;
  1682. begin
  1683. regs.reg2:=longint(oldname);
  1684. regs.reg3:=longint(newname);
  1685. Sys_Rename:=SysCall(SysCall_nr_rename,regs);
  1686. end;
  1687. Function Sys_Stat(Filename:pchar;var Buffer: stat):longint;
  1688. {
  1689. We need this for getcwd
  1690. }
  1691. var
  1692. regs : SysCallregs;
  1693. begin
  1694. regs.reg2:=longint(filename);
  1695. regs.reg3:=longint(@buffer);
  1696. Sys_Stat:=SysCall(SysCall_nr_stat,regs);
  1697. end;
  1698. Function Sys_Symlink(oldname,newname:pchar):longint;
  1699. {
  1700. We need this for erase
  1701. }
  1702. var
  1703. regs : SysCallregs;
  1704. begin
  1705. regs.reg2:=longint(oldname);
  1706. regs.reg3:=longint(newname);
  1707. Sys_symlink:=SysCall(SysCall_nr_symlink,regs);
  1708. end;
  1709. Function Sys_ReadLink(name,linkname:pchar;maxlen:longint):longint;
  1710. var
  1711. regs : SysCallRegs;
  1712. begin
  1713. regs.reg2:=longint(name);
  1714. regs.reg3:=longint(linkname);
  1715. regs.reg4:=maxlen;
  1716. Sys_ReadLink:=SysCall(Syscall_nr_readlink,regs);
  1717. end;
  1718. {*****************************************************************************
  1719. --- Directory:Directory related calls ---
  1720. *****************************************************************************}
  1721. Function Sys_Chdir(Filename:pchar):longint;
  1722. var
  1723. regs : SysCallregs;
  1724. begin
  1725. regs.reg2:=longint(filename);
  1726. Sys_ChDir:=SysCall(SysCall_nr_chdir,regs);
  1727. end;
  1728. Function Sys_Mkdir(Filename:pchar;mode:longint):longint;
  1729. var
  1730. regs : SysCallregs;
  1731. begin
  1732. regs.reg2:=longint(filename);
  1733. regs.reg3:=mode;
  1734. Sys_MkDir:=SysCall(SysCall_nr_mkdir,regs);
  1735. end;
  1736. Function Sys_Rmdir(Filename:pchar):longint;
  1737. var
  1738. regs : SysCallregs;
  1739. begin
  1740. regs.reg2:=longint(filename);
  1741. Sys_Rmdir:=SysCall(SysCall_nr_rmdir,regs);
  1742. end;
  1743. { we need this for getcwd }
  1744. Function OpenDir(f:pchar):pdir;
  1745. var
  1746. fd:integer;
  1747. st:stat;
  1748. ptr:pdir;
  1749. begin
  1750. opendir:=nil;
  1751. if sys_stat(f,st)<0 then
  1752. exit;
  1753. { Is it a dir ? }
  1754. if not((st.mode and $f000)=$4000)then
  1755. begin
  1756. errno:=sys_enotdir;
  1757. exit
  1758. end;
  1759. { Open it}
  1760. fd:=sys_open(f,OPEN_RDONLY,438);
  1761. if fd<0 then
  1762. exit;
  1763. new(ptr);
  1764. if ptr=nil then
  1765. exit;
  1766. new(ptr^.buf);
  1767. if ptr^.buf=nil then
  1768. exit;
  1769. ptr^.fd:=fd;
  1770. ptr^.loc:=0;
  1771. ptr^.size:=0;
  1772. ptr^.dd_max:=sizeof(ptr^.buf^);
  1773. opendir:=ptr;
  1774. end;
  1775. function CloseDir(p:pdir):integer;
  1776. begin
  1777. closedir:=sys_close(p^.fd);
  1778. dispose(p^.buf);
  1779. dispose(p);
  1780. end;
  1781. Function Sys_ReadDir(p:pdir):pdirent;
  1782. var
  1783. regs :SysCallregs;
  1784. dummy:longint;
  1785. begin
  1786. regs.reg3:=longint(p^.buf);
  1787. regs.reg2:=p^.fd;
  1788. regs.reg4:=1;
  1789. dummy:=SysCall(SysCall_nr_readdir,regs);
  1790. { the readdir system call returns the number of bytes written }
  1791. if dummy=0 then
  1792. sys_readdir:=nil
  1793. else
  1794. sys_readdir:=p^.buf
  1795. end;
  1796. {*****************************************************************************
  1797. --- Process:Process & program handling - related calls ---
  1798. *****************************************************************************}
  1799. Function Sys_GetPid:LongInt;
  1800. var
  1801. regs : SysCallregs;
  1802. begin
  1803. Sys_GetPid:=SysCall(SysCall_nr_getpid,regs);
  1804. end;
  1805. Procedure Sys_Exit(ExitCode:Integer);
  1806. var
  1807. regs : SysCallregs;
  1808. begin
  1809. regs.reg2:=exitcode;
  1810. SysCall(SysCall_nr_exit,regs)
  1811. end;
  1812. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  1813. {
  1814. Change action of process upon receipt of a signal.
  1815. Signum specifies the signal (all except SigKill and SigStop).
  1816. If Act is non-nil, it is used to specify the new action.
  1817. If OldAct is non-nil the previous action is saved there.
  1818. }
  1819. Var
  1820. sr : Syscallregs;
  1821. begin
  1822. sr.reg2:=Signum;
  1823. sr.reg3:=Longint(act);
  1824. sr.reg4:=Longint(oldact);
  1825. SysCall(Syscall_nr_sigaction,sr);
  1826. end;
  1827. function Sys_FTruncate(Handle,Pos:longint):longint; //moved from sysunix.inc Do_Truncate
  1828. var
  1829. sr : syscallregs;
  1830. begin
  1831. sr.reg2:=Handle;
  1832. sr.reg3:=Pos;
  1833. Sys_FTruncate:=syscall(syscall_nr_ftruncate,sr);
  1834. end;
  1835. Function Sys_mmap(adr,len,prot,flags,fdes,off:longint):longint; // moved from sysunix.inc, used in sbrk
  1836. type
  1837. tmmapargs=packed record
  1838. address : longint;
  1839. size : longint;
  1840. prot : longint;
  1841. flags : longint;
  1842. fd : longint;
  1843. offset : longint;
  1844. end;
  1845. var
  1846. t : syscallregs;
  1847. mmapargs : tmmapargs;
  1848. begin
  1849. mmapargs.address:=adr;
  1850. mmapargs.size:=len;
  1851. mmapargs.prot:=prot;
  1852. mmapargs.flags:=flags;
  1853. mmapargs.fd:=fdes;
  1854. mmapargs.offset:=off;
  1855. t.reg2:=longint(@mmapargs);
  1856. do_syscall(syscall_nr_mmap,t);
  1857. Sys_mmap:=t.reg1;
  1858. if t.reg1=-1 then
  1859. errno:=-1;
  1860. end;
  1861. {
  1862. Interface to Unix ioctl call.
  1863. Performs various operations on the filedescriptor Handle.
  1864. Ndx describes the operation to perform.
  1865. Data points to data needed for the Ndx function. The structure of this
  1866. data is function-dependent.
  1867. }
  1868. Function Sys_IOCtl(Handle,Ndx: Longint;Data: Pointer):LongInt; // This was missing here, instead hardcode in Do_IsDevice
  1869. var
  1870. sr: SysCallRegs;
  1871. begin
  1872. sr.reg2:=Handle;
  1873. sr.reg3:=Ndx;
  1874. sr.reg4:=Longint(Data);
  1875. Sys_IOCtl:=SysCall(Syscall_nr_ioctl,sr);
  1876. end;
  1877. Function Sys_SigAltStack(ss, oss :psigaltstack):longint;
  1878. var
  1879. regs : SysCallregs;
  1880. begin
  1881. regs.reg2:=longint(ss);
  1882. regs.reg3:=longint(oss);
  1883. sys_sigaltstack:=SysCall(syscall_nr_sigaltstack,regs);
  1884. end;
  1885. Function Fork:longint;
  1886. {
  1887. This function issues the 'fork' System call. the program is duplicated in memory
  1888. and Execution continues in parent and child process.
  1889. In the parent process, fork returns the PID of the child. In the child process,
  1890. zero is returned.
  1891. A negative value indicates that an error has occurred, the error is returned in
  1892. LinuxError.
  1893. }
  1894. var
  1895. regs:SysCallregs;
  1896. begin
  1897. Fork:=SysCall(SysCall_nr_fork,regs);
  1898. LinuxError:=Errno;
  1899. End;
  1900. function clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  1901. begin
  1902. if (pointer(func)=nil) or (sp=nil) then
  1903. begin
  1904. LinuxError:=Sys_EInval;
  1905. exit(-1); // give an error result
  1906. end;
  1907. {$ifdef cpui386}
  1908. {$ASMMODE ATT}
  1909. asm
  1910. { Insert the argument onto the new stack. }
  1911. movl sp,%ecx
  1912. subl $8,%ecx
  1913. movl args,%eax
  1914. movl %eax,4(%ecx)
  1915. { Save the function pointer as the zeroth argument.
  1916. It will be popped off in the child in the ebx frobbing below. }
  1917. movl func,%eax
  1918. movl %eax,0(%ecx)
  1919. { Do the system call }
  1920. pushl %ebx
  1921. movl flags,%ebx
  1922. movl SysCall_nr_clone,%eax
  1923. int $0x80
  1924. popl %ebx
  1925. test %eax,%eax
  1926. jnz .Lclone_end
  1927. { We're in the new thread }
  1928. subl %ebp,%ebp { terminate the stack frame }
  1929. call *%ebx
  1930. { exit process }
  1931. movl %eax,%ebx
  1932. movl $1,%eax
  1933. int $0x80
  1934. .Lclone_end:
  1935. movl %eax,__RESULT
  1936. end;
  1937. {$endif cpui386}
  1938. {$ifdef cpum68k}
  1939. { No yet translated, my m68k assembler is too weak for such things PM }
  1940. (*
  1941. asm
  1942. { Insert the argument onto the new stack. }
  1943. movl sp,%ecx
  1944. subl $8,%ecx
  1945. movl args,%eax
  1946. movl %eax,4(%ecx)
  1947. { Save the function pointer as the zeroth argument.
  1948. It will be popped off in the child in the ebx frobbing below. }
  1949. movl func,%eax
  1950. movl %eax,0(%ecx)
  1951. { Do the system call }
  1952. pushl %ebx
  1953. movl flags,%ebx
  1954. movl SysCall_nr_clone,%eax
  1955. int $0x80
  1956. popl %ebx
  1957. test %eax,%eax
  1958. jnz .Lclone_end
  1959. { We're in the new thread }
  1960. subl %ebp,%ebp { terminate the stack frame }
  1961. call *%ebx
  1962. { exit process }
  1963. movl %eax,%ebx
  1964. movl $1,%eax
  1965. int $0x80
  1966. .Lclone_end:
  1967. movl %eax,__RESULT
  1968. end;
  1969. *)
  1970. {$endif cpum68k}
  1971. end;
  1972. Procedure Execve(path:pathstr;args:ppchar;ep:ppchar);
  1973. {
  1974. Replaces the current program by the program specified in path,
  1975. arguments in args are passed to Execve.
  1976. environment specified in ep is passed on.
  1977. }
  1978. var
  1979. regs:SysCallregs;
  1980. begin
  1981. path:=path+#0;
  1982. regs.reg2:=longint(@path[1]);
  1983. regs.reg3:=longint(args);
  1984. regs.reg4:=longint(ep);
  1985. SysCall(SysCall_nr_Execve,regs);
  1986. { This only gets set when the call fails, otherwise we don't get here ! }
  1987. Linuxerror:=errno;
  1988. end;
  1989. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  1990. {
  1991. Replaces the current program by the program specified in path,
  1992. arguments in args are passed to Execve.
  1993. environment specified in ep is passed on.
  1994. }
  1995. var
  1996. regs:SysCallregs;
  1997. begin
  1998. regs.reg2:=longint(path);
  1999. regs.reg3:=longint(args);
  2000. regs.reg4:=longint(ep);
  2001. SysCall(SysCall_nr_Execve,regs);
  2002. { This only gets set when the call fails, otherwise we don't get here ! }
  2003. Linuxerror:=errno;
  2004. end;
  2005. Procedure ExitProcess(val:longint);
  2006. var
  2007. regs : SysCallregs;
  2008. begin
  2009. regs.reg2:=val;
  2010. SysCall(SysCall_nr_exit,regs);
  2011. end;
  2012. Function WaitPid(Pid:longint;Status:pointer;Options:Longint):Longint;
  2013. {
  2014. Waits until a child with PID Pid exits, or returns if it is exited already.
  2015. Any resources used by the child are freed.
  2016. The exit status is reported in the adress referred to by Status. It should
  2017. be a longint.
  2018. }
  2019. var
  2020. regs : SysCallregs;
  2021. begin
  2022. regs.reg2:=pid;
  2023. regs.reg3:=longint(status);
  2024. regs.reg4:=options;
  2025. WaitPid:=SysCall(SysCall_nr_waitpid,regs);
  2026. LinuxError:=errno;
  2027. end;
  2028. Procedure GetTimeOfDay(var tv:timeval);
  2029. {
  2030. Get the number of seconds since 00:00, January 1 1970, GMT
  2031. the time NOT corrected any way
  2032. }
  2033. var
  2034. regs : SysCallregs;
  2035. begin
  2036. regs.reg2:=longint(@tv);
  2037. regs.reg3:=0;
  2038. SysCall(SysCall_nr_gettimeofday,regs);
  2039. LinuxError:=Errno;
  2040. end;
  2041. Function GetPriority(Which,Who:Integer):integer;
  2042. {
  2043. Get Priority of process, process group, or user.
  2044. Which : selects what kind of priority is used.
  2045. can be one of the following predefined Constants :
  2046. Prio_User.
  2047. Prio_PGrp.
  2048. Prio_Process.
  2049. Who : depending on which, this is , respectively :
  2050. Uid
  2051. Pid
  2052. Process Group id
  2053. Errors are reported in linuxerror _only_. (priority can be negative)
  2054. }
  2055. var
  2056. sr : Syscallregs;
  2057. begin
  2058. errno:=0;
  2059. if (which<prio_process) or (which>prio_user) then
  2060. begin
  2061. { We can save an interrupt here }
  2062. getpriority:=0;
  2063. linuxerror:=Sys_einval;
  2064. end
  2065. else
  2066. begin
  2067. sr.reg2:=which;
  2068. sr.reg3:=who;
  2069. getpriority:=SysCall(Syscall_nr_getpriority,sr);
  2070. linuxerror:=errno;
  2071. end;
  2072. end;
  2073. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  2074. {
  2075. Set Priority of process, process group, or user.
  2076. Which : selects what kind of priority is used.
  2077. can be one of the following predefined Constants :
  2078. Prio_User.
  2079. Prio_PGrp.
  2080. Prio_Process.
  2081. Who : depending on value of which, this is, respectively :
  2082. Uid
  2083. Pid
  2084. Process Group id
  2085. what : A number between -20 and 20. -20 is most favorable, 20 least.
  2086. 0 is the default.
  2087. }
  2088. var
  2089. sr : Syscallregs;
  2090. begin
  2091. errno:=0;
  2092. if ((which<prio_process) or (which>prio_user)) or ((what<-20) or (what>20)) then
  2093. linuxerror:=Sys_einval { We can save an interrupt here }
  2094. else
  2095. begin
  2096. sr.reg2:=which;
  2097. sr.reg3:=who;
  2098. sr.reg4:=what;
  2099. SysCall(Syscall_nr_setpriority,sr);
  2100. linuxerror:=errno;
  2101. end;
  2102. end;
  2103. Procedure Nice(N:integer);
  2104. {
  2105. Set process priority. A positive N means a lower priority.
  2106. A negative N decreases priority.
  2107. }
  2108. var
  2109. sr : Syscallregs;
  2110. begin
  2111. sr.reg2:=n;
  2112. SysCall(Syscall_nr_nice,sr);
  2113. linuxerror:=errno;
  2114. end;
  2115. Function GetPid:LongInt;
  2116. {
  2117. Get Process ID.
  2118. }
  2119. var
  2120. regs : SysCallregs;
  2121. begin
  2122. GetPid:=SysCall(SysCall_nr_getpid,regs);
  2123. linuxerror:=errno;
  2124. end;
  2125. Function GetPPid:LongInt;
  2126. {
  2127. Get Process ID of parent process.
  2128. }
  2129. var
  2130. regs : SysCallregs;
  2131. begin
  2132. GetPpid:=SysCall(SysCall_nr_getppid,regs);
  2133. linuxerror:=errno;
  2134. end;
  2135. Function GetUid:Longint;
  2136. {
  2137. Get User ID.
  2138. }
  2139. var
  2140. regs : SysCallregs;
  2141. begin
  2142. GetUid:=SysCall(SysCall_nr_getuid,regs);
  2143. Linuxerror:=errno;
  2144. end;
  2145. Function GetEUid:Longint;
  2146. {
  2147. Get _effective_ User ID.
  2148. }
  2149. var
  2150. regs : SysCallregs;
  2151. begin
  2152. GetEuid:=SysCall(SysCall_nr_geteuid,regs);
  2153. Linuxerror:=errno;
  2154. end;
  2155. Function GetGid:Longint;
  2156. {
  2157. Get Group ID.
  2158. }
  2159. var
  2160. regs : SysCallregs;
  2161. begin
  2162. Getgid:=SysCall(SysCall_nr_getgid,regs);
  2163. Linuxerror:=errno;
  2164. end;
  2165. Function GetEGid:Longint;
  2166. {
  2167. Get _effective_ Group ID.
  2168. }
  2169. var
  2170. regs : SysCallregs;
  2171. begin
  2172. GetEgid:=SysCall(SysCall_nr_getegid,regs);
  2173. Linuxerror:=errno;
  2174. end;
  2175. Function GetTimeOfDay: longint;
  2176. {
  2177. Get the number of seconds since 00:00, January 1 1970, GMT
  2178. the time NOT corrected any way
  2179. }
  2180. var
  2181. regs : SysCallregs;
  2182. tv : timeval;
  2183. begin
  2184. regs.reg2:=longint(@tv);
  2185. regs.reg3:=0;
  2186. SysCall(SysCall_nr_gettimeofday,regs);
  2187. LinuxError:=Errno;
  2188. GetTimeOfDay:=tv.sec;
  2189. end;
  2190. Function fdTruncate(fd,size:longint):boolean;
  2191. var
  2192. Regs : SysCallRegs;
  2193. begin
  2194. Regs.reg2:=fd;
  2195. Regs.reg3:=size;
  2196. fdTruncate:=(SysCall(Syscall_nr_ftruncate,regs)=0);
  2197. LinuxError:=Errno;
  2198. end;
  2199. Function fdFlush (fd : Longint) : Boolean;
  2200. var
  2201. SR: SysCallRegs;
  2202. begin
  2203. SR.reg2 := fd;
  2204. fdFlush := (SysCall(syscall_nr_fsync, SR)=0);
  2205. LinuxError:=Errno;
  2206. end;
  2207. Function Fcntl(Fd:longint;Cmd:longint): longint;
  2208. {
  2209. Read or manipulate a file.(See also fcntl (2) )
  2210. Possible values for Cmd are :
  2211. F_GetFd,F_GetFl,F_GetOwn
  2212. Errors are reported in Linuxerror;
  2213. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  2214. }
  2215. var
  2216. sr : Syscallregs;
  2217. begin
  2218. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  2219. begin
  2220. sr.reg2:=Fd;
  2221. sr.reg3:=cmd;
  2222. Linuxerror:=SysCall(Syscall_nr_fcntl,sr);
  2223. if linuxerror=-1 then
  2224. begin
  2225. linuxerror:=errno;
  2226. fcntl:=0;
  2227. end
  2228. else
  2229. begin
  2230. fcntl:=linuxerror;
  2231. linuxerror:=0;
  2232. end;
  2233. end
  2234. else
  2235. begin
  2236. linuxerror:=Sys_einval;
  2237. Fcntl:=0;
  2238. end;
  2239. end;
  2240. Procedure Fcntl(Fd:longint;Cmd:LongInt;Arg:Longint);
  2241. {
  2242. Read or manipulate a file. (See also fcntl (2) )
  2243. Possible values for Cmd are :
  2244. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  2245. Errors are reported in Linuxerror;
  2246. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  2247. F_DupFD is not allowed, due to the structure of Files in Pascal.
  2248. }
  2249. var
  2250. sr : Syscallregs;
  2251. begin
  2252. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  2253. begin
  2254. sr.reg2:=Fd;
  2255. sr.reg3:=cmd;
  2256. sr.reg4:=arg;
  2257. SysCall(Syscall_nr_fcntl,sr);
  2258. linuxerror:=errno;
  2259. end
  2260. else
  2261. linuxerror:=Sys_einval;
  2262. end;
  2263. Function Chmod(path:pathstr;Newmode:longint):Boolean;
  2264. {
  2265. Changes the permissions of a file.
  2266. }
  2267. var
  2268. sr : Syscallregs;
  2269. begin
  2270. path:=path+#0;
  2271. sr.reg2:=longint(@(path[1]));
  2272. sr.reg3:=newmode;
  2273. Chmod:=(SysCall(Syscall_nr_chmod,sr)=0);
  2274. linuxerror:=errno;
  2275. end;
  2276. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  2277. {
  2278. Change the owner and group of a file.
  2279. A user can only change the group to a group of which he is a member.
  2280. The super-user can change uid and gid of any file.
  2281. }
  2282. var
  2283. sr : Syscallregs;
  2284. begin
  2285. path:=path+#0;
  2286. sr.reg2:=longint(@(path[1]));
  2287. sr.reg3:=newuid;
  2288. sr.reg4:=newgid;
  2289. ChOwn:=(Syscall(Syscall_nr_chown,sr)=0);
  2290. linuxerror:=errno;
  2291. end;
  2292. Function Utime(const path:pathstr;utim:utimebuf):boolean;
  2293. var
  2294. sr : Syscallregs;
  2295. buf : pathstr;
  2296. begin
  2297. buf:=path+#0;
  2298. sr.reg2:=longint(@(buf[1]));
  2299. sr.reg3:=longint(@utim);
  2300. Utime:=SysCall(Syscall_nr_utime,sr)=0;
  2301. linuxerror:=errno;
  2302. end;
  2303. Function Flock (fd,mode : longint) : boolean;
  2304. var
  2305. sr : Syscallregs;
  2306. begin
  2307. sr.reg2:=fd;
  2308. sr.reg3:=mode;
  2309. flock:=Syscall(Syscall_nr_flock,sr)=0;
  2310. LinuxError:=errno;
  2311. end;
  2312. Function Fstat(Fd:Longint;var Info:stat):Boolean;
  2313. {
  2314. Get all information on a file descriptor, and return it in info.
  2315. }
  2316. var
  2317. regs : SysCallregs;
  2318. begin
  2319. regs.reg2:=Fd;
  2320. regs.reg3:=longint(@Info);
  2321. FStat:=(SysCall(SysCall_nr_fstat,regs)=0);
  2322. LinuxError:=Errno;
  2323. end;
  2324. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  2325. {
  2326. Get all information on a link (the link itself), and return it in info.
  2327. }
  2328. var
  2329. regs : SysCallregs;
  2330. begin
  2331. FileName:=FileName+#0;
  2332. regs.reg2:=longint(@filename[1]);
  2333. regs.reg3:=longint(@Info);
  2334. LStat:=(SysCall(SysCall_nr_lstat,regs)=0);
  2335. LinuxError:=Errno;
  2336. end;
  2337. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  2338. {
  2339. Get all information on a fileSystem, and return it in Info.
  2340. Path is the name of a file/directory on the fileSystem you wish to
  2341. investigate.
  2342. }
  2343. var
  2344. regs : SysCallregs;
  2345. begin
  2346. path:=path+#0;
  2347. regs.reg2:=longint(@path[1]);
  2348. regs.reg3:=longint(@Info);
  2349. FSStat:=(SysCall(SysCall_nr_statfs,regs)=0);
  2350. LinuxError:=errno;
  2351. end;
  2352. Function FSStat(Fd:Longint;Var Info:statfs):Boolean;
  2353. {
  2354. Get all information on a fileSystem, and return it in Info.
  2355. Fd is the file descriptor of a file/directory on the fileSystem
  2356. you wish to investigate.
  2357. }
  2358. var
  2359. regs : SysCallregs;
  2360. begin
  2361. regs.reg2:=Fd;
  2362. regs.reg3:=longint(@Info);
  2363. FSStat:=(SysCall(SysCall_nr_fstatfs,regs)=0);
  2364. LinuxError:=errno;
  2365. end;
  2366. Function Link(OldPath,NewPath:pathstr):boolean;
  2367. {
  2368. Proceduces a hard link from new to old.
  2369. In effect, new will be the same file as old.
  2370. }
  2371. var
  2372. regs : SysCallregs;
  2373. begin
  2374. oldpath:=oldpath+#0;
  2375. newpath:=newpath+#0;
  2376. regs.reg2:=longint(@oldpath[1]);
  2377. regs.reg3:=longint(@newpath[1]);
  2378. Link:=SysCall(SysCall_nr_link,regs)=0;
  2379. linuxerror:=errno;
  2380. end;
  2381. Function Umask(Mask:Integer):integer;
  2382. {
  2383. Sets file creation mask to (Mask and 0777 (octal) ), and returns the
  2384. previous value.
  2385. }
  2386. var
  2387. sr : Syscallregs;
  2388. begin
  2389. sr.reg2:=mask;
  2390. Umask:=SysCall(Syscall_nr_umask,sr);
  2391. linuxerror:=0;
  2392. end;
  2393. Function Access(Path:Pathstr ;mode:integer):boolean;
  2394. {
  2395. Test users access rights on the specified file.
  2396. Mode is a mask xosisting of one or more of R_OK, W_OK, X_OK, F_OK.
  2397. R,W,X stand for read,write and Execute access, simultaneously.
  2398. F_OK checks whether the test would be allowed on the file.
  2399. i.e. It checks the search permissions in all directory components
  2400. of the path.
  2401. The test is done with the real user-ID, instead of the effective.
  2402. If access is denied, or an error occurred, false is returned.
  2403. If access is granted, true is returned.
  2404. Errors other than no access,are reported in linuxerror.
  2405. }
  2406. var
  2407. sr : Syscallregs;
  2408. begin
  2409. path:=path+#0;
  2410. sr.reg2:=longint(@(path[1]));
  2411. sr.reg3:=mode;
  2412. access:=(SysCall(Syscall_nr_access,sr)=0);
  2413. linuxerror:=errno;
  2414. end;
  2415. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  2416. {
  2417. Copies the filedescriptor oldfile to newfile
  2418. }
  2419. var
  2420. sr : Syscallregs;
  2421. begin
  2422. sr.reg2:=oldfile;
  2423. newfile:=Syscall(Syscall_nr_dup,sr);
  2424. linuxerror:=errno;
  2425. Dup:=(LinuxError=0);
  2426. end;
  2427. Function Dup2(oldfile,newfile:longint):Boolean;
  2428. {
  2429. Copies the filedescriptor oldfile to newfile
  2430. }
  2431. var
  2432. sr : Syscallregs;
  2433. begin
  2434. sr.reg2:=oldfile;
  2435. sr.reg3:=newfile;
  2436. SysCall(Syscall_nr_dup2,sr);
  2437. linuxerror:=errno;
  2438. Dup2:=(LinuxError=0);
  2439. end;
  2440. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  2441. {
  2442. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  2443. have changed.
  2444. }
  2445. Var
  2446. SelectArray : Array[1..5] of longint;
  2447. Sr : Syscallregs;
  2448. begin
  2449. SelectArray[1]:=n;
  2450. SelectArray[2]:=longint(Readfds);
  2451. Selectarray[3]:=longint(Writefds);
  2452. selectarray[4]:=longint(exceptfds);
  2453. Selectarray[5]:=longint(TimeOut);
  2454. sr.reg2:=longint(@selectarray);
  2455. Select:=SysCall(Syscall_nr_select,sr);
  2456. LinuxError:=Errno;
  2457. end;
  2458. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  2459. {
  2460. Sets up a pair of file variables, which act as a pipe. The first one can
  2461. be read from, the second one can be written to.
  2462. If the operation was unsuccesful, linuxerror is set.
  2463. }
  2464. var
  2465. pip : tpipe;
  2466. regs : SysCallregs;
  2467. begin
  2468. regs.reg2:=longint(@pip);
  2469. SysCall(SysCall_nr_pipe,regs);
  2470. pipe_in:=pip[1];
  2471. pipe_out:=pip[2];
  2472. linuxerror:=errno;
  2473. AssignPipe:=(LinuxError=0);
  2474. end;
  2475. Function PClose(Var F:text) :longint;
  2476. var
  2477. sr : syscallregs;
  2478. pl : ^longint;
  2479. res : longint;
  2480. begin
  2481. sr.reg2:=Textrec(F).Handle;
  2482. SysCall (syscall_nr_close,sr);
  2483. { closed our side, Now wait for the other - this appears to be needed ?? }
  2484. pl:=@(textrec(f).userdata[2]);
  2485. waitpid(pl^,@res,0);
  2486. pclose:=res shr 8;
  2487. end;
  2488. Function PClose(Var F:file) : longint;
  2489. var
  2490. sr : syscallregs;
  2491. pl : ^longint;
  2492. res : longint;
  2493. begin
  2494. sr.reg2:=FileRec(F).Handle;
  2495. SysCall (Syscall_nr_close,sr);
  2496. { closed our side, Now wait for the other - this appears to be needed ?? }
  2497. pl:=@(filerec(f).userdata[2]);
  2498. waitpid(pl^,@res,0);
  2499. pclose:=res shr 8;
  2500. end;
  2501. Function Sysinfo(var Info:TSysinfo):Boolean;
  2502. {
  2503. Get system info
  2504. }
  2505. var
  2506. regs : SysCallregs;
  2507. Begin
  2508. regs.reg2:=longint(@info);
  2509. Sysinfo:=SysCall(SysCall_nr_Sysinfo,regs)=0;
  2510. End;
  2511. Function mkFifo(pathname:string;mode:longint):boolean;
  2512. var
  2513. regs : SysCallRegs;
  2514. begin
  2515. pathname:=pathname+#0;
  2516. regs.reg2:=longint(@pathname[1]);
  2517. regs.reg3:=mode or STAT_IFIFO;
  2518. regs.reg4:=0;
  2519. mkFifo:=(SysCall(syscall_nr_mknod,regs)=0);
  2520. end;
  2521. Function Uname(var unamerec:utsname):Boolean;
  2522. {
  2523. Get machine's names
  2524. }
  2525. var
  2526. regs : SysCallregs;
  2527. Begin
  2528. regs.reg2:=longint(@unamerec);
  2529. Uname:=SysCall(SysCall_nr_uname,regs)=0;
  2530. LinuxError:=Errno;
  2531. End;
  2532. Function Kill(Pid:longint;Sig:longint):integer;
  2533. {
  2534. Send signal 'sig' to a process, or a group of processes.
  2535. If Pid > 0 then the signal is sent to pid
  2536. pid=-1 to all processes except process 1
  2537. pid < -1 to process group -pid
  2538. Return value is zero, except for case three, where the return value
  2539. is the number of processes to which the signal was sent.
  2540. }
  2541. var
  2542. regs : Syscallregs;
  2543. begin
  2544. regs.reg2:=Pid;
  2545. regs.reg3:=Sig;
  2546. kill:=SysCall(Syscall_nr_kill,regs);
  2547. if kill<0 then
  2548. Kill:=0;
  2549. linuxerror:=errno;
  2550. end;
  2551. Procedure SigProcMask(How:longint;SSet,OldSSet:PSigSet);
  2552. {
  2553. Change the list of currently blocked signals.
  2554. How determines which signals will be blocked :
  2555. SigBlock : Add SSet to the current list of blocked signals
  2556. SigUnBlock : Remove the signals in SSet from the list of blocked signals.
  2557. SigSetMask : Set the list of blocked signals to SSet
  2558. if OldSSet is non-null, the old set will be saved there.
  2559. }
  2560. Var
  2561. sr : SyscallRegs;
  2562. begin
  2563. sr.reg2:=how;
  2564. sr.reg3:=longint(SSet);
  2565. sr.reg4:=longint(OldSSet);
  2566. SysCall(Syscall_nr_sigprocmask,sr);
  2567. linuxerror:=errno;
  2568. end;
  2569. Function SigPending:SigSet;
  2570. {
  2571. Allows examination of pending signals. The signal mask of pending
  2572. signals is set in SSet
  2573. }
  2574. Var
  2575. sr : SyscallRegs;
  2576. dummy : Sigset;
  2577. begin
  2578. sr.reg2:=longint(@dummy);
  2579. SysCall(Syscall_nr_sigpending,sr);
  2580. linuxerror:=errno;
  2581. Sigpending:=dummy;
  2582. end;
  2583. Procedure SigSuspend(Mask:Sigset);
  2584. {
  2585. Set the signal mask with Mask, and suspend the program until a signal
  2586. is received.
  2587. }
  2588. Var
  2589. sr : SyscallRegs;
  2590. begin
  2591. sr.reg2:=mask;
  2592. SysCall(Syscall_nr_sigsuspend,sr);
  2593. linuxerror:=errno;
  2594. end;
  2595. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  2596. {
  2597. Install a new handler for signal Signum.
  2598. The old signal handler is returned.
  2599. This call does, in fact, the same as SigAction.
  2600. }
  2601. var
  2602. sr : Syscallregs;
  2603. begin
  2604. sr.reg2:=signum;
  2605. sr.reg3:=longint(handler);
  2606. Linuxerror:=SysCall(Syscall_nr_signal,sr);
  2607. If linuxerror=Sig_Err then
  2608. begin
  2609. Signal:=nil;
  2610. Linuxerror:=errno;
  2611. end
  2612. else
  2613. begin
  2614. Signal:=signalhandler(Linuxerror);
  2615. linuxerror:=0;
  2616. end;
  2617. end;
  2618. Function Alarm(Sec : Longint) : longint;
  2619. Var Sr : Syscallregs;
  2620. begin
  2621. sr.reg2:=Sec;
  2622. Alarm:=Syscall(syscall_nr_alarm,sr);
  2623. end;
  2624. Procedure Pause;
  2625. Var Sr : Syscallregs;
  2626. begin
  2627. syscall(syscall_nr_pause,sr);
  2628. end;
  2629. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  2630. var Sr : Syscallregs;
  2631. begin
  2632. sr.reg2:=longint(@req);
  2633. sr.reg3:=longint(@rem);
  2634. NanoSleep:=Syscall(syscall_nr_nanosleep,sr);
  2635. LinuxError:=Errno;
  2636. end;
  2637. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  2638. {
  2639. Interface to Unix ioctl call.
  2640. Performs various operations on the filedescriptor Handle.
  2641. Ndx describes the operation to perform.
  2642. Data points to data needed for the Ndx function. The structure of this
  2643. data is function-dependent.
  2644. }
  2645. var
  2646. sr: SysCallRegs;
  2647. begin
  2648. sr.reg2:=Handle;
  2649. sr.reg3:=Ndx;
  2650. sr.reg4:=Longint(Data);
  2651. IOCtl:=(SysCall(Syscall_nr_ioctl,sr)=0);
  2652. LinuxError:=Errno;
  2653. end;
  2654. function MMap(const m:tmmapargs):longint;
  2655. Var
  2656. Sr : Syscallregs;
  2657. begin
  2658. Sr.reg2:=longint(@m);
  2659. MMap:=syscall(syscall_nr_mmap,sr);
  2660. LinuxError:=Errno;
  2661. end;
  2662. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  2663. Var
  2664. Sr : Syscallregs;
  2665. begin
  2666. Sr.reg2:=longint(P);
  2667. sr.reg3:=Size;
  2668. MUnMap:=syscall(syscall_nr_munmap,sr)=0;
  2669. LinuxError:=Errno;
  2670. end;
  2671. {--------------------------------
  2672. Port IO functions
  2673. --------------------------------}
  2674. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  2675. {
  2676. Set permissions on NUM ports starting with port FROM to VALUE
  2677. this works ONLY as root.
  2678. }
  2679. Var
  2680. Sr : Syscallregs;
  2681. begin
  2682. Sr.Reg2:=From;
  2683. Sr.Reg3:=Num;
  2684. Sr.Reg4:=Value;
  2685. IOPerm:=Syscall(Syscall_nr_ioperm,sr)=0;
  2686. LinuxError:=Errno;
  2687. end;
  2688. Function IoPL(Level : longint) : Boolean;
  2689. Var
  2690. Sr : Syscallregs;
  2691. begin
  2692. Sr.Reg2:=Level;
  2693. IOPL:=Syscall(Syscall_nr_iopl,sr)=0;
  2694. LinuxError:=Errno;
  2695. end;
  2696. {******************************************************************************
  2697. Process related calls
  2698. ******************************************************************************}
  2699. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  2700. Function WaitProcess(Pid:longint):Longint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  2701. var r,s : LongInt;
  2702. begin
  2703. repeat
  2704. s:=$7F00;
  2705. r:=WaitPid(Pid,@s,0);
  2706. until (r<>-1) or (LinuxError<>Sys_EINTR);
  2707. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  2708. WaitProcess:=-1 // return -1 to indicate an error
  2709. else
  2710. begin
  2711. {$ifdef solaris}
  2712. if (s and $FF)=0 then // Only this is a valid returncode
  2713. {$else solaris}
  2714. { the following is at least correct for Linux and Darwin (JM) }
  2715. if (s and $7F)=0 then
  2716. {$endif solaris}
  2717. WaitProcess:=s shr 8
  2718. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  2719. WaitProcess:=-s // normal case
  2720. else
  2721. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  2722. end;
  2723. end;
  2724. function InternalCreateShellArgV(cmd:pChar; len:longint):ppchar;
  2725. {
  2726. Create an argv which executes a command in a shell using /bin/sh -c
  2727. }
  2728. const Shell = '/bin/sh'#0'-c'#0;
  2729. var
  2730. pp,p : ppchar;
  2731. // temp : string; !! Never pass a local var back!!
  2732. begin
  2733. getmem(pp,4*4);
  2734. p:=pp;
  2735. p^:=@Shell[1];
  2736. inc(p);
  2737. p^:=@Shell[9];
  2738. inc(p);
  2739. getmem(p^,len+1);
  2740. move(cmd^,p^^,len);
  2741. pchar(p^)[len]:=#0;
  2742. inc(p);
  2743. p^:=Nil;
  2744. InternalCreateShellArgV:=pp;
  2745. end;
  2746. function CreateShellArgV(const prog:string):ppchar;
  2747. begin
  2748. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  2749. end;
  2750. function CreateShellArgV(const prog:Ansistring):ppchar;
  2751. {
  2752. Create an argv which executes a command in a shell using /bin/sh -c
  2753. using a AnsiString;
  2754. }
  2755. begin
  2756. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  2757. end;
  2758. procedure FreeShellArgV(p:ppchar);
  2759. begin
  2760. if (p<>nil) then begin
  2761. freemem(p[2]);
  2762. freemem(p);
  2763. end;
  2764. end;
  2765. Procedure Execve(Path: AnsiString;args:ppchar;ep:ppchar);
  2766. {
  2767. overloaded ansistring version.
  2768. }
  2769. begin
  2770. ExecVE(PChar(Path),args,ep);
  2771. end;
  2772. Procedure Execv(const path: AnsiString;args:ppchar);
  2773. {
  2774. Overloaded ansistring version.
  2775. }
  2776. begin
  2777. ExecVe(Path,Args,envp)
  2778. end;
  2779. Procedure Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar);
  2780. {
  2781. Overloaded ansistring version
  2782. }
  2783. var
  2784. thepath : Ansistring;
  2785. begin
  2786. if path[1]<>'/' then
  2787. begin
  2788. Thepath:=strpas(getenv('PATH'));
  2789. if thepath='' then
  2790. thepath:='.';
  2791. Path:=FSearch(path,thepath)
  2792. end
  2793. else
  2794. Path:='';
  2795. if Path='' then
  2796. linuxerror:=Sys_enoent
  2797. else
  2798. Execve(Path,args,ep);{On error linuxerror will get set there}
  2799. end;
  2800. Procedure Execv(const path:pathstr;args:ppchar);
  2801. {
  2802. Replaces the current program by the program specified in path,
  2803. arguments in args are passed to Execve.
  2804. the current environment is passed on.
  2805. }
  2806. begin
  2807. Execve(path,args,envp); {On error linuxerror will get set there}
  2808. end;
  2809. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  2810. {
  2811. This does the same as Execve, only it searches the PATH environment
  2812. for the place of the Executable, except when Path starts with a slash.
  2813. if the PATH environment variable is unavailable, the path is set to '.'
  2814. }
  2815. var
  2816. thepath : string;
  2817. begin
  2818. if path[1]<>'/' then
  2819. begin
  2820. Thepath:=strpas(getenv('PATH'));
  2821. if thepath='' then
  2822. thepath:='.';
  2823. Path:=FSearch(path,thepath)
  2824. end
  2825. else
  2826. Path:='';
  2827. if Path='' then
  2828. linuxerror:=Sys_enoent
  2829. else
  2830. Execve(Path,args,ep);{On error linuxerror will get set there}
  2831. end;
  2832. Procedure Execle(Todo:string;Ep:ppchar);
  2833. {
  2834. This procedure takes the string 'Todo', parses it for command and
  2835. command options, and Executes the command with the given options.
  2836. The string 'Todo' shoud be of the form 'command options', options
  2837. separated by commas.
  2838. the PATH environment is not searched for 'command'.
  2839. The specified environment(in 'ep') is passed on to command
  2840. }
  2841. var
  2842. p : ppchar;
  2843. begin
  2844. p:=StringToPPChar(ToDo);
  2845. if (p=nil) or (p^=nil) then
  2846. exit;
  2847. ExecVE(p^,p,EP);
  2848. end;
  2849. Procedure Execle(Todo:AnsiString;Ep:ppchar);
  2850. {
  2851. This procedure takes the string 'Todo', parses it for command and
  2852. command options, and Executes the command with the given options.
  2853. The string 'Todo' shoud be of the form 'command options', options
  2854. separated by commas.
  2855. the PATH environment is not searched for 'command'.
  2856. The specified environment(in 'ep') is passed on to command
  2857. }
  2858. var
  2859. p : ppchar;
  2860. begin
  2861. p:=StringToPPChar(ToDo);
  2862. if (p=nil) or (p^=nil) then
  2863. exit;
  2864. ExecVE(p^,p,EP);
  2865. end;
  2866. Procedure Execl(const Todo:string);
  2867. {
  2868. This procedure takes the string 'Todo', parses it for command and
  2869. command options, and Executes the command with the given options.
  2870. The string 'Todo' shoud be of the form 'command options', options
  2871. separated by commas.
  2872. the PATH environment is not searched for 'command'.
  2873. The current environment is passed on to command
  2874. }
  2875. begin
  2876. ExecLE(ToDo,EnvP);
  2877. end;
  2878. Procedure Execl(const Todo:Ansistring);
  2879. {
  2880. Overloaded AnsiString Version of ExecL.
  2881. }
  2882. begin
  2883. ExecLE(ToDo,EnvP);
  2884. end;
  2885. Procedure Execlp(Todo:string;Ep:ppchar);
  2886. {
  2887. This procedure takes the string 'Todo', parses it for command and
  2888. command options, and Executes the command with the given options.
  2889. The string 'Todo' shoud be of the form 'command options', options
  2890. separated by commas.
  2891. the PATH environment is searched for 'command'.
  2892. The specified environment (in 'ep') is passed on to command
  2893. }
  2894. var
  2895. p : ppchar;
  2896. begin
  2897. p:=StringToPPchar(todo);
  2898. if (p=nil) or (p^=nil) then
  2899. exit;
  2900. ExecVP(StrPas(p^),p,EP);
  2901. end;
  2902. Procedure Execlp(Todo: Ansistring;Ep:ppchar);
  2903. {
  2904. Overloaded ansistring version.
  2905. }
  2906. var
  2907. p : ppchar;
  2908. begin
  2909. p:=StringToPPchar(todo);
  2910. if (p=nil) or (p^=nil) then
  2911. exit;
  2912. ExecVP(StrPas(p^),p,EP);
  2913. end;
  2914. Function Shell(const Command:String):Longint;
  2915. {
  2916. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  2917. The current environment is passed to the shell.
  2918. It waits for the shell to exit, and returns its exit status.
  2919. If the Exec call failed exit status 127 is reported.
  2920. }
  2921. { Changed the structure:
  2922. - the previous version returns an undefinied value if fork fails
  2923. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  2924. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  2925. - ShellArgs are now released
  2926. - The Old CreateShellArg gives back pointers to a local var
  2927. }
  2928. var
  2929. p : ppchar;
  2930. pid,r,s : longint;
  2931. begin
  2932. p:=CreateShellArgv(command);
  2933. pid:=fork;
  2934. if pid=0 then // We are in the Child
  2935. begin
  2936. {This is the child.}
  2937. Execve(p^,p,envp);
  2938. ExitProcess(127); // was Exit(127)
  2939. end
  2940. else if (pid<>-1) then // Successfull started
  2941. begin
  2942. repeat
  2943. s:=$7F00;
  2944. r:=WaitPid(Pid,@s,0);
  2945. until (r<>-1) or (LinuxError<>Sys_EINTR);
  2946. if (r=-1) or (r=0) then
  2947. Shell:=-1
  2948. else
  2949. Shell:=s;
  2950. end
  2951. else // no success
  2952. Shell:=-1; // indicate an error
  2953. FreeShellArgV(p);
  2954. end;
  2955. Function Shell(const Command:AnsiString):Longint;
  2956. {
  2957. AnsiString version of Shell
  2958. }
  2959. var
  2960. p : ppchar;
  2961. pid : longint;
  2962. begin { Changes as above }
  2963. p:=CreateShellArgv(command);
  2964. pid:=fork;
  2965. if pid=0 then // We are in the Child
  2966. begin
  2967. Execve(p^,p,envp);
  2968. ExitProcess(127); // was exit(127)!! We must exit the Process, not the function
  2969. end
  2970. else if (pid<>-1) then // Successfull started
  2971. Shell:=WaitProcess(pid) {Linuxerror is set there}
  2972. else // no success
  2973. Shell:=-1;
  2974. FreeShellArgV(p);
  2975. end;
  2976. function WEXITSTATUS(Status: Integer): Integer;
  2977. begin
  2978. WEXITSTATUS:=(Status and $FF00) shr 8;
  2979. end;
  2980. function WTERMSIG(Status: Integer): Integer;
  2981. begin
  2982. WTERMSIG:=(Status and $7F);
  2983. end;
  2984. function WSTOPSIG(Status: Integer): Integer;
  2985. begin
  2986. WSTOPSIG:=WEXITSTATUS(Status);
  2987. end;
  2988. Function WIFEXITED(Status: Integer): Boolean;
  2989. begin
  2990. WIFEXITED:=(WTERMSIG(Status)=0);
  2991. end;
  2992. Function WIFSTOPPED(Status: Integer): Boolean;
  2993. begin
  2994. WIFSTOPPED:=((Status and $FF)=$7F);
  2995. end;
  2996. Function WIFSIGNALED(Status: Integer): Boolean;
  2997. begin
  2998. WIFSIGNALED:=(not WIFSTOPPED(Status)) and
  2999. (not WIFEXITED(Status));
  3000. end;
  3001. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  3002. begin
  3003. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  3004. end;
  3005. Function W_STOPCODE(Signal: Integer): Integer;
  3006. begin
  3007. W_STOPCODE:=(Signal shl 8) or $7F;
  3008. end;
  3009. {******************************************************************************
  3010. Date and Time related calls
  3011. ******************************************************************************}
  3012. Const
  3013. {Date Translation}
  3014. C1970=2440588;
  3015. D0 = 1461;
  3016. D1 = 146097;
  3017. D2 =1721119;
  3018. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  3019. Var
  3020. Century,XYear: LongInt;
  3021. Begin
  3022. If Month<=2 Then
  3023. Begin
  3024. Dec(Year);
  3025. Inc(Month,12);
  3026. End;
  3027. Dec(Month,3);
  3028. Century:=(longint(Year Div 100)*D1) shr 2;
  3029. XYear:=(longint(Year Mod 100)*D0) shr 2;
  3030. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  3031. End;
  3032. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  3033. Var
  3034. YYear,XYear,Temp,TempMonth : LongInt;
  3035. Begin
  3036. Temp:=((JulianDN-D2) shl 2)-1;
  3037. JulianDN:=Temp Div D1;
  3038. XYear:=(Temp Mod D1) or 3;
  3039. YYear:=(XYear Div D0);
  3040. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  3041. Day:=((Temp Mod 153)+5) Div 5;
  3042. TempMonth:=Temp Div 153;
  3043. If TempMonth>=10 Then
  3044. Begin
  3045. inc(YYear);
  3046. dec(TempMonth,12);
  3047. End;
  3048. inc(TempMonth,3);
  3049. Month := TempMonth;
  3050. Year:=YYear+(JulianDN*100);
  3051. end;
  3052. Function GetEpochTime: longint;
  3053. {
  3054. Get the number of seconds since 00:00, January 1 1970, GMT
  3055. the time NOT corrected any way
  3056. }
  3057. begin
  3058. GetEpochTime:=GetTimeOfDay;
  3059. end;
  3060. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  3061. {
  3062. Transforms Epoch time into local time (hour, minute,seconds)
  3063. }
  3064. Var
  3065. DateNum: LongInt;
  3066. Begin
  3067. inc(Epoch,TZSeconds);
  3068. Datenum:=(Epoch Div 86400) + c1970;
  3069. JulianToGregorian(DateNum,Year,Month,day);
  3070. Epoch:=Abs(Epoch Mod 86400);
  3071. Hour:=Epoch Div 3600;
  3072. Epoch:=Epoch Mod 3600;
  3073. Minute:=Epoch Div 60;
  3074. Second:=Epoch Mod 60;
  3075. End;
  3076. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  3077. {
  3078. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  3079. (seconds since 00:00, january 1 1970, corrected for local time zone)
  3080. }
  3081. Begin
  3082. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  3083. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  3084. End;
  3085. procedure GetTime(var hour,min,sec,msec,usec:word);
  3086. {
  3087. Gets the current time, adjusted to local time
  3088. }
  3089. var
  3090. year,day,month:Word;
  3091. t : timeval;
  3092. begin
  3093. gettimeofday(t);
  3094. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  3095. msec:=t.usec div 1000;
  3096. usec:=t.usec mod 1000;
  3097. end;
  3098. procedure GetTime(var hour,min,sec,sec100:word);
  3099. {
  3100. Gets the current time, adjusted to local time
  3101. }
  3102. var
  3103. usec : word;
  3104. begin
  3105. gettime(hour,min,sec,sec100,usec);
  3106. sec100:=sec100 div 10;
  3107. end;
  3108. Procedure GetTime(Var Hour,Min,Sec:Word);
  3109. {
  3110. Gets the current time, adjusted to local time
  3111. }
  3112. var
  3113. msec,usec : Word;
  3114. Begin
  3115. gettime(hour,min,sec,msec,usec);
  3116. End;
  3117. Procedure GetDate(Var Year,Month,Day:Word);
  3118. {
  3119. Gets the current date, adjusted to local time
  3120. }
  3121. var
  3122. hour,minute,second : word;
  3123. Begin
  3124. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  3125. End;
  3126. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  3127. {
  3128. Gets the current date, adjusted to local time
  3129. }
  3130. Begin
  3131. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  3132. End;
  3133. {$ifndef BSD} {Fix for 1.0.x starting compiler only}
  3134. {$ifdef linux}
  3135. Function stime (t : longint) : Boolean;
  3136. var
  3137. sr : Syscallregs;
  3138. begin
  3139. sr.reg2:=longint(@t);
  3140. SysCall(Syscall_nr_stime,sr);
  3141. linuxerror:=errno;
  3142. stime:=linuxerror=0;
  3143. end;
  3144. {$endif}
  3145. {$endif}
  3146. {$ifdef BSD}
  3147. Function stime (t : longint) : Boolean;
  3148. begin
  3149. stime:=false;
  3150. end;
  3151. {$endif}
  3152. Function SetTime(Hour,Min,Sec:word) : boolean;
  3153. var
  3154. Year, Month, Day : Word;
  3155. begin
  3156. GetDate (Year, Month, Day);
  3157. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  3158. end;
  3159. Function SetDate(Year,Month,Day:Word) : boolean;
  3160. var
  3161. Hour, Minute, Second, Sec100 : Word;
  3162. begin
  3163. GetTime ( Hour, Minute, Second, Sec100 );
  3164. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  3165. end;
  3166. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  3167. begin
  3168. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  3169. end;
  3170. { Include timezone handling routines which use /usr/share/timezone info }
  3171. type
  3172. plongint=^longint;
  3173. pbyte=^byte;
  3174. ttzhead=packed record
  3175. tzh_reserved : array[0..19] of byte;
  3176. tzh_ttisgmtcnt,
  3177. tzh_ttisstdcnt,
  3178. tzh_leapcnt,
  3179. tzh_timecnt,
  3180. tzh_typecnt,
  3181. tzh_charcnt : longint;
  3182. end;
  3183. pttinfo=^tttinfo;
  3184. tttinfo=packed record
  3185. offset : longint;
  3186. isdst : boolean;
  3187. idx : byte;
  3188. isstd : byte;
  3189. isgmt : byte;
  3190. end;
  3191. pleap=^tleap;
  3192. tleap=record
  3193. transition : longint;
  3194. change : longint;
  3195. end;
  3196. var
  3197. num_transitions,
  3198. num_leaps,
  3199. num_types : longint;
  3200. transitions : plongint;
  3201. type_idxs : pbyte;
  3202. types : pttinfo;
  3203. zone_names : pchar;
  3204. leaps : pleap;
  3205. function find_transition(timer:longint):pttinfo;
  3206. var
  3207. i : longint;
  3208. begin
  3209. if (num_transitions=0) or (timer<transitions[0]) then
  3210. begin
  3211. i:=0;
  3212. while (i<num_types) and (types[i].isdst) do
  3213. inc(i);
  3214. if (i=num_types) then
  3215. i:=0;
  3216. end
  3217. else
  3218. begin
  3219. for i:=1 to num_transitions do
  3220. if (timer<transitions[i]) then
  3221. break;
  3222. i:=type_idxs[i-1];
  3223. end;
  3224. find_transition:=@types[i];
  3225. end;
  3226. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  3227. var
  3228. info : pttinfo;
  3229. i : longint;
  3230. begin
  3231. { reset }
  3232. TZDaylight:=false;
  3233. TZSeconds:=0;
  3234. TZName[false]:=nil;
  3235. TZName[true]:=nil;
  3236. leap_correct:=0;
  3237. leap_hit:=0;
  3238. { get info }
  3239. info:=find_transition(timer);
  3240. if not assigned(info) then
  3241. exit;
  3242. TZDaylight:=info^.isdst;
  3243. TZSeconds:=info^.offset;
  3244. i:=0;
  3245. while (i<num_types) do
  3246. begin
  3247. tzname[types[i].isdst]:=@zone_names[types[i].idx];
  3248. inc(i);
  3249. end;
  3250. tzname[info^.isdst]:=@zone_names[info^.idx];
  3251. i:=num_leaps;
  3252. repeat
  3253. if i=0 then
  3254. exit;
  3255. dec(i);
  3256. until (timer>leaps[i].transition);
  3257. leap_correct:=leaps[i].change;
  3258. if (timer=leaps[i].transition) and
  3259. (((i=0) and (leaps[i].change>0)) or
  3260. (leaps[i].change>leaps[i-1].change)) then
  3261. begin
  3262. leap_hit:=1;
  3263. while (i>0) and
  3264. (leaps[i].transition=leaps[i-1].transition+1) and
  3265. (leaps[i].change=leaps[i-1].change+1) do
  3266. begin
  3267. inc(leap_hit);
  3268. dec(i);
  3269. end;
  3270. end;
  3271. end;
  3272. procedure GetLocalTimezone(timer:longint);
  3273. var
  3274. lc,lh : longint;
  3275. begin
  3276. GetLocalTimezone(timer,lc,lh);
  3277. end;
  3278. procedure ReadTimezoneFile(fn:string);
  3279. procedure decode(var l:longint);
  3280. var
  3281. k : longint;
  3282. p : pbyte;
  3283. begin
  3284. p:=pbyte(@l);
  3285. if (p[0] and (1 shl 7))<>0 then
  3286. k:=not 0
  3287. else
  3288. k:=0;
  3289. k:=(k shl 8) or p[0];
  3290. k:=(k shl 8) or p[1];
  3291. k:=(k shl 8) or p[2];
  3292. k:=(k shl 8) or p[3];
  3293. l:=k;
  3294. end;
  3295. var
  3296. f : longint;
  3297. tzdir : string;
  3298. tzhead : ttzhead;
  3299. i : longint;
  3300. chars : longint;
  3301. buf : pbyte;
  3302. begin
  3303. if fn='' then
  3304. fn:='localtime';
  3305. if fn[1]<>'/' then
  3306. begin
  3307. tzdir:=getenv('TZDIR');
  3308. if tzdir='' then
  3309. tzdir:='/usr/share/zoneinfo';
  3310. if tzdir[length(tzdir)]<>'/' then
  3311. tzdir:=tzdir+'/';
  3312. fn:=tzdir+fn;
  3313. end;
  3314. f:=fdopen(fn,Open_RdOnly);
  3315. if f<0 then
  3316. exit;
  3317. i:=fdread(f,tzhead,sizeof(tzhead));
  3318. if i<>sizeof(tzhead) then
  3319. exit;
  3320. decode(tzhead.tzh_timecnt);
  3321. decode(tzhead.tzh_typecnt);
  3322. decode(tzhead.tzh_charcnt);
  3323. decode(tzhead.tzh_leapcnt);
  3324. decode(tzhead.tzh_ttisstdcnt);
  3325. decode(tzhead.tzh_ttisgmtcnt);
  3326. num_transitions:=tzhead.tzh_timecnt;
  3327. num_types:=tzhead.tzh_typecnt;
  3328. chars:=tzhead.tzh_charcnt;
  3329. reallocmem(transitions,num_transitions*sizeof(longint));
  3330. reallocmem(type_idxs,num_transitions);
  3331. reallocmem(types,num_types*sizeof(tttinfo));
  3332. reallocmem(zone_names,chars);
  3333. reallocmem(leaps,num_leaps*sizeof(tleap));
  3334. fdread(f,transitions^,num_transitions*4);
  3335. fdread(f,type_idxs^,num_transitions);
  3336. for i:=0 to num_transitions-1 do
  3337. decode(transitions[i]);
  3338. for i:=0 to num_types-1 do
  3339. begin
  3340. fdread(f,types[i].offset,4);
  3341. fdread(f,types[i].isdst,1);
  3342. fdread(f,types[i].idx,1);
  3343. decode(types[i].offset);
  3344. types[i].isstd:=0;
  3345. types[i].isgmt:=0;
  3346. end;
  3347. fdread(f,zone_names^,chars);
  3348. for i:=0 to num_leaps-1 do
  3349. begin
  3350. fdread(f,leaps[i].transition,4);
  3351. fdread(f,leaps[i].change,4);
  3352. decode(leaps[i].transition);
  3353. decode(leaps[i].change);
  3354. end;
  3355. getmem(buf,tzhead.tzh_ttisstdcnt);
  3356. fdread(f,buf^,tzhead.tzh_ttisstdcnt);
  3357. for i:=0 to tzhead.tzh_ttisstdcnt-1 do
  3358. types[i].isstd:=byte(buf[i]<>0);
  3359. freemem(buf);
  3360. getmem(buf,tzhead.tzh_ttisgmtcnt);
  3361. fdread(f,buf^,tzhead.tzh_ttisgmtcnt);
  3362. for i:=0 to tzhead.tzh_ttisgmtcnt-1 do
  3363. types[i].isgmt:=byte(buf[i]<>0);
  3364. freemem(buf);
  3365. fdclose(f);
  3366. end;
  3367. Const
  3368. // Debian system; contains location of timezone file.
  3369. TimeZoneLocationFile = '/etc/timezone';
  3370. // SuSE has link in /usr/lib/zoneinfo/localtime to /etc/localtime
  3371. // RedHat uses /etc/localtime
  3372. TimeZoneFile = '/usr/lib/zoneinfo/localtime';
  3373. AltTimeZoneFile = '/etc/localtime';
  3374. function GetTimezoneFile:string;
  3375. var
  3376. f,len : longint;
  3377. s : string;
  3378. info : stat;
  3379. begin
  3380. GetTimezoneFile:='';
  3381. f:=fdopen(TimeZoneLocationFile,Open_RdOnly);
  3382. if f>0 then
  3383. begin
  3384. len:=fdread(f,s[1],high(s));
  3385. s[0]:=chr(len);
  3386. len:=pos(#10,s);
  3387. if len<>0 then
  3388. s[0]:=chr(len-1);
  3389. fdclose(f);
  3390. GetTimezoneFile:=s;
  3391. end
  3392. // Try SuSE
  3393. else if fstat(TimeZoneFile,info) then
  3394. GetTimeZoneFile:=TimeZoneFile
  3395. // Try RedHat
  3396. else If fstat(AltTimeZoneFile,Info) then
  3397. GetTimeZoneFile:=AltTimeZoneFile;
  3398. end;
  3399. procedure InitLocalTime;
  3400. begin
  3401. ReadTimezoneFile(GetTimezoneFile);
  3402. GetLocalTimezone(GetTimeOfDay);
  3403. end;
  3404. procedure DoneLocalTime;
  3405. begin
  3406. if assigned(transitions) then
  3407. freemem(transitions);
  3408. if assigned(type_idxs) then
  3409. freemem(type_idxs);
  3410. if assigned(types) then
  3411. freemem(types);
  3412. if assigned(zone_names) then
  3413. freemem(zone_names);
  3414. if assigned(leaps) then
  3415. freemem(leaps);
  3416. num_transitions:=0;
  3417. num_leaps:=0;
  3418. num_types:=0;
  3419. end;
  3420. {******************************************************************************
  3421. FileSystem calls
  3422. ******************************************************************************}
  3423. Function fdOpen(pathname:string;flags:longint):longint;
  3424. begin
  3425. pathname:=pathname+#0;
  3426. fdOpen:=Sys_Open(@pathname[1],flags,438);
  3427. LinuxError:=Errno;
  3428. end;
  3429. Function fdOpen(pathname:string;flags,mode:longint):longint;
  3430. begin
  3431. pathname:=pathname+#0;
  3432. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  3433. LinuxError:=Errno;
  3434. end;
  3435. Function fdOpen(pathname:pchar;flags:longint):longint;
  3436. begin
  3437. fdOpen:=Sys_Open(pathname,flags,0);
  3438. LinuxError:=Errno;
  3439. end;
  3440. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  3441. begin
  3442. fdOpen:=Sys_Open(pathname,flags,mode);
  3443. LinuxError:=Errno;
  3444. end;
  3445. Function fdClose(fd:longint):boolean;
  3446. begin
  3447. fdClose:=(Sys_Close(fd)=0);
  3448. LinuxError:=Errno;
  3449. end;
  3450. Function fdRead(fd:longint;var buf;size:longint):longint;
  3451. begin
  3452. fdRead:=Sys_Read(fd,pchar(@buf),size);
  3453. LinuxError:=Errno;
  3454. end;
  3455. Function fdWrite(fd:longint;const buf;size:longint):longint;
  3456. begin
  3457. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  3458. LinuxError:=Errno;
  3459. end;
  3460. Function fdSeek (fd,pos,seektype :longint): longint;
  3461. {
  3462. Do a Seek on a file descriptor fd to position pos, starting from seektype
  3463. }
  3464. begin
  3465. fdseek:=Sys_LSeek (fd,pos,seektype);
  3466. LinuxError:=Errno;
  3467. end;
  3468. {$ifdef BSD}
  3469. Function Fcntl(Fd:longint;Cmd:longint):longint;
  3470. {
  3471. Read or manipulate a file.(See also fcntl (2) )
  3472. Possible values for Cmd are :
  3473. F_GetFd,F_GetFl,F_GetOwn
  3474. Errors are reported in Linuxerror;
  3475. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  3476. }
  3477. begin
  3478. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  3479. begin
  3480. Linuxerror:=sys_fcntl(fd,cmd,0);
  3481. if linuxerror=-1 then
  3482. begin
  3483. linuxerror:=errno;
  3484. fcntl:=0;
  3485. end
  3486. else
  3487. begin
  3488. fcntl:=linuxerror;
  3489. linuxerror:=0;
  3490. end;
  3491. end
  3492. else
  3493. begin
  3494. linuxerror:=Sys_einval;
  3495. Fcntl:=0;
  3496. end;
  3497. end;
  3498. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  3499. {
  3500. Read or manipulate a file. (See also fcntl (2) )
  3501. Possible values for Cmd are :
  3502. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  3503. Errors are reported in Linuxerror;
  3504. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  3505. F_DupFD is not allowed, due to the structure of Files in Pascal.
  3506. }
  3507. begin
  3508. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  3509. begin
  3510. sys_fcntl(fd,cmd,arg);
  3511. LinuxError:=ErrNo;
  3512. end
  3513. else
  3514. linuxerror:=Sys_einval;
  3515. end;
  3516. {$endif}
  3517. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  3518. begin
  3519. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  3520. end;
  3521. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  3522. begin
  3523. Fcntl(textrec(Fd).handle, Cmd, Arg);
  3524. end;
  3525. Function Flock (var T : text;mode : longint) : boolean;
  3526. begin
  3527. Flock:=Flock(TextRec(T).Handle,mode);
  3528. end;
  3529. Function Flock (var F : File;mode : longint) : boolean;
  3530. begin
  3531. Flock:=Flock(FileRec(F).Handle,mode);
  3532. end;
  3533. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  3534. {
  3535. Get all information on a file, and return it in Info.
  3536. }
  3537. begin
  3538. path:=path+#0;
  3539. FStat:=(Sys_stat(@(path[1]),Info)=0);
  3540. LinuxError:=errno;
  3541. end;
  3542. Function FStat(var F:Text;Var Info:stat):Boolean;
  3543. {
  3544. Get all information on a text file, and return it in info.
  3545. }
  3546. begin
  3547. FStat:=Fstat(TextRec(F).Handle,INfo);
  3548. end;
  3549. Function FStat(var F:File;Var Info:stat):Boolean;
  3550. {
  3551. Get all information on a untyped file, and return it in info.
  3552. }
  3553. begin
  3554. FStat:=Fstat(FileRec(F).Handle,Info);
  3555. end;
  3556. Function SymLink(OldPath,newPath:pathstr):boolean;
  3557. {
  3558. Proceduces a soft link from new to old.
  3559. }
  3560. begin
  3561. oldpath:=oldpath+#0;
  3562. newpath:=newpath+#0;
  3563. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  3564. linuxerror:=errno;
  3565. end;
  3566. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  3567. {
  3568. Read a link (where it points to)
  3569. }
  3570. begin
  3571. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  3572. linuxerror:=errno;
  3573. end;
  3574. Function ReadLink(Name:pathstr):pathstr;
  3575. {
  3576. Read a link (where it points to)
  3577. }
  3578. var
  3579. LinkName : pathstr;
  3580. i : longint;
  3581. begin
  3582. Name:=Name+#0;
  3583. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  3584. if i>0 then
  3585. begin
  3586. linkname[0]:=chr(i);
  3587. ReadLink:=LinkName;
  3588. end
  3589. else
  3590. ReadLink:='';
  3591. end;
  3592. Function UnLink(Path:pathstr):boolean;
  3593. {
  3594. Removes the file in 'Path' (that is, it decreases the link count with one.
  3595. if the link count is zero, the file is removed from the disk.
  3596. }
  3597. begin
  3598. path:=path+#0;
  3599. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  3600. linuxerror:=errno;
  3601. end;
  3602. Function UnLink(Path:pchar):Boolean;
  3603. {
  3604. Removes the file in 'Path' (that is, it decreases the link count with one.
  3605. if the link count is zero, the file is removed from the disk.
  3606. }
  3607. begin
  3608. Unlink:=(Sys_unlink(path)=0);
  3609. linuxerror:=errno;
  3610. end;
  3611. Function FRename (OldName,NewName : Pchar) : Boolean;
  3612. begin
  3613. FRename:=Sys_rename(OldName,NewName)=0;
  3614. LinuxError:=Errno;
  3615. end;
  3616. Function FRename (OldName,NewName : String) : Boolean;
  3617. begin
  3618. OldName:=OldName+#0;
  3619. NewName:=NewName+#0;
  3620. FRename:=FRename (@OldName[1],@NewName[1]);
  3621. end;
  3622. Function Dup(var oldfile,newfile:text):Boolean;
  3623. {
  3624. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  3625. oldfile.
  3626. After which the two textfiles are, in effect, the same, except
  3627. that they don't share the same buffer, and don't share the same
  3628. close_on_exit flag.
  3629. }
  3630. begin
  3631. flush(oldfile);{ We cannot share buffers, so we flush them. }
  3632. textrec(newfile):=textrec(oldfile);
  3633. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  3634. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  3635. end;
  3636. Function Dup(var oldfile,newfile:file):Boolean;
  3637. {
  3638. Copies the filedescriptor oldfile to newfile
  3639. }
  3640. begin
  3641. filerec(newfile):=filerec(oldfile);
  3642. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  3643. end;
  3644. Function Dup2(var oldfile,newfile:text):Boolean;
  3645. {
  3646. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  3647. oldfile. It closes newfile if it was still open.
  3648. After which the two textfiles are, in effect, the same, except
  3649. that they don't share the same buffer, and don't share the same
  3650. close_on_exit flag.
  3651. }
  3652. var
  3653. tmphandle : word;
  3654. begin
  3655. case TextRec(oldfile).mode of
  3656. fmOutput, fmInOut, fmAppend :
  3657. flush(oldfile);{ We cannot share buffers, so we flush them. }
  3658. end;
  3659. case TextRec(newfile).mode of
  3660. fmOutput, fmInOut, fmAppend :
  3661. flush(newfile);
  3662. end;
  3663. tmphandle:=textrec(newfile).handle;
  3664. textrec(newfile):=textrec(oldfile);
  3665. textrec(newfile).handle:=tmphandle;
  3666. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  3667. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  3668. end;
  3669. Function Dup2(var oldfile,newfile:file):Boolean;
  3670. {
  3671. Copies the filedescriptor oldfile to newfile
  3672. }
  3673. begin
  3674. filerec(newfile):=filerec(oldfile);
  3675. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  3676. end;
  3677. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  3678. {
  3679. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  3680. have changed.
  3681. This function allows specification of a timeout as a longint.
  3682. }
  3683. var
  3684. p : PTimeVal;
  3685. tv : TimeVal;
  3686. begin
  3687. if TimeOut=-1 then
  3688. p:=nil
  3689. else
  3690. begin
  3691. tv.Sec:=Timeout div 1000;
  3692. tv.Usec:=(Timeout mod 1000)*1000;
  3693. p:=@tv;
  3694. end;
  3695. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  3696. end;
  3697. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  3698. Var
  3699. F:FDSet;
  3700. begin
  3701. if textrec(t).mode=fmclosed then
  3702. begin
  3703. LinuxError:=Sys_EBADF;
  3704. exit(-1);
  3705. end;
  3706. FD_Zero(f);
  3707. FD_Set(textrec(T).handle,f);
  3708. if textrec(T).mode=fminput then
  3709. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  3710. else
  3711. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  3712. end;
  3713. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  3714. var
  3715. p : PTimeVal;
  3716. tv : TimeVal;
  3717. begin
  3718. if TimeOut=-1 then
  3719. p:=nil
  3720. else
  3721. begin
  3722. tv.Sec:=Timeout div 1000;
  3723. tv.Usec:=(Timeout mod 1000)*1000;
  3724. p:=@tv;
  3725. end;
  3726. SelectText:=SelectText(T,p);
  3727. end;
  3728. {******************************************************************************
  3729. Directory
  3730. ******************************************************************************}
  3731. Function OpenDir(F:String):PDir;
  3732. begin
  3733. F:=F+#0;
  3734. OpenDir:=OpenDir(@F[1]);
  3735. LinuxError:=ErrNo;
  3736. end;
  3737. {$ifndef newreaddir}
  3738. procedure SeekDir(p:pdir;off:longint);
  3739. begin
  3740. if p=nil then
  3741. begin
  3742. errno:=Sys_EBADF;
  3743. exit;
  3744. end;
  3745. {$ifndef bsd}
  3746. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  3747. {$endif}
  3748. p^.size:=0;
  3749. p^.loc:=0;
  3750. end;
  3751. function TellDir(p:pdir):longint;
  3752. begin
  3753. if p=nil then
  3754. begin
  3755. errno:=Sys_EBADF;
  3756. telldir:=-1;
  3757. exit;
  3758. end;
  3759. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  3760. { We could try to use the nextoff field here, but on my 1.2.13
  3761. kernel, this gives nothing... This may have to do with
  3762. the readdir implementation of libc... I also didn't find any trace of
  3763. the field in the kernel code itself, So I suspect it is an artifact of libc.
  3764. Michael. }
  3765. end;
  3766. {$endif}
  3767. Function ReadDir(P:pdir):pdirent;
  3768. begin
  3769. ReadDir:=Sys_ReadDir(p);
  3770. LinuxError:=Errno;
  3771. end;
  3772. {******************************************************************************
  3773. Pipes/Fifo
  3774. ******************************************************************************}
  3775. Procedure OpenPipe(var F:Text);
  3776. begin
  3777. case textrec(f).mode of
  3778. fmoutput :
  3779. if textrec(f).userdata[1]<>P_OUT then
  3780. textrec(f).mode:=fmclosed;
  3781. fminput :
  3782. if textrec(f).userdata[1]<>P_IN then
  3783. textrec(f).mode:=fmclosed;
  3784. else
  3785. textrec(f).mode:=fmclosed;
  3786. end;
  3787. end;
  3788. Procedure IOPipe(var F:text);
  3789. begin
  3790. case textrec(f).mode of
  3791. fmoutput :
  3792. begin
  3793. { first check if we need something to write, else we may
  3794. get a SigPipe when Close() is called (PFV) }
  3795. if textrec(f).bufpos>0 then
  3796. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  3797. end;
  3798. fminput :
  3799. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  3800. end;
  3801. textrec(f).bufpos:=0;
  3802. end;
  3803. Procedure FlushPipe(var F:Text);
  3804. begin
  3805. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  3806. IOPipe(f);
  3807. textrec(f).bufpos:=0;
  3808. end;
  3809. Procedure ClosePipe(var F:text);
  3810. begin
  3811. textrec(f).mode:=fmclosed;
  3812. Sys_close(textrec(f).handle);
  3813. end;
  3814. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  3815. {
  3816. Sets up a pair of file variables, which act as a pipe. The first one can
  3817. be read from, the second one can be written to.
  3818. If the operation was unsuccesful, linuxerror is set.
  3819. }
  3820. var
  3821. f_in,f_out : longint;
  3822. begin
  3823. if not AssignPipe(f_in,f_out) then
  3824. begin
  3825. AssignPipe:=false;
  3826. exit;
  3827. end;
  3828. { Set up input }
  3829. Assign(Pipe_in,'');
  3830. Textrec(Pipe_in).Handle:=f_in;
  3831. Textrec(Pipe_in).Mode:=fmInput;
  3832. Textrec(Pipe_in).userdata[1]:=P_IN;
  3833. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  3834. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  3835. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  3836. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  3837. { Set up output }
  3838. Assign(Pipe_out,'');
  3839. Textrec(Pipe_out).Handle:=f_out;
  3840. Textrec(Pipe_out).Mode:=fmOutput;
  3841. Textrec(Pipe_out).userdata[1]:=P_OUT;
  3842. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  3843. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  3844. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  3845. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  3846. AssignPipe:=true;
  3847. end;
  3848. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  3849. {
  3850. Sets up a pair of file variables, which act as a pipe. The first one can
  3851. be read from, the second one can be written to.
  3852. If the operation was unsuccesful, linuxerror is set.
  3853. }
  3854. var
  3855. f_in,f_out : longint;
  3856. begin
  3857. if not AssignPipe(f_in,f_out) then
  3858. begin
  3859. AssignPipe:=false;
  3860. exit;
  3861. end;
  3862. { Set up input }
  3863. Assign(Pipe_in,'');
  3864. Filerec(Pipe_in).Handle:=f_in;
  3865. Filerec(Pipe_in).Mode:=fmInput;
  3866. Filerec(Pipe_in).recsize:=1;
  3867. Filerec(Pipe_in).userdata[1]:=P_IN;
  3868. { Set up output }
  3869. Assign(Pipe_out,'');
  3870. Filerec(Pipe_out).Handle:=f_out;
  3871. Filerec(Pipe_out).Mode:=fmoutput;
  3872. Filerec(Pipe_out).recsize:=1;
  3873. Filerec(Pipe_out).userdata[1]:=P_OUT;
  3874. AssignPipe:=true;
  3875. end;
  3876. Procedure PCloseText(Var F:text);
  3877. {
  3878. May not use @PClose due overloading
  3879. }
  3880. begin
  3881. PClose(f);
  3882. end;
  3883. Procedure POpen(var F:text;const Prog:String;rw:char);
  3884. {
  3885. Starts the program in 'Prog' and makes it's input or out put the
  3886. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  3887. F, will be read from stdin by the program in 'Prog'. The inverse is true
  3888. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  3889. read from 'f'.
  3890. }
  3891. var
  3892. pipi,
  3893. pipo : text;
  3894. pid : longint;
  3895. pl : ^longint;
  3896. pp : ppchar;
  3897. begin
  3898. LinuxError:=0;
  3899. rw:=upcase(rw);
  3900. if not (rw in ['R','W']) then
  3901. begin
  3902. LinuxError:=Sys_enoent;
  3903. exit;
  3904. end;
  3905. AssignPipe(pipi,pipo);
  3906. if Linuxerror<>0 then
  3907. exit;
  3908. pid:=fork;
  3909. if linuxerror<>0 then
  3910. begin
  3911. close(pipi);
  3912. close(pipo);
  3913. exit;
  3914. end;
  3915. if pid=0 then
  3916. begin
  3917. {$ifdef BSD} // FreeBSD checked only
  3918. { We're in the child }
  3919. close(pipi);
  3920. if textrec(pipo).handle<>textrec(output).handle Then
  3921. begin
  3922. dup2(textrec(pipo).handle,textrec(output).handle);
  3923. if rw='W' Then
  3924. dup2(textrec(output).handle,textrec(input).handle);
  3925. end
  3926. else
  3927. if (rw='W') and (textrec(pipi).handle<>textrec(input).handle) then
  3928. dup2(textrec(output).handle,textrec(input).handle);
  3929. close(pipo);
  3930. if linuxerror<>0 then
  3931. halt(127);
  3932. pp:=createshellargv(prog);
  3933. Execve(pp^,pp,envp);
  3934. halt(127);
  3935. end
  3936. {$else}
  3937. { We're in the child }
  3938. if rw='W' then
  3939. begin
  3940. close(pipo);
  3941. dup2(pipi,input);
  3942. close(pipi);
  3943. if linuxerror<>0 then
  3944. halt(127);
  3945. end
  3946. else
  3947. begin
  3948. close(pipi);
  3949. dup2(pipo,output);
  3950. close(pipo);
  3951. if linuxerror<>0 then
  3952. halt(127);
  3953. end;
  3954. pp:=createshellargv(prog);
  3955. Execve(pp^,pp,envp);
  3956. halt(127);
  3957. end
  3958. {$endif}
  3959. else
  3960. begin
  3961. { We're in the parent }
  3962. if rw='W' then
  3963. begin
  3964. close(pipi);
  3965. f:=pipo;
  3966. textrec(f).bufptr:=@textrec(f).buffer;
  3967. end
  3968. else
  3969. begin
  3970. close(pipo);
  3971. f:=pipi;
  3972. textrec(f).bufptr:=@textrec(f).buffer;
  3973. end;
  3974. {Save the process ID - needed when closing }
  3975. pl:=@(textrec(f).userdata[2]);
  3976. pl^:=pid;
  3977. textrec(f).closefunc:=@PCloseText;
  3978. end;
  3979. end;
  3980. Procedure POpen(var F:file;const Prog:String;rw:char);
  3981. {
  3982. Starts the program in 'Prog' and makes it's input or out put the
  3983. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  3984. F, will be read from stdin by the program in 'Prog'. The inverse is true
  3985. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  3986. read from 'f'.
  3987. }
  3988. var
  3989. pipi,
  3990. pipo : file;
  3991. pid : longint;
  3992. pl : ^longint;
  3993. p,pp : ppchar;
  3994. temp : string[255];
  3995. begin
  3996. LinuxError:=0;
  3997. rw:=upcase(rw);
  3998. if not (rw in ['R','W']) then
  3999. begin
  4000. LinuxError:=Sys_enoent;
  4001. exit;
  4002. end;
  4003. AssignPipe(pipi,pipo);
  4004. if Linuxerror<>0 then
  4005. exit;
  4006. pid:=fork;
  4007. if linuxerror<>0 then
  4008. begin
  4009. close(pipi);
  4010. close(pipo);
  4011. exit;
  4012. end;
  4013. if pid=0 then
  4014. begin
  4015. { We're in the child }
  4016. if rw='W' then
  4017. begin
  4018. close(pipo);
  4019. dup2(filerec(pipi).handle,stdinputhandle);
  4020. close(pipi);
  4021. if linuxerror<>0 then
  4022. halt(127);
  4023. end
  4024. else
  4025. begin
  4026. close(pipi);
  4027. dup2(filerec(pipo).handle,stdoutputhandle);
  4028. close(pipo);
  4029. if linuxerror<>0 then
  4030. halt(127);
  4031. end;
  4032. getmem(pp,sizeof(pchar)*4);
  4033. temp:='/bin/sh'#0'-c'#0+prog+#0;
  4034. p:=pp;
  4035. p^:=@temp[1];
  4036. inc(p);
  4037. p^:=@temp[9];
  4038. inc(p);
  4039. p^:=@temp[12];
  4040. inc(p);
  4041. p^:=Nil;
  4042. Execve('/bin/sh',pp,envp);
  4043. halt(127);
  4044. end
  4045. else
  4046. begin
  4047. { We're in the parent }
  4048. if rw='W' then
  4049. begin
  4050. close(pipi);
  4051. f:=pipo;
  4052. end
  4053. else
  4054. begin
  4055. close(pipo);
  4056. f:=pipi;
  4057. end;
  4058. {Save the process ID - needed when closing }
  4059. pl:=@(filerec(f).userdata[2]);
  4060. pl^:=pid;
  4061. end;
  4062. end;
  4063. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  4064. {
  4065. Starts the program in 'Prog' and makes its input and output the
  4066. other end of two pipes, which are the stdin and stdout of a program
  4067. specified in 'Prog'.
  4068. streamout can be used to write to the program, streamin can be used to read
  4069. the output of the program. See the following diagram :
  4070. Parent Child
  4071. STreamout --> Input
  4072. Streamin <-- Output
  4073. Return value is the process ID of the process being spawned, or -1 in case of failure.
  4074. }
  4075. var
  4076. pipi,
  4077. pipo : text;
  4078. pid : longint;
  4079. pl : ^Longint;
  4080. begin
  4081. LinuxError:=0;
  4082. AssignStream:=-1;
  4083. AssignPipe(streamin,pipo);
  4084. if Linuxerror<>0 then
  4085. exit;
  4086. AssignPipe(pipi,streamout);
  4087. if Linuxerror<>0 then
  4088. exit;
  4089. pid:=fork;
  4090. if linuxerror<>0 then
  4091. begin
  4092. close(pipi);
  4093. close(pipo);
  4094. close (streamin);
  4095. close (streamout);
  4096. exit;
  4097. end;
  4098. if pid=0 then
  4099. begin
  4100. { We're in the child }
  4101. { Close what we don't need }
  4102. close(streamout);
  4103. close(streamin);
  4104. dup2(pipi,input);
  4105. if linuxerror<>0 then
  4106. halt(127);
  4107. close(pipi);
  4108. dup2(pipo,output);
  4109. if linuxerror<>0 then
  4110. halt (127);
  4111. close(pipo);
  4112. Execl(Prog);
  4113. halt(127);
  4114. end
  4115. else
  4116. begin
  4117. { we're in the parent}
  4118. close(pipo);
  4119. close(pipi);
  4120. {Save the process ID - needed when closing }
  4121. pl:=@(textrec(StreamIn).userdata[2]);
  4122. pl^:=pid;
  4123. textrec(StreamIn).closefunc:=@PCloseText;
  4124. {Save the process ID - needed when closing }
  4125. pl:=@(textrec(StreamOut).userdata[2]);
  4126. pl^:=pid;
  4127. textrec(StreamOut).closefunc:=@PCloseText;
  4128. AssignStream:=Pid;
  4129. end;
  4130. end;
  4131. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  4132. {
  4133. Starts the program in 'prog' and makes its input, output and error output the
  4134. other end of three pipes, which are the stdin, stdout and stderr of a program
  4135. specified in 'prog'.
  4136. StreamOut can be used to write to the program, StreamIn can be used to read
  4137. the output of the program, StreamErr reads the error output of the program.
  4138. See the following diagram :
  4139. Parent Child
  4140. StreamOut --> StdIn (input)
  4141. StreamIn <-- StdOut (output)
  4142. StreamErr <-- StdErr (error output)
  4143. }
  4144. var
  4145. PipeIn, PipeOut, PipeErr: text;
  4146. pid: LongInt;
  4147. pl: ^LongInt;
  4148. begin
  4149. LinuxError := 0;
  4150. AssignStream := -1;
  4151. // Assign pipes
  4152. AssignPipe(StreamIn, PipeOut);
  4153. if LinuxError <> 0 then exit;
  4154. AssignPipe(StreamErr, PipeErr);
  4155. if LinuxError <> 0 then begin
  4156. Close(StreamIn);
  4157. Close(PipeOut);
  4158. exit;
  4159. end;
  4160. AssignPipe(PipeIn, StreamOut);
  4161. if LinuxError <> 0 then begin
  4162. Close(StreamIn);
  4163. Close(PipeOut);
  4164. Close(StreamErr);
  4165. Close(PipeErr);
  4166. exit;
  4167. end;
  4168. // Fork
  4169. pid := Fork;
  4170. if LinuxError <> 0 then begin
  4171. Close(StreamIn);
  4172. Close(PipeOut);
  4173. Close(StreamErr);
  4174. Close(PipeErr);
  4175. Close(PipeIn);
  4176. Close(StreamOut);
  4177. exit;
  4178. end;
  4179. if pid = 0 then begin
  4180. // *** We are in the child ***
  4181. // Close what we don not need
  4182. Close(StreamOut);
  4183. Close(StreamIn);
  4184. Close(StreamErr);
  4185. // Connect pipes
  4186. dup2(PipeIn, Input);
  4187. if LinuxError <> 0 then Halt(127);
  4188. Close(PipeIn);
  4189. dup2(PipeOut, Output);
  4190. if LinuxError <> 0 then Halt(127);
  4191. Close(PipeOut);
  4192. dup2(PipeErr, StdErr);
  4193. if LinuxError <> 0 then Halt(127);
  4194. Close(PipeErr);
  4195. // Execute program
  4196. Execl(Prog);
  4197. Halt(127);
  4198. end else begin
  4199. // *** We are in the parent ***
  4200. Close(PipeErr);
  4201. Close(PipeOut);
  4202. Close(PipeIn);
  4203. // Save the process ID - needed when closing
  4204. pl := @(TextRec(StreamIn).userdata[2]);
  4205. pl^ := pid;
  4206. TextRec(StreamIn).closefunc := @PCloseText;
  4207. // Save the process ID - needed when closing
  4208. pl := @(TextRec(StreamOut).userdata[2]);
  4209. pl^ := pid;
  4210. TextRec(StreamOut).closefunc := @PCloseText;
  4211. // Save the process ID - needed when closing
  4212. pl := @(TextRec(StreamErr).userdata[2]);
  4213. pl^ := pid;
  4214. TextRec(StreamErr).closefunc := @PCloseText;
  4215. AssignStream := pid;
  4216. end;
  4217. end;
  4218. {******************************************************************************
  4219. General information calls
  4220. ******************************************************************************}
  4221. Function GetEnv(P:string):Pchar;
  4222. {
  4223. Searches the environment for a string with name p and
  4224. returns a pchar to it's value.
  4225. A pchar is used to accomodate for strings of length > 255
  4226. }
  4227. var
  4228. ep : ppchar;
  4229. found : boolean;
  4230. Begin
  4231. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  4232. ep:=envp;
  4233. found:=false;
  4234. if ep<>nil then
  4235. begin
  4236. while (not found) and (ep^<>nil) do
  4237. begin
  4238. if strlcomp(@p[1],(ep^),length(p))=0 then
  4239. found:=true
  4240. else
  4241. inc(ep);
  4242. end;
  4243. end;
  4244. if found then
  4245. getenv:=ep^+length(p)
  4246. else
  4247. getenv:=nil;
  4248. end;
  4249. {$ifndef bsd}
  4250. Function GetDomainName:String;
  4251. {
  4252. Get machines domain name. Returns empty string if not set.
  4253. }
  4254. Var
  4255. Sysn : utsname;
  4256. begin
  4257. Uname(Sysn);
  4258. linuxerror:=errno;
  4259. If linuxerror<>0 then
  4260. getdomainname:=''
  4261. else
  4262. getdomainname:=strpas(@Sysn.domainname[0]);
  4263. end;
  4264. Function GetHostName:String;
  4265. {
  4266. Get machines name. Returns empty string if not set.
  4267. }
  4268. Var
  4269. Sysn : utsname;
  4270. begin
  4271. uname(Sysn);
  4272. linuxerror:=errno;
  4273. If linuxerror<>0 then
  4274. gethostname:=''
  4275. else
  4276. gethostname:=strpas(@Sysn.nodename[0]);
  4277. end;
  4278. {$endif}
  4279. {******************************************************************************
  4280. Signal handling calls
  4281. ******************************************************************************}
  4282. procedure SigRaise(sig:integer);
  4283. begin
  4284. Kill(GetPid,Sig);
  4285. end;
  4286. {******************************************************************************
  4287. IOCtl and Termios calls
  4288. ******************************************************************************}
  4289. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  4290. begin
  4291. {$ifndef BSD}
  4292. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  4293. {$else}
  4294. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  4295. {$endif}
  4296. end;
  4297. Function TCSetAttr(fd:longint;OptAct:longint;const tios:TermIOS):boolean;
  4298. var
  4299. nr:longint;
  4300. begin
  4301. {$ifndef BSD}
  4302. case OptAct of
  4303. TCSANOW : nr:=TCSETS;
  4304. TCSADRAIN : nr:=TCSETSW;
  4305. TCSAFLUSH : nr:=TCSETSF;
  4306. {$else}
  4307. case OptAct of
  4308. TCSANOW : nr:=TIOCSETA;
  4309. TCSADRAIN : nr:=TIOCSETAW;
  4310. TCSAFLUSH : nr:=TIOCSETAF;
  4311. {$endif}
  4312. else
  4313. begin
  4314. ErrNo:=Sys_EINVAL;
  4315. TCSetAttr:=false;
  4316. exit;
  4317. end;
  4318. end;
  4319. TCSetAttr:=IOCtl(fd,nr,@Tios);
  4320. end;
  4321. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal);
  4322. begin
  4323. {$ifndef BSD}
  4324. tios.c_cflag:=Cardinal(tios.c_cflag and cardinal(not CBAUD)) or speed;
  4325. {$else}
  4326. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  4327. {$endif}
  4328. end;
  4329. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  4330. begin
  4331. {$ifndef BSD}
  4332. CFSetISpeed(tios,speed);
  4333. {$else}
  4334. tios.c_ospeed:=speed;
  4335. {$endif}
  4336. end;
  4337. Procedure CFMakeRaw(var tios:TermIOS);
  4338. begin
  4339. {$ifndef BSD}
  4340. with tios do
  4341. begin
  4342. c_iflag:=c_iflag and cardinal(not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  4343. INLCR or IGNCR or ICRNL or IXON));
  4344. c_oflag:=c_oflag and cardinal(not OPOST);
  4345. c_lflag:=c_lflag and cardinal(not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  4346. c_cflag:=(c_cflag and cardinal(not (CSIZE or PARENB))) or CS8;
  4347. end;
  4348. {$else}
  4349. with tios do
  4350. begin
  4351. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  4352. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  4353. IGNPAR));
  4354. c_iflag:=c_iflag OR IGNBRK;
  4355. c_oflag:=c_oflag and (not OPOST);
  4356. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  4357. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  4358. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  4359. c_cc[VMIN]:=1;
  4360. c_cc[VTIME]:=0;
  4361. end;
  4362. {$endif}
  4363. end;
  4364. Function TCSendBreak(fd,duration:longint):boolean;
  4365. begin
  4366. {$ifndef BSD}
  4367. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  4368. {$else}
  4369. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  4370. {$endif}
  4371. end;
  4372. Function TCSetPGrp(fd,id:longint):boolean;
  4373. begin
  4374. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  4375. end;
  4376. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  4377. begin
  4378. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  4379. end;
  4380. Function TCDrain(fd:longint):boolean;
  4381. begin
  4382. {$ifndef BSD}
  4383. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  4384. {$else}
  4385. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  4386. {$endif}
  4387. end;
  4388. Function TCFlow(fd,act:longint):boolean;
  4389. begin
  4390. {$ifndef BSD}
  4391. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  4392. {$else}
  4393. case act OF
  4394. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  4395. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  4396. TCIOFF : {N/I}
  4397. end;
  4398. {$endif}
  4399. end;
  4400. Function TCFlush(fd,qsel:longint):boolean;
  4401. begin
  4402. {$ifndef BSD}
  4403. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  4404. {$else}
  4405. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  4406. {$endif}
  4407. end;
  4408. Function IsATTY(Handle:Longint):Boolean;
  4409. {
  4410. Check if the filehandle described by 'handle' is a TTY (Terminal)
  4411. }
  4412. var
  4413. t : Termios;
  4414. begin
  4415. IsAtty:=TCGetAttr(Handle,t);
  4416. end;
  4417. Function IsATTY(var f: text):Boolean;
  4418. {
  4419. Idem as previous, only now for text variables.
  4420. }
  4421. begin
  4422. IsATTY:=IsaTTY(textrec(f).handle);
  4423. end;
  4424. function TTYName(Handle:Longint):string;
  4425. {
  4426. Return the name of the current tty described by handle f.
  4427. returns empty string in case of an error.
  4428. }
  4429. {$ifdef BSD}
  4430. var
  4431. mydev,
  4432. myino : cardinal;
  4433. {$else not BSD}
  4434. var
  4435. mydev,
  4436. myino : longint;
  4437. {$endif not BSD}
  4438. st : stat;
  4439. function mysearch(n:string): boolean;
  4440. {searches recursively for the device in the directory given by n,
  4441. returns true if found and sets the name of the device in ttyname}
  4442. var dirstream : pdir;
  4443. d : pdirent;
  4444. name : string;
  4445. st : stat;
  4446. begin
  4447. dirstream:=opendir(n);
  4448. if (linuxerror<>0) then
  4449. exit;
  4450. d:=Readdir(dirstream);
  4451. while (d<>nil) do
  4452. begin
  4453. name:=n+'/'+strpas(@(d^.name));
  4454. fstat(name,st);
  4455. if linuxerror=0 then
  4456. begin
  4457. if ((st.mode and $E000)=$4000) and { if it is a directory }
  4458. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  4459. (strpas(@(d^.name))<>'..') and
  4460. (strpas(@(d^.name))<>'') and
  4461. (strpas(@(d^.name))<>'fd') then
  4462. begin {we found a directory, search inside it}
  4463. if mysearch(name) then
  4464. begin {the device is here}
  4465. closedir(dirstream); {then don't continue searching}
  4466. mysearch:=true;
  4467. exit;
  4468. end;
  4469. end
  4470. else if (d^.ino=myino) and (st.dev=mydev) then
  4471. begin
  4472. closedir(dirstream);
  4473. ttyname:=name;
  4474. mysearch:=true;
  4475. exit;
  4476. end;
  4477. end;
  4478. d:=Readdir(dirstream);
  4479. end;
  4480. closedir(dirstream);
  4481. mysearch:=false;
  4482. end;
  4483. begin
  4484. TTYName:='';
  4485. fstat(handle,st);
  4486. if (errno<>0) and isatty (handle) then
  4487. exit;
  4488. mydev:=st.dev;
  4489. myino:=st.ino;
  4490. mysearch('/dev');
  4491. end;
  4492. function TTYName(var F:Text):string;
  4493. {
  4494. Idem as previous, only now for text variables;
  4495. }
  4496. begin
  4497. TTYName:=TTYName(textrec(f).handle);
  4498. end;
  4499. {******************************************************************************
  4500. Utility calls
  4501. ******************************************************************************}
  4502. Function Octal(l:longint):longint;
  4503. {
  4504. Convert an octal specified number to decimal;
  4505. }
  4506. var
  4507. octnr,
  4508. oct : longint;
  4509. begin
  4510. octnr:=0;
  4511. oct:=0;
  4512. while (l>0) do
  4513. begin
  4514. oct:=oct or ((l mod 10) shl octnr);
  4515. l:=l div 10;
  4516. inc(octnr,3);
  4517. end;
  4518. Octal:=oct;
  4519. end;
  4520. Function StringToPPChar(S: PChar):ppchar;
  4521. var
  4522. nr : longint;
  4523. Buf : ^char;
  4524. p : ppchar;
  4525. begin
  4526. buf:=s;
  4527. nr:=0;
  4528. while(buf^<>#0) do
  4529. begin
  4530. while (buf^ in [' ',#9,#10]) do
  4531. inc(buf);
  4532. inc(nr);
  4533. while not (buf^ in [' ',#0,#9,#10]) do
  4534. inc(buf);
  4535. end;
  4536. getmem(p,(nr+1)*4);
  4537. StringToPPChar:=p;
  4538. if p=nil then
  4539. begin
  4540. LinuxError:=sys_enomem;
  4541. exit;
  4542. end;
  4543. buf:=s;
  4544. while (buf^<>#0) do
  4545. begin
  4546. while (buf^ in [' ',#9,#10]) do
  4547. begin
  4548. buf^:=#0;
  4549. inc(buf);
  4550. end;
  4551. p^:=buf;
  4552. inc(p);
  4553. p^:=nil;
  4554. while not (buf^ in [' ',#0,#9,#10]) do
  4555. inc(buf);
  4556. end;
  4557. end;
  4558. Function StringToPPChar(Var S:String):ppchar;
  4559. {
  4560. Create a PPChar to structure of pchars which are the arguments specified
  4561. in the string S. Especially usefull for creating an ArgV for Exec-calls
  4562. Note that the string S is destroyed by this call.
  4563. }
  4564. begin
  4565. S:=S+#0;
  4566. StringToPPChar:=StringToPPChar(@S[1]);
  4567. end;
  4568. Function StringToPPChar(Var S:AnsiString):ppchar;
  4569. {
  4570. Create a PPChar to structure of pchars which are the arguments specified
  4571. in the string S. Especially usefull for creating an ArgV for Exec-calls
  4572. }
  4573. begin
  4574. StringToPPChar:=StringToPPChar(PChar(S));
  4575. end;
  4576. {
  4577. function FExpand (const Path: PathStr): PathStr;
  4578. - declared in fexpand.inc
  4579. }
  4580. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  4581. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  4582. const
  4583. LFNSupport = true;
  4584. FileNameCaseSensitive = true;
  4585. {$I fexpand.inc}
  4586. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  4587. {$UNDEF FPC_FEXPAND_TILDE}
  4588. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  4589. {
  4590. Searches for a file 'path' in the list of direcories in 'dirlist'.
  4591. returns an empty string if not found. Wildcards are NOT allowed.
  4592. If dirlist is empty, it is set to '.'
  4593. }
  4594. Var
  4595. NewDir : PathStr;
  4596. p1 : Longint;
  4597. Info : Stat;
  4598. Begin
  4599. {Replace ':' with ';'}
  4600. for p1:=1to length(dirlist) do
  4601. if dirlist[p1]=':' then
  4602. dirlist[p1]:=';';
  4603. {Check for WildCards}
  4604. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  4605. FSearch:='' {No wildcards allowed in these things.}
  4606. Else
  4607. Begin
  4608. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  4609. Repeat
  4610. p1:=Pos(';',DirList);
  4611. If p1=0 Then
  4612. p1:=255;
  4613. NewDir:=Copy(DirList,1,P1 - 1);
  4614. if NewDir[Length(NewDir)]<>'/' then
  4615. NewDir:=NewDir+'/';
  4616. NewDir:=NewDir+Path;
  4617. Delete(DirList,1,p1);
  4618. if FStat(NewDir,Info) then
  4619. Begin
  4620. If Pos('./',NewDir)=1 Then
  4621. Delete(NewDir,1,2);
  4622. {DOS strips off an initial .\}
  4623. End
  4624. Else
  4625. NewDir:='';
  4626. Until (DirList='') or (Length(NewDir) > 0);
  4627. FSearch:=NewDir;
  4628. End;
  4629. End;
  4630. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  4631. Var
  4632. DotPos,SlashPos,i : longint;
  4633. Begin
  4634. SlashPos:=0;
  4635. DotPos:=256;
  4636. i:=Length(Path);
  4637. While (i>0) and (SlashPos=0) Do
  4638. Begin
  4639. If (DotPos=256) and (Path[i]='.') Then
  4640. begin
  4641. DotPos:=i;
  4642. end;
  4643. If (Path[i]='/') Then
  4644. SlashPos:=i;
  4645. Dec(i);
  4646. End;
  4647. Ext:=Copy(Path,DotPos,255);
  4648. Dir:=Copy(Path,1,SlashPos);
  4649. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  4650. End;
  4651. Function Dirname(Const path:pathstr):pathstr;
  4652. {
  4653. This function returns the directory part of a complete path.
  4654. Unless the directory is root '/', The last character is not
  4655. a slash.
  4656. }
  4657. var
  4658. Dir : PathStr;
  4659. Name : NameStr;
  4660. Ext : ExtStr;
  4661. begin
  4662. FSplit(Path,Dir,Name,Ext);
  4663. if length(Dir)>1 then
  4664. Delete(Dir,length(Dir),1);
  4665. DirName:=Dir;
  4666. end;
  4667. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  4668. {
  4669. This function returns the filename part of a complete path. If suf is
  4670. supplied, it is cut off the filename.
  4671. }
  4672. var
  4673. Dir : PathStr;
  4674. Name : NameStr;
  4675. Ext : ExtStr;
  4676. begin
  4677. FSplit(Path,Dir,Name,Ext);
  4678. if Suf<>Ext then
  4679. Name:=Name+Ext;
  4680. BaseName:=Name;
  4681. end;
  4682. Function FNMatch(const Pattern,Name:string):Boolean;
  4683. Var
  4684. LenPat,LenName : longint;
  4685. Function DoFNMatch(i,j:longint):Boolean;
  4686. Var
  4687. Found : boolean;
  4688. Begin
  4689. Found:=true;
  4690. While Found and (i<=LenPat) Do
  4691. Begin
  4692. Case Pattern[i] of
  4693. '?' : Found:=(j<=LenName);
  4694. '*' : Begin
  4695. {find the next character in pattern, different of ? and *}
  4696. while Found and (i<LenPat) do
  4697. begin
  4698. inc(i);
  4699. case Pattern[i] of
  4700. '*' : ;
  4701. '?' : begin
  4702. inc(j);
  4703. Found:=(j<=LenName);
  4704. end;
  4705. else
  4706. Found:=false;
  4707. end;
  4708. end;
  4709. {Now, find in name the character which i points to, if the * or ?
  4710. wasn't the last character in the pattern, else, use up all the
  4711. chars in name}
  4712. Found:=true;
  4713. if (i<=LenPat) then
  4714. begin
  4715. repeat
  4716. {find a letter (not only first !) which maches pattern[i]}
  4717. while (j<=LenName) and (name[j]<>pattern[i]) do
  4718. inc (j);
  4719. if (j<LenName) then
  4720. begin
  4721. if DoFnMatch(i+1,j+1) then
  4722. begin
  4723. i:=LenPat;
  4724. j:=LenName;{we can stop}
  4725. Found:=true;
  4726. end
  4727. else
  4728. inc(j);{We didn't find one, need to look further}
  4729. end;
  4730. until (j>=LenName);
  4731. end
  4732. else
  4733. j:=LenName;{we can stop}
  4734. end;
  4735. else {not a wildcard character in pattern}
  4736. Found:=(j<=LenName) and (pattern[i]=name[j]);
  4737. end;
  4738. inc(i);
  4739. inc(j);
  4740. end;
  4741. DoFnMatch:=Found and (j>LenName);
  4742. end;
  4743. Begin {start FNMatch}
  4744. LenPat:=Length(Pattern);
  4745. LenName:=Length(Name);
  4746. FNMatch:=DoFNMatch(1,1);
  4747. End;
  4748. Procedure Globfree(var p : pglob);
  4749. {
  4750. Release memory occupied by pglob structure, and names in it.
  4751. sets p to nil.
  4752. }
  4753. var
  4754. temp : pglob;
  4755. begin
  4756. while assigned(p) do
  4757. begin
  4758. temp:=p^.next;
  4759. if assigned(p^.name) then
  4760. freemem(p^.name);
  4761. dispose(p);
  4762. p:=temp;
  4763. end;
  4764. end;
  4765. Function Glob(Const path:pathstr):pglob;
  4766. {
  4767. Fills a tglob structure with entries matching path,
  4768. and returns a pointer to it. Returns nil on error,
  4769. linuxerror is set accordingly.
  4770. }
  4771. var
  4772. temp,
  4773. temp2 : string[255];
  4774. thedir : pdir;
  4775. buffer : pdirent;
  4776. root,
  4777. current : pglob;
  4778. begin
  4779. { Get directory }
  4780. temp:=dirname(path);
  4781. if temp='' then
  4782. temp:='.';
  4783. temp:=temp+#0;
  4784. thedir:=opendir(@temp[1]);
  4785. if thedir=nil then
  4786. begin
  4787. glob:=nil;
  4788. linuxerror:=errno;
  4789. exit;
  4790. end;
  4791. temp:=basename(path,''); { get the pattern }
  4792. if thedir^.fd<0 then
  4793. begin
  4794. linuxerror:=errno;
  4795. glob:=nil;
  4796. exit;
  4797. end;
  4798. {get the entries}
  4799. root:=nil;
  4800. current:=nil;
  4801. repeat
  4802. buffer:=Sys_readdir(thedir);
  4803. if buffer=nil then
  4804. break;
  4805. temp2:=strpas(@(buffer^.name[0]));
  4806. if fnmatch(temp,temp2) then
  4807. begin
  4808. if root=nil then
  4809. begin
  4810. new(root);
  4811. current:=root;
  4812. end
  4813. else
  4814. begin
  4815. new(current^.next);
  4816. current:=current^.next;
  4817. end;
  4818. if current=nil then
  4819. begin
  4820. linuxerror:=Sys_ENOMEM;
  4821. globfree(root);
  4822. break;
  4823. end;
  4824. current^.next:=nil;
  4825. getmem(current^.name,length(temp2)+1);
  4826. if current^.name=nil then
  4827. begin
  4828. linuxerror:=Sys_ENOMEM;
  4829. globfree(root);
  4830. break;
  4831. end;
  4832. move(buffer^.name[0],current^.name^,length(temp2)+1);
  4833. end;
  4834. until false;
  4835. closedir(thedir);
  4836. glob:=root;
  4837. end;
  4838. {--------------------------------
  4839. FiledescriptorSets
  4840. --------------------------------}
  4841. Procedure FD_Zero(var fds:fdSet);
  4842. {
  4843. Clear the set of filedescriptors
  4844. }
  4845. begin
  4846. FillChar(fds,sizeof(fdSet),0);
  4847. end;
  4848. Procedure FD_Clr(fd:longint;var fds:fdSet);
  4849. {
  4850. Remove fd from the set of filedescriptors
  4851. }
  4852. begin
  4853. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  4854. end;
  4855. Procedure FD_Set(fd:longint;var fds:fdSet);
  4856. {
  4857. Add fd to the set of filedescriptors
  4858. }
  4859. begin
  4860. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  4861. end;
  4862. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  4863. {
  4864. Test if fd is part of the set of filedescriptors
  4865. }
  4866. begin
  4867. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  4868. end;
  4869. Function GetFS (var T:Text):longint;
  4870. {
  4871. Get File Descriptor of a text file.
  4872. }
  4873. begin
  4874. if textrec(t).mode=fmclosed then
  4875. exit(-1)
  4876. else
  4877. GETFS:=textrec(t).Handle
  4878. end;
  4879. Function GetFS(Var F:File):longint;
  4880. {
  4881. Get File Descriptor of an unTyped file.
  4882. }
  4883. begin
  4884. { Handle and mode are on the same place in textrec and filerec. }
  4885. if filerec(f).mode=fmclosed then
  4886. exit(-1)
  4887. else
  4888. GETFS:=filerec(f).Handle
  4889. end;
  4890. {--------------------------------
  4891. Stat.Mode Macro's
  4892. --------------------------------}
  4893. Function S_ISLNK(m:word):boolean;
  4894. {
  4895. Check mode field of inode for link.
  4896. }
  4897. begin
  4898. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  4899. end;
  4900. Function S_ISREG(m:word):boolean;
  4901. {
  4902. Check mode field of inode for regular file.
  4903. }
  4904. begin
  4905. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  4906. end;
  4907. Function S_ISDIR(m:word):boolean;
  4908. {
  4909. Check mode field of inode for directory.
  4910. }
  4911. begin
  4912. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  4913. end;
  4914. Function S_ISCHR(m:word):boolean;
  4915. {
  4916. Check mode field of inode for character device.
  4917. }
  4918. begin
  4919. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  4920. end;
  4921. Function S_ISBLK(m:word):boolean;
  4922. {
  4923. Check mode field of inode for block device.
  4924. }
  4925. begin
  4926. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  4927. end;
  4928. Function S_ISFIFO(m:word):boolean;
  4929. {
  4930. Check mode field of inode for named pipe (FIFO).
  4931. }
  4932. begin
  4933. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  4934. end;
  4935. Function S_ISSOCK(m:word):boolean;
  4936. {
  4937. Check mode field of inode for socket.
  4938. }
  4939. begin
  4940. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  4941. end;
  4942. Procedure WritePort (Port : Longint; Value : Byte);oldfpccall;
  4943. {
  4944. Writes 'Value' to port 'Port'
  4945. }
  4946. begin
  4947. asm
  4948. movl port,%edx
  4949. movb value,%al
  4950. outb %al,%dx
  4951. end ['EAX','EDX'];
  4952. end;
  4953. Procedure WritePort (Port : Longint; Value : Word);oldfpccall;
  4954. {
  4955. Writes 'Value' to port 'Port'
  4956. }
  4957. begin
  4958. asm
  4959. movl port,%edx
  4960. movw value,%ax
  4961. outw %ax,%dx
  4962. end ['EAX','EDX'];
  4963. end;
  4964. Procedure WritePort (Port : Longint; Value : Longint);oldfpccall;
  4965. {
  4966. Writes 'Value' to port 'Port'
  4967. }
  4968. begin
  4969. asm
  4970. movl port,%edx
  4971. movl value,%eax
  4972. outl %eax,%dx
  4973. end ['EAX','EDX'];
  4974. end;
  4975. Procedure WritePortB (Port : Longint; Value : Byte);oldfpccall;
  4976. {
  4977. Writes 'Value' to port 'Port'
  4978. }
  4979. begin
  4980. asm
  4981. movl port,%edx
  4982. movb value,%al
  4983. outb %al,%dx
  4984. end ['EAX','EDX'];
  4985. end;
  4986. Procedure WritePortW (Port : Longint; Value : Word);oldfpccall;
  4987. {
  4988. Writes 'Value' to port 'Port'
  4989. }
  4990. begin
  4991. asm
  4992. movl port,%edx
  4993. movw value,%ax
  4994. outw %ax,%dx
  4995. end ['EAX','EDX'];
  4996. end;
  4997. Procedure WritePortL (Port : Longint; Value : Longint);oldfpccall;
  4998. {
  4999. Writes 'Value' to port 'Port'
  5000. }
  5001. begin
  5002. asm
  5003. movl port,%edx
  5004. movl value,%eax
  5005. outl %eax,%dx
  5006. end ['EAX','EDX'];
  5007. end;
  5008. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5009. {
  5010. Writes 'Count' longints from 'Buf' to Port
  5011. }
  5012. begin
  5013. asm
  5014. movl count,%ecx
  5015. movl buf,%esi
  5016. movl port,%edx
  5017. cld
  5018. rep
  5019. outsl
  5020. end ['ECX','ESI','EDX'];
  5021. end;
  5022. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5023. {
  5024. Writes 'Count' words from 'Buf' to Port
  5025. }
  5026. begin
  5027. asm
  5028. movl count,%ecx
  5029. movl buf,%esi
  5030. movl port,%edx
  5031. cld
  5032. rep
  5033. outsw
  5034. end ['ECX','ESI','EDX'];
  5035. end;
  5036. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5037. {
  5038. Writes 'Count' bytes from 'Buf' to Port
  5039. }
  5040. begin
  5041. asm
  5042. movl count,%ecx
  5043. movl buf,%esi
  5044. movl port,%edx
  5045. cld
  5046. rep
  5047. outsb
  5048. end ['ECX','ESI','EDX'];
  5049. end;
  5050. Procedure ReadPort (Port : Longint; Var Value : Byte);oldfpccall;
  5051. {
  5052. Reads 'Value' from port 'Port'
  5053. }
  5054. begin
  5055. asm
  5056. movl port,%edx
  5057. inb %dx,%al
  5058. movl value,%edx
  5059. movb %al,(%edx)
  5060. end ['EAX','EDX'];
  5061. end;
  5062. Procedure ReadPort (Port : Longint; Var Value : Word);oldfpccall;
  5063. {
  5064. Reads 'Value' from port 'Port'
  5065. }
  5066. begin
  5067. asm
  5068. movl port,%edx
  5069. inw %dx,%ax
  5070. movl value,%edx
  5071. movw %ax,(%edx)
  5072. end ['EAX','EDX'];
  5073. end;
  5074. Procedure ReadPort (Port : Longint; Var Value : Longint);oldfpccall;
  5075. {
  5076. Reads 'Value' from port 'Port'
  5077. }
  5078. begin
  5079. asm
  5080. movl port,%edx
  5081. inl %dx,%eax
  5082. movl value,%edx
  5083. movl %eax,(%edx)
  5084. end ['EAX','EDX'];
  5085. end;
  5086. function ReadPortB (Port : Longint): Byte;oldfpccall; assembler;
  5087. {
  5088. Reads a byte from port 'Port'
  5089. }
  5090. asm
  5091. xorl %eax,%eax
  5092. movl port,%edx
  5093. inb %dx,%al
  5094. end ['EAX','EDX'];
  5095. function ReadPortW (Port : Longint): Word;oldfpccall; assembler;
  5096. {
  5097. Reads a word from port 'Port'
  5098. }
  5099. asm
  5100. xorl %eax,%eax
  5101. movl port,%edx
  5102. inw %dx,%ax
  5103. end ['EAX','EDX'];
  5104. function ReadPortL (Port : Longint): LongInt;oldfpccall; assembler;
  5105. {
  5106. Reads a LongInt from port 'Port'
  5107. }
  5108. asm
  5109. movl port,%edx
  5110. inl %dx,%eax
  5111. end ['EAX','EDX'];
  5112. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5113. {
  5114. Reads 'Count' longints from port 'Port' to 'Buf'.
  5115. }
  5116. begin
  5117. asm
  5118. movl count,%ecx
  5119. movl buf,%edi
  5120. movl port,%edx
  5121. cld
  5122. rep
  5123. insl
  5124. end ['ECX','EDI','EDX'];
  5125. end;
  5126. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5127. {
  5128. Reads 'Count' words from port 'Port' to 'Buf'.
  5129. }
  5130. begin
  5131. asm
  5132. movl count,%ecx
  5133. movl buf,%edi
  5134. movl port,%edx
  5135. cld
  5136. rep
  5137. insw
  5138. end ['ECX','EDI','EDX'];
  5139. end;
  5140. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);oldfpccall;
  5141. {
  5142. Reads 'Count' bytes from port 'Port' to 'Buf'.
  5143. }
  5144. begin
  5145. asm
  5146. movl count,%ecx
  5147. movl buf,%edi
  5148. movl port,%edx
  5149. cld
  5150. rep
  5151. insb
  5152. end ['ECX','EDI','EDX'];
  5153. end;
  5154. {--------------------------------
  5155. Memory functions
  5156. --------------------------------}
  5157. Initialization
  5158. InitLocalTime;
  5159. finalization
  5160. DoneLocalTime;
  5161. End.
  5162. {
  5163. $Log: linuxold.inc,v $
  5164. Revision 1.19 2005/02/14 17:13:31 peter
  5165. * truncate log
  5166. }