linux.pp 80 KB

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