linux.pp 85 KB

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