linux.pp 69 KB

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