2
0

linux.pp 84 KB

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