linux.pp 82 KB

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