unix.pp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022
  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:=Abs(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. LinuxError:=ErrNo;
  1124. end;
  1125. procedure SeekDir(p:pdir;off:longint);
  1126. begin
  1127. if p=nil then
  1128. begin
  1129. errno:=Sys_EBADF;
  1130. exit;
  1131. end;
  1132. {$ifndef bsd}
  1133. p^.nextoff:=Sys_lseek(p^.fd,off,seek_set);
  1134. {$endif}
  1135. p^.size:=0;
  1136. p^.loc:=0;
  1137. end;
  1138. function TellDir(p:pdir):longint;
  1139. begin
  1140. if p=nil then
  1141. begin
  1142. errno:=Sys_EBADF;
  1143. telldir:=-1;
  1144. exit;
  1145. end;
  1146. telldir:=Sys_lseek(p^.fd,0,seek_cur)
  1147. { We could try to use the nextoff field here, but on my 1.2.13
  1148. kernel, this gives nothing... This may have to do with
  1149. the readdir implementation of libc... I also didn't find any trace of
  1150. the field in the kernel code itself, So I suspect it is an artifact of libc.
  1151. Michael. }
  1152. end;
  1153. Function ReadDir(P:pdir):pdirent;
  1154. begin
  1155. ReadDir:=Sys_ReadDir(p);
  1156. LinuxError:=Errno;
  1157. end;
  1158. {******************************************************************************
  1159. Pipes/Fifo
  1160. ******************************************************************************}
  1161. Procedure OpenPipe(var F:Text);
  1162. begin
  1163. case textrec(f).mode of
  1164. fmoutput :
  1165. if textrec(f).userdata[1]<>P_OUT then
  1166. textrec(f).mode:=fmclosed;
  1167. fminput :
  1168. if textrec(f).userdata[1]<>P_IN then
  1169. textrec(f).mode:=fmclosed;
  1170. else
  1171. textrec(f).mode:=fmclosed;
  1172. end;
  1173. end;
  1174. Procedure IOPipe(var F:text);
  1175. begin
  1176. case textrec(f).mode of
  1177. fmoutput :
  1178. begin
  1179. { first check if we need something to write, else we may
  1180. get a SigPipe when Close() is called (PFV) }
  1181. if textrec(f).bufpos>0 then
  1182. Sys_write(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  1183. end;
  1184. fminput :
  1185. textrec(f).bufend:=Sys_read(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  1186. end;
  1187. textrec(f).bufpos:=0;
  1188. end;
  1189. Procedure FlushPipe(var F:Text);
  1190. begin
  1191. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  1192. IOPipe(f);
  1193. textrec(f).bufpos:=0;
  1194. end;
  1195. Procedure ClosePipe(var F:text);
  1196. begin
  1197. textrec(f).mode:=fmclosed;
  1198. Sys_close(textrec(f).handle);
  1199. end;
  1200. Function AssignPipe(var pipe_in,pipe_out:text):boolean;
  1201. {
  1202. Sets up a pair of file variables, which act as a pipe. The first one can
  1203. be read from, the second one can be written to.
  1204. If the operation was unsuccesful, linuxerror is set.
  1205. }
  1206. var
  1207. f_in,f_out : longint;
  1208. begin
  1209. if not AssignPipe(f_in,f_out) then
  1210. begin
  1211. AssignPipe:=false;
  1212. exit;
  1213. end;
  1214. { Set up input }
  1215. Assign(Pipe_in,'');
  1216. Textrec(Pipe_in).Handle:=f_in;
  1217. Textrec(Pipe_in).Mode:=fmInput;
  1218. Textrec(Pipe_in).userdata[1]:=P_IN;
  1219. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  1220. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  1221. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  1222. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  1223. { Set up output }
  1224. Assign(Pipe_out,'');
  1225. Textrec(Pipe_out).Handle:=f_out;
  1226. Textrec(Pipe_out).Mode:=fmOutput;
  1227. Textrec(Pipe_out).userdata[1]:=P_OUT;
  1228. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  1229. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  1230. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  1231. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  1232. AssignPipe:=true;
  1233. end;
  1234. Function AssignPipe(var pipe_in,pipe_out:file):boolean;
  1235. {
  1236. Sets up a pair of file variables, which act as a pipe. The first one can
  1237. be read from, the second one can be written to.
  1238. If the operation was unsuccesful, linuxerror is set.
  1239. }
  1240. var
  1241. f_in,f_out : longint;
  1242. begin
  1243. if not AssignPipe(f_in,f_out) then
  1244. begin
  1245. AssignPipe:=false;
  1246. exit;
  1247. end;
  1248. { Set up input }
  1249. Assign(Pipe_in,'');
  1250. Filerec(Pipe_in).Handle:=f_in;
  1251. Filerec(Pipe_in).Mode:=fmInput;
  1252. Filerec(Pipe_in).recsize:=1;
  1253. Filerec(Pipe_in).userdata[1]:=P_IN;
  1254. { Set up output }
  1255. Assign(Pipe_out,'');
  1256. Filerec(Pipe_out).Handle:=f_out;
  1257. Filerec(Pipe_out).Mode:=fmoutput;
  1258. Filerec(Pipe_out).recsize:=1;
  1259. Filerec(Pipe_out).userdata[1]:=P_OUT;
  1260. AssignPipe:=true;
  1261. end;
  1262. Procedure PCloseText(Var F:text);
  1263. {
  1264. May not use @PClose due overloading
  1265. }
  1266. begin
  1267. PClose(f);
  1268. end;
  1269. Procedure POpen(var F:text;const Prog:String;rw:char);
  1270. {
  1271. Starts the program in 'Prog' and makes it's input or out put the
  1272. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1273. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1274. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1275. read from 'f'.
  1276. }
  1277. var
  1278. pipi,
  1279. pipo : text;
  1280. pid : longint;
  1281. pl : ^longint;
  1282. pp : ppchar;
  1283. begin
  1284. LinuxError:=0;
  1285. rw:=upcase(rw);
  1286. if not (rw in ['R','W']) then
  1287. begin
  1288. LinuxError:=Sys_enoent;
  1289. exit;
  1290. end;
  1291. AssignPipe(pipi,pipo);
  1292. if Linuxerror<>0 then
  1293. exit;
  1294. pid:=fork;
  1295. if linuxerror<>0 then
  1296. begin
  1297. close(pipi);
  1298. close(pipo);
  1299. exit;
  1300. end;
  1301. if pid=0 then
  1302. begin
  1303. { We're in the child }
  1304. if rw='W' then
  1305. begin
  1306. close(pipo);
  1307. dup2(pipi,input);
  1308. close(pipi);
  1309. if linuxerror<>0 then
  1310. halt(127);
  1311. end
  1312. else
  1313. begin
  1314. close(pipi);
  1315. dup2(pipo,output);
  1316. close(pipo);
  1317. if linuxerror<>0 then
  1318. halt(127);
  1319. end;
  1320. pp:=createshellargv(prog);
  1321. Execve(pp^,pp,envp);
  1322. halt(127);
  1323. end
  1324. else
  1325. begin
  1326. { We're in the parent }
  1327. if rw='W' then
  1328. begin
  1329. close(pipi);
  1330. f:=pipo;
  1331. textrec(f).bufptr:=@textrec(f).buffer;
  1332. end
  1333. else
  1334. begin
  1335. close(pipo);
  1336. f:=pipi;
  1337. textrec(f).bufptr:=@textrec(f).buffer;
  1338. end;
  1339. {Save the process ID - needed when closing }
  1340. pl:=@(textrec(f).userdata[2]);
  1341. pl^:=pid;
  1342. textrec(f).closefunc:=@PCloseText;
  1343. end;
  1344. end;
  1345. Procedure POpen(var F:file;const Prog:String;rw:char);
  1346. {
  1347. Starts the program in 'Prog' and makes it's input or out put the
  1348. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  1349. F, will be read from stdin by the program in 'Prog'. The inverse is true
  1350. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  1351. read from 'f'.
  1352. }
  1353. var
  1354. pipi,
  1355. pipo : file;
  1356. pid : longint;
  1357. pl : ^longint;
  1358. p,pp : ppchar;
  1359. temp : string[255];
  1360. begin
  1361. LinuxError:=0;
  1362. rw:=upcase(rw);
  1363. if not (rw in ['R','W']) then
  1364. begin
  1365. LinuxError:=Sys_enoent;
  1366. exit;
  1367. end;
  1368. AssignPipe(pipi,pipo);
  1369. if Linuxerror<>0 then
  1370. exit;
  1371. pid:=fork;
  1372. if linuxerror<>0 then
  1373. begin
  1374. close(pipi);
  1375. close(pipo);
  1376. exit;
  1377. end;
  1378. if pid=0 then
  1379. begin
  1380. { We're in the child }
  1381. if rw='W' then
  1382. begin
  1383. close(pipo);
  1384. dup2(filerec(pipi).handle,stdinputhandle);
  1385. close(pipi);
  1386. if linuxerror<>0 then
  1387. halt(127);
  1388. end
  1389. else
  1390. begin
  1391. close(pipi);
  1392. dup2(filerec(pipo).handle,stdoutputhandle);
  1393. close(pipo);
  1394. if linuxerror<>0 then
  1395. halt(127);
  1396. end;
  1397. getmem(pp,sizeof(pchar)*4);
  1398. temp:='/bin/sh'#0'-c'#0+prog+#0;
  1399. p:=pp;
  1400. p^:=@temp[1];
  1401. inc(p);
  1402. p^:=@temp[9];
  1403. inc(p);
  1404. p^:=@temp[12];
  1405. inc(p);
  1406. p^:=Nil;
  1407. Execve('/bin/sh',pp,envp);
  1408. halt(127);
  1409. end
  1410. else
  1411. begin
  1412. { We're in the parent }
  1413. if rw='W' then
  1414. begin
  1415. close(pipi);
  1416. f:=pipo;
  1417. end
  1418. else
  1419. begin
  1420. close(pipo);
  1421. f:=pipi;
  1422. end;
  1423. {Save the process ID - needed when closing }
  1424. pl:=@(filerec(f).userdata[2]);
  1425. pl^:=pid;
  1426. end;
  1427. end;
  1428. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : longint;
  1429. {
  1430. Starts the program in 'Prog' and makes its input and output the
  1431. other end of two pipes, which are the stdin and stdout of a program
  1432. specified in 'Prog'.
  1433. streamout can be used to write to the program, streamin can be used to read
  1434. the output of the program. See the following diagram :
  1435. Parent Child
  1436. STreamout --> Input
  1437. Streamin <-- Output
  1438. Return value is the process ID of the process being spawned, or -1 in case of failure.
  1439. }
  1440. var
  1441. pipi,
  1442. pipo : text;
  1443. pid : longint;
  1444. pl : ^Longint;
  1445. begin
  1446. LinuxError:=0;
  1447. AssignStream:=-1;
  1448. AssignPipe(streamin,pipo);
  1449. if Linuxerror<>0 then
  1450. exit;
  1451. AssignPipe(pipi,streamout);
  1452. if Linuxerror<>0 then
  1453. exit;
  1454. pid:=fork;
  1455. if linuxerror<>0 then
  1456. begin
  1457. close(pipi);
  1458. close(pipo);
  1459. close (streamin);
  1460. close (streamout);
  1461. exit;
  1462. end;
  1463. if pid=0 then
  1464. begin
  1465. { We're in the child }
  1466. { Close what we don't need }
  1467. close(streamout);
  1468. close(streamin);
  1469. dup2(pipi,input);
  1470. if linuxerror<>0 then
  1471. halt(127);
  1472. close(pipi);
  1473. dup2(pipo,output);
  1474. if linuxerror<>0 then
  1475. halt (127);
  1476. close(pipo);
  1477. Execl(Prog);
  1478. halt(127);
  1479. end
  1480. else
  1481. begin
  1482. { we're in the parent}
  1483. close(pipo);
  1484. close(pipi);
  1485. {Save the process ID - needed when closing }
  1486. pl:=@(textrec(StreamIn).userdata[2]);
  1487. pl^:=pid;
  1488. textrec(StreamIn).closefunc:=@PCloseText;
  1489. {Save the process ID - needed when closing }
  1490. pl:=@(textrec(StreamOut).userdata[2]);
  1491. pl^:=pid;
  1492. textrec(StreamOut).closefunc:=@PCloseText;
  1493. AssignStream:=Pid;
  1494. end;
  1495. end;
  1496. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): LongInt;
  1497. {
  1498. Starts the program in 'prog' and makes its input, output and error output the
  1499. other end of three pipes, which are the stdin, stdout and stderr of a program
  1500. specified in 'prog'.
  1501. StreamOut can be used to write to the program, StreamIn can be used to read
  1502. the output of the program, StreamErr reads the error output of the program.
  1503. See the following diagram :
  1504. Parent Child
  1505. StreamOut --> StdIn (input)
  1506. StreamIn <-- StdOut (output)
  1507. StreamErr <-- StdErr (error output)
  1508. }
  1509. var
  1510. PipeIn, PipeOut, PipeErr: text;
  1511. pid: LongInt;
  1512. pl: ^LongInt;
  1513. begin
  1514. LinuxError := 0;
  1515. AssignStream := -1;
  1516. // Assign pipes
  1517. AssignPipe(StreamIn, PipeOut);
  1518. if LinuxError <> 0 then exit;
  1519. AssignPipe(StreamErr, PipeErr);
  1520. if LinuxError <> 0 then begin
  1521. Close(StreamIn);
  1522. Close(PipeOut);
  1523. exit;
  1524. end;
  1525. AssignPipe(PipeIn, StreamOut);
  1526. if LinuxError <> 0 then begin
  1527. Close(StreamIn);
  1528. Close(PipeOut);
  1529. Close(StreamErr);
  1530. Close(PipeErr);
  1531. exit;
  1532. end;
  1533. // Fork
  1534. pid := Fork;
  1535. if LinuxError <> 0 then begin
  1536. Close(StreamIn);
  1537. Close(PipeOut);
  1538. Close(StreamErr);
  1539. Close(PipeErr);
  1540. Close(PipeIn);
  1541. Close(StreamOut);
  1542. exit;
  1543. end;
  1544. if pid = 0 then begin
  1545. // *** We are in the child ***
  1546. // Close what we don not need
  1547. Close(StreamOut);
  1548. Close(StreamIn);
  1549. Close(StreamErr);
  1550. // Connect pipes
  1551. dup2(PipeIn, Input);
  1552. if LinuxError <> 0 then Halt(127);
  1553. Close(PipeIn);
  1554. dup2(PipeOut, Output);
  1555. if LinuxError <> 0 then Halt(127);
  1556. Close(PipeOut);
  1557. dup2(PipeErr, StdErr);
  1558. if LinuxError <> 0 then Halt(127);
  1559. Close(PipeErr);
  1560. // Execute program
  1561. Execl(Prog);
  1562. Halt(127);
  1563. end else begin
  1564. // *** We are in the parent ***
  1565. Close(PipeErr);
  1566. Close(PipeOut);
  1567. Close(PipeIn);
  1568. // Save the process ID - needed when closing
  1569. pl := @(TextRec(StreamIn).userdata[2]);
  1570. pl^ := pid;
  1571. TextRec(StreamIn).closefunc := @PCloseText;
  1572. // Save the process ID - needed when closing
  1573. pl := @(TextRec(StreamOut).userdata[2]);
  1574. pl^ := pid;
  1575. TextRec(StreamOut).closefunc := @PCloseText;
  1576. // Save the process ID - needed when closing
  1577. pl := @(TextRec(StreamErr).userdata[2]);
  1578. pl^ := pid;
  1579. TextRec(StreamErr).closefunc := @PCloseText;
  1580. AssignStream := pid;
  1581. end;
  1582. end;
  1583. {******************************************************************************
  1584. General information calls
  1585. ******************************************************************************}
  1586. Function GetEnv(P:string):Pchar;
  1587. {
  1588. Searches the environment for a string with name p and
  1589. returns a pchar to it's value.
  1590. A pchar is used to accomodate for strings of length > 255
  1591. }
  1592. var
  1593. ep : ppchar;
  1594. found : boolean;
  1595. Begin
  1596. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1597. ep:=envp;
  1598. found:=false;
  1599. if ep<>nil then
  1600. begin
  1601. while (not found) and (ep^<>nil) do
  1602. begin
  1603. if strlcomp(@p[1],(ep^),length(p))=0 then
  1604. found:=true
  1605. else
  1606. inc(ep);
  1607. end;
  1608. end;
  1609. if found then
  1610. getenv:=ep^+length(p)
  1611. else
  1612. getenv:=nil;
  1613. end;
  1614. {$ifndef bsd}
  1615. Function GetDomainName:String;
  1616. {
  1617. Get machines domain name. Returns empty string if not set.
  1618. }
  1619. Var
  1620. Sysn : utsname;
  1621. begin
  1622. Uname(Sysn);
  1623. linuxerror:=errno;
  1624. If linuxerror<>0 then
  1625. getdomainname:=''
  1626. else
  1627. getdomainname:=strpas(@Sysn.domainname[0]);
  1628. end;
  1629. Function GetHostName:String;
  1630. {
  1631. Get machines name. Returns empty string if not set.
  1632. }
  1633. Var
  1634. Sysn : utsname;
  1635. begin
  1636. uname(Sysn);
  1637. linuxerror:=errno;
  1638. If linuxerror<>0 then
  1639. gethostname:=''
  1640. else
  1641. gethostname:=strpas(@Sysn.nodename[0]);
  1642. end;
  1643. {$endif}
  1644. {******************************************************************************
  1645. Signal handling calls
  1646. ******************************************************************************}
  1647. procedure SigRaise(sig:integer);
  1648. begin
  1649. Kill(GetPid,Sig);
  1650. end;
  1651. {******************************************************************************
  1652. IOCtl and Termios calls
  1653. ******************************************************************************}
  1654. Function TCGetAttr(fd:longint;var tios:TermIOS):boolean;
  1655. begin
  1656. {$ifndef BSD}
  1657. TCGetAttr:=IOCtl(fd,TCGETS,@tios);
  1658. {$else}
  1659. TCGETAttr:=IoCtl(Fd,TIOCGETA,@tios);
  1660. {$endif}
  1661. end;
  1662. Function TCSetAttr(fd:longint;OptAct:longint;var tios:TermIOS):boolean;
  1663. var
  1664. nr:longint;
  1665. begin
  1666. {$ifndef BSD}
  1667. case OptAct of
  1668. TCSANOW : nr:=TCSETS;
  1669. TCSADRAIN : nr:=TCSETSW;
  1670. TCSAFLUSH : nr:=TCSETSF;
  1671. {$else}
  1672. case OptAct of
  1673. TCSANOW : nr:=TIOCSETA;
  1674. TCSADRAIN : nr:=TIOCSETAW;
  1675. TCSAFLUSH : nr:=TIOCSETAF;
  1676. {$endif}
  1677. else
  1678. begin
  1679. ErrNo:=Sys_EINVAL;
  1680. TCSetAttr:=false;
  1681. exit;
  1682. end;
  1683. end;
  1684. TCSetAttr:=IOCtl(fd,nr,@Tios);
  1685. end;
  1686. Procedure CFSetISpeed(var tios:TermIOS;speed:Longint);
  1687. begin
  1688. {$ifndef BSD}
  1689. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  1690. {$else}
  1691. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  1692. {$endif}
  1693. end;
  1694. Procedure CFSetOSpeed(var tios:TermIOS;speed:Longint);
  1695. begin
  1696. {$ifndef BSD}
  1697. CFSetISpeed(tios,speed);
  1698. {$else}
  1699. tios.c_ospeed:=speed;
  1700. {$endif}
  1701. end;
  1702. Procedure CFMakeRaw(var tios:TermIOS);
  1703. begin
  1704. {$ifndef BSD}
  1705. with tios do
  1706. begin
  1707. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  1708. INLCR or IGNCR or ICRNL or IXON));
  1709. c_oflag:=c_oflag and (not OPOST);
  1710. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  1711. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  1712. end;
  1713. {$else}
  1714. with tios do
  1715. begin
  1716. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  1717. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  1718. IGNPAR));
  1719. c_iflag:=c_iflag OR IGNBRK;
  1720. c_oflag:=c_oflag and (not OPOST);
  1721. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  1722. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  1723. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  1724. c_cc[VMIN]:=1;
  1725. c_cc[VTIME]:=0;
  1726. end;
  1727. {$endif}
  1728. end;
  1729. Function TCSendBreak(fd,duration:longint):boolean;
  1730. begin
  1731. {$ifndef BSD}
  1732. TCSendBreak:=IOCtl(fd,TCSBRK,pointer(duration));
  1733. {$else}
  1734. TCSendBreak:=IOCtl(fd,TIOCSBRK,0);
  1735. {$endif}
  1736. end;
  1737. Function TCSetPGrp(fd,id:longint):boolean;
  1738. begin
  1739. TCSetPGrp:=IOCtl(fd,TIOCSPGRP,pointer(id));
  1740. end;
  1741. Function TCGetPGrp(fd:longint;var id:longint):boolean;
  1742. begin
  1743. TCGetPGrp:=IOCtl(fd,TIOCGPGRP,@id);
  1744. end;
  1745. Function TCDrain(fd:longint):boolean;
  1746. begin
  1747. {$ifndef BSD}
  1748. TCDrain:=IOCtl(fd,TCSBRK,pointer(1));
  1749. {$else}
  1750. TCDrain:=IOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  1751. {$endif}
  1752. end;
  1753. Function TCFlow(fd,act:longint):boolean;
  1754. begin
  1755. {$ifndef BSD}
  1756. TCFlow:=IOCtl(fd,TCXONC,pointer(act));
  1757. {$else}
  1758. case act OF
  1759. TCOOFF : TCFlow:=Ioctl(fd,TIOCSTOP,0);
  1760. TCOOn : TCFlow:=IOctl(Fd,TIOCStart,0);
  1761. TCIOFF : {N/I}
  1762. end;
  1763. {$endif}
  1764. end;
  1765. Function TCFlush(fd,qsel:longint):boolean;
  1766. begin
  1767. {$ifndef BSD}
  1768. TCFlush:=IOCtl(fd,TCFLSH,pointer(qsel));
  1769. {$else}
  1770. TCFlush:=IOCtl(fd,TIOCFLUSH,pointer(qsel));
  1771. {$endif}
  1772. end;
  1773. Function IsATTY(Handle:Longint):Boolean;
  1774. {
  1775. Check if the filehandle described by 'handle' is a TTY (Terminal)
  1776. }
  1777. var
  1778. t : Termios;
  1779. begin
  1780. IsAtty:=TCGetAttr(Handle,t);
  1781. end;
  1782. Function IsATTY(f: text):Boolean;
  1783. {
  1784. Idem as previous, only now for text variables.
  1785. }
  1786. begin
  1787. IsATTY:=IsaTTY(textrec(f).handle);
  1788. end;
  1789. function TTYName(Handle:Longint):string;
  1790. {
  1791. Return the name of the current tty described by handle f.
  1792. returns empty string in case of an error.
  1793. }
  1794. var
  1795. mydev,
  1796. myino : longint;
  1797. st : stat;
  1798. function mysearch(n:string): boolean;
  1799. {searches recursively for the device in the directory given by n,
  1800. returns true if found and sets the name of the device in ttyname}
  1801. var dirstream : pdir;
  1802. d : pdirent;
  1803. name : string;
  1804. st : stat;
  1805. begin
  1806. dirstream:=opendir(n);
  1807. if (linuxerror<>0) then
  1808. exit;
  1809. d:=Readdir(dirstream);
  1810. while (d<>nil) do
  1811. begin
  1812. name:=n+'/'+strpas(@(d^.name));
  1813. fstat(name,st);
  1814. if linuxerror=0 then
  1815. begin
  1816. if ((st.mode and $E000)=$4000) and { if it is a directory }
  1817. (strpas(@(d^.name))<>'.') and { but not ., .. and fd subdirs }
  1818. (strpas(@(d^.name))<>'..') and
  1819. (strpas(@(d^.name))<>'fd') then
  1820. begin {we found a directory, search inside it}
  1821. if mysearch(name) then
  1822. begin {the device is here}
  1823. closedir(dirstream); {then don't continue searching}
  1824. mysearch:=true;
  1825. exit;
  1826. end;
  1827. end
  1828. else if (d^.ino=myino) and (st.dev=mydev) then
  1829. begin
  1830. closedir(dirstream);
  1831. ttyname:=name;
  1832. mysearch:=true;
  1833. exit;
  1834. end;
  1835. end;
  1836. d:=Readdir(dirstream);
  1837. end;
  1838. closedir(dirstream);
  1839. mysearch:=false;
  1840. end;
  1841. begin
  1842. TTYName:='';
  1843. fstat(handle,st);
  1844. if (errno<>0) and isatty (handle) then
  1845. exit;
  1846. mydev:=st.dev;
  1847. myino:=st.ino;
  1848. mysearch('/dev');
  1849. end;
  1850. function TTYName(var F:Text):string;
  1851. {
  1852. Idem as previous, only now for text variables;
  1853. }
  1854. begin
  1855. TTYName:=TTYName(textrec(f).handle);
  1856. end;
  1857. {******************************************************************************
  1858. Utility calls
  1859. ******************************************************************************}
  1860. Function Octal(l:longint):longint;
  1861. {
  1862. Convert an octal specified number to decimal;
  1863. }
  1864. var
  1865. octnr,
  1866. oct : longint;
  1867. begin
  1868. octnr:=0;
  1869. oct:=0;
  1870. while (l>0) do
  1871. begin
  1872. oct:=oct or ((l mod 10) shl octnr);
  1873. l:=l div 10;
  1874. inc(octnr,3);
  1875. end;
  1876. Octal:=oct;
  1877. end;
  1878. Function StringToPPChar(Var S:STring):ppchar;
  1879. {
  1880. Create a PPChar to structure of pchars which are the arguments specified
  1881. in the string S. Especially usefull for creating an ArgV for Exec-calls
  1882. }
  1883. var
  1884. nr : longint;
  1885. Buf : ^char;
  1886. p : ppchar;
  1887. begin
  1888. s:=s+#0;
  1889. buf:=@s[1];
  1890. nr:=0;
  1891. while(buf^<>#0) do
  1892. begin
  1893. while (buf^ in [' ',#8,#10]) do
  1894. inc(buf);
  1895. inc(nr);
  1896. while not (buf^ in [' ',#0,#8,#10]) do
  1897. inc(buf);
  1898. end;
  1899. getmem(p,nr*4);
  1900. StringToPPChar:=p;
  1901. if p=nil then
  1902. begin
  1903. LinuxError:=sys_enomem;
  1904. exit;
  1905. end;
  1906. buf:=@s[1];
  1907. while (buf^<>#0) do
  1908. begin
  1909. while (buf^ in [' ',#8,#10]) do
  1910. begin
  1911. buf^:=#0;
  1912. inc(buf);
  1913. end;
  1914. p^:=buf;
  1915. inc(p);
  1916. p^:=nil;
  1917. while not (buf^ in [' ',#0,#8,#10]) do
  1918. inc(buf);
  1919. end;
  1920. end;
  1921. {
  1922. function FExpand (const Path: PathStr): PathStr;
  1923. - declared in fexpand.inc
  1924. }
  1925. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1926. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1927. {$I fexpand.inc}
  1928. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1929. {$UNDEF FPC_FEXPAND_TILDE}
  1930. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1931. {
  1932. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1933. returns an empty string if not found. Wildcards are NOT allowed.
  1934. If dirlist is empty, it is set to '.'
  1935. }
  1936. Var
  1937. NewDir : PathStr;
  1938. p1 : Longint;
  1939. Info : Stat;
  1940. Begin
  1941. {Replace ':' with ';'}
  1942. for p1:=1to length(dirlist) do
  1943. if dirlist[p1]=':' then
  1944. dirlist[p1]:=';';
  1945. {Check for WildCards}
  1946. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1947. FSearch:='' {No wildcards allowed in these things.}
  1948. Else
  1949. Begin
  1950. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1951. Repeat
  1952. p1:=Pos(';',DirList);
  1953. If p1=0 Then
  1954. p1:=255;
  1955. NewDir:=Copy(DirList,1,P1 - 1);
  1956. if NewDir[Length(NewDir)]<>'/' then
  1957. NewDir:=NewDir+'/';
  1958. NewDir:=NewDir+Path;
  1959. Delete(DirList,1,p1);
  1960. if FStat(NewDir,Info) then
  1961. Begin
  1962. If Pos('./',NewDir)=1 Then
  1963. Delete(NewDir,1,2);
  1964. {DOS strips off an initial .\}
  1965. End
  1966. Else
  1967. NewDir:='';
  1968. Until (DirList='') or (Length(NewDir) > 0);
  1969. FSearch:=NewDir;
  1970. End;
  1971. End;
  1972. Procedure FSplit(const Path:PathStr;Var Dir:DirStr;Var Name:NameStr;Var Ext:ExtStr);
  1973. Var
  1974. DotPos,SlashPos,i : longint;
  1975. Begin
  1976. SlashPos:=0;
  1977. DotPos:=256;
  1978. i:=Length(Path);
  1979. While (i>0) and (SlashPos=0) Do
  1980. Begin
  1981. If (DotPos=256) and (Path[i]='.') Then
  1982. begin
  1983. DotPos:=i;
  1984. end;
  1985. If (Path[i]='/') Then
  1986. SlashPos:=i;
  1987. Dec(i);
  1988. End;
  1989. Ext:=Copy(Path,DotPos,255);
  1990. Dir:=Copy(Path,1,SlashPos);
  1991. Name:=Copy(Path,SlashPos + 1,DotPos - SlashPos - 1);
  1992. End;
  1993. Function Dirname(Const path:pathstr):pathstr;
  1994. {
  1995. This function returns the directory part of a complete path.
  1996. Unless the directory is root '/', The last character is not
  1997. a slash.
  1998. }
  1999. var
  2000. Dir : PathStr;
  2001. Name : NameStr;
  2002. Ext : ExtStr;
  2003. begin
  2004. FSplit(Path,Dir,Name,Ext);
  2005. if length(Dir)>1 then
  2006. Delete(Dir,length(Dir),1);
  2007. DirName:=Dir;
  2008. end;
  2009. Function Basename(Const path:pathstr;Const suf:pathstr):pathstr;
  2010. {
  2011. This function returns the filename part of a complete path. If suf is
  2012. supplied, it is cut off the filename.
  2013. }
  2014. var
  2015. Dir : PathStr;
  2016. Name : NameStr;
  2017. Ext : ExtStr;
  2018. begin
  2019. FSplit(Path,Dir,Name,Ext);
  2020. if Suf<>Ext then
  2021. Name:=Name+Ext;
  2022. BaseName:=Name;
  2023. end;
  2024. Function FNMatch(const Pattern,Name:string):Boolean;
  2025. Var
  2026. LenPat,LenName : longint;
  2027. Function DoFNMatch(i,j:longint):Boolean;
  2028. Var
  2029. Found : boolean;
  2030. Begin
  2031. Found:=true;
  2032. While Found and (i<=LenPat) Do
  2033. Begin
  2034. Case Pattern[i] of
  2035. '?' : Found:=(j<=LenName);
  2036. '*' : Begin
  2037. {find the next character in pattern, different of ? and *}
  2038. while Found and (i<LenPat) do
  2039. begin
  2040. inc(i);
  2041. case Pattern[i] of
  2042. '*' : ;
  2043. '?' : begin
  2044. inc(j);
  2045. Found:=(j<=LenName);
  2046. end;
  2047. else
  2048. Found:=false;
  2049. end;
  2050. end;
  2051. {Now, find in name the character which i points to, if the * or ?
  2052. wasn't the last character in the pattern, else, use up all the
  2053. chars in name}
  2054. Found:=true;
  2055. if (i<=LenPat) then
  2056. begin
  2057. repeat
  2058. {find a letter (not only first !) which maches pattern[i]}
  2059. while (j<=LenName) and (name[j]<>pattern[i]) do
  2060. inc (j);
  2061. if (j<LenName) then
  2062. begin
  2063. if DoFnMatch(i+1,j+1) then
  2064. begin
  2065. i:=LenPat;
  2066. j:=LenName;{we can stop}
  2067. Found:=true;
  2068. end
  2069. else
  2070. inc(j);{We didn't find one, need to look further}
  2071. end;
  2072. until (j>=LenName);
  2073. end
  2074. else
  2075. j:=LenName;{we can stop}
  2076. end;
  2077. else {not a wildcard character in pattern}
  2078. Found:=(j<=LenName) and (pattern[i]=name[j]);
  2079. end;
  2080. inc(i);
  2081. inc(j);
  2082. end;
  2083. DoFnMatch:=Found and (j>LenName);
  2084. end;
  2085. Begin {start FNMatch}
  2086. LenPat:=Length(Pattern);
  2087. LenName:=Length(Name);
  2088. FNMatch:=DoFNMatch(1,1);
  2089. End;
  2090. Procedure Globfree(var p : pglob);
  2091. {
  2092. Release memory occupied by pglob structure, and names in it.
  2093. sets p to nil.
  2094. }
  2095. var
  2096. temp : pglob;
  2097. begin
  2098. while assigned(p) do
  2099. begin
  2100. temp:=p^.next;
  2101. if assigned(p^.name) then
  2102. freemem(p^.name);
  2103. dispose(p);
  2104. p:=temp;
  2105. end;
  2106. end;
  2107. Function Glob(Const path:pathstr):pglob;
  2108. {
  2109. Fills a tglob structure with entries matching path,
  2110. and returns a pointer to it. Returns nil on error,
  2111. linuxerror is set accordingly.
  2112. }
  2113. var
  2114. temp,
  2115. temp2 : string[255];
  2116. thedir : pdir;
  2117. buffer : pdirent;
  2118. root,
  2119. current : pglob;
  2120. begin
  2121. { Get directory }
  2122. temp:=dirname(path);
  2123. if temp='' then
  2124. temp:='.';
  2125. temp:=temp+#0;
  2126. thedir:=opendir(@temp[1]);
  2127. if thedir=nil then
  2128. begin
  2129. glob:=nil;
  2130. linuxerror:=errno;
  2131. exit;
  2132. end;
  2133. temp:=basename(path,''); { get the pattern }
  2134. if thedir^.fd<0 then
  2135. begin
  2136. linuxerror:=errno;
  2137. glob:=nil;
  2138. exit;
  2139. end;
  2140. {get the entries}
  2141. root:=nil;
  2142. current:=nil;
  2143. repeat
  2144. buffer:=Sys_readdir(thedir);
  2145. if buffer=nil then
  2146. break;
  2147. temp2:=strpas(@(buffer^.name[0]));
  2148. if fnmatch(temp,temp2) then
  2149. begin
  2150. if root=nil then
  2151. begin
  2152. new(root);
  2153. current:=root;
  2154. end
  2155. else
  2156. begin
  2157. new(current^.next);
  2158. current:=current^.next;
  2159. end;
  2160. if current=nil then
  2161. begin
  2162. linuxerror:=Sys_ENOMEM;
  2163. globfree(root);
  2164. break;
  2165. end;
  2166. current^.next:=nil;
  2167. getmem(current^.name,length(temp2)+1);
  2168. if current^.name=nil then
  2169. begin
  2170. linuxerror:=Sys_ENOMEM;
  2171. globfree(root);
  2172. break;
  2173. end;
  2174. move(buffer^.name[0],current^.name^,length(temp2)+1);
  2175. end;
  2176. until false;
  2177. closedir(thedir);
  2178. glob:=root;
  2179. end;
  2180. {--------------------------------
  2181. FiledescriptorSets
  2182. --------------------------------}
  2183. Procedure FD_Zero(var fds:fdSet);
  2184. {
  2185. Clear the set of filedescriptors
  2186. }
  2187. begin
  2188. FillChar(fds,sizeof(fdSet),0);
  2189. end;
  2190. Procedure FD_Clr(fd:longint;var fds:fdSet);
  2191. {
  2192. Remove fd from the set of filedescriptors
  2193. }
  2194. begin
  2195. fds[fd shr 5]:=fds[fd shr 5] and (not (1 shl (fd and 31)));
  2196. end;
  2197. Procedure FD_Set(fd:longint;var fds:fdSet);
  2198. {
  2199. Add fd to the set of filedescriptors
  2200. }
  2201. begin
  2202. fds[fd shr 5]:=fds[fd shr 5] or (1 shl (fd and 31));
  2203. end;
  2204. Function FD_IsSet(fd:longint;var fds:fdSet):boolean;
  2205. {
  2206. Test if fd is part of the set of filedescriptors
  2207. }
  2208. begin
  2209. FD_IsSet:=((fds[fd shr 5] and (1 shl (fd and 31)))<>0);
  2210. end;
  2211. Function GetFS (var T:Text):longint;
  2212. {
  2213. Get File Descriptor of a text file.
  2214. }
  2215. begin
  2216. if textrec(t).mode=fmclosed then
  2217. exit(-1)
  2218. else
  2219. GETFS:=textrec(t).Handle
  2220. end;
  2221. Function GetFS(Var F:File):longint;
  2222. {
  2223. Get File Descriptor of an unTyped file.
  2224. }
  2225. begin
  2226. { Handle and mode are on the same place in textrec and filerec. }
  2227. if filerec(f).mode=fmclosed then
  2228. exit(-1)
  2229. else
  2230. GETFS:=filerec(f).Handle
  2231. end;
  2232. {--------------------------------
  2233. Stat.Mode Macro's
  2234. --------------------------------}
  2235. Function S_ISLNK(m:word):boolean;
  2236. {
  2237. Check mode field of inode for link.
  2238. }
  2239. begin
  2240. S_ISLNK:=(m and STAT_IFMT)=STAT_IFLNK;
  2241. end;
  2242. Function S_ISREG(m:word):boolean;
  2243. {
  2244. Check mode field of inode for regular file.
  2245. }
  2246. begin
  2247. S_ISREG:=(m and STAT_IFMT)=STAT_IFREG;
  2248. end;
  2249. Function S_ISDIR(m:word):boolean;
  2250. {
  2251. Check mode field of inode for directory.
  2252. }
  2253. begin
  2254. S_ISDIR:=(m and STAT_IFMT)=STAT_IFDIR;
  2255. end;
  2256. Function S_ISCHR(m:word):boolean;
  2257. {
  2258. Check mode field of inode for character device.
  2259. }
  2260. begin
  2261. S_ISCHR:=(m and STAT_IFMT)=STAT_IFCHR;
  2262. end;
  2263. Function S_ISBLK(m:word):boolean;
  2264. {
  2265. Check mode field of inode for block device.
  2266. }
  2267. begin
  2268. S_ISBLK:=(m and STAT_IFMT)=STAT_IFBLK;
  2269. end;
  2270. Function S_ISFIFO(m:word):boolean;
  2271. {
  2272. Check mode field of inode for named pipe (FIFO).
  2273. }
  2274. begin
  2275. S_ISFIFO:=(m and STAT_IFMT)=STAT_IFIFO;
  2276. end;
  2277. Function S_ISSOCK(m:word):boolean;
  2278. {
  2279. Check mode field of inode for socket.
  2280. }
  2281. begin
  2282. S_ISSOCK:=(m and STAT_IFMT)=STAT_IFSOCK;
  2283. end;
  2284. {--------------------------------
  2285. Memory functions
  2286. --------------------------------}
  2287. {$IFDEF I386}
  2288. Procedure WritePort (Port : Longint; Value : Byte);
  2289. {
  2290. Writes 'Value' to port 'Port'
  2291. }
  2292. begin
  2293. asm
  2294. movl port,%edx
  2295. movb value,%al
  2296. outb %al,%dx
  2297. end ['EAX','EDX'];
  2298. end;
  2299. Procedure WritePort (Port : Longint; Value : Word);
  2300. {
  2301. Writes 'Value' to port 'Port'
  2302. }
  2303. begin
  2304. asm
  2305. movl port,%edx
  2306. movw value,%ax
  2307. outw %ax,%dx
  2308. end ['EAX','EDX'];
  2309. end;
  2310. Procedure WritePort (Port : Longint; Value : Longint);
  2311. {
  2312. Writes 'Value' to port 'Port'
  2313. }
  2314. begin
  2315. asm
  2316. movl port,%edx
  2317. movl value,%eax
  2318. outl %eax,%dx
  2319. end ['EAX','EDX'];
  2320. end;
  2321. Procedure WritePortB (Port : Longint; Value : Byte);
  2322. {
  2323. Writes 'Value' to port 'Port'
  2324. }
  2325. begin
  2326. asm
  2327. movl port,%edx
  2328. movb value,%al
  2329. outb %al,%dx
  2330. end ['EAX','EDX'];
  2331. end;
  2332. Procedure WritePortW (Port : Longint; Value : Word);
  2333. {
  2334. Writes 'Value' to port 'Port'
  2335. }
  2336. begin
  2337. asm
  2338. movl port,%edx
  2339. movw value,%ax
  2340. outw %ax,%dx
  2341. end ['EAX','EDX'];
  2342. end;
  2343. Procedure WritePortL (Port : Longint; Value : Longint);
  2344. {
  2345. Writes 'Value' to port 'Port'
  2346. }
  2347. begin
  2348. asm
  2349. movl port,%edx
  2350. movl value,%eax
  2351. outl %eax,%dx
  2352. end ['EAX','EDX'];
  2353. end;
  2354. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  2355. {
  2356. Writes 'Count' longints from 'Buf' to Port
  2357. }
  2358. begin
  2359. asm
  2360. movl count,%ecx
  2361. movl buf,%esi
  2362. movl port,%edx
  2363. cld
  2364. rep
  2365. outsl
  2366. end ['ECX','ESI','EDX'];
  2367. end;
  2368. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  2369. {
  2370. Writes 'Count' words from 'Buf' to Port
  2371. }
  2372. begin
  2373. asm
  2374. movl count,%ecx
  2375. movl buf,%esi
  2376. movl port,%edx
  2377. cld
  2378. rep
  2379. outsw
  2380. end ['ECX','ESI','EDX'];
  2381. end;
  2382. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  2383. {
  2384. Writes 'Count' bytes from 'Buf' to Port
  2385. }
  2386. begin
  2387. asm
  2388. movl count,%ecx
  2389. movl buf,%esi
  2390. movl port,%edx
  2391. cld
  2392. rep
  2393. outsb
  2394. end ['ECX','ESI','EDX'];
  2395. end;
  2396. Procedure ReadPort (Port : Longint; Var Value : Byte);
  2397. {
  2398. Reads 'Value' from port 'Port'
  2399. }
  2400. begin
  2401. asm
  2402. movl port,%edx
  2403. inb %dx,%al
  2404. movl value,%edx
  2405. movb %al,(%edx)
  2406. end ['EAX','EDX'];
  2407. end;
  2408. Procedure ReadPort (Port : Longint; Var Value : Word);
  2409. {
  2410. Reads 'Value' from port 'Port'
  2411. }
  2412. begin
  2413. asm
  2414. movl port,%edx
  2415. inw %dx,%ax
  2416. movl value,%edx
  2417. movw %ax,(%edx)
  2418. end ['EAX','EDX'];
  2419. end;
  2420. Procedure ReadPort (Port : Longint; Var Value : Longint);
  2421. {
  2422. Reads 'Value' from port 'Port'
  2423. }
  2424. begin
  2425. asm
  2426. movl port,%edx
  2427. inl %dx,%eax
  2428. movl value,%edx
  2429. movl %eax,(%edx)
  2430. end ['EAX','EDX'];
  2431. end;
  2432. function ReadPortB (Port : Longint): Byte; assembler;
  2433. {
  2434. Reads a byte from port 'Port'
  2435. }
  2436. asm
  2437. xorl %eax,%eax
  2438. movl port,%edx
  2439. inb %dx,%al
  2440. end ['EAX','EDX'];
  2441. function ReadPortW (Port : Longint): Word; assembler;
  2442. {
  2443. Reads a word from port 'Port'
  2444. }
  2445. asm
  2446. xorl %eax,%eax
  2447. movl port,%edx
  2448. inw %dx,%ax
  2449. end ['EAX','EDX'];
  2450. function ReadPortL (Port : Longint): LongInt; assembler;
  2451. {
  2452. Reads a LongInt from port 'Port'
  2453. }
  2454. asm
  2455. movl port,%edx
  2456. inl %dx,%eax
  2457. end ['EAX','EDX'];
  2458. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  2459. {
  2460. Reads 'Count' longints from port 'Port' to 'Buf'.
  2461. }
  2462. begin
  2463. asm
  2464. movl count,%ecx
  2465. movl buf,%edi
  2466. movl port,%edx
  2467. cld
  2468. rep
  2469. insl
  2470. end ['ECX','EDI','EDX'];
  2471. end;
  2472. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  2473. {
  2474. Reads 'Count' words from port 'Port' to 'Buf'.
  2475. }
  2476. begin
  2477. asm
  2478. movl count,%ecx
  2479. movl buf,%edi
  2480. movl port,%edx
  2481. cld
  2482. rep
  2483. insw
  2484. end ['ECX','EDI','EDX'];
  2485. end;
  2486. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  2487. {
  2488. Reads 'Count' bytes from port 'Port' to 'Buf'.
  2489. }
  2490. begin
  2491. asm
  2492. movl count,%ecx
  2493. movl buf,%edi
  2494. movl port,%edx
  2495. cld
  2496. rep
  2497. insb
  2498. end ['ECX','EDI','EDX'];
  2499. end;
  2500. {$ENDIF}
  2501. Initialization
  2502. InitLocalTime;
  2503. finalization
  2504. DoneLocalTime;
  2505. End.
  2506. {
  2507. $Log$
  2508. Revision 1.16 2001-09-17 21:36:31 peter
  2509. * merged fixes
  2510. Revision 1.15 2001/08/12 18:05:19 peter
  2511. * Range check fix with epoch (merged)
  2512. Revision 1.14 2001/07/12 12:42:39 marco
  2513. * Fixes to the FreeBSD compability of the datetime patches
  2514. Revision 1.13 2001/07/12 07:20:05 michael
  2515. + Added setdate/time/datetime functions
  2516. Revision 1.12 2001/06/20 15:24:47 marco
  2517. * readlink for Unix fix.
  2518. Revision 1.11 2001/06/19 17:19:50 marco
  2519. * Again uncommented readlink. Merges or typo, I dunno
  2520. Revision 1.10 2001/06/13 22:20:11 hajny
  2521. + platform specific information
  2522. Revision 1.9 2001/06/03 20:19:09 peter
  2523. * FSStat to StatFS
  2524. * StatFS structure to TStatFS
  2525. Revision 1.8 2001/06/02 00:31:31 peter
  2526. * merge unix updates from the 1.0 branch, mostly related to the
  2527. solaris target
  2528. Revision 1.7 2001/04/19 12:57:33 marco
  2529. * Readlink uncommented for FreeBSD.
  2530. Revision 1.6 2001/04/13 22:37:21 peter
  2531. * remove warning
  2532. Revision 1.5 2001/03/27 11:47:25 michael
  2533. + Fixed F_[G,S]etOwn constants. By Alexander Sychev
  2534. Revision 1.4 2001/03/17 16:04:37 hajny
  2535. * FExpand omission fixed
  2536. Revision 1.3 2001/03/16 20:09:58 hajny
  2537. * universal FExpand
  2538. Revision 1.2 2001/01/22 07:25:10 marco
  2539. * IOPERM for FreeBSD. Port routines moved from linsysca to Unix again .
  2540. Revision 1.1 2001/01/21 20:21:41 marco
  2541. * Rename fest II. Rtl OK
  2542. Revision 1.6 2000/12/28 20:42:12 peter
  2543. * ttyname fix from the mailinglist (merged)
  2544. Revision 1.5 2000/10/26 22:51:12 peter
  2545. * nano sleep (merged)
  2546. Revision 1.4 2000/10/11 13:59:16 marco
  2547. * FreeBSD TermIOS support and minor changes to some related files.
  2548. Revision 1.3 2000/10/10 12:02:35 marco
  2549. * Terminal stuff of Linux moved to separate file. I think it is too
  2550. much to do with ifdefs.
  2551. Revision 1.2 2000/09/18 13:14:50 marco
  2552. * Global Linux +bsd to (rtl/freebsd rtl/unix rtl/linux structure)
  2553. Revision 1.7 2000/09/12 08:51:43 marco
  2554. * fixed some small problems left from merging. (waitpid has now last param longint)
  2555. Revision 1.6 2000/09/11 14:05:31 marco
  2556. * FreeBSD support and removed old signalhandling
  2557. Revision 1.5 2000/09/06 20:47:34 peter
  2558. * removed previous fsplit() patch as it's not the correct behaviour for
  2559. LFNs. The code showing the bug could easily be adapted (merged)
  2560. Revision 1.4 2000/09/04 20:17:53 peter
  2561. * fixed previous commit (merged)
  2562. Revision 1.3 2000/09/04 19:38:13 peter
  2563. * fsplit with .. fix from Thomas (merged)
  2564. Revision 1.2 2000/07/13 11:33:48 michael
  2565. + removed logs
  2566. }