unix.pp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. { Get Types and Constants }
  16. {$i sysconst.inc}
  17. {$i systypes.inc}
  18. { Get System call numbers and error-numbers}
  19. {$i sysnr.inc}
  20. {$i errno.inc}
  21. {$I signal.inc}
  22. var
  23. ErrNo,
  24. LinuxError : Longint;
  25. {********************
  26. Process
  27. ********************}
  28. const
  29. {Checked for BSD using Linuxthreads port}
  30. { cloning flags }
  31. CSIGNAL = $000000ff; // signal mask to be sent at exit
  32. CLONE_VM = $00000100; // set if VM shared between processes
  33. CLONE_FS = $00000200; // set if fs info shared between processes
  34. CLONE_FILES = $00000400; // set if open files shared between processes
  35. CLONE_SIGHAND = $00000800; // set if signal handlers shared
  36. CLONE_PID = $00001000; // set if pid shared
  37. type
  38. TCloneFunc=function(args:pointer):longint;cdecl;
  39. const
  40. { For getting/setting priority }
  41. Prio_Process = 0;
  42. Prio_PGrp = 1;
  43. Prio_User = 2;
  44. {$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. {$ifndef BSD}
  763. {$ifdef linux}
  764. Function stime (t : longint) : Boolean;
  765. var
  766. sr : Syscallregs;
  767. begin
  768. sr.reg2:=longint(@t);
  769. SysCall(Syscall_nr_stime,sr);
  770. linuxerror:=errno;
  771. stime:=linuxerror=0;
  772. end;
  773. {$endif}
  774. {$endif}
  775. {$ifdef BSD}
  776. Function stime (t : longint) : Boolean;
  777. begin
  778. end;
  779. {$endif}
  780. Function SetTime(Hour,Min,Sec:word) : boolean;
  781. var
  782. Year, Month, Day : Word;
  783. begin
  784. GetDate (Year, Month, Day);
  785. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  786. end;
  787. Function SetDate(Year,Month,Day:Word) : boolean;
  788. var
  789. Hour, Minute, Second, Sec100 : Word;
  790. begin
  791. GetTime ( Hour, Minute, Second, Sec100 );
  792. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  793. end;
  794. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  795. begin
  796. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  797. end;
  798. { Include timezone handling routines which use /usr/share/timezone info }
  799. {$i timezone.inc}
  800. {******************************************************************************
  801. FileSystem calls
  802. ******************************************************************************}
  803. Function fdOpen(pathname:string;flags:longint):longint;
  804. begin
  805. pathname:=pathname+#0;
  806. fdOpen:=Sys_Open(@pathname[1],flags,438);
  807. LinuxError:=Errno;
  808. end;
  809. Function fdOpen(pathname:string;flags,mode:longint):longint;
  810. begin
  811. pathname:=pathname+#0;
  812. fdOpen:=Sys_Open(@pathname[1],flags,mode);
  813. LinuxError:=Errno;
  814. end;
  815. Function fdOpen(pathname:pchar;flags:longint):longint;
  816. begin
  817. fdOpen:=Sys_Open(pathname,flags,0);
  818. LinuxError:=Errno;
  819. end;
  820. Function fdOpen(pathname:pchar;flags,mode:longint):longint;
  821. begin
  822. fdOpen:=Sys_Open(pathname,flags,mode);
  823. LinuxError:=Errno;
  824. end;
  825. Function fdClose(fd:longint):boolean;
  826. begin
  827. fdClose:=(Sys_Close(fd)=0);
  828. LinuxError:=Errno;
  829. end;
  830. Function fdRead(fd:longint;var buf;size:longint):longint;
  831. begin
  832. fdRead:=Sys_Read(fd,pchar(@buf),size);
  833. LinuxError:=Errno;
  834. end;
  835. Function fdWrite(fd:longint;const buf;size:longint):longint;
  836. begin
  837. fdWrite:=Sys_Write(fd,pchar(@buf),size);
  838. LinuxError:=Errno;
  839. end;
  840. Function fdSeek (fd,pos,seektype :longint): longint;
  841. {
  842. Do a Seek on a file descriptor fd to position pos, starting from seektype
  843. }
  844. begin
  845. fdseek:=Sys_LSeek (fd,pos,seektype);
  846. LinuxError:=Errno;
  847. end;
  848. {$ifdef BSD}
  849. Function Fcntl(Fd:longint;Cmd:longint):longint;
  850. {
  851. Read or manipulate a file.(See also fcntl (2) )
  852. Possible values for Cmd are :
  853. F_GetFd,F_GetFl,F_GetOwn
  854. Errors are reported in Linuxerror;
  855. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  856. }
  857. begin
  858. if (cmd in [F_GetFd,F_GetFl,F_GetOwn]) then
  859. begin
  860. Linuxerror:=sys_fcntl(fd,cmd,0);
  861. if linuxerror=-1 then
  862. begin
  863. linuxerror:=errno;
  864. fcntl:=0;
  865. end
  866. else
  867. begin
  868. fcntl:=linuxerror;
  869. linuxerror:=0;
  870. end;
  871. end
  872. else
  873. begin
  874. linuxerror:=Sys_einval;
  875. Fcntl:=0;
  876. end;
  877. end;
  878. Procedure Fcntl(Fd:longint;Cmd:longint;Arg:Longint);
  879. {
  880. Read or manipulate a file. (See also fcntl (2) )
  881. Possible values for Cmd are :
  882. F_setFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkW,F_SetOwn;
  883. Errors are reported in Linuxerror;
  884. If Cmd is different from the allowed values, linuxerror=Sys_eninval.
  885. F_DupFD is not allowed, due to the structure of Files in Pascal.
  886. }
  887. begin
  888. if (cmd in [F_SetFd,F_SetFl,F_GetLk,F_SetLk,F_SetLkw,F_SetOwn]) then
  889. begin
  890. sys_fcntl(fd,cmd,arg);
  891. LinuxError:=ErrNo;
  892. end
  893. else
  894. linuxerror:=Sys_einval;
  895. end;
  896. {$endif}
  897. {$ifdef BSD}
  898. Function Fcntl(var Fd:Text;Cmd:longint):longint;
  899. {$else}
  900. Function Fcntl(var Fd:Text;Cmd:integer):integer;
  901. {$endif}
  902. begin
  903. Fcntl := Fcntl(textrec(Fd).handle, Cmd);
  904. end;
  905. {$ifdef BSD}
  906. Procedure Fcntl(var Fd:Text;Cmd,Arg:Longint);
  907. {$else}
  908. Procedure Fcntl(var Fd:Text;Cmd:Integer;Arg:Longint);
  909. {$endif}
  910. begin
  911. Fcntl(textrec(Fd).handle, Cmd, Arg);
  912. end;
  913. Function Flock (var T : text;mode : longint) : boolean;
  914. begin
  915. Flock:=Flock(TextRec(T).Handle,mode);
  916. end;
  917. Function Flock (var F : File;mode : longint) : boolean;
  918. begin
  919. Flock:=Flock(FileRec(F).Handle,mode);
  920. end;
  921. Function FStat(Path:Pathstr;Var Info:stat):Boolean;
  922. {
  923. Get all information on a file, and return it in Info.
  924. }
  925. begin
  926. path:=path+#0;
  927. FStat:=(Sys_stat(@(path[1]),Info)=0);
  928. LinuxError:=errno;
  929. end;
  930. Function FStat(var F:Text;Var Info:stat):Boolean;
  931. {
  932. Get all information on a text file, and return it in info.
  933. }
  934. begin
  935. FStat:=Fstat(TextRec(F).Handle,INfo);
  936. end;
  937. Function FStat(var F:File;Var Info:stat):Boolean;
  938. {
  939. Get all information on a untyped file, and return it in info.
  940. }
  941. begin
  942. FStat:=Fstat(FileRec(F).Handle,Info);
  943. end;
  944. Function SymLink(OldPath,newPath:pathstr):boolean;
  945. {
  946. Proceduces a soft link from new to old.
  947. }
  948. begin
  949. oldpath:=oldpath+#0;
  950. newpath:=newpath+#0;
  951. Symlink:=Sys_symlink(pchar(@(oldpath[1])),pchar(@(newpath[1])))=0;
  952. linuxerror:=errno;
  953. end;
  954. Function ReadLink(name,linkname:pchar;maxlen:longint):longint;
  955. {
  956. Read a link (where it points to)
  957. }
  958. begin
  959. Readlink:=Sys_readlink(Name,LinkName,maxlen);
  960. linuxerror:=errno;
  961. end;
  962. Function ReadLink(Name:pathstr):pathstr;
  963. {
  964. Read a link (where it points to)
  965. }
  966. var
  967. LinkName : pathstr;
  968. i : longint;
  969. begin
  970. Name:=Name+#0;
  971. i:=ReadLink(@Name[1],@LinkName[1],high(linkname));
  972. if i>0 then
  973. begin
  974. linkname[0]:=chr(i);
  975. ReadLink:=LinkName;
  976. end
  977. else
  978. ReadLink:='';
  979. end;
  980. Function UnLink(Path:pathstr):boolean;
  981. {
  982. Removes the file in 'Path' (that is, it decreases the link count with one.
  983. if the link count is zero, the file is removed from the disk.
  984. }
  985. begin
  986. path:=path+#0;
  987. Unlink:=Sys_unlink(pchar(@(path[1])))=0;
  988. linuxerror:=errno;
  989. end;
  990. Function UnLink(Path:pchar):Boolean;
  991. {
  992. Removes the file in 'Path' (that is, it decreases the link count with one.
  993. if the link count is zero, the file is removed from the disk.
  994. }
  995. begin
  996. Unlink:=(Sys_unlink(path)=0);
  997. linuxerror:=errno;
  998. end;
  999. Function FRename (OldName,NewName : Pchar) : Boolean;
  1000. begin
  1001. FRename:=Sys_rename(OldName,NewName)=0;
  1002. LinuxError:=Errno;
  1003. end;
  1004. Function FRename (OldName,NewName : String) : Boolean;
  1005. begin
  1006. OldName:=OldName+#0;
  1007. NewName:=NewName+#0;
  1008. FRename:=FRename (@OldName[1],@NewName[1]);
  1009. end;
  1010. Function Dup(var oldfile,newfile:text):Boolean;
  1011. {
  1012. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1013. oldfile.
  1014. After which the two textfiles are, in effect, the same, except
  1015. that they don't share the same buffer, and don't share the same
  1016. close_on_exit flag.
  1017. }
  1018. begin
  1019. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1020. textrec(newfile):=textrec(oldfile);
  1021. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1022. Dup:=Dup(textrec(oldfile).handle,textrec(newfile).handle);
  1023. end;
  1024. Function Dup(var oldfile,newfile:file):Boolean;
  1025. {
  1026. Copies the filedescriptor oldfile to newfile
  1027. }
  1028. begin
  1029. filerec(newfile):=filerec(oldfile);
  1030. Dup:=Dup(filerec(oldfile).handle,filerec(newfile).handle);
  1031. end;
  1032. Function Dup2(var oldfile,newfile:text):Boolean;
  1033. {
  1034. Copies the filedescriptor oldfile to newfile, after flushing the buffer of
  1035. oldfile. It closes newfile if it was still open.
  1036. After which the two textfiles are, in effect, the same, except
  1037. that they don't share the same buffer, and don't share the same
  1038. close_on_exit flag.
  1039. }
  1040. var
  1041. tmphandle : word;
  1042. begin
  1043. case TextRec(oldfile).mode of
  1044. fmOutput, fmInOut, fmAppend :
  1045. flush(oldfile);{ We cannot share buffers, so we flush them. }
  1046. end;
  1047. case TextRec(newfile).mode of
  1048. fmOutput, fmInOut, fmAppend :
  1049. flush(newfile);
  1050. end;
  1051. tmphandle:=textrec(newfile).handle;
  1052. textrec(newfile):=textrec(oldfile);
  1053. textrec(newfile).handle:=tmphandle;
  1054. textrec(newfile).bufptr:=@(textrec(newfile).buffer);{ No shared buffer. }
  1055. Dup2:=Dup2(textrec(oldfile).handle,textrec(newfile).handle);
  1056. end;
  1057. Function Dup2(var oldfile,newfile:file):Boolean;
  1058. {
  1059. Copies the filedescriptor oldfile to newfile
  1060. }
  1061. begin
  1062. filerec(newfile):=filerec(oldfile);
  1063. Dup2:=Dup2(filerec(oldfile).handle,filerec(newfile).handle);
  1064. end;
  1065. Function Select(N:longint;readfds,writefds,exceptfds:PFDSet;TimeOut:Longint):longint;
  1066. {
  1067. Select checks whether the file descriptor sets in readfs/writefs/exceptfs
  1068. have changed.
  1069. This function allows specification of a timeout as a longint.
  1070. }
  1071. var
  1072. p : PTimeVal;
  1073. tv : TimeVal;
  1074. begin
  1075. if TimeOut=-1 then
  1076. p:=nil
  1077. else
  1078. begin
  1079. tv.Sec:=Timeout div 1000;
  1080. tv.Usec:=(Timeout mod 1000)*1000;
  1081. p:=@tv;
  1082. end;
  1083. Select:=Select(N,Readfds,WriteFds,ExceptFds,p);
  1084. end;
  1085. Function SelectText(var T:Text;TimeOut :PTimeval):Longint;
  1086. Var
  1087. F:FDSet;
  1088. begin
  1089. if textrec(t).mode=fmclosed then
  1090. begin
  1091. LinuxError:=Sys_EBADF;
  1092. exit(-1);
  1093. end;
  1094. FD_Zero(f);
  1095. FD_Set(textrec(T).handle,f);
  1096. if textrec(T).mode=fminput then
  1097. SelectText:=select(textrec(T).handle+1,@f,nil,nil,TimeOut)
  1098. else
  1099. SelectText:=select(textrec(T).handle+1,nil,@f,nil,TimeOut);
  1100. end;
  1101. Function SelectText(var T:Text;TimeOut :Longint):Longint;
  1102. var
  1103. p : PTimeVal;
  1104. tv : TimeVal;
  1105. begin
  1106. if TimeOut=-1 then
  1107. p:=nil
  1108. else
  1109. begin
  1110. tv.Sec:=Timeout div 1000;
  1111. tv.Usec:=(Timeout mod 1000)*1000;
  1112. p:=@tv;
  1113. end;
  1114. SelectText:=SelectText(T,p);
  1115. end;
  1116. {******************************************************************************
  1117. Directory
  1118. ******************************************************************************}
  1119. Function OpenDir(F:String):PDir;
  1120. begin
  1121. F:=F+#0;
  1122. OpenDir:=OpenDir(@F[1]);
  1123. end;
  1124. procedure SeekDir(p:pdir;off:longint);
  1125. begin
  1126. if p=nil then
  1127. begin
  1128. errno:=Sys_EBADF;
  1129. exit;
  1130. end;
  1131. {$ifndef bsd}
  1132. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1133. {$endif}
  1134. p^.size:=0;
  1135. p^.loc:=0;
  1136. end;
  1137. function TellDir(p:pdir):longint;
  1138. begin
  1139. if p=nil then
  1140. begin
  1141. errno:=Sys_EBADF;
  1142. telldir:=-1;
  1143. exit;
  1144. end;
  1145. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1146. { We could try to use the nextoff field here, but on my 1.2.13
  1147. kernel, this gives nothing... This may have to do with
  1148. the readdir implementation of libc... I also didn't find any trace of
  1149. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1150. Michael. }
  1151. end;
  1152. Function ReadDir(P:pdir):pdirent;
  1153. begin
  1154. ReadDir:=Sys_ReadDir(p);
  1155. LinuxError:=Errno;
  1156. end;
  1157. {******************************************************************************
  1158. Pipes/Fifo
  1159. ******************************************************************************}
  1160. Procedure OpenPipe(var F:Text);
  1161. begin
  1162. case textrec(f).mode of
  1163. fmoutput :
  1164. if textrec(f).userdata[1]<>P_OUT then
  1165. textrec(f).mode:=fmclosed;
  1166. fminput :
  1167. if textrec(f).userdata[1]<>P_IN then
  1168. textrec(f).mode:=fmclosed;
  1169. else
  1170. textrec(f).mode:=fmclosed;
  1171. end;
  1172. end;
  1173. Procedure IOPipe(var F:text);
  1174. begin
  1175. case textrec(f).mode of
  1176. fmoutput :
  1177. begin
  1178. { first check if we need something to write, else we may
  1179. get a SigPipe when Close() is called (PFV) }
  1180. if textrec(f).bufpos>0 then
  1181. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1182. end;
  1183. fminput :
  1184. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1185. end;
  1186. textrec(f).bufpos:=0;
  1187. end;
  1188. Procedure FlushPipe(var F:Text);
  1189. begin
  1190. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1191. IOPipe(f);
  1192. textrec(f).bufpos:=0;
  1193. end;
  1194. Procedure ClosePipe(var F:text);
  1195. begin
  1196. textrec(f).mode:=fmclosed;
  1197. Sys_close(textrec(f).handle);
  1198. end;
  1199. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1200. {
  1201. Sets up a pair of file variables, which act as a pipe. The first one can
  1202. be read from, the second one can be written to.
  1203. If the operation was unsuccesful, linuxerror is set.
  1204. }
  1205. var
  1206. f_in,f_out : longint;
  1207. begin
  1208. if not AssignPipe(f_in,f_out) then
  1209. begin
  1210. AssignPipe:=false;
  1211. exit;
  1212. end;
  1213. { Set up input }
  1214. Assign(Pipe_in,'');
  1215. Textrec(Pipe_in).Handle:=f_in;
  1216. Textrec(Pipe_in).Mode:=fmInput;
  1217. Textrec(Pipe_in).userdata[1]:=P_IN;
  1218. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1219. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1220. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1221. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1222. { Set up output }
  1223. Assign(Pipe_out,'');
  1224. Textrec(Pipe_out).Handle:=f_out;
  1225. Textrec(Pipe_out).Mode:=fmOutput;
  1226. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1227. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1228. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1229. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1230. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1231. AssignPipe:=true;
  1232. end;
  1233. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1234. {
  1235. Sets up a pair of file variables, which act as a pipe. The first one can
  1236. be read from, the second one can be written to.
  1237. If the operation was unsuccesful, linuxerror is set.
  1238. }
  1239. var
  1240. f_in,f_out : longint;
  1241. begin
  1242. if not AssignPipe(f_in,f_out) then
  1243. begin
  1244. AssignPipe:=false;
  1245. exit;
  1246. end;
  1247. { Set up input }
  1248. Assign(Pipe_in,'');
  1249. Filerec(Pipe_in).Handle:=f_in;
  1250. Filerec(Pipe_in).Mode:=fmInput;
  1251. Filerec(Pipe_in).recsize:=1;
  1252. Filerec(Pipe_in).userdata[1]:=P_IN;
  1253. { Set up output }
  1254. Assign(Pipe_out,'');
  1255. Filerec(Pipe_out).Handle:=f_out;
  1256. Filerec(Pipe_out).Mode:=fmoutput;
  1257. Filerec(Pipe_out).recsize:=1;
  1258. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1259. AssignPipe:=true;
  1260. end;
  1261. Procedure PCloseText(Var F:text);
  1262. {
  1263. May not use @PClose due overloading
  1264. }
  1265. begin
  1266. PClose(f);
  1267. end;
  1268. Procedure POpen(var F:text;const Prog:String;rw:char);
  1269. {
  1270. Starts the program in 'Prog' and makes it's input or out put the
  1271. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1272. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1273. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1274. read from 'f'.
  1275. }
  1276. var
  1277. pipi,
  1278. pipo : text;
  1279. pid : longint;
  1280. pl : ^longint;
  1281. pp : ppchar;
  1282. begin
  1283. LinuxError:=0;
  1284. rw:=upcase(rw);
  1285. if not (rw in ['R','W']) then
  1286. begin
  1287. LinuxError:=Sys_enoent;
  1288. exit;
  1289. end;
  1290. AssignPipe(pipi,pipo);
  1291. if Linuxerror<>0 then
  1292. exit;
  1293. pid:=fork;
  1294. if linuxerror<>0 then
  1295. begin
  1296. close(pipi);
  1297. close(pipo);
  1298. exit;
  1299. end;
  1300. if pid=0 then
  1301. begin
  1302. { We're in the child }
  1303. if rw='W' then
  1304. begin
  1305. close(pipo);
  1306. dup2(pipi,input);
  1307. close(pipi);
  1308. if linuxerror<>0 then
  1309. halt(127);
  1310. end
  1311. else
  1312. begin
  1313. close(pipi);
  1314. dup2(pipo,output);
  1315. close(pipo);
  1316. if linuxerror<>0 then
  1317. halt(127);
  1318. end;
  1319. pp:=createshellargv(prog);
  1320. Execve(pp^,pp,envp);
  1321. halt(127);
  1322. end
  1323. else
  1324. begin
  1325. { We're in the parent }
  1326. if rw='W' then
  1327. begin
  1328. close(pipi);
  1329. f:=pipo;
  1330. textrec(f).bufptr:=@textrec(f).buffer;
  1331. end
  1332. else
  1333. begin
  1334. close(pipo);
  1335. f:=pipi;
  1336. textrec(f).bufptr:=@textrec(f).buffer;
  1337. end;
  1338. {Save the process ID - needed when closing }
  1339. pl:=@(textrec(f).userdata[2]);
  1340. pl^:=pid;
  1341. textrec(f).closefunc:=@PCloseText;
  1342. end;
  1343. end;
  1344. Procedure POpen(var F:file;const Prog:String;rw:char);
  1345. {
  1346. Starts the program in 'Prog' and makes it's input or out put the
  1347. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1348. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1349. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1350. read from 'f'.
  1351. }
  1352. var
  1353. pipi,
  1354. pipo : file;
  1355. pid : longint;
  1356. pl : ^longint;
  1357. p,pp : ppchar;
  1358. temp : string[255];
  1359. begin
  1360. LinuxError:=0;
  1361. rw:=upcase(rw);
  1362. if not (rw in ['R','W']) then
  1363. begin
  1364. LinuxError:=Sys_enoent;
  1365. exit;
  1366. end;
  1367. AssignPipe(pipi,pipo);
  1368. if Linuxerror<>0 then
  1369. exit;
  1370. pid:=fork;
  1371. if linuxerror<>0 then
  1372. begin
  1373. close(pipi);
  1374. close(pipo);
  1375. exit;
  1376. end;
  1377. if pid=0 then
  1378. begin
  1379. { We're in the child }
  1380. if rw='W' then
  1381. begin
  1382. close(pipo);
  1383. dup2(filerec(pipi).handle,stdinputhandle);
  1384. close(pipi);
  1385. if linuxerror<>0 then
  1386. halt(127);
  1387. end
  1388. else
  1389. begin
  1390. close(pipi);
  1391. dup2(filerec(pipo).handle,stdoutputhandle);
  1392. close(pipo);
  1393. if linuxerror<>0 then
  1394. halt(127);
  1395. end;
  1396. getmem(pp,sizeof(pchar)*4);
  1397. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1398. p:=pp;
  1399. p^:=@temp[1];
  1400. inc(p);
  1401. p^:=@temp[9];
  1402. inc(p);
  1403. p^:=@temp[12];
  1404. inc(p);
  1405. p^:=Nil;
  1406. Execve('/bin/sh',pp,envp);
  1407. halt(127);
  1408. end
  1409. else
  1410. begin
  1411. { We're in the parent }
  1412. if rw='W' then
  1413. begin
  1414. close(pipi);
  1415. f:=pipo;
  1416. end
  1417. else
  1418. begin
  1419. close(pipo);
  1420. f:=pipi;
  1421. end;
  1422. {Save the process ID - needed when closing }
  1423. pl:=@(filerec(f).userdata[2]);
  1424. pl^:=pid;
  1425. end;
  1426. end;
  1427. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1428. {
  1429. Starts the program in 'Prog' and makes its input and output the
  1430. other end of two pipes, which are the stdin and stdout of a program
  1431. specified in 'Prog'.
  1432. streamout can be used to write to the program, streamin can be used to read
  1433. the output of the program. See the following diagram :
  1434. Parent Child
  1435. STreamout --> Input
  1436. Streamin <-- Output
  1437. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1438. }
  1439. var
  1440. pipi,
  1441. pipo : text;
  1442. pid : longint;
  1443. pl : ^Longint;
  1444. begin
  1445. LinuxError:=0;
  1446. AssignStream:=-1;
  1447. AssignPipe(streamin,pipo);
  1448. if Linuxerror<>0 then
  1449. exit;
  1450. AssignPipe(pipi,streamout);
  1451. if Linuxerror<>0 then
  1452. exit;
  1453. pid:=fork;
  1454. if linuxerror<>0 then
  1455. begin
  1456. close(pipi);
  1457. close(pipo);
  1458. close (streamin);
  1459. close (streamout);
  1460. exit;
  1461. end;
  1462. if pid=0 then
  1463. begin
  1464. { We're in the child }
  1465. { Close what we don't need }
  1466. close(streamout);
  1467. close(streamin);
  1468. dup2(pipi,input);
  1469. if linuxerror<>0 then
  1470. halt(127);
  1471. close(pipi);
  1472. dup2(pipo,output);
  1473. if linuxerror<>0 then
  1474. halt (127);
  1475. close(pipo);
  1476. Execl(Prog);
  1477. halt(127);
  1478. end
  1479. else
  1480. begin
  1481. { we're in the parent}
  1482. close(pipo);
  1483. close(pipi);
  1484. {Save the process ID - needed when closing }
  1485. pl:=@(textrec(StreamIn).userdata[2]);
  1486. pl^:=pid;
  1487. textrec(StreamIn).closefunc:=@PCloseText;
  1488. {Save the process ID - needed when closing }
  1489. pl:=@(textrec(StreamOut).userdata[2]);
  1490. pl^:=pid;
  1491. textrec(StreamOut).closefunc:=@PCloseText;
  1492. AssignStream:=Pid;
  1493. end;
  1494. end;
  1495. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1496. {
  1497. Starts the program in 'prog' and makes its input, output and error output the
  1498. other end of three pipes, which are the stdin, stdout and stderr of a program
  1499. specified in 'prog'.
  1500. StreamOut can be used to write to the program, StreamIn can be used to read
  1501. the output of the program, StreamErr reads the error output of the program.
  1502. See the following diagram :
  1503. Parent Child
  1504. StreamOut --> StdIn (input)
  1505. StreamIn <-- StdOut (output)
  1506. StreamErr <-- StdErr (error output)
  1507. }
  1508. var
  1509. PipeIn, PipeOut, PipeErr: text;
  1510. pid: LongInt;
  1511. pl: ^LongInt;
  1512. begin
  1513. LinuxError := 0;
  1514. AssignStream := -1;
  1515. // Assign pipes
  1516. AssignPipe(StreamIn, PipeOut);
  1517. if LinuxError <> 0 then exit;
  1518. AssignPipe(StreamErr, PipeErr);
  1519. if LinuxError <> 0 then begin
  1520. Close(StreamIn);
  1521. Close(PipeOut);
  1522. exit;
  1523. end;
  1524. AssignPipe(PipeIn, StreamOut);
  1525. if LinuxError <> 0 then begin
  1526. Close(StreamIn);
  1527. Close(PipeOut);
  1528. Close(StreamErr);
  1529. Close(PipeErr);
  1530. exit;
  1531. end;
  1532. // Fork
  1533. pid := Fork;
  1534. if LinuxError <> 0 then begin
  1535. Close(StreamIn);
  1536. Close(PipeOut);
  1537. Close(StreamErr);
  1538. Close(PipeErr);
  1539. Close(PipeIn);
  1540. Close(StreamOut);
  1541. exit;
  1542. end;
  1543. if pid = 0 then begin
  1544. // *** We are in the child ***
  1545. // Close what we don not need
  1546. Close(StreamOut);
  1547. Close(StreamIn);
  1548. Close(StreamErr);
  1549. // Connect pipes
  1550. dup2(PipeIn, Input);
  1551. if LinuxError <> 0 then Halt(127);
  1552. Close(PipeIn);
  1553. dup2(PipeOut, Output);
  1554. if LinuxError <> 0 then Halt(127);
  1555. Close(PipeOut);
  1556. dup2(PipeErr, StdErr);
  1557. if LinuxError <> 0 then Halt(127);
  1558. Close(PipeErr);
  1559. // Execute program
  1560. Execl(Prog);
  1561. Halt(127);
  1562. end else begin
  1563. // *** We are in the parent ***
  1564. Close(PipeErr);
  1565. Close(PipeOut);
  1566. Close(PipeIn);
  1567. // Save the process ID - needed when closing
  1568. pl := @(TextRec(StreamIn).userdata[2]);
  1569. pl^ := pid;
  1570. TextRec(StreamIn).closefunc := @PCloseText;
  1571. // Save the process ID - needed when closing
  1572. pl := @(TextRec(StreamOut).userdata[2]);
  1573. pl^ := pid;
  1574. TextRec(StreamOut).closefunc := @PCloseText;
  1575. // Save the process ID - needed when closing
  1576. pl := @(TextRec(StreamErr).userdata[2]);
  1577. pl^ := pid;
  1578. TextRec(StreamErr).closefunc := @PCloseText;
  1579. AssignStream := pid;
  1580. end;
  1581. end;
  1582. {******************************************************************************
  1583. General information calls
  1584. ******************************************************************************}
  1585. Function GetEnv(P:string):Pchar;
  1586. {
  1587. Searches the environment for a string with name p and
  1588. returns a pchar to it's value.
  1589. A pchar is used to accomodate for strings of length > 255
  1590. }
  1591. var
  1592. ep : ppchar;
  1593. found : boolean;
  1594. Begin
  1595. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1596. ep:=envp;
  1597. found:=false;
  1598. if ep<>nil then
  1599. begin
  1600. while (not found) and (ep^<>nil) do
  1601. begin
  1602. if strlcomp(@p[1],(ep^),length(p))=0 then
  1603. found:=true
  1604. else
  1605. inc(ep);
  1606. end;
  1607. end;
  1608. if found then
  1609. getenv:=ep^+length(p)
  1610. else
  1611. getenv:=nil;
  1612. end;
  1613. {$ifndef bsd}
  1614. Function GetDomainName:String;
  1615. {
  1616. Get machines domain name. Returns empty string if not set.
  1617. }
  1618. Var
  1619. Sysn : utsname;
  1620. begin
  1621. Uname(Sysn);
  1622. linuxerror:=errno;
  1623. If linuxerror<>0 then
  1624. getdomainname:=''
  1625. else
  1626. getdomainname:=strpas(@Sysn.domainname[0]);
  1627. end;
  1628. Function GetHostName:String;
  1629. {
  1630. Get machines name. Returns empty string if not set.
  1631. }
  1632. Var
  1633. Sysn : utsname;
  1634. begin
  1635. uname(Sysn);
  1636. linuxerror:=errno;
  1637. If linuxerror<>0 then
  1638. gethostname:=''
  1639. else
  1640. gethostname:=strpas(@Sysn.nodename[0]);
  1641. end;
  1642. {$endif}
  1643. {******************************************************************************
  1644. Signal handling calls
  1645. ******************************************************************************}
  1646. procedure SigRaise(sig:integer);
  1647. begin
  1648. Kill(GetPid,Sig);
  1649. end;
  1650. {******************************************************************************
  1651. IOCtl and Termios calls
  1652. ******************************************************************************}
  1653. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1654. begin
  1655. {$ifndef BSD}
  1656. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1657. {$else}
  1658. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1659. {$endif}
  1660. end;
  1661. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  1662. var
  1663. nr:longint;
  1664. begin
  1665. {$ifndef BSD}
  1666. case OptAct of
  1667. TCSANOW : nr:=TCSETS;
  1668. TCSADRAIN : nr:=TCSETSW;
  1669. TCSAFLUSH : nr:=TCSETSF;
  1670. {$else}
  1671. case OptAct of
  1672. TCSANOW : nr:=TIOCSETA;
  1673. TCSADRAIN : nr:=TIOCSETAW;
  1674. TCSAFLUSH : nr:=TIOCSETAF;
  1675. {$endif}
  1676. else
  1677. begin
  1678. ErrNo:=Sys_EINVAL;
  1679. TCSetAttr:=false;
  1680. exit;
  1681. end;
  1682. end;
  1683. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1684. end;
  1685. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1686. begin
  1687. {$ifndef BSD}
  1688. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1689. {$else}
  1690. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1691. {$endif}
  1692. end;
  1693. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1694. begin
  1695. {$ifndef BSD}
  1696. CFSetISpeed(tios,speed);
  1697. {$else}
  1698. tios.c_ospeed:=speed;
  1699. {$endif}
  1700. end;
  1701. Procedure CFMakeRaw(var tios:TermIOS);
  1702. begin
  1703. {$ifndef BSD}
  1704. with tios do
  1705. begin
  1706. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1707. INLCR or IGNCR or ICRNL or IXON));
  1708. c_oflag:=c_oflag and (not OPOST);
  1709. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1710. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1711. end;
  1712. {$else}
  1713. with tios do
  1714. begin
  1715. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1716. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1717. IGNPAR));
  1718. c_iflag:=c_iflag OR IGNBRK;
  1719. c_oflag:=c_oflag and (not OPOST);
  1720. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1721. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1722. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1723. c_cc[VMIN]:=1;
  1724. c_cc[VTIME]:=0;
  1725. end;
  1726. {$endif}
  1727. end;
  1728. Function TCSendBreak(fd,duration:longint):boolean;
  1729. begin
  1730. {$ifndef BSD}
  1731. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1732. {$else}
  1733. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1734. {$endif}
  1735. end;
  1736. Function TCSetPGrp(fd,id:longint):boolean;
  1737. begin
  1738. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1739. end;
  1740. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1741. begin
  1742. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1743. end;
  1744. Function TCDrain(fd:longint):boolean;
  1745. begin
  1746. {$ifndef BSD}
  1747. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1748. {$else}
  1749. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1750. {$endif}
  1751. end;
  1752. Function TCFlow(fd,act:longint):boolean;
  1753. begin
  1754. {$ifndef BSD}
  1755. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1756. {$else}
  1757. case act OF
  1758. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1759. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1760. TCIOFF : {N/I}
  1761. end;
  1762. {$endif}
  1763. end;
  1764. Function TCFlush(fd,qsel:longint):boolean;
  1765. begin
  1766. {$ifndef BSD}
  1767. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1768. {$else}
  1769. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1770. {$endif}
  1771. end;
  1772. Function IsATTY(Handle:Longint):Boolean;
  1773. {
  1774. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1775. }
  1776. var
  1777. t : Termios;
  1778. begin
  1779. IsAtty:=TCGetAttr(Handle,t);
  1780. end;
  1781. Function IsATTY(f: text):Boolean;
  1782. {
  1783. Idem as previous, only now for text variables.
  1784. }
  1785. begin
  1786. IsATTY:=IsaTTY(textrec(f).handle);
  1787. end;
  1788. function TTYName(Handle:Longint):string;
  1789. {
  1790. Return the name of the current tty described by handle f.
  1791. returns empty string in case of an error.
  1792. }
  1793. var
  1794. mydev,
  1795. myino : longint;
  1796. st : stat;
  1797. function mysearch(n:string): boolean;
  1798. {searches recursively for the device in the directory given by n,
  1799. returns true if found and sets the name of the device in ttyname}
  1800. var dirstream : pdir;
  1801. d : pdirent;
  1802. name : string;
  1803. st : stat;
  1804. begin
  1805. dirstream:=opendir(n);
  1806. if (linuxerror<>0) then
  1807. exit;
  1808. d:=Readdir(dirstream);
  1809. while (d<>nil) do
  1810. begin
  1811. name:=n+'/'+strpas(@(d^.name));
  1812. fstat(name,st);
  1813. if linuxerror=0 then
  1814. begin
  1815. if ((st.mode and $E000)=$4000) and { if it is a directory }
  1816. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  1817. (strpas(@(d^.name))<>'..') and
  1818. (strpas(@(d^.name))<>'fd') then
  1819. begin {we found a directory, search inside it}
  1820. if mysearch(name) then
  1821. begin {the device is here}
  1822. closedir(dirstream); {then don't continue searching}
  1823. mysearch:=true;
  1824. exit;
  1825. end;
  1826. end
  1827. else if (d^.ino=myino) and (st.dev=mydev) then
  1828. begin
  1829. closedir(dirstream);
  1830. ttyname:=name;
  1831. mysearch:=true;
  1832. exit;
  1833. end;
  1834. end;
  1835. d:=Readdir(dirstream);
  1836. end;
  1837. closedir(dirstream);
  1838. mysearch:=false;
  1839. end;
  1840. begin
  1841. TTYName:='';
  1842. fstat(handle,st);
  1843. if (errno<>0) and isatty (handle) then
  1844. exit;
  1845. mydev:=st.dev;
  1846. myino:=st.ino;
  1847. mysearch('/dev');
  1848. end;
  1849. function TTYName(var F:Text):string;
  1850. {
  1851. Idem as previous, only now for text variables;
  1852. }
  1853. begin
  1854. TTYName:=TTYName(textrec(f).handle);
  1855. end;
  1856. {******************************************************************************
  1857. Utility calls
  1858. ******************************************************************************}
  1859. Function Octal(l:longint):longint;
  1860. {
  1861. Convert an octal specified number to decimal;
  1862. }
  1863. var
  1864. octnr,
  1865. oct : longint;
  1866. begin
  1867. octnr:=0;
  1868. oct:=0;
  1869. while (l>0) do
  1870. begin
  1871. oct:=oct or ((l mod 10) shl octnr);
  1872. l:=l div 10;
  1873. inc(octnr,3);
  1874. end;
  1875. Octal:=oct;
  1876. end;
  1877. Function StringToPPChar(Var S:STring):ppchar;
  1878. {
  1879. Create a PPChar to structure of pchars which are the arguments specified
  1880. in the string S. Especially usefull for creating an ArgV for Exec-calls
  1881. }
  1882. var
  1883. nr : longint;
  1884. Buf : ^char;
  1885. p : ppchar;
  1886. begin
  1887. s:=s+#0;
  1888. buf:=@s[1];
  1889. nr:=0;
  1890. while(buf^<>#0) do
  1891. begin
  1892. while (buf^ in [' ',#8,#10]) do
  1893. inc(buf);
  1894. inc(nr);
  1895. while not (buf^ in [' ',#0,#8,#10]) do
  1896. inc(buf);
  1897. end;
  1898. getmem(p,nr*4);
  1899. StringToPPChar:=p;
  1900. if p=nil then
  1901. begin
  1902. LinuxError:=sys_enomem;
  1903. exit;
  1904. end;
  1905. buf:=@s[1];
  1906. while (buf^<>#0) do
  1907. begin
  1908. while (buf^ in [' ',#8,#10]) do
  1909. begin
  1910. buf^:=#0;
  1911. inc(buf);
  1912. end;
  1913. p^:=buf;
  1914. inc(p);
  1915. p^:=nil;
  1916. while not (buf^ in [' ',#0,#8,#10]) do
  1917. inc(buf);
  1918. end;
  1919. end;
  1920. {
  1921. function FExpand (const Path: PathStr): PathStr;
  1922. - declared in fexpand.inc
  1923. }
  1924. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1925. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1926. {$I fexpand.inc}
  1927. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1928. {$UNDEF FPC_FEXPAND_TILDE}
  1929. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1930. {
  1931. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1932. returns an empty string if not found. Wildcards are NOT allowed.
  1933. If dirlist is empty, it is set to '.'
  1934. }
  1935. Var
  1936. NewDir : PathStr;
  1937. p1 : Longint;
  1938. Info : Stat;
  1939. Begin
  1940. {Replace ':' with ';'}
  1941. for p1:=1to length(dirlist) do
  1942. if dirlist[p1]=':' then
  1943. dirlist[p1]:=';';
  1944. {Check for WildCards}
  1945. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1946. FSearch:='' {No wildcards allowed in these things.}
  1947. Else
  1948. Begin
  1949. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1950. Repeat
  1951. p1:=Pos(';',DirList);
  1952. If p1=0 Then
  1953. p1:=255;
  1954. NewDir:=Copy(DirList,1,P1 - 1);
  1955. if NewDir[Length(NewDir)]<>'/' then
  1956. NewDir:=NewDir+'/';
  1957. NewDir:=NewDir+Path;
  1958. Delete(DirList,1,p1);
  1959. if FStat(NewDir,Info) then
  1960. Begin
  1961. If Pos('./',NewDir)=1 Then
  1962. Delete(NewDir,1,2);
  1963. {DOS strips off an initial .\}
  1964. End
  1965. Else
  1966. NewDir:='';
  1967. Until (DirList='') or (Length(NewDir) > 0);
  1968. FSearch:=NewDir;
  1969. End;
  1970. End;
  1971. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  1972. Var
  1973. DotPos,SlashPos,i : longint;
  1974. Begin
  1975. SlashPos:=0;
  1976. DotPos:=256;
  1977. i:=Length(Path);
  1978. While (i>0) and (SlashPos=0) Do
  1979. Begin
  1980. If (DotPos=256) and (Path[i]='.') Then
  1981. begin
  1982. DotPos:=i;
  1983. end;
  1984. If (Path[i]='/') Then
  1985. SlashPos:=i;
  1986. Dec(i);
  1987. End;
  1988. Ext:=Copy(Path,DotPos,255);
  1989. Dir:=Copy(Path,1,SlashPos);
  1990. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  1991. End;
  1992. Function Dirname(Const path:pathstr):pathstr;
  1993. {
  1994. This function returns the directory part of a complete path.
  1995. Unless the directory is root '/', The last character is not
  1996. a slash.
  1997. }
  1998. var
  1999. Dir : PathStr;
  2000. Name : NameStr;
  2001. Ext : ExtStr;
  2002. begin
  2003. FSplit(Path,Dir,Name,Ext);
  2004. if length(Dir)>1 then
  2005. Delete(Dir,length(Dir),1);
  2006. DirName:=Dir;
  2007. end;
  2008. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2009. {
  2010. This function returns the filename part of a complete path. If suf is
  2011. supplied, it is cut off the filename.
  2012. }
  2013. var
  2014. Dir : PathStr;
  2015. Name : NameStr;
  2016. Ext : ExtStr;
  2017. begin
  2018. FSplit(Path,Dir,Name,Ext);
  2019. if Suf<>Ext then
  2020. Name:=Name+Ext;
  2021. BaseName:=Name;
  2022. end;
  2023. Function FNMatch(const Pattern,Name:string):Boolean;
  2024. Var
  2025. LenPat,LenName : longint;
  2026. Function DoFNMatch(i,j:longint):Boolean;
  2027. Var
  2028. Found : boolean;
  2029. Begin
  2030. Found:=true;
  2031. While Found and (i<=LenPat) Do
  2032. Begin
  2033. Case Pattern[i] of
  2034. '?' : Found:=(j<=LenName);
  2035. '*' : Begin
  2036. {find the next character in pattern, different of ? and *}
  2037. while Found and (i<LenPat) do
  2038. begin
  2039. inc(i);
  2040. case Pattern[i] of
  2041. '*' : ;
  2042. '?' : begin
  2043. inc(j);
  2044. Found:=(j<=LenName);
  2045. end;
  2046. else
  2047. Found:=false;
  2048. end;
  2049. end;
  2050. {Now, find in name the character which i points to, if the * or ?
  2051. wasn't the last character in the pattern, else, use up all the
  2052. chars in name}
  2053. Found:=true;
  2054. if (i<=LenPat) then
  2055. begin
  2056. repeat
  2057. {find a letter (not only first !) which maches pattern[i]}
  2058. while (j<=LenName) and (name[j]<>pattern[i]) do
  2059. inc (j);
  2060. if (j<LenName) then
  2061. begin
  2062. if DoFnMatch(i+1,j+1) then
  2063. begin
  2064. i:=LenPat;
  2065. j:=LenName;{we can stop}
  2066. Found:=true;
  2067. end
  2068. else
  2069. inc(j);{We didn't find one, need to look further}
  2070. end;
  2071. until (j>=LenName);
  2072. end
  2073. else
  2074. j:=LenName;{we can stop}
  2075. end;
  2076. else {not a wildcard character in pattern}
  2077. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2078. end;
  2079. inc(i);
  2080. inc(j);
  2081. end;
  2082. DoFnMatch:=Found and (j>LenName);
  2083. end;
  2084. Begin {start FNMatch}
  2085. LenPat:=Length(Pattern);
  2086. LenName:=Length(Name);
  2087. FNMatch:=DoFNMatch(1,1);
  2088. End;
  2089. Procedure Globfree(var p : pglob);
  2090. {
  2091. Release memory occupied by pglob structure, and names in it.
  2092. sets p to nil.
  2093. }
  2094. var
  2095. temp : pglob;
  2096. begin
  2097. while assigned(p) do
  2098. begin
  2099. temp:=p^.next;
  2100. if assigned(p^.name) then
  2101. freemem(p^.name);
  2102. dispose(p);
  2103. p:=temp;
  2104. end;
  2105. end;
  2106. Function Glob(Const path:pathstr):pglob;
  2107. {
  2108. Fills a tglob structure with entries matching path,
  2109. and returns a pointer to it. Returns nil on error,
  2110. linuxerror is set accordingly.
  2111. }
  2112. var
  2113. temp,
  2114. temp2 : string[255];
  2115. thedir : pdir;
  2116. buffer : pdirent;
  2117. root,
  2118. current : pglob;
  2119. begin
  2120. { Get directory }
  2121. temp:=dirname(path);
  2122. if temp='' then
  2123. temp:='.';
  2124. temp:=temp+#0;
  2125. thedir:=opendir(@temp[1]);
  2126. if thedir=nil then
  2127. begin
  2128. glob:=nil;
  2129. linuxerror:=errno;
  2130. exit;
  2131. end;
  2132. temp:=basename(path,''); { get the pattern }
  2133. if thedir^.fd<0 then
  2134. begin
  2135. linuxerror:=errno;
  2136. glob:=nil;
  2137. exit;
  2138. end;
  2139. {get the entries}
  2140. root:=nil;
  2141. current:=nil;
  2142. repeat
  2143. buffer:=Sys_readdir(thedir);
  2144. if buffer=nil then
  2145. break;
  2146. temp2:=strpas(@(buffer^.name[0]));
  2147. if fnmatch(temp,temp2) then
  2148. begin
  2149. if root=nil then
  2150. begin
  2151. new(root);
  2152. current:=root;
  2153. end
  2154. else
  2155. begin
  2156. new(current^.next);
  2157. current:=current^.next;
  2158. end;
  2159. if current=nil then
  2160. begin
  2161. linuxerror:=Sys_ENOMEM;
  2162. globfree(root);
  2163. break;
  2164. end;
  2165. current^.next:=nil;
  2166. getmem(current^.name,length(temp2)+1);
  2167. if current^.name=nil then
  2168. begin
  2169. linuxerror:=Sys_ENOMEM;
  2170. globfree(root);
  2171. break;
  2172. end;
  2173. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2174. end;
  2175. until false;
  2176. closedir(thedir);
  2177. glob:=root;
  2178. end;
  2179. {--------------------------------
  2180. FiledescriptorSets
  2181. --------------------------------}
  2182. Procedure FD_Zero(var fds:fdSet);
  2183. {
  2184. Clear the set of filedescriptors
  2185. }
  2186. begin
  2187. FillChar(fds,sizeof(fdSet),0);
  2188. end;
  2189. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2190. {
  2191. Remove fd from the set of filedescriptors
  2192. }
  2193. begin
  2194. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2195. end;
  2196. Procedure FD_Set(fd:longint;var fds:fdSet);
  2197. {
  2198. Add fd to the set of filedescriptors
  2199. }
  2200. begin
  2201. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2202. end;
  2203. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2204. {
  2205. Test if fd is part of the set of filedescriptors
  2206. }
  2207. begin
  2208. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2209. end;
  2210. Function GetFS (var T:Text):longint;
  2211. {
  2212. Get File Descriptor of a text file.
  2213. }
  2214. begin
  2215. if textrec(t).mode=fmclosed then
  2216. exit(-1)
  2217. else
  2218. GETFS:=textrec(t).Handle
  2219. end;
  2220. Function GetFS(Var F:File):longint;
  2221. {
  2222. Get File Descriptor of an unTyped file.
  2223. }
  2224. begin
  2225. { Handle and mode are on the same place in textrec and filerec. }
  2226. if filerec(f).mode=fmclosed then
  2227. exit(-1)
  2228. else
  2229. GETFS:=filerec(f).Handle
  2230. end;
  2231. {--------------------------------
  2232. Stat.Mode Macro's
  2233. --------------------------------}
  2234. Function S_ISLNK(m:word):boolean;
  2235. {
  2236. Check mode field of inode for link.
  2237. }
  2238. begin
  2239. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2240. end;
  2241. Function S_ISREG(m:word):boolean;
  2242. {
  2243. Check mode field of inode for regular file.
  2244. }
  2245. begin
  2246. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2247. end;
  2248. Function S_ISDIR(m:word):boolean;
  2249. {
  2250. Check mode field of inode for directory.
  2251. }
  2252. begin
  2253. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2254. end;
  2255. Function S_ISCHR(m:word):boolean;
  2256. {
  2257. Check mode field of inode for character device.
  2258. }
  2259. begin
  2260. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2261. end;
  2262. Function S_ISBLK(m:word):boolean;
  2263. {
  2264. Check mode field of inode for block device.
  2265. }
  2266. begin
  2267. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2268. end;
  2269. Function S_ISFIFO(m:word):boolean;
  2270. {
  2271. Check mode field of inode for named pipe (FIFO).
  2272. }
  2273. begin
  2274. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2275. end;
  2276. Function S_ISSOCK(m:word):boolean;
  2277. {
  2278. Check mode field of inode for socket.
  2279. }
  2280. begin
  2281. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2282. end;
  2283. {--------------------------------
  2284. Memory functions
  2285. --------------------------------}
  2286. {$IFDEF I386}
  2287. Procedure WritePort (Port : Longint; Value : Byte);
  2288. {
  2289. Writes 'Value' to port 'Port'
  2290. }
  2291. begin
  2292. asm
  2293. movl port,%edx
  2294. movb value,%al
  2295. outb %al,%dx
  2296. end ['EAX','EDX'];
  2297. end;
  2298. Procedure WritePort (Port : Longint; Value : Word);
  2299. {
  2300. Writes 'Value' to port 'Port'
  2301. }
  2302. begin
  2303. asm
  2304. movl port,%edx
  2305. movw value,%ax
  2306. outw %ax,%dx
  2307. end ['EAX','EDX'];
  2308. end;
  2309. Procedure WritePort (Port : Longint; Value : Longint);
  2310. {
  2311. Writes 'Value' to port 'Port'
  2312. }
  2313. begin
  2314. asm
  2315. movl port,%edx
  2316. movl value,%eax
  2317. outl %eax,%dx
  2318. end ['EAX','EDX'];
  2319. end;
  2320. Procedure WritePortB (Port : Longint; Value : Byte);
  2321. {
  2322. Writes 'Value' to port 'Port'
  2323. }
  2324. begin
  2325. asm
  2326. movl port,%edx
  2327. movb value,%al
  2328. outb %al,%dx
  2329. end ['EAX','EDX'];
  2330. end;
  2331. Procedure WritePortW (Port : Longint; Value : Word);
  2332. {
  2333. Writes 'Value' to port 'Port'
  2334. }
  2335. begin
  2336. asm
  2337. movl port,%edx
  2338. movw value,%ax
  2339. outw %ax,%dx
  2340. end ['EAX','EDX'];
  2341. end;
  2342. Procedure WritePortL (Port : Longint; Value : Longint);
  2343. {
  2344. Writes 'Value' to port 'Port'
  2345. }
  2346. begin
  2347. asm
  2348. movl port,%edx
  2349. movl value,%eax
  2350. outl %eax,%dx
  2351. end ['EAX','EDX'];
  2352. end;
  2353. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2354. {
  2355. Writes 'Count' longints from 'Buf' to Port
  2356. }
  2357. begin
  2358. asm
  2359. movl count,%ecx
  2360. movl buf,%esi
  2361. movl port,%edx
  2362. cld
  2363. rep
  2364. outsl
  2365. end ['ECX','ESI','EDX'];
  2366. end;
  2367. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2368. {
  2369. Writes 'Count' words from 'Buf' to Port
  2370. }
  2371. begin
  2372. asm
  2373. movl count,%ecx
  2374. movl buf,%esi
  2375. movl port,%edx
  2376. cld
  2377. rep
  2378. outsw
  2379. end ['ECX','ESI','EDX'];
  2380. end;
  2381. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2382. {
  2383. Writes 'Count' bytes from 'Buf' to Port
  2384. }
  2385. begin
  2386. asm
  2387. movl count,%ecx
  2388. movl buf,%esi
  2389. movl port,%edx
  2390. cld
  2391. rep
  2392. outsb
  2393. end ['ECX','ESI','EDX'];
  2394. end;
  2395. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2396. {
  2397. Reads 'Value' from port 'Port'
  2398. }
  2399. begin
  2400. asm
  2401. movl port,%edx
  2402. inb %dx,%al
  2403. movl value,%edx
  2404. movb %al,(%edx)
  2405. end ['EAX','EDX'];
  2406. end;
  2407. Procedure ReadPort (Port : Longint; Var Value : Word);
  2408. {
  2409. Reads 'Value' from port 'Port'
  2410. }
  2411. begin
  2412. asm
  2413. movl port,%edx
  2414. inw %dx,%ax
  2415. movl value,%edx
  2416. movw %ax,(%edx)
  2417. end ['EAX','EDX'];
  2418. end;
  2419. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2420. {
  2421. Reads 'Value' from port 'Port'
  2422. }
  2423. begin
  2424. asm
  2425. movl port,%edx
  2426. inl %dx,%eax
  2427. movl value,%edx
  2428. movl %eax,(%edx)
  2429. end ['EAX','EDX'];
  2430. end;
  2431. function ReadPortB (Port : Longint): Byte; assembler;
  2432. {
  2433. Reads a byte from port 'Port'
  2434. }
  2435. asm
  2436. xorl %eax,%eax
  2437. movl port,%edx
  2438. inb %dx,%al
  2439. end ['EAX','EDX'];
  2440. function ReadPortW (Port : Longint): Word; assembler;
  2441. {
  2442. Reads a word from port 'Port'
  2443. }
  2444. asm
  2445. xorl %eax,%eax
  2446. movl port,%edx
  2447. inw %dx,%ax
  2448. end ['EAX','EDX'];
  2449. function ReadPortL (Port : Longint): LongInt; assembler;
  2450. {
  2451. Reads a LongInt from port 'Port'
  2452. }
  2453. asm
  2454. movl port,%edx
  2455. inl %dx,%eax
  2456. end ['EAX','EDX'];
  2457. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  2458. {
  2459. Reads 'Count' longints from port 'Port' to 'Buf'.
  2460. }
  2461. begin
  2462. asm
  2463. movl count,%ecx
  2464. movl buf,%edi
  2465. movl port,%edx
  2466. cld
  2467. rep
  2468. insl
  2469. end ['ECX','EDI','EDX'];
  2470. end;
  2471. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  2472. {
  2473. Reads 'Count' words from port 'Port' to 'Buf'.
  2474. }
  2475. begin
  2476. asm
  2477. movl count,%ecx
  2478. movl buf,%edi
  2479. movl port,%edx
  2480. cld
  2481. rep
  2482. insw
  2483. end ['ECX','EDI','EDX'];
  2484. end;
  2485. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  2486. {
  2487. Reads 'Count' bytes from port 'Port' to 'Buf'.
  2488. }
  2489. begin
  2490. asm
  2491. movl count,%ecx
  2492. movl buf,%edi
  2493. movl port,%edx
  2494. cld
  2495. rep
  2496. insb
  2497. end ['ECX','EDI','EDX'];
  2498. end;
  2499. {$ENDIF}
  2500. Initialization
  2501. InitLocalTime;
  2502. finalization
  2503. DoneLocalTime;
  2504. End.
  2505. {
  2506. $Log$
  2507. Revision 1.14 2001-07-12 12:42:39 marco
  2508. * Fixes to the FreeBSD compability of the datetime patches
  2509. Revision 1.13 2001/07/12 07:20:05 michael
  2510. + Added setdate/time/datetime functions
  2511. Revision 1.12 2001/06/20 15:24:47 marco
  2512. * readlink for Unix fix.
  2513. Revision 1.11 2001/06/19 17:19:50 marco
  2514. * Again uncommented readlink. Merges or typo, I dunno
  2515. Revision 1.10 2001/06/13 22:20:11 hajny
  2516. + platform specific information
  2517. Revision 1.9 2001/06/03 20:19:09 peter
  2518. * FSStat to StatFS
  2519. * StatFS structure to TStatFS
  2520. Revision 1.8 2001/06/02 00:31:31 peter
  2521. * merge unix updates from the 1.0 branch, mostly related to the
  2522. solaris target
  2523. Revision 1.7 2001/04/19 12:57:33 marco
  2524. * Readlink uncommented for FreeBSD.
  2525. Revision 1.6 2001/04/13 22:37:21 peter
  2526. * remove warning
  2527. Revision 1.5 2001/03/27 11:47:25 michael
  2528. + Fixed F_[G,S]etOwn constants. By Alexander Sychev
  2529. Revision 1.4 2001/03/17 16:04:37 hajny
  2530. * FExpand omission fixed
  2531. Revision 1.3 2001/03/16 20:09:58 hajny
  2532. * universal FExpand
  2533. Revision 1.2 2001/01/22 07:25:10 marco
  2534. * IOPERM for FreeBSD. Port routines moved from linsysca to Unix again .
  2535. Revision 1.1 2001/01/21 20:21:41 marco
  2536. * Rename fest II. Rtl OK
  2537. Revision 1.6 2000/12/28 20:42:12 peter
  2538. * ttyname fix from the mailinglist (merged)
  2539. Revision 1.5 2000/10/26 22:51:12 peter
  2540. * nano sleep (merged)
  2541. Revision 1.4 2000/10/11 13:59:16 marco
  2542. * FreeBSD TermIOS support and minor changes to some related files.
  2543. Revision 1.3 2000/10/10 12:02:35 marco
  2544. * Terminal stuff of Linux moved to separate file. I think it is too
  2545. much to do with ifdefs.
  2546. Revision 1.2 2000/09/18 13:14:50 marco
  2547. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2548. Revision 1.7 2000/09/12 08:51:43 marco
  2549. * fixed some small problems left from merging. (waitpid has now last param longint)
  2550. Revision 1.6 2000/09/11 14:05:31 marco
  2551. * FreeBSD support and removed old signalhandling
  2552. Revision 1.5 2000/09/06 20:47:34 peter
  2553. * removed previous fsplit() patch as it's not the correct behaviour for
  2554. LFNs. The code showing the bug could easily be adapted (merged)
  2555. Revision 1.4 2000/09/04 20:17:53 peter
  2556. * fixed previous commit (merged)
  2557. Revision 1.3 2000/09/04 19:38:13 peter
  2558. * fsplit with .. fix from Thomas (merged)
  2559. Revision 1.2 2000/07/13 11:33:48 michael
  2560. + removed logs
  2561. }