unix.pp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. { Get Types and Constants }
  16. {$i sysconst.inc}
  17. {$i systypes.inc}
  18. { Get System call numbers and error-numbers}
  19. {$i sysnr.inc}
  20. {$i errno.inc}
  21. {$I signal.inc}
  22. var
  23. ErrNo,
  24. LinuxError : Longint;
  25. {********************
  26. Process
  27. ********************}
  28. const
  29. {Checked for BSD using Linuxthreads port}
  30. { cloning flags }
  31. CSIGNAL = $000000ff; // signal mask to be sent at exit
  32. CLONE_VM = $00000100; // set if VM shared between processes
  33. CLONE_FS = $00000200; // set if fs info shared between processes
  34. CLONE_FILES = $00000400; // set if open files shared between processes
  35. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  36. CLONE_PID = $00001000; // set if pid shared
  37. type
  38. TCloneFunc=function(args:pointer):longint;cdecl;
  39. const
  40. { For getting/setting priority }
  41. Prio_Process = 0;
  42. Prio_PGrp = 1;
  43. Prio_User = 2;
  44. WNOHANG = $1;
  45. WUNTRACED = $2;
  46. __WCLONE = $80000000;
  47. {********************
  48. File
  49. ********************}
  50. Const
  51. P_IN = 1;
  52. P_OUT = 2;
  53. Const
  54. LOCK_SH = 1;
  55. LOCK_EX = 2;
  56. LOCK_UN = 8;
  57. LOCK_NB = 4;
  58. Type
  59. Tpipe = array[1..2] of longint;
  60. pglob = ^tglob;
  61. tglob = record
  62. name : pchar;
  63. next : pglob;
  64. end;
  65. ComStr = String[255];
  66. PathStr = String[255];
  67. DirStr = String[255];
  68. NameStr = String[255];
  69. ExtStr = String[255];
  70. const
  71. { For testing access rights }
  72. R_OK = 4;
  73. W_OK = 2;
  74. X_OK = 1;
  75. F_OK = 0;
  76. { For File control mechanism }
  77. F_GetFd = 1;
  78. F_SetFd = 2;
  79. F_GetFl = 3;
  80. F_SetFl = 4;
  81. F_GetLk = 5;
  82. F_SetLk = 6;
  83. F_SetLkW = 7;
  84. F_SetOwn = 8;
  85. F_GetOwn = 9;
  86. {********************
  87. IOCtl(TermIOS)
  88. ********************}
  89. {Is too freebsd/Linux specific}
  90. {$I termios.inc}
  91. {********************
  92. Info
  93. ********************}
  94. Type
  95. UTimBuf = packed record{in BSD array[0..1] of timeval, but this is
  96. backwards compatible with linux version}
  97. actime,
  98. {$ifdef BSD}
  99. uactime, {BSD Micro seconds}
  100. {$endif}
  101. modtime
  102. {$ifdef BSD}
  103. ,
  104. umodtime {BSD Micro seconds}
  105. {$endif}
  106. : longint;
  107. end;
  108. UTimeBuf=UTimBuf;
  109. TUTimeBuf=UTimeBuf;
  110. PUTimeBuf=^UTimeBuf;
  111. TSysinfo = packed record
  112. uptime : longint;
  113. loads : array[1..3] of longint;
  114. totalram,
  115. freeram,
  116. sharedram,
  117. bufferram,
  118. totalswap,
  119. freeswap : longint;
  120. procs : integer;
  121. s : string[18];
  122. end;
  123. PSysInfo = ^TSysInfo;
  124. {******************************************************************************
  125. Procedure/Functions
  126. ******************************************************************************}
  127. {$ifdef bsd}
  128. function Do_SysCall(sysnr:longint):longint;
  129. function Do_Syscall(sysnr,param1:integer):longint;
  130. function Do_SysCall(sysnr,param1:LONGINT):longint;
  131. function Do_SysCall(sysnr,param1,param2:LONGINT):longint;
  132. function Do_SysCall(sysnr,param1,param2,param3:LONGINT):longint;
  133. function Do_SysCall(sysnr,param1,param2,param3,param4:LONGINT):longint;
  134. function Do_SysCall(sysnr,param1,param2,param3,param4,param5:LONGINT):longint;
  135. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6:LONGINT):longint;
  136. function Do_SysCall(sysnr,param1,param2,param3,param4,param5,param6,param7:LONGINT):longint;
  137. {$else}
  138. Function SysCall(callnr:longint;var regs:SysCallregs):longint;
  139. {$endif}
  140. {**************************
  141. Time/Date Handling
  142. ***************************}
  143. var
  144. tzdaylight : boolean;
  145. tzseconds : longint;
  146. tzname : array[boolean] of pchar;
  147. { timezone support }
  148. procedure GetLocalTimezone(timer:longint;var leap_correct,leap_hit:longint);
  149. procedure GetLocalTimezone(timer:longint);
  150. procedure ReadTimezoneFile(fn:string);
  151. function GetTimezoneFile:string;
  152. Procedure GetTimeOfDay(var tv:timeval);
  153. Function GetTimeOfDay:longint;
  154. {$ifndef bsd}
  155. Function GetEpochTime: longint;
  156. {$endif}
  157. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  158. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  159. procedure GetTime(var hour,min,sec,msec,usec:word);
  160. procedure GetTime(var hour,min,sec,sec100:word);
  161. procedure GetTime(var hour,min,sec:word);
  162. Procedure GetDate(Var Year,Month,Day:Word);
  163. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  164. {**************************
  165. Process Handling
  166. ***************************}
  167. function CreateShellArgV(const prog:string):ppchar;
  168. function CreateShellArgV(const prog:Ansistring):ppchar;
  169. Procedure Execve(Path:pathstr;args:ppchar;ep:ppchar);
  170. Procedure Execve(path:pchar;args:ppchar;ep:ppchar);
  171. Procedure Execv(const path:pathstr;args:ppchar);
  172. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  173. Procedure Execl(const Todo:string);
  174. Procedure Execle(Todo:string;Ep:ppchar);
  175. Procedure Execlp(Todo:string;Ep:ppchar);
  176. Function Shell(const Command:String):Longint;
  177. Function Shell(const Command:AnsiString):Longint;
  178. Function Fork:longint;
  179. {Clone for FreeBSD is copied from the LinuxThread port, and rfork based}
  180. function Clone(func:TCloneFunc;sp:pointer;flags:longint;args:pointer):longint;
  181. Procedure ExitProcess(val:longint);
  182. Function WaitPid(Pid:longint;Status:pointer;Options:longint):Longint;
  183. Procedure Nice(N:integer);
  184. {$ifdef bsd}
  185. Function GetPriority(Which,Who:longint):longint;
  186. procedure SetPriority(Which,Who,What:longint);
  187. {$else}
  188. Function GetPriority(Which,Who:Integer):integer;
  189. Procedure SetPriority(Which:Integer;Who:Integer;What:Integer);
  190. {$endif}
  191. Function GetPid:LongInt;
  192. Function GetPPid:LongInt;
  193. Function GetUid:Longint;
  194. Function GetEUid:Longint;
  195. Function GetGid:Longint;
  196. Function GetEGid:Longint;
  197. {**************************
  198. File Handling
  199. ***************************}
  200. Function fdOpen(pathname:string;flags:longint):longint;
  201. Function fdOpen(pathname:string;flags,mode:longint):longint;
  202. Function fdOpen(pathname:pchar;flags:longint):longint;
  203. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  204. Function fdClose(fd:longint):boolean;
  205. Function fdRead(fd:longint;var buf;size:longint):longint;
  206. Function fdWrite(fd:longint;var buf;size:longint):longint;
  207. Function fdTruncate(fd,size:longint):boolean;
  208. Function fdSeek (fd,pos,seektype :longint): longint;
  209. Function fdFlush (fd : Longint) : Boolean;
  210. Function Link(OldPath,NewPath:pathstr):boolean;
  211. Function SymLink(OldPath,NewPath:pathstr):boolean;
  212. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  213. Function ReadLink(name:pathstr):pathstr;
  214. Function UnLink(Path:pathstr):boolean;
  215. Function UnLink(Path:pchar):Boolean;
  216. Function FReName (OldName,NewName : Pchar) : Boolean;
  217. Function FReName (OldName,NewName : String) : Boolean;
  218. Function Chown(path:pathstr;NewUid,NewGid:longint):boolean;
  219. Function Chmod(path:pathstr;Newmode:longint):boolean;
  220. Function Utime(path:pathstr;utim:utimebuf):boolean;
  221. {$ifdef BSD}
  222. Function Access(Path:Pathstr ;mode:longint):boolean;
  223. {$else}
  224. Function Access(Path:Pathstr ;mode:integer):boolean;
  225. {$endif}
  226. Function Umask(Mask:Integer):integer;
  227. Function Flock (fd,mode : longint) : boolean;
  228. Function Flock (var T : text;mode : longint) : boolean;
  229. Function Flock (var F : File;mode : longint) : boolean;
  230. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  231. Function FStat(Fd:longint;Var Info:stat):Boolean;
  232. Function FStat(var F:Text;Var Info:stat):Boolean;
  233. Function FStat(var F:File;Var Info:stat):Boolean;
  234. Function Lstat(Filename: PathStr;var Info:stat):Boolean;
  235. Function FSStat(Path:Pathstr;Var Info:statfs):Boolean;
  236. Function FSStat(Fd: Longint;Var Info:statfs):Boolean;
  237. {$ifdef bsd}
  238. Function Fcntl(Fd:longint;Cmd:longint):longint;
  239. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  240. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  241. Procedure Fcntl(var Fd:Text;Cmd:longint;Arg:Longint);
  242. {$else}
  243. Function Fcntl(Fd:longint;Cmd:Integer):integer;
  244. Procedure Fcntl(Fd:longint;Cmd:Integer;Arg:Longint);
  245. Function Fcntl(var Fd:Text;Cmd:Integer):integer;
  246. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  247. {$endif}
  248. Function Dup(oldfile:longint;var newfile:longint):Boolean;
  249. Function Dup(var oldfile,newfile:text):Boolean;
  250. Function Dup(var oldfile,newfile:file):Boolean;
  251. Function Dup2(oldfile,newfile:longint):Boolean;
  252. Function Dup2(var oldfile,newfile:text):Boolean;
  253. Function Dup2(var oldfile,newfile:file):Boolean;
  254. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:PTimeVal):longint;
  255. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  256. Function SelectText(var T:Text;TimeOut :PTimeVal):Longint;
  257. {**************************
  258. Directory Handling
  259. ***************************}
  260. Function OpenDir(f:pchar):pdir;
  261. Function OpenDir(f: String):pdir;
  262. function CloseDir(p:pdir):integer;
  263. Function ReadDir(p:pdir):pdirent;
  264. procedure SeekDir(p:pdir;off:longint);
  265. function TellDir(p:pdir):longint;
  266. {**************************
  267. Pipe/Fifo/Stream
  268. ***************************}
  269. Function AssignPipe(var pipe_in,pipe_out:longint):boolean;
  270. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  271. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  272. Function PClose(Var F:text) : longint;
  273. Function PClose(Var F:file) : longint;
  274. Procedure POpen(var F:text;const Prog:String;rw:char);
  275. Procedure POpen(var F:file;const Prog:String;rw:char);
  276. Function mkFifo(pathname:string;mode:longint):boolean;
  277. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  278. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  279. {**************************
  280. General information
  281. ***************************}
  282. Function GetEnv(P:string):Pchar;
  283. {$ifndef BSD}
  284. Function GetDomainName:String;
  285. Function GetHostName:String;
  286. Function Sysinfo(var Info:TSysinfo):Boolean;
  287. Function Uname(var unamerec:utsname):Boolean;
  288. {$endif}
  289. {**************************
  290. Signal
  291. ***************************}
  292. Procedure SigAction(Signum:longint;Act,OldAct:PSigActionRec );
  293. Procedure SigProcMask (How:longint;SSet,OldSSet:PSigSet);
  294. Function SigPending:SigSet;
  295. Procedure SigSuspend(Mask:Sigset);
  296. Function Signal(Signum:longint;Handler:SignalHandler):SignalHandler;
  297. Function Kill(Pid:longint;Sig:longint):integer;
  298. Procedure SigRaise(Sig:integer);
  299. {$ifndef BSD}
  300. Function Alarm(Sec : Longint) : longint;
  301. Procedure Pause;
  302. {$endif}
  303. Function NanoSleep(const req : timespec;var rem : timespec) : longint;
  304. {**************************
  305. IOCtl/Termios Functions
  306. ***************************}
  307. Function IOCtl(Handle,Ndx: Longint;Data: Pointer):boolean;
  308. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  309. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  310. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  311. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  312. Procedure CFMakeRaw(var tios:TermIOS);
  313. Function TCSendBreak(fd,duration:longint):boolean;
  314. Function TCSetPGrp(fd,id:longint):boolean;
  315. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  316. Function TCFlush(fd,qsel:longint):boolean;
  317. Function TCDrain(fd:longint):boolean;
  318. Function TCFlow(fd,act:longint):boolean;
  319. Function IsATTY(Handle:Longint):Boolean;
  320. Function IsATTY(f:text):Boolean;
  321. function TTYname(Handle:Longint):string;
  322. function TTYname(var F:Text):string;
  323. {**************************
  324. Memory functions
  325. ***************************}
  326. const
  327. PROT_READ = $1; { page can be read }
  328. PROT_WRITE = $2; { page can be written }
  329. PROT_EXEC = $4; { page can be executed }
  330. PROT_NONE = $0; { page can not be accessed }
  331. MAP_SHARED = $1; { Share changes }
  332. MAP_PRIVATE = $2; { Changes are private }
  333. MAP_TYPE = $f; { Mask for type of mapping }
  334. MAP_FIXED = $10; { Interpret addr exactly }
  335. MAP_ANONYMOUS = $20; { don't use a file }
  336. MAP_GROWSDOWN = $100; { stack-like segment }
  337. MAP_DENYWRITE = $800; { ETXTBSY }
  338. MAP_EXECUTABLE = $1000; { mark it as an executable }
  339. MAP_LOCKED = $2000; { pages are locked }
  340. MAP_NORESERVE = $4000; { don't check for reservations }
  341. type
  342. tmmapargs=record
  343. address : longint;
  344. size : longint;
  345. prot : longint;
  346. flags : longint;
  347. fd : longint;
  348. offset : longint;
  349. end;
  350. function MMap(const m:tmmapargs):longint;
  351. function MUnMap (P : Pointer; Size : Longint) : Boolean;
  352. {**************************
  353. Port IO functions
  354. ***************************}
  355. Function IOperm (From,Num : Cardinal; Value : Longint) : boolean;
  356. {$ifdef i386}
  357. Procedure WritePort (Port : Longint; Value : Byte);
  358. Procedure WritePort (Port : Longint; Value : Word);
  359. Procedure WritePort (Port : Longint; Value : Longint);
  360. Procedure WritePortB (Port : Longint; Value : Byte);
  361. Procedure WritePortW (Port : Longint; Value : Word);
  362. Procedure WritePortL (Port : Longint; Value : Longint);
  363. Procedure WritePortL (Port : Longint; Var Buf; Count: longint);
  364. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  365. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  366. Procedure ReadPort (Port : Longint; Var Value : Byte);
  367. Procedure ReadPort (Port : Longint; Var Value : Word);
  368. Procedure ReadPort (Port : Longint; Var Value : Longint);
  369. function ReadPortB (Port : Longint): Byte;
  370. function ReadPortW (Port : Longint): Word;
  371. function ReadPortL (Port : Longint): LongInt;
  372. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  373. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  374. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  375. {$endif}
  376. {**************************
  377. Utility functions
  378. ***************************}
  379. Function Octal(l:longint):longint;
  380. Function FExpand(Const Path: PathStr):PathStr;
  381. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  382. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  383. Function Dirname(Const path:pathstr):pathstr;
  384. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  385. Function FNMatch(const Pattern,Name:string):Boolean;
  386. Function Glob(Const path:pathstr):pglob;
  387. Procedure Globfree(var p:pglob);
  388. Function StringToPPChar(Var S:STring):ppchar;
  389. Function GetFS(var T:Text):longint;
  390. Function GetFS(Var F:File):longint;
  391. {Filedescriptorsets}
  392. Procedure FD_Zero(var fds:fdSet);
  393. Procedure FD_Clr(fd:longint;var fds:fdSet);
  394. Procedure FD_Set(fd:longint;var fds:fdSet);
  395. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  396. {Stat.Mode Types}
  397. Function S_ISLNK(m:word):boolean;
  398. Function S_ISREG(m:word):boolean;
  399. Function S_ISDIR(m:word):boolean;
  400. Function S_ISCHR(m:word):boolean;
  401. Function S_ISBLK(m:word):boolean;
  402. Function S_ISFIFO(m:word):boolean;
  403. Function S_ISSOCK(m:word):boolean;
  404. {******************************************************************************
  405. Implementation
  406. ******************************************************************************}
  407. Implementation
  408. Uses Strings;
  409. { Get the definitions of textrec and filerec }
  410. {$i textrec.inc}
  411. {$i filerec.inc}
  412. { Raw System calls are in Syscalls.inc}
  413. {$i syscalls.inc}
  414. {$ifdef BSD}
  415. {$i bsdsysca.inc}
  416. {$else}
  417. {$i linsysca.inc}
  418. {$endif}
  419. {******************************************************************************
  420. Process related calls
  421. ******************************************************************************}
  422. function CreateShellArgV(const prog:string):ppchar;
  423. {
  424. Create an argv which executes a command in a shell using /bin/sh -c
  425. }
  426. var
  427. pp,p : ppchar;
  428. temp : string;
  429. begin
  430. getmem(pp,4*4);
  431. temp:='/bin/sh'#0'-c'#0+prog+#0;
  432. p:=pp;
  433. p^:=@temp[1];
  434. inc(p);
  435. p^:=@temp[9];
  436. inc(p);
  437. p^:=@temp[12];
  438. inc(p);
  439. p^:=Nil;
  440. CreateShellArgV:=pp;
  441. end;
  442. function CreateShellArgV(const prog:Ansistring):ppchar;
  443. {
  444. Create an argv which executes a command in a shell using /bin/sh -c
  445. using a AnsiString;
  446. }
  447. var
  448. pp,p : ppchar;
  449. temp : AnsiString;
  450. begin
  451. getmem(pp,4*4);
  452. temp:='/bin/sh'#0'-c'#0+prog+#0;
  453. p:=pp;
  454. GetMem(p^,Length(Temp));
  455. Move(Temp[1],p^^,Length(Temp));
  456. inc(p);
  457. p^:=@pp[0][8];
  458. inc(p);
  459. p^:=@pp[0][11];
  460. inc(p);
  461. p^:=Nil;
  462. CreateShellArgV:=pp;
  463. end;
  464. Procedure Execv(const path:pathstr;args:ppchar);
  465. {
  466. Replaces the current program by the program specified in path,
  467. arguments in args are passed to Execve.
  468. the current environment is passed on.
  469. }
  470. begin
  471. Execve(path,args,envp); {On error linuxerror will get set there}
  472. end;
  473. Procedure Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar);
  474. {
  475. This does the same as Execve, only it searches the PATH environment
  476. for the place of the Executable, except when Path starts with a slash.
  477. if the PATH environment variable is unavailable, the path is set to '.'
  478. }
  479. var
  480. thepath : string;
  481. begin
  482. if path[1]<>'/' then
  483. begin
  484. Thepath:=strpas(getenv('PATH'));
  485. if thepath='' then
  486. thepath:='.';
  487. Path:=FSearch(path,thepath)
  488. end
  489. else
  490. Path:='';
  491. if Path='' then
  492. linuxerror:=Sys_enoent
  493. else
  494. Execve(Path,args,ep);{On error linuxerror will get set there}
  495. end;
  496. Procedure Execle(Todo:string;Ep:ppchar);
  497. {
  498. This procedure takes the string 'Todo', parses it for command and
  499. command options, and Executes the command with the given options.
  500. The string 'Todo' shoud be of the form 'command options', options
  501. separated by commas.
  502. the PATH environment is not searched for 'command'.
  503. The specified environment(in 'ep') is passed on to command
  504. }
  505. var
  506. p : ppchar;
  507. begin
  508. p:=StringToPPChar(ToDo);
  509. if (p=nil) or (p^=nil) then
  510. exit;
  511. ExecVE(p^,p,EP);
  512. end;
  513. Procedure Execl(const Todo:string);
  514. {
  515. This procedure takes the string 'Todo', parses it for command and
  516. command options, and Executes the command with the given options.
  517. The string 'Todo' shoud be of the form 'command options', options
  518. separated by commas.
  519. the PATH environment is not searched for 'command'.
  520. The current environment is passed on to command
  521. }
  522. begin
  523. ExecLE(ToDo,EnvP);
  524. end;
  525. Procedure Execlp(Todo:string;Ep:ppchar);
  526. {
  527. This procedure takes the string 'Todo', parses it for command and
  528. command options, and Executes the command with the given options.
  529. The string 'Todo' shoud be of the form 'command options', options
  530. separated by commas.
  531. the PATH environment is searched for 'command'.
  532. The specified environment (in 'ep') is passed on to command
  533. }
  534. var
  535. p : ppchar;
  536. begin
  537. p:=StringToPPchar(todo);
  538. if (p=nil) or (p^=nil) then
  539. exit;
  540. ExecVP(StrPas(p^),p,EP);
  541. end;
  542. Function Shell(const Command:String):Longint;
  543. {
  544. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  545. The current environment is passed to the shell.
  546. It waits for the shell to exit, and returns its exit status.
  547. If the Exec call failed exit status 127 is reported.
  548. }
  549. var
  550. p : ppchar;
  551. temp,pid : longint;
  552. begin
  553. pid:=fork;
  554. if pid=-1 then
  555. exit; {Linuxerror already set in Fork}
  556. if pid=0 then
  557. begin
  558. {This is the child.}
  559. p:=CreateShellArgv(command);
  560. Execve(p^,p,envp);
  561. exit(127);
  562. end;
  563. temp:=0;
  564. WaitPid(pid,@temp,0);{Linuxerror is set there}
  565. Shell:=temp;{ Return exit status }
  566. end;
  567. Function Shell(const Command:AnsiString):Longint;
  568. {
  569. AnsiString version of Shell
  570. }
  571. var
  572. p : ppchar;
  573. temp,pid : longint;
  574. begin
  575. pid:=fork;
  576. if pid=-1 then
  577. exit; {Linuxerror already set in Fork}
  578. if pid=0 then
  579. begin
  580. {This is the child.}
  581. p:=CreateShellArgv(command);
  582. Execve(p^,p,envp);
  583. exit(127);
  584. end;
  585. temp:=0;
  586. WaitPid(pid,@temp,0);{Linuxerror is set there}
  587. Shell:=temp;{ Return exit status }
  588. end;
  589. {******************************************************************************
  590. Date and Time related calls
  591. ******************************************************************************}
  592. Const
  593. {Date Translation}
  594. C1970=2440588;
  595. D0 = 1461;
  596. D1 = 146097;
  597. D2 =1721119;
  598. Function GregorianToJulian(Year,Month,Day:Longint):LongInt;
  599. Var
  600. Century,XYear: LongInt;
  601. Begin
  602. If Month<=2 Then
  603. Begin
  604. Dec(Year);
  605. Inc(Month,12);
  606. End;
  607. Dec(Month,3);
  608. Century:=(longint(Year Div 100)*D1) shr 2;
  609. XYear:=(longint(Year Mod 100)*D0) shr 2;
  610. GregorianToJulian:=((((Month*153)+2) div 5)+Day)+D2+XYear+Century;
  611. End;
  612. Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word);
  613. Var
  614. YYear,XYear,Temp,TempMonth : LongInt;
  615. Begin
  616. Temp:=((JulianDN-D2) shl 2)-1;
  617. JulianDN:=Temp Div D1;
  618. XYear:=(Temp Mod D1) or 3;
  619. YYear:=(XYear Div D0);
  620. Temp:=((((XYear mod D0)+4) shr 2)*5)-3;
  621. Day:=((Temp Mod 153)+5) Div 5;
  622. TempMonth:=Temp Div 153;
  623. If TempMonth>=10 Then
  624. Begin
  625. inc(YYear);
  626. dec(TempMonth,12);
  627. End;
  628. inc(TempMonth,3);
  629. Month := TempMonth;
  630. Year:=YYear+(JulianDN*100);
  631. end;
  632. Function GetEpochTime: longint;
  633. {
  634. Get the number of seconds since 00:00, January 1 1970, GMT
  635. the time NOT corrected any way
  636. }
  637. begin
  638. GetEpochTime:=GetTimeOfDay;
  639. end;
  640. Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word);
  641. {
  642. Transforms Epoch time into local time (hour, minute,seconds)
  643. }
  644. Var
  645. DateNum: LongInt;
  646. Begin
  647. inc(Epoch,TZSeconds);
  648. Datenum:=(Epoch Div 86400) + c1970;
  649. JulianToGregorian(DateNum,Year,Month,day);
  650. Epoch:=Epoch Mod 86400;
  651. Hour:=Epoch Div 3600;
  652. Epoch:=Epoch Mod 3600;
  653. Minute:=Epoch Div 60;
  654. Second:=Epoch Mod 60;
  655. End;
  656. Function LocalToEpoch(year,month,day,hour,minute,second:Word):Longint;
  657. {
  658. Transforms local time (year,month,day,hour,minutes,second) to Epoch time
  659. (seconds since 00:00, january 1 1970, corrected for local time zone)
  660. }
  661. Begin
  662. LocalToEpoch:=((GregorianToJulian(Year,Month,Day)-c1970)*86400)+
  663. (LongInt(Hour)*3600)+(Minute*60)+Second-TZSeconds;
  664. End;
  665. procedure GetTime(var hour,min,sec,msec,usec:word);
  666. {
  667. Gets the current time, adjusted to local time
  668. }
  669. var
  670. year,day,month:Word;
  671. t : timeval;
  672. begin
  673. gettimeofday(t);
  674. EpochToLocal(t.sec,year,month,day,hour,min,sec);
  675. msec:=t.usec div 1000;
  676. usec:=t.usec mod 1000;
  677. end;
  678. procedure GetTime(var hour,min,sec,sec100:word);
  679. {
  680. Gets the current time, adjusted to local time
  681. }
  682. var
  683. usec : word;
  684. begin
  685. gettime(hour,min,sec,sec100,usec);
  686. sec100:=sec100 div 10;
  687. end;
  688. Procedure GetTime(Var Hour,Min,Sec:Word);
  689. {
  690. Gets the current time, adjusted to local time
  691. }
  692. var
  693. msec,usec : Word;
  694. Begin
  695. gettime(hour,min,sec,msec,usec);
  696. End;
  697. Procedure GetDate(Var Year,Month,Day:Word);
  698. {
  699. Gets the current date, adjusted to local time
  700. }
  701. var
  702. hour,minute,second : word;
  703. Begin
  704. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  705. End;
  706. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  707. {
  708. Gets the current date, adjusted to local time
  709. }
  710. Begin
  711. EpochToLocal(GetTimeOfDay,year,month,day,hour,minute,second);
  712. End;
  713. { Include timezone handling routines which use /usr/share/timezone info }
  714. {$i timezone.inc}
  715. {******************************************************************************
  716. FileSystem calls
  717. ******************************************************************************}
  718. Function fdOpen(pathname:string;flags:longint):longint;
  719. begin
  720. pathname:=pathname+#0;
  721. fdOpen:=Sys_Open(@pathname[1],flags,438);
  722. LinuxError:=Errno;
  723. end;
  724. Function fdOpen(pathname:string;flags,mode:longint):longint;
  725. begin
  726. pathname:=pathname+#0;
  727. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  728. LinuxError:=Errno;
  729. end;
  730. Function fdOpen(pathname:pchar;flags:longint):longint;
  731. begin
  732. fdOpen:=Sys_Open(pathname,flags,0);
  733. LinuxError:=Errno;
  734. end;
  735. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  736. begin
  737. fdOpen:=Sys_Open(pathname,flags,mode);
  738. LinuxError:=Errno;
  739. end;
  740. Function fdClose(fd:longint):boolean;
  741. begin
  742. fdClose:=(Sys_Close(fd)=0);
  743. LinuxError:=Errno;
  744. end;
  745. Function fdRead(fd:longint;var buf;size:longint):longint;
  746. begin
  747. fdRead:=Sys_Read(fd,pchar(@buf),size);
  748. LinuxError:=Errno;
  749. end;
  750. Function fdWrite(fd:longint;var buf;size:longint):longint;
  751. begin
  752. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  753. LinuxError:=Errno;
  754. end;
  755. Function fdSeek (fd,pos,seektype :longint): longint;
  756. {
  757. Do a Seek on a file descriptor fd to position pos, starting from seektype
  758. }
  759. begin
  760. fdseek:=Sys_LSeek (fd,pos,seektype);
  761. LinuxError:=Errno;
  762. end;
  763. {$ifdef BSD}
  764. Function Fcntl(Fd:longint;Cmd:longint):longint;
  765. {
  766. Read or manipulate a file.(See also fcntl (2) )
  767. Possible values for Cmd are :
  768. F_GetFd,F_GetFl,F_GetOwn
  769. Errors are reported in Linuxerror;
  770. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  771. }
  772. begin
  773. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  774. begin
  775. Linuxerror:=sys_fcntl(fd,cmd,0);
  776. if linuxerror=-1 then
  777. begin
  778. linuxerror:=errno;
  779. fcntl:=0;
  780. end
  781. else
  782. begin
  783. fcntl:=linuxerror;
  784. linuxerror:=0;
  785. end;
  786. end
  787. else
  788. begin
  789. linuxerror:=Sys_einval;
  790. Fcntl:=0;
  791. end;
  792. end;
  793. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  794. {
  795. Read or manipulate a file. (See also fcntl (2) )
  796. Possible values for Cmd are :
  797. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  798. Errors are reported in Linuxerror;
  799. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  800. F_DupFD is not allowed, due to the structure of Files in Pascal.
  801. }
  802. begin
  803. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  804. begin
  805. sys_fcntl(fd,cmd,arg);
  806. LinuxError:=ErrNo;
  807. end
  808. else
  809. linuxerror:=Sys_einval;
  810. end;
  811. {$endif}
  812. {$ifdef BSD}
  813. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  814. {$else}
  815. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  816. {$endif}
  817. begin
  818. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  819. end;
  820. {$ifdef BSD}
  821. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  822. {$else}
  823. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  824. {$endif}
  825. begin
  826. Fcntl(textrec(Fd).handle, Cmd, Arg);
  827. end;
  828. Function Flock (var T : text;mode : longint) : boolean;
  829. begin
  830. Flock:=Flock(TextRec(T).Handle,mode);
  831. end;
  832. Function Flock (var F : File;mode : longint) : boolean;
  833. begin
  834. Flock:=Flock(FileRec(F).Handle,mode);
  835. end;
  836. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  837. {
  838. Get all information on a file, and return it in Info.
  839. }
  840. begin
  841. path:=path+#0;
  842. FStat:=(Sys_stat(@(path[1]),Info)=0);
  843. LinuxError:=errno;
  844. end;
  845. Function FStat(var F:Text;Var Info:stat):Boolean;
  846. {
  847. Get all information on a text file, and return it in info.
  848. }
  849. begin
  850. FStat:=Fstat(TextRec(F).Handle,INfo);
  851. end;
  852. Function FStat(var F:File;Var Info:stat):Boolean;
  853. {
  854. Get all information on a untyped file, and return it in info.
  855. }
  856. begin
  857. FStat:=Fstat(FileRec(F).Handle,Info);
  858. end;
  859. Function SymLink(OldPath,newPath:pathstr):boolean;
  860. {
  861. Proceduces a soft link from new to old.
  862. }
  863. begin
  864. oldpath:=oldpath+#0;
  865. newpath:=newpath+#0;
  866. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  867. linuxerror:=errno;
  868. end;
  869. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  870. {
  871. Read a link (where it points to)
  872. }
  873. begin
  874. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  875. linuxerror:=errno;
  876. end;
  877. Function ReadLink(Name:pathstr):pathstr;
  878. {
  879. Read a link (where it points to)
  880. }
  881. var
  882. LinkName : pathstr;
  883. i : longint;
  884. begin
  885. Name:=Name+#0;
  886. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  887. if i>0 then
  888. begin
  889. linkname[0]:=chr(i);
  890. ReadLink:=LinkName;
  891. end
  892. else
  893. ReadLink:='';
  894. end;
  895. Function UnLink(Path:pathstr):boolean;
  896. {
  897. Removes the file in 'Path' (that is, it decreases the link count with one.
  898. if the link count is zero, the file is removed from the disk.
  899. }
  900. begin
  901. path:=path+#0;
  902. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  903. linuxerror:=errno;
  904. end;
  905. Function UnLink(Path:pchar):Boolean;
  906. {
  907. Removes the file in 'Path' (that is, it decreases the link count with one.
  908. if the link count is zero, the file is removed from the disk.
  909. }
  910. begin
  911. Unlink:=(Sys_unlink(path)=0);
  912. linuxerror:=errno;
  913. end;
  914. Function FRename (OldName,NewName : Pchar) : Boolean;
  915. begin
  916. FRename:=Sys_rename(OldName,NewName)=0;
  917. LinuxError:=Errno;
  918. end;
  919. Function FRename (OldName,NewName : String) : Boolean;
  920. begin
  921. OldName:=OldName+#0;
  922. NewName:=NewName+#0;
  923. FRename:=FRename (@OldName[1],@NewName[1]);
  924. end;
  925. Function Dup(var oldfile,newfile:text):Boolean;
  926. {
  927. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  928. oldfile.
  929. After which the two textfiles are, in effect, the same, except
  930. that they don't share the same buffer, and don't share the same
  931. close_on_exit flag.
  932. }
  933. begin
  934. flush(oldfile);{ We cannot share buffers, so we flush them. }
  935. textrec(newfile):=textrec(oldfile);
  936. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  937. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  938. end;
  939. Function Dup(var oldfile,newfile:file):Boolean;
  940. {
  941. Copies the filedescriptor oldfile to newfile
  942. }
  943. begin
  944. filerec(newfile):=filerec(oldfile);
  945. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  946. end;
  947. Function Dup2(var oldfile,newfile:text):Boolean;
  948. {
  949. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  950. oldfile. It closes newfile if it was still open.
  951. After which the two textfiles are, in effect, the same, except
  952. that they don't share the same buffer, and don't share the same
  953. close_on_exit flag.
  954. }
  955. var
  956. tmphandle : word;
  957. begin
  958. case TextRec(oldfile).mode of
  959. fmOutput, fmInOut, fmAppend :
  960. flush(oldfile);{ We cannot share buffers, so we flush them. }
  961. end;
  962. case TextRec(newfile).mode of
  963. fmOutput, fmInOut, fmAppend :
  964. flush(newfile);
  965. end;
  966. tmphandle:=textrec(newfile).handle;
  967. textrec(newfile):=textrec(oldfile);
  968. textrec(newfile).handle:=tmphandle;
  969. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  970. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  971. end;
  972. Function Dup2(var oldfile,newfile:file):Boolean;
  973. {
  974. Copies the filedescriptor oldfile to newfile
  975. }
  976. begin
  977. filerec(newfile):=filerec(oldfile);
  978. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  979. end;
  980. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  981. {
  982. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  983. have changed.
  984. This function allows specification of a timeout as a longint.
  985. }
  986. var
  987. p : PTimeVal;
  988. tv : TimeVal;
  989. begin
  990. if TimeOut=-1 then
  991. p:=nil
  992. else
  993. begin
  994. tv.Sec:=Timeout div 1000;
  995. tv.Usec:=(Timeout mod 1000)*1000;
  996. p:=@tv;
  997. end;
  998. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  999. end;
  1000. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1001. Var
  1002. F:FDSet;
  1003. begin
  1004. if textrec(t).mode=fmclosed then
  1005. begin
  1006. LinuxError:=Sys_EBADF;
  1007. exit(-1);
  1008. end;
  1009. FD_Zero(f);
  1010. FD_Set(textrec(T).handle,f);
  1011. if textrec(T).mode=fminput then
  1012. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1013. else
  1014. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1015. end;
  1016. {******************************************************************************
  1017. Directory
  1018. ******************************************************************************}
  1019. Function OpenDir(F:String):PDir;
  1020. begin
  1021. F:=F+#0;
  1022. OpenDir:=OpenDir(@F[1]);
  1023. end;
  1024. procedure SeekDir(p:pdir;off:longint);
  1025. begin
  1026. if p=nil then
  1027. begin
  1028. errno:=Sys_EBADF;
  1029. exit;
  1030. end;
  1031. {$ifndef bsd}
  1032. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1033. {$endif}
  1034. p^.size:=0;
  1035. p^.loc:=0;
  1036. end;
  1037. function TellDir(p:pdir):longint;
  1038. begin
  1039. if p=nil then
  1040. begin
  1041. errno:=Sys_EBADF;
  1042. telldir:=-1;
  1043. exit;
  1044. end;
  1045. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1046. { We could try to use the nextoff field here, but on my 1.2.13
  1047. kernel, this gives nothing... This may have to do with
  1048. the readdir implementation of libc... I also didn't find any trace of
  1049. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1050. Michael. }
  1051. end;
  1052. Function ReadDir(P:pdir):pdirent;
  1053. begin
  1054. ReadDir:=Sys_ReadDir(p);
  1055. LinuxError:=Errno;
  1056. end;
  1057. {******************************************************************************
  1058. Pipes/Fifo
  1059. ******************************************************************************}
  1060. Procedure OpenPipe(var F:Text);
  1061. begin
  1062. case textrec(f).mode of
  1063. fmoutput :
  1064. if textrec(f).userdata[1]<>P_OUT then
  1065. textrec(f).mode:=fmclosed;
  1066. fminput :
  1067. if textrec(f).userdata[1]<>P_IN then
  1068. textrec(f).mode:=fmclosed;
  1069. else
  1070. textrec(f).mode:=fmclosed;
  1071. end;
  1072. end;
  1073. Procedure IOPipe(var F:text);
  1074. begin
  1075. case textrec(f).mode of
  1076. fmoutput :
  1077. begin
  1078. { first check if we need something to write, else we may
  1079. get a SigPipe when Close() is called (PFV) }
  1080. if textrec(f).bufpos>0 then
  1081. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1082. end;
  1083. fminput :
  1084. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1085. end;
  1086. textrec(f).bufpos:=0;
  1087. end;
  1088. Procedure FlushPipe(var F:Text);
  1089. begin
  1090. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1091. IOPipe(f);
  1092. textrec(f).bufpos:=0;
  1093. end;
  1094. Procedure ClosePipe(var F:text);
  1095. begin
  1096. textrec(f).mode:=fmclosed;
  1097. Sys_close(textrec(f).handle);
  1098. end;
  1099. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1100. {
  1101. Sets up a pair of file variables, which act as a pipe. The first one can
  1102. be read from, the second one can be written to.
  1103. If the operation was unsuccesful, linuxerror is set.
  1104. }
  1105. var
  1106. f_in,f_out : longint;
  1107. begin
  1108. if not AssignPipe(f_in,f_out) then
  1109. begin
  1110. AssignPipe:=false;
  1111. exit;
  1112. end;
  1113. { Set up input }
  1114. Assign(Pipe_in,'');
  1115. Textrec(Pipe_in).Handle:=f_in;
  1116. Textrec(Pipe_in).Mode:=fmInput;
  1117. Textrec(Pipe_in).userdata[1]:=P_IN;
  1118. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1119. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1120. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1121. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1122. { Set up output }
  1123. Assign(Pipe_out,'');
  1124. Textrec(Pipe_out).Handle:=f_out;
  1125. Textrec(Pipe_out).Mode:=fmOutput;
  1126. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1127. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1128. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1129. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1130. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1131. AssignPipe:=true;
  1132. end;
  1133. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1134. {
  1135. Sets up a pair of file variables, which act as a pipe. The first one can
  1136. be read from, the second one can be written to.
  1137. If the operation was unsuccesful, linuxerror is set.
  1138. }
  1139. var
  1140. f_in,f_out : longint;
  1141. begin
  1142. if not AssignPipe(f_in,f_out) then
  1143. begin
  1144. AssignPipe:=false;
  1145. exit;
  1146. end;
  1147. { Set up input }
  1148. Assign(Pipe_in,'');
  1149. Filerec(Pipe_in).Handle:=f_in;
  1150. Filerec(Pipe_in).Mode:=fmInput;
  1151. Filerec(Pipe_in).recsize:=1;
  1152. Filerec(Pipe_in).userdata[1]:=P_IN;
  1153. { Set up output }
  1154. Assign(Pipe_out,'');
  1155. Filerec(Pipe_out).Handle:=f_out;
  1156. Filerec(Pipe_out).Mode:=fmoutput;
  1157. Filerec(Pipe_out).recsize:=1;
  1158. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1159. AssignPipe:=true;
  1160. end;
  1161. Procedure PCloseText(Var F:text);
  1162. {
  1163. May not use @PClose due overloading
  1164. }
  1165. begin
  1166. PClose(f);
  1167. end;
  1168. Procedure POpen(var F:text;const Prog:String;rw:char);
  1169. {
  1170. Starts the program in 'Prog' and makes it's input or out put the
  1171. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1172. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1173. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1174. read from 'f'.
  1175. }
  1176. var
  1177. pipi,
  1178. pipo : text;
  1179. pid : longint;
  1180. pl : ^longint;
  1181. pp : ppchar;
  1182. begin
  1183. LinuxError:=0;
  1184. rw:=upcase(rw);
  1185. if not (rw in ['R','W']) then
  1186. begin
  1187. LinuxError:=Sys_enoent;
  1188. exit;
  1189. end;
  1190. AssignPipe(pipi,pipo);
  1191. if Linuxerror<>0 then
  1192. exit;
  1193. pid:=fork;
  1194. if linuxerror<>0 then
  1195. begin
  1196. close(pipi);
  1197. close(pipo);
  1198. exit;
  1199. end;
  1200. if pid=0 then
  1201. begin
  1202. { We're in the child }
  1203. if rw='W' then
  1204. begin
  1205. close(pipo);
  1206. dup2(pipi,input);
  1207. close(pipi);
  1208. if linuxerror<>0 then
  1209. halt(127);
  1210. end
  1211. else
  1212. begin
  1213. close(pipi);
  1214. dup2(pipo,output);
  1215. close(pipo);
  1216. if linuxerror<>0 then
  1217. halt(127);
  1218. end;
  1219. pp:=createshellargv(prog);
  1220. Execve(pp^,pp,envp);
  1221. halt(127);
  1222. end
  1223. else
  1224. begin
  1225. { We're in the parent }
  1226. if rw='W' then
  1227. begin
  1228. close(pipi);
  1229. f:=pipo;
  1230. textrec(f).bufptr:=@textrec(f).buffer;
  1231. end
  1232. else
  1233. begin
  1234. close(pipo);
  1235. f:=pipi;
  1236. textrec(f).bufptr:=@textrec(f).buffer;
  1237. end;
  1238. {Save the process ID - needed when closing }
  1239. pl:=@(textrec(f).userdata[2]);
  1240. pl^:=pid;
  1241. textrec(f).closefunc:=@PCloseText;
  1242. end;
  1243. end;
  1244. Procedure POpen(var F:file;const Prog:String;rw:char);
  1245. {
  1246. Starts the program in 'Prog' and makes it's input or out put the
  1247. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1248. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1249. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1250. read from 'f'.
  1251. }
  1252. var
  1253. pipi,
  1254. pipo : file;
  1255. pid : longint;
  1256. pl : ^longint;
  1257. p,pp : ppchar;
  1258. temp : string[255];
  1259. begin
  1260. LinuxError:=0;
  1261. rw:=upcase(rw);
  1262. if not (rw in ['R','W']) then
  1263. begin
  1264. LinuxError:=Sys_enoent;
  1265. exit;
  1266. end;
  1267. AssignPipe(pipi,pipo);
  1268. if Linuxerror<>0 then
  1269. exit;
  1270. pid:=fork;
  1271. if linuxerror<>0 then
  1272. begin
  1273. close(pipi);
  1274. close(pipo);
  1275. exit;
  1276. end;
  1277. if pid=0 then
  1278. begin
  1279. { We're in the child }
  1280. if rw='W' then
  1281. begin
  1282. close(pipo);
  1283. dup2(filerec(pipi).handle,stdinputhandle);
  1284. close(pipi);
  1285. if linuxerror<>0 then
  1286. halt(127);
  1287. end
  1288. else
  1289. begin
  1290. close(pipi);
  1291. dup2(filerec(pipo).handle,stdoutputhandle);
  1292. close(pipo);
  1293. if linuxerror<>0 then
  1294. halt(127);
  1295. end;
  1296. getmem(pp,sizeof(pchar)*4);
  1297. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1298. p:=pp;
  1299. p^:=@temp[1];
  1300. inc(p);
  1301. p^:=@temp[9];
  1302. inc(p);
  1303. p^:=@temp[12];
  1304. inc(p);
  1305. p^:=Nil;
  1306. Execve('/bin/sh',pp,envp);
  1307. halt(127);
  1308. end
  1309. else
  1310. begin
  1311. { We're in the parent }
  1312. if rw='W' then
  1313. begin
  1314. close(pipi);
  1315. f:=pipo;
  1316. end
  1317. else
  1318. begin
  1319. close(pipo);
  1320. f:=pipi;
  1321. end;
  1322. {Save the process ID - needed when closing }
  1323. pl:=@(filerec(f).userdata[2]);
  1324. pl^:=pid;
  1325. end;
  1326. end;
  1327. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1328. {
  1329. Starts the program in 'Prog' and makes its input and output the
  1330. other end of two pipes, which are the stdin and stdout of a program
  1331. specified in 'Prog'.
  1332. streamout can be used to write to the program, streamin can be used to read
  1333. the output of the program. See the following diagram :
  1334. Parent Child
  1335. STreamout --> Input
  1336. Streamin <-- Output
  1337. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1338. }
  1339. var
  1340. pipi,
  1341. pipo : text;
  1342. pid : longint;
  1343. pl : ^Longint;
  1344. begin
  1345. LinuxError:=0;
  1346. AssignStream:=-1;
  1347. AssignPipe(streamin,pipo);
  1348. if Linuxerror<>0 then
  1349. exit;
  1350. AssignPipe(pipi,streamout);
  1351. if Linuxerror<>0 then
  1352. exit;
  1353. pid:=fork;
  1354. if linuxerror<>0 then
  1355. begin
  1356. close(pipi);
  1357. close(pipo);
  1358. close (streamin);
  1359. close (streamout);
  1360. exit;
  1361. end;
  1362. if pid=0 then
  1363. begin
  1364. { We're in the child }
  1365. { Close what we don't need }
  1366. close(streamout);
  1367. close(streamin);
  1368. dup2(pipi,input);
  1369. if linuxerror<>0 then
  1370. halt(127);
  1371. close(pipi);
  1372. dup2(pipo,output);
  1373. if linuxerror<>0 then
  1374. halt (127);
  1375. close(pipo);
  1376. Execl(Prog);
  1377. halt(127);
  1378. end
  1379. else
  1380. begin
  1381. { we're in the parent}
  1382. close(pipo);
  1383. close(pipi);
  1384. {Save the process ID - needed when closing }
  1385. pl:=@(textrec(StreamIn).userdata[2]);
  1386. pl^:=pid;
  1387. textrec(StreamIn).closefunc:=@PCloseText;
  1388. {Save the process ID - needed when closing }
  1389. pl:=@(textrec(StreamOut).userdata[2]);
  1390. pl^:=pid;
  1391. textrec(StreamOut).closefunc:=@PCloseText;
  1392. AssignStream:=Pid;
  1393. end;
  1394. end;
  1395. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1396. {
  1397. Starts the program in 'prog' and makes its input, output and error output the
  1398. other end of three pipes, which are the stdin, stdout and stderr of a program
  1399. specified in 'prog'.
  1400. StreamOut can be used to write to the program, StreamIn can be used to read
  1401. the output of the program, StreamErr reads the error output of the program.
  1402. See the following diagram :
  1403. Parent Child
  1404. StreamOut --> StdIn (input)
  1405. StreamIn <-- StdOut (output)
  1406. StreamErr <-- StdErr (error output)
  1407. }
  1408. var
  1409. PipeIn, PipeOut, PipeErr: text;
  1410. pid: LongInt;
  1411. pl: ^LongInt;
  1412. begin
  1413. LinuxError := 0;
  1414. AssignStream := -1;
  1415. // Assign pipes
  1416. AssignPipe(StreamIn, PipeOut);
  1417. if LinuxError <> 0 then exit;
  1418. AssignPipe(StreamErr, PipeErr);
  1419. if LinuxError <> 0 then begin
  1420. Close(StreamIn);
  1421. Close(PipeOut);
  1422. exit;
  1423. end;
  1424. AssignPipe(PipeIn, StreamOut);
  1425. if LinuxError <> 0 then begin
  1426. Close(StreamIn);
  1427. Close(PipeOut);
  1428. Close(StreamErr);
  1429. Close(PipeErr);
  1430. exit;
  1431. end;
  1432. // Fork
  1433. pid := Fork;
  1434. if LinuxError <> 0 then begin
  1435. Close(StreamIn);
  1436. Close(PipeOut);
  1437. Close(StreamErr);
  1438. Close(PipeErr);
  1439. Close(PipeIn);
  1440. Close(StreamOut);
  1441. exit;
  1442. end;
  1443. if pid = 0 then begin
  1444. // *** We are in the child ***
  1445. // Close what we don not need
  1446. Close(StreamOut);
  1447. Close(StreamIn);
  1448. Close(StreamErr);
  1449. // Connect pipes
  1450. dup2(PipeIn, Input);
  1451. if LinuxError <> 0 then Halt(127);
  1452. Close(PipeIn);
  1453. dup2(PipeOut, Output);
  1454. if LinuxError <> 0 then Halt(127);
  1455. Close(PipeOut);
  1456. dup2(PipeErr, StdErr);
  1457. if LinuxError <> 0 then Halt(127);
  1458. Close(PipeErr);
  1459. // Execute program
  1460. Execl(Prog);
  1461. Halt(127);
  1462. end else begin
  1463. // *** We are in the parent ***
  1464. Close(PipeErr);
  1465. Close(PipeOut);
  1466. Close(PipeIn);
  1467. // Save the process ID - needed when closing
  1468. pl := @(TextRec(StreamIn).userdata[2]);
  1469. pl^ := pid;
  1470. TextRec(StreamIn).closefunc := @PCloseText;
  1471. // Save the process ID - needed when closing
  1472. pl := @(TextRec(StreamOut).userdata[2]);
  1473. pl^ := pid;
  1474. TextRec(StreamOut).closefunc := @PCloseText;
  1475. // Save the process ID - needed when closing
  1476. pl := @(TextRec(StreamErr).userdata[2]);
  1477. pl^ := pid;
  1478. TextRec(StreamErr).closefunc := @PCloseText;
  1479. AssignStream := pid;
  1480. end;
  1481. end;
  1482. {******************************************************************************
  1483. General information calls
  1484. ******************************************************************************}
  1485. Function GetEnv(P:string):Pchar;
  1486. {
  1487. Searches the environment for a string with name p and
  1488. returns a pchar to it's value.
  1489. A pchar is used to accomodate for strings of length > 255
  1490. }
  1491. var
  1492. ep : ppchar;
  1493. found : boolean;
  1494. Begin
  1495. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1496. ep:=envp;
  1497. found:=false;
  1498. if ep<>nil then
  1499. begin
  1500. while (not found) and (ep^<>nil) do
  1501. begin
  1502. if strlcomp(@p[1],(ep^),length(p))=0 then
  1503. found:=true
  1504. else
  1505. inc(ep);
  1506. end;
  1507. end;
  1508. if found then
  1509. getenv:=ep^+length(p)
  1510. else
  1511. getenv:=nil;
  1512. end;
  1513. {$ifndef bsd}
  1514. Function GetDomainName:String;
  1515. {
  1516. Get machines domain name. Returns empty string if not set.
  1517. }
  1518. Var
  1519. Sysn : utsname;
  1520. begin
  1521. Uname(Sysn);
  1522. linuxerror:=errno;
  1523. If linuxerror<>0 then
  1524. getdomainname:=''
  1525. else
  1526. getdomainname:=strpas(@Sysn.domainname[0]);
  1527. end;
  1528. Function GetHostName:String;
  1529. {
  1530. Get machines name. Returns empty string if not set.
  1531. }
  1532. Var
  1533. Sysn : utsname;
  1534. begin
  1535. uname(Sysn);
  1536. linuxerror:=errno;
  1537. If linuxerror<>0 then
  1538. gethostname:=''
  1539. else
  1540. gethostname:=strpas(@Sysn.nodename[0]);
  1541. end;
  1542. {$endif}
  1543. {******************************************************************************
  1544. Signal handling calls
  1545. ******************************************************************************}
  1546. procedure SigRaise(sig:integer);
  1547. begin
  1548. Kill(GetPid,Sig);
  1549. end;
  1550. {******************************************************************************
  1551. IOCtl and Termios calls
  1552. ******************************************************************************}
  1553. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1554. begin
  1555. {$ifndef BSD}
  1556. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1557. {$else}
  1558. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1559. {$endif}
  1560. end;
  1561. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  1562. var
  1563. nr:longint;
  1564. begin
  1565. {$ifndef BSD}
  1566. case OptAct of
  1567. TCSANOW : nr:=TCSETS;
  1568. TCSADRAIN : nr:=TCSETSW;
  1569. TCSAFLUSH : nr:=TCSETSF;
  1570. {$else}
  1571. case OptAct of
  1572. TCSANOW : nr:=TIOCSETA;
  1573. TCSADRAIN : nr:=TIOCSETAW;
  1574. TCSAFLUSH : nr:=TIOCSETAF;
  1575. {$endif}
  1576. else
  1577. begin
  1578. ErrNo:=Sys_EINVAL;
  1579. TCSetAttr:=false;
  1580. exit;
  1581. end;
  1582. end;
  1583. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1584. end;
  1585. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1586. begin
  1587. {$ifndef BSD}
  1588. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1589. {$else}
  1590. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1591. {$endif}
  1592. end;
  1593. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1594. begin
  1595. {$ifndef BSD}
  1596. CFSetISpeed(tios,speed);
  1597. {$else}
  1598. tios.c_ospeed:=speed;
  1599. {$endif}
  1600. end;
  1601. Procedure CFMakeRaw(var tios:TermIOS);
  1602. begin
  1603. {$ifndef BSD}
  1604. with tios do
  1605. begin
  1606. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1607. INLCR or IGNCR or ICRNL or IXON));
  1608. c_oflag:=c_oflag and (not OPOST);
  1609. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1610. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1611. end;
  1612. {$else}
  1613. with tios do
  1614. begin
  1615. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1616. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1617. IGNPAR));
  1618. c_iflag:=c_iflag OR IGNBRK;
  1619. c_oflag:=c_oflag and (not OPOST);
  1620. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1621. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1622. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1623. c_cc[VMIN]:=1;
  1624. c_cc[VTIME]:=0;
  1625. end;
  1626. {$endif}
  1627. end;
  1628. Function TCSendBreak(fd,duration:longint):boolean;
  1629. begin
  1630. {$ifndef BSD}
  1631. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1632. {$else}
  1633. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1634. {$endif}
  1635. end;
  1636. Function TCSetPGrp(fd,id:longint):boolean;
  1637. begin
  1638. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1639. end;
  1640. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1641. begin
  1642. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1643. end;
  1644. Function TCDrain(fd:longint):boolean;
  1645. begin
  1646. {$ifndef BSD}
  1647. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1648. {$else}
  1649. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1650. {$endif}
  1651. end;
  1652. Function TCFlow(fd,act:longint):boolean;
  1653. begin
  1654. {$ifndef BSD}
  1655. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1656. {$else}
  1657. case act OF
  1658. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1659. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1660. TCIOFF : {N/I}
  1661. end;
  1662. {$endif}
  1663. end;
  1664. Function TCFlush(fd,qsel:longint):boolean;
  1665. {var com:longint;}
  1666. begin
  1667. {$ifndef BSD}
  1668. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1669. {$else}
  1670. {
  1671. CASE Qsel of
  1672. TCIFLUSH : com:=fread;
  1673. TCOFLUSH : com:=FWRITE;
  1674. TCIOFLUSH: com:=FREAD OR FWRITE;
  1675. else
  1676. exit(false);
  1677. end;
  1678. }
  1679. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1680. {$endif}
  1681. end;
  1682. Function IsATTY(Handle:Longint):Boolean;
  1683. {
  1684. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1685. }
  1686. var
  1687. t : Termios;
  1688. begin
  1689. IsAtty:=TCGetAttr(Handle,t);
  1690. end;
  1691. Function IsATTY(f: text):Boolean;
  1692. {
  1693. Idem as previous, only now for text variables.
  1694. }
  1695. begin
  1696. IsATTY:=IsaTTY(textrec(f).handle);
  1697. end;
  1698. function TTYName(Handle:Longint):string;
  1699. {
  1700. Return the name of the current tty described by handle f.
  1701. returns empty string in case of an error.
  1702. }
  1703. var
  1704. mydev,
  1705. myino : longint;
  1706. st : stat;
  1707. function mysearch(n:string): boolean;
  1708. {searches recursively for the device in the directory given by n,
  1709. returns true if found and sets the name of the device in ttyname}
  1710. var dirstream : pdir;
  1711. d : pdirent;
  1712. name : string;
  1713. st : stat;
  1714. begin
  1715. dirstream:=opendir(n);
  1716. if (linuxerror<>0) then
  1717. exit;
  1718. d:=Readdir(dirstream);
  1719. while (d<>nil) do
  1720. begin
  1721. name:=n+'/'+strpas(@(d^.name));
  1722. fstat(name,st);
  1723. if linuxerror=0 then
  1724. begin
  1725. if ((st.mode and $E000)=$4000) and { if it is a directory }
  1726. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  1727. (strpas(@(d^.name))<>'..') and
  1728. (strpas(@(d^.name))<>'fd') then
  1729. begin {we found a directory, search inside it}
  1730. if mysearch(name) then
  1731. begin {the device is here}
  1732. closedir(dirstream); {then don't continue searching}
  1733. mysearch:=true;
  1734. exit;
  1735. end;
  1736. end
  1737. else if (d^.ino=myino) and (st.dev=mydev) then
  1738. begin
  1739. closedir(dirstream);
  1740. ttyname:=name;
  1741. mysearch:=true;
  1742. exit;
  1743. end;
  1744. end;
  1745. d:=Readdir(dirstream);
  1746. end;
  1747. closedir(dirstream);
  1748. mysearch:=false;
  1749. end;
  1750. begin
  1751. TTYName:='';
  1752. fstat(handle,st);
  1753. if (errno<>0) and isatty (handle) then
  1754. exit;
  1755. mydev:=st.dev;
  1756. myino:=st.ino;
  1757. mysearch('/dev');
  1758. end;
  1759. function TTYName(var F:Text):string;
  1760. {
  1761. Idem as previous, only now for text variables;
  1762. }
  1763. begin
  1764. TTYName:=TTYName(textrec(f).handle);
  1765. end;
  1766. {******************************************************************************
  1767. Utility calls
  1768. ******************************************************************************}
  1769. Function Octal(l:longint):longint;
  1770. {
  1771. Convert an octal specified number to decimal;
  1772. }
  1773. var
  1774. octnr,
  1775. oct : longint;
  1776. begin
  1777. octnr:=0;
  1778. oct:=0;
  1779. while (l>0) do
  1780. begin
  1781. oct:=oct or ((l mod 10) shl octnr);
  1782. l:=l div 10;
  1783. inc(octnr,3);
  1784. end;
  1785. Octal:=oct;
  1786. end;
  1787. Function StringToPPChar(Var S:STring):ppchar;
  1788. {
  1789. Create a PPChar to structure of pchars which are the arguments specified
  1790. in the string S. Especially usefull for creating an ArgV for Exec-calls
  1791. }
  1792. var
  1793. nr : longint;
  1794. Buf : ^char;
  1795. p : ppchar;
  1796. begin
  1797. s:=s+#0;
  1798. buf:=@s[1];
  1799. nr:=0;
  1800. while(buf^<>#0) do
  1801. begin
  1802. while (buf^ in [' ',#8,#10]) do
  1803. inc(buf);
  1804. inc(nr);
  1805. while not (buf^ in [' ',#0,#8,#10]) do
  1806. inc(buf);
  1807. end;
  1808. getmem(p,nr*4);
  1809. StringToPPChar:=p;
  1810. if p=nil then
  1811. begin
  1812. LinuxError:=sys_enomem;
  1813. exit;
  1814. end;
  1815. buf:=@s[1];
  1816. while (buf^<>#0) do
  1817. begin
  1818. while (buf^ in [' ',#8,#10]) do
  1819. begin
  1820. buf^:=#0;
  1821. inc(buf);
  1822. end;
  1823. p^:=buf;
  1824. inc(p);
  1825. p^:=nil;
  1826. while not (buf^ in [' ',#0,#8,#10]) do
  1827. inc(buf);
  1828. end;
  1829. end;
  1830. (*
  1831. function FExpand (const Path: PathStr): PathStr;
  1832. - declared in fexpand.inc
  1833. *)
  1834. {$DEFINE FPC_FEXPAND_TILDE} (* Tilde is expanded to home *)
  1835. {$DEFINE FPC_FEXPAND_GETENVPCHAR} (* GetEnv result is a PChar *)
  1836. const
  1837. LFNSupport = true;
  1838. FileNameCaseSensitive = true;
  1839. {$I fexpand.inc}
  1840. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1841. {$UNDEF FPC_FEXPAND_TILDE}
  1842. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1843. {
  1844. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1845. returns an empty string if not found. Wildcards are NOT allowed.
  1846. If dirlist is empty, it is set to '.'
  1847. }
  1848. Var
  1849. NewDir : PathStr;
  1850. p1 : Longint;
  1851. Info : Stat;
  1852. Begin
  1853. {Replace ':' with ';'}
  1854. for p1:=1to length(dirlist) do
  1855. if dirlist[p1]=':' then
  1856. dirlist[p1]:=';';
  1857. {Check for WildCards}
  1858. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1859. FSearch:='' {No wildcards allowed in these things.}
  1860. Else
  1861. Begin
  1862. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1863. Repeat
  1864. p1:=Pos(';',DirList);
  1865. If p1=0 Then
  1866. p1:=255;
  1867. NewDir:=Copy(DirList,1,P1 - 1);
  1868. if NewDir[Length(NewDir)]<>'/' then
  1869. NewDir:=NewDir+'/';
  1870. NewDir:=NewDir+Path;
  1871. Delete(DirList,1,p1);
  1872. if FStat(NewDir,Info) then
  1873. Begin
  1874. If Pos('./',NewDir)=1 Then
  1875. Delete(NewDir,1,2);
  1876. {DOS strips off an initial .\}
  1877. End
  1878. Else
  1879. NewDir:='';
  1880. Until (DirList='') or (Length(NewDir) > 0);
  1881. FSearch:=NewDir;
  1882. End;
  1883. End;
  1884. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  1885. Var
  1886. DotPos,SlashPos,i : longint;
  1887. Begin
  1888. SlashPos:=0;
  1889. DotPos:=256;
  1890. i:=Length(Path);
  1891. While (i>0) and (SlashPos=0) Do
  1892. Begin
  1893. If (DotPos=256) and (Path[i]='.') Then
  1894. begin
  1895. DotPos:=i;
  1896. end;
  1897. If (Path[i]='/') Then
  1898. SlashPos:=i;
  1899. Dec(i);
  1900. End;
  1901. Ext:=Copy(Path,DotPos,255);
  1902. Dir:=Copy(Path,1,SlashPos);
  1903. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  1904. End;
  1905. Function Dirname(Const path:pathstr):pathstr;
  1906. {
  1907. This function returns the directory part of a complete path.
  1908. Unless the directory is root '/', The last character is not
  1909. a slash.
  1910. }
  1911. var
  1912. Dir : PathStr;
  1913. Name : NameStr;
  1914. Ext : ExtStr;
  1915. begin
  1916. FSplit(Path,Dir,Name,Ext);
  1917. if length(Dir)>1 then
  1918. Delete(Dir,length(Dir),1);
  1919. DirName:=Dir;
  1920. end;
  1921. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  1922. {
  1923. This function returns the filename part of a complete path. If suf is
  1924. supplied, it is cut off the filename.
  1925. }
  1926. var
  1927. Dir : PathStr;
  1928. Name : NameStr;
  1929. Ext : ExtStr;
  1930. begin
  1931. FSplit(Path,Dir,Name,Ext);
  1932. if Suf<>Ext then
  1933. Name:=Name+Ext;
  1934. BaseName:=Name;
  1935. end;
  1936. Function FNMatch(const Pattern,Name:string):Boolean;
  1937. Var
  1938. LenPat,LenName : longint;
  1939. Function DoFNMatch(i,j:longint):Boolean;
  1940. Var
  1941. Found : boolean;
  1942. Begin
  1943. Found:=true;
  1944. While Found and (i<=LenPat) Do
  1945. Begin
  1946. Case Pattern[i] of
  1947. '?' : Found:=(j<=LenName);
  1948. '*' : Begin
  1949. {find the next character in pattern, different of ? and *}
  1950. while Found and (i<LenPat) do
  1951. begin
  1952. inc(i);
  1953. case Pattern[i] of
  1954. '*' : ;
  1955. '?' : begin
  1956. inc(j);
  1957. Found:=(j<=LenName);
  1958. end;
  1959. else
  1960. Found:=false;
  1961. end;
  1962. end;
  1963. {Now, find in name the character which i points to, if the * or ?
  1964. wasn't the last character in the pattern, else, use up all the
  1965. chars in name}
  1966. Found:=true;
  1967. if (i<=LenPat) then
  1968. begin
  1969. repeat
  1970. {find a letter (not only first !) which maches pattern[i]}
  1971. while (j<=LenName) and (name[j]<>pattern[i]) do
  1972. inc (j);
  1973. if (j<LenName) then
  1974. begin
  1975. if DoFnMatch(i+1,j+1) then
  1976. begin
  1977. i:=LenPat;
  1978. j:=LenName;{we can stop}
  1979. Found:=true;
  1980. end
  1981. else
  1982. inc(j);{We didn't find one, need to look further}
  1983. end;
  1984. until (j>=LenName);
  1985. end
  1986. else
  1987. j:=LenName;{we can stop}
  1988. end;
  1989. else {not a wildcard character in pattern}
  1990. Found:=(j<=LenName) and (pattern[i]=name[j]);
  1991. end;
  1992. inc(i);
  1993. inc(j);
  1994. end;
  1995. DoFnMatch:=Found and (j>LenName);
  1996. end;
  1997. Begin {start FNMatch}
  1998. LenPat:=Length(Pattern);
  1999. LenName:=Length(Name);
  2000. FNMatch:=DoFNMatch(1,1);
  2001. End;
  2002. Procedure Globfree(var p : pglob);
  2003. {
  2004. Release memory occupied by pglob structure, and names in it.
  2005. sets p to nil.
  2006. }
  2007. var
  2008. temp : pglob;
  2009. begin
  2010. while assigned(p) do
  2011. begin
  2012. temp:=p^.next;
  2013. if assigned(p^.name) then
  2014. freemem(p^.name);
  2015. dispose(p);
  2016. p:=temp;
  2017. end;
  2018. end;
  2019. Function Glob(Const path:pathstr):pglob;
  2020. {
  2021. Fills a tglob structure with entries matching path,
  2022. and returns a pointer to it. Returns nil on error,
  2023. linuxerror is set accordingly.
  2024. }
  2025. var
  2026. temp,
  2027. temp2 : string[255];
  2028. thedir : pdir;
  2029. buffer : pdirent;
  2030. root,
  2031. current : pglob;
  2032. begin
  2033. { Get directory }
  2034. temp:=dirname(path);
  2035. if temp='' then
  2036. temp:='.';
  2037. temp:=temp+#0;
  2038. thedir:=opendir(@temp[1]);
  2039. if thedir=nil then
  2040. begin
  2041. glob:=nil;
  2042. linuxerror:=errno;
  2043. exit;
  2044. end;
  2045. temp:=basename(path,''); { get the pattern }
  2046. if thedir^.fd<0 then
  2047. begin
  2048. linuxerror:=errno;
  2049. glob:=nil;
  2050. exit;
  2051. end;
  2052. {get the entries}
  2053. root:=nil;
  2054. current:=nil;
  2055. repeat
  2056. buffer:=Sys_readdir(thedir);
  2057. if buffer=nil then
  2058. break;
  2059. temp2:=strpas(@(buffer^.name[0]));
  2060. if fnmatch(temp,temp2) then
  2061. begin
  2062. if root=nil then
  2063. begin
  2064. new(root);
  2065. current:=root;
  2066. end
  2067. else
  2068. begin
  2069. new(current^.next);
  2070. current:=current^.next;
  2071. end;
  2072. if current=nil then
  2073. begin
  2074. linuxerror:=Sys_ENOMEM;
  2075. globfree(root);
  2076. break;
  2077. end;
  2078. current^.next:=nil;
  2079. getmem(current^.name,length(temp2)+1);
  2080. if current^.name=nil then
  2081. begin
  2082. linuxerror:=Sys_ENOMEM;
  2083. globfree(root);
  2084. break;
  2085. end;
  2086. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2087. end;
  2088. until false;
  2089. closedir(thedir);
  2090. glob:=root;
  2091. end;
  2092. {--------------------------------
  2093. FiledescriptorSets
  2094. --------------------------------}
  2095. Procedure FD_Zero(var fds:fdSet);
  2096. {
  2097. Clear the set of filedescriptors
  2098. }
  2099. begin
  2100. FillChar(fds,sizeof(fdSet),0);
  2101. end;
  2102. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2103. {
  2104. Remove fd from the set of filedescriptors
  2105. }
  2106. begin
  2107. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2108. end;
  2109. Procedure FD_Set(fd:longint;var fds:fdSet);
  2110. {
  2111. Add fd to the set of filedescriptors
  2112. }
  2113. begin
  2114. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2115. end;
  2116. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2117. {
  2118. Test if fd is part of the set of filedescriptors
  2119. }
  2120. begin
  2121. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2122. end;
  2123. Function GetFS (var T:Text):longint;
  2124. {
  2125. Get File Descriptor of a text file.
  2126. }
  2127. begin
  2128. if textrec(t).mode=fmclosed then
  2129. exit(-1)
  2130. else
  2131. GETFS:=textrec(t).Handle
  2132. end;
  2133. Function GetFS(Var F:File):longint;
  2134. {
  2135. Get File Descriptor of an unTyped file.
  2136. }
  2137. begin
  2138. { Handle and mode are on the same place in textrec and filerec. }
  2139. if filerec(f).mode=fmclosed then
  2140. exit(-1)
  2141. else
  2142. GETFS:=filerec(f).Handle
  2143. end;
  2144. {--------------------------------
  2145. Stat.Mode Macro's
  2146. --------------------------------}
  2147. Function S_ISLNK(m:word):boolean;
  2148. {
  2149. Check mode field of inode for link.
  2150. }
  2151. begin
  2152. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2153. end;
  2154. Function S_ISREG(m:word):boolean;
  2155. {
  2156. Check mode field of inode for regular file.
  2157. }
  2158. begin
  2159. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2160. end;
  2161. Function S_ISDIR(m:word):boolean;
  2162. {
  2163. Check mode field of inode for directory.
  2164. }
  2165. begin
  2166. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2167. end;
  2168. Function S_ISCHR(m:word):boolean;
  2169. {
  2170. Check mode field of inode for character device.
  2171. }
  2172. begin
  2173. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2174. end;
  2175. Function S_ISBLK(m:word):boolean;
  2176. {
  2177. Check mode field of inode for block device.
  2178. }
  2179. begin
  2180. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2181. end;
  2182. Function S_ISFIFO(m:word):boolean;
  2183. {
  2184. Check mode field of inode for named pipe (FIFO).
  2185. }
  2186. begin
  2187. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2188. end;
  2189. Function S_ISSOCK(m:word):boolean;
  2190. {
  2191. Check mode field of inode for socket.
  2192. }
  2193. begin
  2194. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2195. end;
  2196. {--------------------------------
  2197. Memory functions
  2198. --------------------------------}
  2199. {$IFDEF I386}
  2200. Procedure WritePort (Port : Longint; Value : Byte);
  2201. {
  2202. Writes 'Value' to port 'Port'
  2203. }
  2204. begin
  2205. asm
  2206. movl port,%edx
  2207. movb value,%al
  2208. outb %al,%dx
  2209. end ['EAX','EDX'];
  2210. end;
  2211. Procedure WritePort (Port : Longint; Value : Word);
  2212. {
  2213. Writes 'Value' to port 'Port'
  2214. }
  2215. begin
  2216. asm
  2217. movl port,%edx
  2218. movw value,%ax
  2219. outw %ax,%dx
  2220. end ['EAX','EDX'];
  2221. end;
  2222. Procedure WritePort (Port : Longint; Value : Longint);
  2223. {
  2224. Writes 'Value' to port 'Port'
  2225. }
  2226. begin
  2227. asm
  2228. movl port,%edx
  2229. movl value,%eax
  2230. outl %eax,%dx
  2231. end ['EAX','EDX'];
  2232. end;
  2233. Procedure WritePortB (Port : Longint; Value : Byte);
  2234. {
  2235. Writes 'Value' to port 'Port'
  2236. }
  2237. begin
  2238. asm
  2239. movl port,%edx
  2240. movb value,%al
  2241. outb %al,%dx
  2242. end ['EAX','EDX'];
  2243. end;
  2244. Procedure WritePortW (Port : Longint; Value : Word);
  2245. {
  2246. Writes 'Value' to port 'Port'
  2247. }
  2248. begin
  2249. asm
  2250. movl port,%edx
  2251. movw value,%ax
  2252. outw %ax,%dx
  2253. end ['EAX','EDX'];
  2254. end;
  2255. Procedure WritePortL (Port : Longint; Value : Longint);
  2256. {
  2257. Writes 'Value' to port 'Port'
  2258. }
  2259. begin
  2260. asm
  2261. movl port,%edx
  2262. movl value,%eax
  2263. outl %eax,%dx
  2264. end ['EAX','EDX'];
  2265. end;
  2266. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2267. {
  2268. Writes 'Count' longints from 'Buf' to Port
  2269. }
  2270. begin
  2271. asm
  2272. movl count,%ecx
  2273. movl buf,%esi
  2274. movl port,%edx
  2275. cld
  2276. rep
  2277. outsl
  2278. end ['ECX','ESI','EDX'];
  2279. end;
  2280. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2281. {
  2282. Writes 'Count' words from 'Buf' to Port
  2283. }
  2284. begin
  2285. asm
  2286. movl count,%ecx
  2287. movl buf,%esi
  2288. movl port,%edx
  2289. cld
  2290. rep
  2291. outsw
  2292. end ['ECX','ESI','EDX'];
  2293. end;
  2294. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2295. {
  2296. Writes 'Count' bytes from 'Buf' to Port
  2297. }
  2298. begin
  2299. asm
  2300. movl count,%ecx
  2301. movl buf,%esi
  2302. movl port,%edx
  2303. cld
  2304. rep
  2305. outsb
  2306. end ['ECX','ESI','EDX'];
  2307. end;
  2308. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2309. {
  2310. Reads 'Value' from port 'Port'
  2311. }
  2312. begin
  2313. asm
  2314. movl port,%edx
  2315. inb %dx,%al
  2316. movl value,%edx
  2317. movb %al,(%edx)
  2318. end ['EAX','EDX'];
  2319. end;
  2320. Procedure ReadPort (Port : Longint; Var Value : Word);
  2321. {
  2322. Reads 'Value' from port 'Port'
  2323. }
  2324. begin
  2325. asm
  2326. movl port,%edx
  2327. inw %dx,%ax
  2328. movl value,%edx
  2329. movw %ax,(%edx)
  2330. end ['EAX','EDX'];
  2331. end;
  2332. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2333. {
  2334. Reads 'Value' from port 'Port'
  2335. }
  2336. begin
  2337. asm
  2338. movl port,%edx
  2339. inl %dx,%eax
  2340. movl value,%edx
  2341. movl %eax,(%edx)
  2342. end ['EAX','EDX'];
  2343. end;
  2344. function ReadPortB (Port : Longint): Byte; assembler;
  2345. {
  2346. Reads a byte from port 'Port'
  2347. }
  2348. asm
  2349. xorl %eax,%eax
  2350. movl port,%edx
  2351. inb %dx,%al
  2352. end ['EAX','EDX'];
  2353. function ReadPortW (Port : Longint): Word; assembler;
  2354. {
  2355. Reads a word from port 'Port'
  2356. }
  2357. asm
  2358. xorl %eax,%eax
  2359. movl port,%edx
  2360. inw %dx,%ax
  2361. end ['EAX','EDX'];
  2362. function ReadPortL (Port : Longint): LongInt; assembler;
  2363. {
  2364. Reads a LongInt from port 'Port'
  2365. }
  2366. asm
  2367. movl port,%edx
  2368. inl %dx,%eax
  2369. end ['EAX','EDX'];
  2370. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  2371. {
  2372. Reads 'Count' longints from port 'Port' to 'Buf'.
  2373. }
  2374. begin
  2375. asm
  2376. movl count,%ecx
  2377. movl buf,%edi
  2378. movl port,%edx
  2379. cld
  2380. rep
  2381. insl
  2382. end ['ECX','EDI','EDX'];
  2383. end;
  2384. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  2385. {
  2386. Reads 'Count' words from port 'Port' to 'Buf'.
  2387. }
  2388. begin
  2389. asm
  2390. movl count,%ecx
  2391. movl buf,%edi
  2392. movl port,%edx
  2393. cld
  2394. rep
  2395. insw
  2396. end ['ECX','EDI','EDX'];
  2397. end;
  2398. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  2399. {
  2400. Reads 'Count' bytes from port 'Port' to 'Buf'.
  2401. }
  2402. begin
  2403. asm
  2404. movl count,%ecx
  2405. movl buf,%edi
  2406. movl port,%edx
  2407. cld
  2408. rep
  2409. insb
  2410. end ['ECX','EDI','EDX'];
  2411. end;
  2412. {$ENDIF}
  2413. Initialization
  2414. InitLocalTime;
  2415. finalization
  2416. DoneLocalTime;
  2417. End.
  2418. {
  2419. $Log$
  2420. Revision 1.7 2001-04-19 12:57:33 marco
  2421. * Readlink uncommented for FreeBSD.
  2422. Revision 1.6 2001/04/13 22:37:21 peter
  2423. * remove warning
  2424. Revision 1.5 2001/03/27 11:47:25 michael
  2425. + Fixed F_[G,S]etOwn constants. By Alexander Sychev
  2426. Revision 1.4 2001/03/17 16:04:37 hajny
  2427. * FExpand omission fixed
  2428. Revision 1.3 2001/03/16 20:09:58 hajny
  2429. * universal FExpand
  2430. Revision 1.2 2001/01/22 07:25:10 marco
  2431. * IOPERM for FreeBSD. Port routines moved from linsysca to Unix again .
  2432. Revision 1.1 2001/01/21 20:21:41 marco
  2433. * Rename fest II. Rtl OK
  2434. Revision 1.6 2000/12/28 20:42:12 peter
  2435. * ttyname fix from the mailinglist (merged)
  2436. Revision 1.5 2000/10/26 22:51:12 peter
  2437. * nano sleep (merged)
  2438. Revision 1.4 2000/10/11 13:59:16 marco
  2439. * FreeBSD TermIOS support and minor changes to some related files.
  2440. Revision 1.3 2000/10/10 12:02:35 marco
  2441. * Terminal stuff of Linux moved to separate file. I think it is too
  2442. much to do with ifdefs.
  2443. Revision 1.2 2000/09/18 13:14:50 marco
  2444. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2445. Revision 1.7 2000/09/12 08:51:43 marco
  2446. * fixed some small problems left from merging. (waitpid has now last param longint)
  2447. Revision 1.6 2000/09/11 14:05:31 marco
  2448. * FreeBSD support and removed old signalhandling
  2449. Revision 1.5 2000/09/06 20:47:34 peter
  2450. * removed previous fsplit() patch as it's not the correct behaviour for
  2451. LFNs. The code showing the bug could easily be adapted (merged)
  2452. Revision 1.4 2000/09/04 20:17:53 peter
  2453. * fixed previous commit (merged)
  2454. Revision 1.3 2000/09/04 19:38:13 peter
  2455. * fsplit with .. fix from Thomas (merged)
  2456. Revision 1.2 2000/07/13 11:33:48 michael
  2457. + removed logs
  2458. }