linux.pp 72 KB

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