linux.pp 82 KB

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