linux.pp 84 KB

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