linux.pp 84 KB

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