2
0

unix.pp 66 KB

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