linux.pp 69 KB

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