linux.pp 61 KB

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