posix.texi 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007,
  4. @c 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2017, 2021 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node POSIX
  7. @section @acronym{POSIX} System Calls and Networking
  8. @cindex POSIX
  9. @menu
  10. * Conventions:: Conventions employed by the POSIX interface.
  11. * Ports and File Descriptors:: Scheme ``ports'' and Unix file descriptors
  12. have different representations.
  13. * File System:: stat, chown, chmod, etc.
  14. * User Information:: Retrieving a user's GECOS (/etc/passwd) entry.
  15. * Time:: gettimeofday, localtime, strftime, etc.
  16. * Runtime Environment:: Accessing and modifying Guile's environment.
  17. * Processes:: getuid, getpid, etc.
  18. * Signals:: sigaction, kill, pause, alarm, setitimer, etc.
  19. * Terminals and Ptys:: ttyname, tcsetpgrp, etc.
  20. * Pipes:: Communicating data between processes.
  21. * Networking:: gethostbyaddr, getnetent, socket, bind, listen.
  22. * System Identification:: Obtaining information about the system.
  23. * Locales:: setlocale, etc.
  24. * Encryption::
  25. @end menu
  26. @node Conventions
  27. @subsection @acronym{POSIX} Interface Conventions
  28. These interfaces provide access to operating system facilities.
  29. They provide a simple wrapping around the underlying C interfaces
  30. to make usage from Scheme more convenient. They are also used
  31. to implement the Guile port of scsh (@pxref{The Scheme shell (scsh)}).
  32. Generally there is a single procedure for each corresponding Unix
  33. facility. There are some exceptions, such as procedures implemented for
  34. speed and convenience in Scheme with no primitive Unix equivalent,
  35. e.g.@: @code{copy-file}.
  36. The interfaces are intended as far as possible to be portable across
  37. different versions of Unix. In some cases procedures which can't be
  38. implemented on particular systems may become no-ops, or perform limited
  39. actions. In other cases they may throw errors.
  40. General naming conventions are as follows:
  41. @itemize @bullet
  42. @item
  43. The Scheme name is often identical to the name of the underlying Unix
  44. facility.
  45. @item
  46. Underscores in Unix procedure names are converted to hyphens.
  47. @item
  48. Procedures which destructively modify Scheme data have exclamation
  49. marks appended, e.g., @code{recv!}.
  50. @item
  51. Predicates (returning only @code{#t} or @code{#f}) have question marks
  52. appended, e.g., @code{access?}.
  53. @item
  54. Some names are changed to avoid conflict with dissimilar interfaces
  55. defined by scsh, e.g., @code{primitive-fork}.
  56. @item
  57. Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted
  58. to Scheme variables of the same name (underscores are not replaced
  59. with hyphens).
  60. @end itemize
  61. Unexpected conditions are generally handled by raising exceptions.
  62. There are a few procedures which return a special value if they don't
  63. succeed, e.g., @code{getenv} returns @code{#f} if it the requested
  64. string is not found in the environment. These cases are noted in
  65. the documentation.
  66. For ways to deal with exceptions, see @ref{Exceptions}.
  67. @cindex @code{errno}
  68. Errors which the C library would report by returning a null pointer or
  69. through some other means are reported by raising a @code{system-error}
  70. exception with @code{scm-error} (@pxref{Error Reporting}). The
  71. @var{data} parameter is a list containing the Unix @code{errno} value
  72. (an integer). For example,
  73. @example
  74. (define (my-handler key func fmt fmtargs data)
  75. (display key) (newline)
  76. (display func) (newline)
  77. (apply format #t fmt fmtargs) (newline)
  78. (display data) (newline))
  79. (catch 'system-error
  80. (lambda () (dup2 -123 -456))
  81. my-handler)
  82. @print{}
  83. system-error
  84. dup2
  85. Bad file descriptor
  86. (9)
  87. @end example
  88. @sp 1
  89. @defun system-error-errno arglist
  90. @cindex @code{errno}
  91. Return the @code{errno} value from a list which is the arguments to an
  92. exception handler. If the exception is not a @code{system-error},
  93. then the return is @code{#f}. For example,
  94. @example
  95. (catch
  96. 'system-error
  97. (lambda ()
  98. (mkdir "/this-ought-to-fail-if-I'm-not-root"))
  99. (lambda stuff
  100. (let ((errno (system-error-errno stuff)))
  101. (cond
  102. ((= errno EACCES)
  103. (display "You're not allowed to do that."))
  104. ((= errno EEXIST)
  105. (display "Already exists."))
  106. (#t
  107. (display (strerror errno))))
  108. (newline))))
  109. @end example
  110. @end defun
  111. @node Ports and File Descriptors
  112. @subsection Ports and File Descriptors
  113. @cindex file descriptor
  114. Conventions generally follow those of scsh, @ref{The Scheme shell (scsh)}.
  115. Each open file port has an associated operating system file descriptor.
  116. File descriptors are generally not useful in Scheme programs; however
  117. they may be needed when interfacing with foreign code and the Unix
  118. environment.
  119. A file descriptor can be extracted from a port and a new port can be
  120. created from a file descriptor. However a file descriptor is just an
  121. integer and the garbage collector doesn't recognize it as a reference
  122. to the port. If all other references to the port were dropped, then
  123. it's likely that the garbage collector would free the port, with the
  124. side-effect of closing the file descriptor prematurely.
  125. To assist the programmer in avoiding this problem, each port has an
  126. associated @dfn{revealed count} which can be used to keep track of how many
  127. times the underlying file descriptor has been stored in other places.
  128. If a port's revealed count is greater than zero, the file descriptor
  129. will not be closed when the port is garbage collected. A programmer
  130. can therefore ensure that the revealed count will be greater than
  131. zero if the file descriptor is needed elsewhere.
  132. For the simple case where a file descriptor is ``imported'' once to become
  133. a port, it does not matter if the file descriptor is closed when the
  134. port is garbage collected. There is no need to maintain a revealed
  135. count. Likewise when ``exporting'' a file descriptor to the external
  136. environment, setting the revealed count is not required provided the
  137. port is kept open (i.e., is pointed to by a live Scheme binding) while
  138. the file descriptor is in use.
  139. To correspond with traditional Unix behaviour, three file descriptors
  140. (0, 1, and 2) are automatically imported when a program starts up and
  141. assigned to the initial values of the current/standard input, output,
  142. and error ports, respectively. The revealed count for each is
  143. initially set to one, so that dropping references to one of these
  144. ports will not result in its garbage collection: it could be retrieved
  145. with @code{fdopen} or @code{fdes->ports}.
  146. Guile's ports can be buffered. This means that writing a byte to a file
  147. port goes to the internal buffer first, and only when the buffer is full
  148. (or the user invokes @code{force-output} on the port) is the data
  149. actually written to the file descriptor. Likewise on input, bytes are
  150. read in from the file descriptor in blocks and placed in a buffer.
  151. Reading a character via @code{read-char} first goes to the buffer,
  152. filling it as needed. Usually read buffering is more or less
  153. transparent, but write buffering can sometimes cause writes to be
  154. delayed unexpectedly, if you forget to call @code{force-output}.
  155. @xref{Buffering}, for more on how to control port buffers.
  156. Note however that some procedures (e.g., @code{recv!}) will accept ports
  157. as arguments, but will actually operate directly on the file descriptor
  158. underlying the port. Any port buffering is ignored, including the
  159. buffer which implements @code{peek-char} and @code{unread-char}.
  160. @deffn {Scheme Procedure} port-revealed port
  161. @deffnx {C Function} scm_port_revealed (port)
  162. Return the revealed count for @var{port}.
  163. @end deffn
  164. @deffn {Scheme Procedure} set-port-revealed! port rcount
  165. @deffnx {C Function} scm_set_port_revealed_x (port, rcount)
  166. Sets the revealed count for a @var{port} to @var{rcount}.
  167. The return value is unspecified.
  168. @end deffn
  169. @deffn {Scheme Procedure} fileno port
  170. @deffnx {C Function} scm_fileno (port)
  171. Return the integer file descriptor underlying @var{port}. Does
  172. not change its revealed count.
  173. @end deffn
  174. @deffn {Scheme Procedure} port->fdes port
  175. Returns the integer file descriptor underlying @var{port}. As a
  176. side effect the revealed count of @var{port} is incremented.
  177. @end deffn
  178. @deffn {Scheme Procedure} fdopen fdes modes
  179. @deffnx {C Function} scm_fdopen (fdes, modes)
  180. Return a new port based on the file descriptor @var{fdes}. Modes are
  181. given by the string @var{modes}. The revealed count of the port is
  182. initialized to zero. The @var{modes} string is the same as that
  183. accepted by @code{open-file} (@pxref{File Ports, open-file}).
  184. @end deffn
  185. @deffn {Scheme Procedure} fdes->ports fdes
  186. @deffnx {C Function} scm_fdes_to_ports (fdes)
  187. Return a list of existing ports which have @var{fdes} as an
  188. underlying file descriptor, without changing their revealed
  189. counts.
  190. @end deffn
  191. @deffn {Scheme Procedure} fdes->inport fdes
  192. Returns an existing input port which has @var{fdes} as its underlying file
  193. descriptor, if one exists, and increments its revealed count.
  194. Otherwise, returns a new input port with a revealed count of 1.
  195. @end deffn
  196. @deffn {Scheme Procedure} fdes->outport fdes
  197. Returns an existing output port which has @var{fdes} as its underlying file
  198. descriptor, if one exists, and increments its revealed count.
  199. Otherwise, returns a new output port with a revealed count of 1.
  200. @end deffn
  201. @deffn {Scheme Procedure} primitive-move->fdes port fdes
  202. @deffnx {C Function} scm_primitive_move_to_fdes (port, fdes)
  203. Moves the underlying file descriptor for @var{port} to the integer
  204. value @var{fdes} without changing the revealed count of @var{port}.
  205. Any other ports already using this descriptor will be automatically
  206. shifted to new descriptors and their revealed counts reset to zero.
  207. The return value is @code{#f} if the file descriptor already had the
  208. required value or @code{#t} if it was moved.
  209. @end deffn
  210. @deffn {Scheme Procedure} move->fdes port fdes
  211. Moves the underlying file descriptor for @var{port} to the integer
  212. value @var{fdes} and sets its revealed count to one. Any other ports
  213. already using this descriptor will be automatically
  214. shifted to new descriptors and their revealed counts reset to zero.
  215. The return value is unspecified.
  216. @end deffn
  217. @deffn {Scheme Procedure} release-port-handle port
  218. Decrements the revealed count for a port.
  219. @end deffn
  220. @deffn {Scheme Procedure} fsync port_or_fd
  221. @deffnx {C Function} scm_fsync (port_or_fd)
  222. Copies any unwritten data for the specified output file descriptor to disk.
  223. If @var{port_or_fd} is a port, its buffer is flushed before the underlying
  224. file descriptor is fsync'd.
  225. The return value is unspecified.
  226. @end deffn
  227. @deffn {Scheme Procedure} open path flags [mode]
  228. @deffnx {C Function} scm_open (path, flags, mode)
  229. Open the file named by @var{path} for reading and/or writing.
  230. @var{flags} is an integer specifying how the file should be opened.
  231. @var{mode} is an integer specifying the permission bits of the file,
  232. if it needs to be created, before the umask (@pxref{Processes}) is
  233. applied. The default is 666 (Unix itself has no default).
  234. @var{flags} can be constructed by combining variables using @code{logior}.
  235. Basic flags are:
  236. @defvar O_RDONLY
  237. Open the file read-only.
  238. @end defvar
  239. @defvar O_WRONLY
  240. Open the file write-only.
  241. @end defvar
  242. @defvar O_RDWR
  243. Open the file read/write.
  244. @end defvar
  245. @defvar O_APPEND
  246. Append to the file instead of truncating.
  247. @end defvar
  248. @defvar O_CREAT
  249. Create the file if it does not already exist.
  250. @end defvar
  251. @xref{File Status Flags,,,libc,The GNU C Library Reference Manual},
  252. for additional flags.
  253. @end deffn
  254. @deffn {Scheme Procedure} open-fdes path flags [mode]
  255. @deffnx {C Function} scm_open_fdes (path, flags, mode)
  256. Similar to @code{open} but return a file descriptor instead of
  257. a port.
  258. @end deffn
  259. @deffn {Scheme Procedure} close fd_or_port
  260. @deffnx {C Function} scm_close (fd_or_port)
  261. Similar to @code{close-port} (@pxref{Ports, close-port}),
  262. but also works on file descriptors. A side
  263. effect of closing a file descriptor is that any ports using that file
  264. descriptor are moved to a different file descriptor and have
  265. their revealed counts set to zero.
  266. @end deffn
  267. @deffn {Scheme Procedure} close-fdes fd
  268. @deffnx {C Function} scm_close_fdes (fd)
  269. A simple wrapper for the @code{close} system call. Close file
  270. descriptor @var{fd}, which must be an integer. Unlike @code{close},
  271. the file descriptor will be closed even if a port is using it. The
  272. return value is unspecified.
  273. @end deffn
  274. @deffn {Scheme Procedure} pipe
  275. @deffnx {C Function} scm_pipe ()
  276. @cindex pipe
  277. Return a newly created pipe: a pair of ports which are linked together
  278. on the local machine. The @acronym{CAR} is the input port and the
  279. @acronym{CDR} is the output port. Data written (and flushed) to the
  280. output port can be read from the input port. Pipes are commonly used
  281. for communication with a newly forked child process. The need to flush
  282. the output port can be avoided by making it unbuffered using
  283. @code{setvbuf} (@pxref{Buffering}).
  284. @defvar PIPE_BUF
  285. A write of up to @code{PIPE_BUF} many bytes to a pipe is atomic,
  286. meaning when done it goes into the pipe instantaneously and as a
  287. contiguous block (@pxref{Pipe Atomicity,, Atomicity of Pipe I/O, libc,
  288. The GNU C Library Reference Manual}).
  289. @end defvar
  290. Note that the output port is likely to block if too much data has been
  291. written but not yet read from the input port. Typically the capacity
  292. is @code{PIPE_BUF} bytes.
  293. @end deffn
  294. The next group of procedures perform a @code{dup2}
  295. system call, if @var{newfd} (an
  296. integer) is supplied, otherwise a @code{dup}. The file descriptor to be
  297. duplicated can be supplied as an integer or contained in a port. The
  298. type of value returned varies depending on which procedure is used.
  299. All procedures also have the side effect when performing @code{dup2} that any
  300. ports using @var{newfd} are moved to a different file descriptor and have
  301. their revealed counts set to zero.
  302. @deffn {Scheme Procedure} dup->fdes fd_or_port [fd]
  303. @deffnx {C Function} scm_dup_to_fdes (fd_or_port, fd)
  304. Return a new integer file descriptor referring to the open file
  305. designated by @var{fd_or_port}, which must be either an open
  306. file port or a file descriptor.
  307. @end deffn
  308. @deffn {Scheme Procedure} dup->inport port/fd [newfd]
  309. Returns a new input port using the new file descriptor.
  310. @end deffn
  311. @deffn {Scheme Procedure} dup->outport port/fd [newfd]
  312. Returns a new output port using the new file descriptor.
  313. @end deffn
  314. @deffn {Scheme Procedure} dup port/fd [newfd]
  315. Returns a new port if @var{port/fd} is a port, with the same mode as the
  316. supplied port, otherwise returns an integer file descriptor.
  317. @end deffn
  318. @deffn {Scheme Procedure} dup->port port/fd mode [newfd]
  319. Returns a new port using the new file descriptor. @var{mode} supplies a
  320. mode string for the port (@pxref{File Ports, open-file}).
  321. @end deffn
  322. @deffn {Scheme Procedure} duplicate-port port modes
  323. Returns a new port which is opened on a duplicate of the file
  324. descriptor underlying @var{port}, with mode string @var{modes}
  325. as for @ref{File Ports, open-file}. The two ports
  326. will share a file position and file status flags.
  327. Unexpected behaviour can result if both ports are subsequently used
  328. and the original and/or duplicate ports are buffered.
  329. The mode string can include @code{0} to obtain an unbuffered duplicate
  330. port.
  331. This procedure is equivalent to @code{(dup->port @var{port} @var{modes})}.
  332. @end deffn
  333. @deffn {Scheme Procedure} redirect-port old_port new_port
  334. @deffnx {C Function} scm_redirect_port (old_port, new_port)
  335. This procedure takes two ports and duplicates the underlying file
  336. descriptor from @var{old_port} into @var{new_port}. The
  337. current file descriptor in @var{new_port} will be closed.
  338. After the redirection the two ports will share a file position
  339. and file status flags.
  340. The return value is unspecified.
  341. Unexpected behaviour can result if both ports are subsequently used
  342. and the original and/or duplicate ports are buffered.
  343. This procedure does not have any side effects on other ports or
  344. revealed counts.
  345. @end deffn
  346. @deffn {Scheme Procedure} dup2 oldfd newfd
  347. @deffnx {C Function} scm_dup2 (oldfd, newfd)
  348. A simple wrapper for the @code{dup2} system call.
  349. Copies the file descriptor @var{oldfd} to descriptor
  350. number @var{newfd}, replacing the previous meaning
  351. of @var{newfd}. Both @var{oldfd} and @var{newfd} must
  352. be integers.
  353. Unlike for @code{dup->fdes} or @code{primitive-move->fdes}, no attempt
  354. is made to move away ports which are using @var{newfd}.
  355. The return value is unspecified.
  356. @end deffn
  357. @deffn {Scheme Procedure} port-for-each proc
  358. @deffnx {C Function} scm_port_for_each (SCM proc)
  359. @deffnx {C Function} scm_c_port_for_each (void (*proc)(void *, SCM), void *data)
  360. Apply @var{proc} to each port in the Guile port table
  361. (FIXME: what is the Guile port table?)
  362. in turn. The return value is unspecified. More specifically,
  363. @var{proc} is applied exactly once to every port that exists in the
  364. system at the time @code{port-for-each} is invoked. Changes to the
  365. port table while @code{port-for-each} is running have no effect as far
  366. as @code{port-for-each} is concerned.
  367. The C function @code{scm_port_for_each} takes a Scheme procedure
  368. encoded as a @code{SCM} value, while @code{scm_c_port_for_each} takes
  369. a pointer to a C function and passes along a arbitrary @var{data}
  370. cookie.
  371. @end deffn
  372. @deffn {Scheme Procedure} fcntl port/fd cmd [value]
  373. @deffnx {C Function} scm_fcntl (object, cmd, value)
  374. Apply @var{cmd} on @var{port/fd}, either a port or file descriptor.
  375. The @var{value} argument is used by the @code{SET} commands described
  376. below, it's an integer value.
  377. Values for @var{cmd} are:
  378. @defvar F_DUPFD
  379. Duplicate the file descriptor, the same as @code{dup->fdes} above
  380. does.
  381. @end defvar
  382. @defvar F_GETFD
  383. @defvarx F_SETFD
  384. Get or set flags associated with the file descriptor. The only flag
  385. is the following,
  386. @defvar FD_CLOEXEC
  387. ``Close on exec'', meaning the file descriptor will be closed on an
  388. @code{exec} call (a successful such call). For example to set that
  389. flag,
  390. @example
  391. (fcntl port F_SETFD FD_CLOEXEC)
  392. @end example
  393. Or better, set it but leave any other possible future flags unchanged,
  394. @example
  395. (fcntl port F_SETFD (logior FD_CLOEXEC
  396. (fcntl port F_GETFD)))
  397. @end example
  398. @end defvar
  399. @end defvar
  400. @defvar F_GETFL
  401. @defvarx F_SETFL
  402. Get or set flags associated with the open file. These flags are
  403. @code{O_RDONLY} etc described under @code{open} above.
  404. A common use is to set @code{O_NONBLOCK} on a network socket. The
  405. following sets that flag, and leaves other flags unchanged.
  406. @example
  407. (fcntl sock F_SETFL (logior O_NONBLOCK
  408. (fcntl sock F_GETFL)))
  409. @end example
  410. @end defvar
  411. @defvar F_GETOWN
  412. @defvarx F_SETOWN
  413. Get or set the process ID of a socket's owner, for @code{SIGIO} signals.
  414. @end defvar
  415. @end deffn
  416. @deffn {Scheme Procedure} flock file operation
  417. @deffnx {C Function} scm_flock (file, operation)
  418. @cindex file locking
  419. Apply or remove an advisory lock on an open file.
  420. @var{operation} specifies the action to be done:
  421. @defvar LOCK_SH
  422. Shared lock. More than one process may hold a shared lock
  423. for a given file at a given time.
  424. @end defvar
  425. @defvar LOCK_EX
  426. Exclusive lock. Only one process may hold an exclusive lock
  427. for a given file at a given time.
  428. @end defvar
  429. @defvar LOCK_UN
  430. Unlock the file.
  431. @end defvar
  432. @defvar LOCK_NB
  433. Don't block when locking. This is combined with one of the other
  434. operations using @code{logior} (@pxref{Bitwise Operations}). If
  435. @code{flock} would block an @code{EWOULDBLOCK} error is thrown
  436. (@pxref{Conventions}).
  437. @end defvar
  438. The return value is not specified. @var{file} may be an open
  439. file descriptor or an open file descriptor port.
  440. Note that @code{flock} does not lock files across NFS.
  441. @end deffn
  442. @deffn {Scheme Procedure} select reads writes excepts [secs [usecs]]
  443. @deffnx {C Function} scm_select (reads, writes, excepts, secs, usecs)
  444. This procedure has a variety of uses: waiting for the ability
  445. to provide input, accept output, or the existence of
  446. exceptional conditions on a collection of ports or file
  447. descriptors, or waiting for a timeout to occur.
  448. When an error occurs, this procedure throws a @code{system-error}
  449. exception (@pxref{Conventions, @code{system-error}}). Note that
  450. @code{select} may return early for other reasons, for example due to
  451. pending interrupts. @xref{Asyncs}, for more on interrupts.
  452. @var{reads}, @var{writes} and @var{excepts} can be lists or
  453. vectors, with each member a port or a file descriptor.
  454. The value returned is a list of three corresponding
  455. lists or vectors containing only the members which meet the
  456. specified requirement. The ability of port buffers to
  457. provide input or accept output is taken into account.
  458. Ordering of the input lists or vectors is not preserved.
  459. The optional arguments @var{secs} and @var{usecs} specify the
  460. timeout. Either @var{secs} can be specified alone, as
  461. either an integer or a real number, or both @var{secs} and
  462. @var{usecs} can be specified as integers, in which case
  463. @var{usecs} is an additional timeout expressed in
  464. microseconds. If @var{secs} is omitted or is @code{#f} then
  465. select will wait for as long as it takes for one of the other
  466. conditions to be satisfied.
  467. The scsh version of @code{select} differs as follows:
  468. Only vectors are accepted for the first three arguments.
  469. The @var{usecs} argument is not supported.
  470. Multiple values are returned instead of a list.
  471. Duplicates in the input vectors appear only once in output.
  472. An additional @code{select!} interface is provided.
  473. @end deffn
  474. While it is sometimes necessary to operate at the level of file
  475. descriptors, this is an operation whose correctness can only be
  476. considered as part of a whole program. So for example while the effects
  477. of @code{(string-set! x 34 #\y)} are limited to the bits of code that
  478. can access @var{x}, @code{(close-fdes 34)} mutates the state of the
  479. entire process. In particular if another thread is using file
  480. descriptor 34 then their state might be corrupted; and another thread
  481. which opens a file might cause file descriptor 34 to be re-used, so that
  482. corruption could manifest itself in a strange way.
  483. @cindex fdes finalizers
  484. @cindex file descriptor finalizers
  485. @cindex finalizers, file descriptor
  486. However when working with file descriptors, it's common to want to
  487. associate information with the file descriptor, perhaps in a side table.
  488. To support this use case and to allow user code to remove an association
  489. when a file descriptor is closed, Guile offers @dfn{fdes finalizers}.
  490. As the name indicates, fdes finalizers are finalizers -- they can run in
  491. response to garbage collection, and they can also run in response to
  492. explicit calls to @code{close-port}, @code{close-fdes}, or the like. As
  493. such they inherit many of the pitfalls of finalizers: they may be
  494. invoked from concurrent threads, or not at all. @xref{Foreign Object
  495. Memory Management}, for more on finalizers.
  496. To use fdes finalizers, import their module;
  497. @example
  498. (use-modules (ice-9 fdes-finalizers))
  499. @end example
  500. @deffn {Scheme Procedure} add-fdes-finalizer! fdes finalizer
  501. @deffnx {Scheme Procedure} remove-fdes-finalizer! fdes finalizer
  502. Add or remove a finalizer for @var{fdes}. A finalizer is a procedure
  503. that is called by Guile when a file descriptor is closed. The file
  504. descriptor being closed is passed as the one argument to the finalizer.
  505. If a finalizer has been added multiple times to a file descriptor, to
  506. remove it would require that number of calls to
  507. @code{remove-fdes-finalizer!}.
  508. The finalizers added to a file descriptor are called by Guile in an
  509. unspecified order, and their return values are ignored.
  510. @end deffn
  511. @node File System
  512. @subsection File System
  513. @cindex file system
  514. These procedures allow querying and setting file system attributes
  515. (such as owner,
  516. permissions, sizes and types of files); deleting, copying, renaming and
  517. linking files; creating and removing directories and querying their
  518. contents; syncing the file system and creating special files.
  519. @deffn {Scheme Procedure} access? path how
  520. @deffnx {C Function} scm_access (path, how)
  521. Test accessibility of a file under the real UID and GID of the calling
  522. process. The return is @code{#t} if @var{path} exists and the
  523. permissions requested by @var{how} are all allowed, or @code{#f} if
  524. not.
  525. @var{how} is an integer which is one of the following values, or a
  526. bitwise-OR (@code{logior}) of multiple values.
  527. @defvar R_OK
  528. Test for read permission.
  529. @end defvar
  530. @defvar W_OK
  531. Test for write permission.
  532. @end defvar
  533. @defvar X_OK
  534. Test for execute permission.
  535. @end defvar
  536. @defvar F_OK
  537. Test for existence of the file. This is implied by each of the other
  538. tests, so there's no need to combine it with them.
  539. @end defvar
  540. It's important to note that @code{access?} does not simply indicate
  541. what will happen on attempting to read or write a file. In normal
  542. circumstances it does, but in a set-UID or set-GID program it doesn't
  543. because @code{access?} tests the real ID, whereas an open or execute
  544. attempt uses the effective ID.
  545. A program which will never run set-UID/GID can ignore the difference
  546. between real and effective IDs, but for maximum generality, especially
  547. in library functions, it's best not to use @code{access?} to predict
  548. the result of an open or execute, instead simply attempt that and
  549. catch any exception.
  550. The main use for @code{access?} is to let a set-UID/GID program
  551. determine what the invoking user would have been allowed to do,
  552. without the greater (or perhaps lesser) privileges afforded by the
  553. effective ID. For more on this, see @ref{Testing File Access,,, libc,
  554. The GNU C Library Reference Manual}.
  555. @end deffn
  556. @findex fstat
  557. @deffn {Scheme Procedure} stat object [exception-on-error?]
  558. @deffnx {C Function} scm_stat (object, exception_on_error)
  559. Return an object containing various information about the file
  560. determined by @var{object}. @var{object} can be a string containing
  561. a file name or a port or integer file descriptor which is open
  562. on a file (in which case @code{fstat} is used as the underlying
  563. system call).
  564. If the optional @var{exception_on_error} argument is true, which
  565. is the default, an exception will be raised if the underlying
  566. system call returns an error, for example if the file is not
  567. found or is not readable. Otherwise, an error will cause
  568. @code{stat} to return @code{#f}.
  569. The object returned by @code{stat} can be passed as a single
  570. parameter to the following procedures, all of which return
  571. integers:
  572. @deffn {Scheme Procedure} stat:dev st
  573. The device number containing the file.
  574. @end deffn
  575. @deffn {Scheme Procedure} stat:ino st
  576. The file serial number, which distinguishes this file from all
  577. other files on the same device.
  578. @end deffn
  579. @deffn {Scheme Procedure} stat:mode st
  580. The mode of the file. This is an integer which incorporates file type
  581. information and file permission bits. See also @code{stat:type} and
  582. @code{stat:perms} below.
  583. @end deffn
  584. @deffn {Scheme Procedure} stat:nlink st
  585. The number of hard links to the file.
  586. @end deffn
  587. @deffn {Scheme Procedure} stat:uid st
  588. The user ID of the file's owner.
  589. @end deffn
  590. @deffn {Scheme Procedure} stat:gid st
  591. The group ID of the file.
  592. @end deffn
  593. @deffn {Scheme Procedure} stat:rdev st
  594. Device ID; this entry is defined only for character or block special
  595. files. On some systems this field is not available at all, in which
  596. case @code{stat:rdev} returns @code{#f}.
  597. @end deffn
  598. @deffn {Scheme Procedure} stat:size st
  599. The size of a regular file in bytes.
  600. @end deffn
  601. @deffn {Scheme Procedure} stat:atime st
  602. The last access time for the file, in seconds.
  603. @end deffn
  604. @deffn {Scheme Procedure} stat:mtime st
  605. The last modification time for the file, in seconds.
  606. @end deffn
  607. @deffn {Scheme Procedure} stat:ctime st
  608. The last modification time for the attributes of the file, in seconds.
  609. @end deffn
  610. @deffn {Scheme Procedure} stat:atimensec st
  611. @deffnx {Scheme Procedure} stat:mtimensec st
  612. @deffnx {Scheme Procedure} stat:ctimensec st
  613. The fractional part of a file's access, modification, or attribute modification
  614. time, in nanoseconds. Nanosecond timestamps are only available on some operating
  615. systems and file systems. If Guile cannot retrieve nanosecond-level timestamps
  616. for a file, these fields will be set to 0.
  617. @end deffn
  618. @deffn {Scheme Procedure} stat:blksize st
  619. The optimal block size for reading or writing the file, in bytes. On
  620. some systems this field is not available, in which case
  621. @code{stat:blksize} returns a sensible suggested block size.
  622. @end deffn
  623. @deffn {Scheme Procedure} stat:blocks st
  624. The amount of disk space that the file occupies measured in units of
  625. 512 byte blocks. On some systems this field is not available, in
  626. which case @code{stat:blocks} returns @code{#f}.
  627. @end deffn
  628. In addition, the following procedures return the information
  629. from @code{stat:mode} in a more convenient form:
  630. @deffn {Scheme Procedure} stat:type st
  631. A symbol representing the type of file. Possible values are
  632. @samp{regular}, @samp{directory}, @samp{symlink},
  633. @samp{block-special}, @samp{char-special}, @samp{fifo}, @samp{socket},
  634. and @samp{unknown}.
  635. @end deffn
  636. @deffn {Scheme Procedure} stat:perms st
  637. An integer representing the access permission bits.
  638. @end deffn
  639. @end deffn
  640. @deffn {Scheme Procedure} lstat path
  641. @deffnx {C Function} scm_lstat (path)
  642. Similar to @code{stat}, but does not follow symbolic links, i.e.,
  643. it will return information about a symbolic link itself, not the
  644. file it points to. @var{path} must be a string.
  645. @end deffn
  646. @deffn {Scheme Procedure} readlink path
  647. @deffnx {C Function} scm_readlink (path)
  648. Return the value of the symbolic link named by @var{path} (a
  649. string), i.e., the file that the link points to.
  650. @end deffn
  651. @findex fchown
  652. @findex lchown
  653. @deffn {Scheme Procedure} chown object owner group
  654. @deffnx {C Function} scm_chown (object, owner, group)
  655. Change the ownership and group of the file referred to by @var{object}
  656. to the integer values @var{owner} and @var{group}. @var{object} can
  657. be a string containing a file name or, if the platform supports
  658. @code{fchown} (@pxref{File Owner,,,libc,The GNU C Library Reference
  659. Manual}), a port or integer file descriptor which is open on the file.
  660. The return value is unspecified.
  661. If @var{object} is a symbolic link, either the
  662. ownership of the link or the ownership of the referenced file will be
  663. changed depending on the operating system (lchown is
  664. unsupported at present). If @var{owner} or @var{group} is specified
  665. as @code{-1}, then that ID is not changed.
  666. @end deffn
  667. @findex fchmod
  668. @deffn {Scheme Procedure} chmod object mode
  669. @deffnx {C Function} scm_chmod (object, mode)
  670. Changes the permissions of the file referred to by @var{object}.
  671. @var{object} can be a string containing a file name or a port or integer file
  672. descriptor which is open on a file (in which case @code{fchmod} is used
  673. as the underlying system call).
  674. @var{mode} specifies
  675. the new permissions as a decimal number, e.g., @code{(chmod "foo" #o755)}.
  676. The return value is unspecified.
  677. @end deffn
  678. @deffn {Scheme Procedure} utime pathname [actime [modtime [actimens [modtimens [flags]]]]]
  679. @deffnx {C Function} scm_utime (pathname, actime, modtime, actimens, modtimens, flags)
  680. @code{utime} sets the access and modification times for the
  681. file named by @var{pathname}. If @var{actime} or @var{modtime} is
  682. not supplied, then the current time is used. @var{actime} and
  683. @var{modtime} must be integer time values as returned by the
  684. @code{current-time} procedure.
  685. The optional @var{actimens} and @var{modtimens} are nanoseconds
  686. to add @var{actime} and @var{modtime}. Nanosecond precision is
  687. only supported on some combinations of file systems and operating
  688. systems.
  689. @lisp
  690. (utime "foo" (- (current-time) 3600))
  691. @end lisp
  692. will set the access time to one hour in the past and the
  693. modification time to the current time.
  694. @vindex AT_SYMLINK_NOFOLLOW
  695. Last, @var{flags} may be either @code{0} or the
  696. @code{AT_SYMLINK_NOFOLLOW} constant, to set the time of
  697. @var{pathname} even if it is a symbolic link.
  698. @end deffn
  699. @findex unlink
  700. @deffn {Scheme Procedure} delete-file str
  701. @deffnx {C Function} scm_delete_file (str)
  702. Deletes (or ``unlinks'') the file whose path is specified by
  703. @var{str}.
  704. @end deffn
  705. @deffn {Scheme Procedure} copy-file oldfile newfile
  706. @deffnx {C Function} scm_copy_file (oldfile, newfile)
  707. Copy the file specified by @var{oldfile} to @var{newfile}.
  708. The return value is unspecified.
  709. @end deffn
  710. @deffn {Scheme Procedure} sendfile out in count [offset]
  711. @deffnx {C Function} scm_sendfile (out, in, count, offset)
  712. Send @var{count} bytes from @var{in} to @var{out}, both of which
  713. must be either open file ports or file descriptors. When
  714. @var{offset} is omitted, start reading from @var{in}'s current
  715. position; otherwise, start reading at @var{offset}. Return
  716. the number of bytes actually sent.
  717. When @var{in} is a port, it is often preferable to specify @var{offset},
  718. because @var{in}'s offset as a port may be different from the offset of
  719. its underlying file descriptor.
  720. On systems that support it, such as GNU/Linux, this procedure uses the
  721. @code{sendfile} libc function, which usually corresponds to a system
  722. call. This is faster than doing a series of @code{read} and
  723. @code{write} system calls. A typical application is to send a file over
  724. a socket.
  725. In some cases, the @code{sendfile} libc function may return
  726. @code{EINVAL} or @code{ENOSYS}. In that case, Guile's @code{sendfile}
  727. procedure automatically falls back to doing a series of @code{read} and
  728. @code{write} calls.
  729. In other cases, the libc function may send fewer bytes than
  730. @var{count}---for instance because @var{out} is a slow or limited
  731. device, such as a pipe. When that happens, Guile's @code{sendfile}
  732. automatically retries until exactly @var{count} bytes were sent or an
  733. error occurs.
  734. @end deffn
  735. @findex rename
  736. @deffn {Scheme Procedure} rename-file oldname newname
  737. @deffnx {C Function} scm_rename (oldname, newname)
  738. Renames the file specified by @var{oldname} to @var{newname}.
  739. The return value is unspecified.
  740. @end deffn
  741. @deffn {Scheme Procedure} link oldpath newpath
  742. @deffnx {C Function} scm_link (oldpath, newpath)
  743. Creates a new name @var{newpath} in the file system for the
  744. file named by @var{oldpath}. If @var{oldpath} is a symbolic
  745. link, the link may or may not be followed depending on the
  746. system.
  747. @end deffn
  748. @deffn {Scheme Procedure} symlink oldpath newpath
  749. @deffnx {C Function} scm_symlink (oldpath, newpath)
  750. Create a symbolic link named @var{newpath} with the value (i.e., pointing to)
  751. @var{oldpath}. The return value is unspecified.
  752. @end deffn
  753. @deffn {Scheme Procedure} mkdir path [mode]
  754. @deffnx {C Function} scm_mkdir (path, mode)
  755. Create a new directory named by @var{path}. If @var{mode} is omitted
  756. then the permissions of the directory are set to @code{#o777}
  757. masked with the current umask (@pxref{Processes, @code{umask}}).
  758. Otherwise they are set to the value specified with @var{mode}.
  759. The return value is unspecified.
  760. @end deffn
  761. @deffn {Scheme Procedure} rmdir path
  762. @deffnx {C Function} scm_rmdir (path)
  763. Remove the existing directory named by @var{path}. The directory must
  764. be empty for this to succeed. The return value is unspecified.
  765. @end deffn
  766. @deffn {Scheme Procedure} opendir dirname
  767. @deffnx {C Function} scm_opendir (dirname)
  768. @cindex directory contents
  769. Open the directory specified by @var{dirname} and return a directory
  770. stream.
  771. Before using this and the procedures below, make sure to see the
  772. higher-level procedures for directory traversal that are available
  773. (@pxref{File Tree Walk}).
  774. @end deffn
  775. @deffn {Scheme Procedure} directory-stream? object
  776. @deffnx {C Function} scm_directory_stream_p (object)
  777. Return a boolean indicating whether @var{object} is a directory
  778. stream as returned by @code{opendir}.
  779. @end deffn
  780. @deffn {Scheme Procedure} readdir stream
  781. @deffnx {C Function} scm_readdir (stream)
  782. Return (as a string) the next directory entry from the directory stream
  783. @var{stream}. If there is no remaining entry to be read then the
  784. end of file object is returned.
  785. @end deffn
  786. @deffn {Scheme Procedure} rewinddir stream
  787. @deffnx {C Function} scm_rewinddir (stream)
  788. Reset the directory port @var{stream} so that the next call to
  789. @code{readdir} will return the first directory entry.
  790. @end deffn
  791. @deffn {Scheme Procedure} closedir stream
  792. @deffnx {C Function} scm_closedir (stream)
  793. Close the directory stream @var{stream}.
  794. The return value is unspecified.
  795. @end deffn
  796. Here is an example showing how to display all the entries in a
  797. directory:
  798. @lisp
  799. (define dir (opendir "/usr/lib"))
  800. (do ((entry (readdir dir) (readdir dir)))
  801. ((eof-object? entry))
  802. (display entry)(newline))
  803. (closedir dir)
  804. @end lisp
  805. @deffn {Scheme Procedure} sync
  806. @deffnx {C Function} scm_sync ()
  807. Flush the operating system disk buffers.
  808. The return value is unspecified.
  809. @end deffn
  810. @deffn {Scheme Procedure} mknod path type perms dev
  811. @deffnx {C Function} scm_mknod (path, type, perms, dev)
  812. @cindex device file
  813. Creates a new special file, such as a file corresponding to a device.
  814. @var{path} specifies the name of the file. @var{type} should be one
  815. of the following symbols: @samp{regular}, @samp{directory},
  816. @samp{symlink}, @samp{block-special}, @samp{char-special},
  817. @samp{fifo}, or @samp{socket}. @var{perms} (an integer) specifies the
  818. file permissions. @var{dev} (an integer) specifies which device the
  819. special file refers to. Its exact interpretation depends on the kind
  820. of special file being created.
  821. E.g.,
  822. @lisp
  823. (mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2))
  824. @end lisp
  825. The return value is unspecified.
  826. @end deffn
  827. @deffn {Scheme Procedure} tmpnam
  828. @deffnx {C Function} scm_tmpnam ()
  829. @cindex temporary file
  830. Return an auto-generated name of a temporary file, a file which
  831. doesn't already exist. The name includes a path, it's usually in
  832. @file{/tmp} but that's system dependent.
  833. Care must be taken when using @code{tmpnam}. In between choosing the
  834. name and creating the file another program might use that name, or an
  835. attacker might even make it a symlink pointing at something important
  836. and causing you to overwrite that.
  837. The safe way is to create the file using @code{open} with
  838. @code{O_EXCL} to avoid any overwriting. A loop can try again with
  839. another name if the file exists (error @code{EEXIST}).
  840. @code{mkstemp!} below does that.
  841. @end deffn
  842. @deffn {Scheme Procedure} mkstemp! tmpl [mode]
  843. @deffnx {C Function} scm_mkstemp (tmpl)
  844. @cindex temporary file
  845. Create a new unique file in the file system and return a new buffered
  846. port open for reading and writing to the file.
  847. @var{tmpl} is a string specifying where the file should be created: it
  848. must end with @samp{XXXXXX} and those @samp{X}s will be changed in the
  849. string to return the name of the file. (@code{port-filename} on the
  850. port also gives the name.)
  851. POSIX doesn't specify the permissions mode of the file, on GNU and
  852. most systems it's @code{#o600}. An application can use @code{chmod}
  853. to relax that if desired. For example @code{#o666} less @code{umask},
  854. which is usual for ordinary file creation,
  855. @example
  856. (let ((port (mkstemp! (string-copy "/tmp/myfile-XXXXXX"))))
  857. (chmod port (logand #o666 (lognot (umask))))
  858. ...)
  859. @end example
  860. The optional @var{mode} argument specifies a mode with which to open the
  861. new file, as a string in the same format that @code{open-file} takes.
  862. It defaults to @code{"w+"}.
  863. @end deffn
  864. @deffn {Scheme Procedure} tmpfile
  865. @deffnx {C Function} scm_tmpfile ()
  866. Return an input/output port to a unique temporary file
  867. named using the path prefix @code{P_tmpdir} defined in
  868. @file{stdio.h}.
  869. The file is automatically deleted when the port is closed
  870. or the program terminates.
  871. @end deffn
  872. @deffn {Scheme Procedure} mkdtemp tmpl
  873. @deffnx {C Function} scm_mkdtemp (tmpl)
  874. @cindex temporary directory
  875. Create a new directory named in accordance with the template string
  876. @var{tmpl}.
  877. @var{tmpl} is a string specifying the directory's name. The last six
  878. characters of @var{tmpl} must be @samp{XXXXXX}. Upon successful
  879. execution, the name of the new directory is returned which has the same
  880. form as @var{tmpl} but with the @samp{XXXXXX} characters modified to
  881. ensure the directory name is unique.
  882. The permissions of the directory created are OS dependent, but, are
  883. usually @code{#o700}.
  884. An error may be thrown if the template has the wrong format or if the
  885. directory cannot be created.
  886. @end deffn
  887. @deffn {Scheme Procedure} dirname filename
  888. @deffnx {C Function} scm_dirname (filename)
  889. Return the directory name component of the file name
  890. @var{filename}. If @var{filename} does not contain a directory
  891. component, @code{.} is returned.
  892. @end deffn
  893. @deffn {Scheme Procedure} basename filename [suffix]
  894. @deffnx {C Function} scm_basename (filename, suffix)
  895. Return the base name of the file name @var{filename}. The
  896. base name is the file name without any directory components.
  897. If @var{suffix} is provided, and is equal to the end of
  898. @var{basename}, it is removed also.
  899. @lisp
  900. (basename "/tmp/test.xml" ".xml")
  901. @result{} "test"
  902. @end lisp
  903. @end deffn
  904. @deffn {Scheme Procedure} canonicalize-path path
  905. @deffnx {C Function} scm_canonicalize_path (path)
  906. Return the canonical (absolute) path of @var{path}.
  907. A canonical path has no @code{.} or @code{..} components,
  908. nor any repeated path separators (@code{/}) nor symlinks.
  909. Raises an error if any component of @var{path} does not
  910. exist.
  911. @lisp
  912. (canonicalize-path "test.xml")
  913. @result{} "/tmp/test.xml"
  914. @end lisp
  915. @end deffn
  916. @deffn {Scheme Procedure} file-exists? filename
  917. Return @code{#t} if the file named @var{filename} exists, @code{#f} if
  918. not.
  919. @end deffn
  920. @cindex file name separator
  921. @cindex absolute file name
  922. Many operating systems, such as GNU, use @code{/} (forward slash) to
  923. separate the components of a file name; any file name starting with
  924. @code{/} is considered an @dfn{absolute file name}. These conventions
  925. are specified by the POSIX Base Definitions, which refer to conforming
  926. file names as ``pathnames''. Some operating systems use a different
  927. convention; in particular, Windows uses @code{\} (backslash) as the file
  928. name separator, and also has the notion of @dfn{volume names} like
  929. @code{C:\} for absolute file names. The following procedures and
  930. variables provide support for portable file name manipulations.
  931. @deffn {Scheme Procedure} system-file-name-convention
  932. Return either @code{posix} or @code{windows}, depending on
  933. what kind of system this Guile is running on.
  934. @end deffn
  935. @deffn {Scheme Procedure} file-name-separator? c
  936. Return true if character @var{c} is a file name separator on the host
  937. platform.
  938. @end deffn
  939. @deffn {Scheme Procedure} absolute-file-name? file-name
  940. Return true if @var{file-name} denotes an absolute file name on the host
  941. platform.
  942. @end deffn
  943. @defvr {Scheme Variable} file-name-separator-string
  944. The preferred file name separator.
  945. Note that on MinGW builds for Windows, both @code{/} and @code{\} are
  946. valid separators. Thus, programs should not assume that
  947. @code{file-name-separator-string} is the @emph{only} file name
  948. separator---e.g., when extracting the components of a file name.
  949. @end defvr
  950. @node User Information
  951. @subsection User Information
  952. @cindex user information
  953. @cindex password file
  954. @cindex group file
  955. The facilities in this section provide an interface to the user and
  956. group database.
  957. They should be used with care since they are not reentrant.
  958. The following functions accept an object representing user information
  959. and return a selected component:
  960. @deffn {Scheme Procedure} passwd:name pw
  961. The name of the userid.
  962. @end deffn
  963. @deffn {Scheme Procedure} passwd:passwd pw
  964. The encrypted passwd.
  965. @end deffn
  966. @deffn {Scheme Procedure} passwd:uid pw
  967. The user id number.
  968. @end deffn
  969. @deffn {Scheme Procedure} passwd:gid pw
  970. The group id number.
  971. @end deffn
  972. @deffn {Scheme Procedure} passwd:gecos pw
  973. The full name.
  974. @end deffn
  975. @deffn {Scheme Procedure} passwd:dir pw
  976. The home directory.
  977. @end deffn
  978. @deffn {Scheme Procedure} passwd:shell pw
  979. The login shell.
  980. @end deffn
  981. @sp 1
  982. @deffn {Scheme Procedure} getpwuid uid
  983. Look up an integer userid in the user database.
  984. @end deffn
  985. @deffn {Scheme Procedure} getpwnam name
  986. Look up a user name string in the user database.
  987. @end deffn
  988. @deffn {Scheme Procedure} setpwent
  989. Initializes a stream used by @code{getpwent} to read from the user database.
  990. The next use of @code{getpwent} will return the first entry. The
  991. return value is unspecified.
  992. @end deffn
  993. @deffn {Scheme Procedure} getpwent
  994. Read the next entry in the user database stream. The return is a
  995. passwd user object as above, or @code{#f} when no more entries.
  996. @end deffn
  997. @deffn {Scheme Procedure} endpwent
  998. Closes the stream used by @code{getpwent}. The return value is unspecified.
  999. @end deffn
  1000. @deffn {Scheme Procedure} setpw [arg]
  1001. @deffnx {C Function} scm_setpwent (arg)
  1002. If called with a true argument, initialize or reset the password data
  1003. stream. Otherwise, close the stream. The @code{setpwent} and
  1004. @code{endpwent} procedures are implemented on top of this.
  1005. @end deffn
  1006. @deffn {Scheme Procedure} getpw [user]
  1007. @deffnx {C Function} scm_getpwuid (user)
  1008. Look up an entry in the user database. @var{user} can be an integer,
  1009. a string, or omitted, giving the behaviour of getpwuid, getpwnam
  1010. or getpwent respectively.
  1011. @end deffn
  1012. The following functions accept an object representing group information
  1013. and return a selected component:
  1014. @deffn {Scheme Procedure} group:name gr
  1015. The group name.
  1016. @end deffn
  1017. @deffn {Scheme Procedure} group:passwd gr
  1018. The encrypted group password.
  1019. @end deffn
  1020. @deffn {Scheme Procedure} group:gid gr
  1021. The group id number.
  1022. @end deffn
  1023. @deffn {Scheme Procedure} group:mem gr
  1024. A list of userids which have this group as a supplementary group.
  1025. @end deffn
  1026. @sp 1
  1027. @deffn {Scheme Procedure} getgrgid gid
  1028. Look up an integer group id in the group database.
  1029. @end deffn
  1030. @deffn {Scheme Procedure} getgrnam name
  1031. Look up a group name in the group database.
  1032. @end deffn
  1033. @deffn {Scheme Procedure} setgrent
  1034. Initializes a stream used by @code{getgrent} to read from the group database.
  1035. The next use of @code{getgrent} will return the first entry.
  1036. The return value is unspecified.
  1037. @end deffn
  1038. @deffn {Scheme Procedure} getgrent
  1039. Return the next entry in the group database, using the stream set by
  1040. @code{setgrent}.
  1041. @end deffn
  1042. @deffn {Scheme Procedure} endgrent
  1043. Closes the stream used by @code{getgrent}.
  1044. The return value is unspecified.
  1045. @end deffn
  1046. @deffn {Scheme Procedure} setgr [arg]
  1047. @deffnx {C Function} scm_setgrent (arg)
  1048. If called with a true argument, initialize or reset the group data
  1049. stream. Otherwise, close the stream. The @code{setgrent} and
  1050. @code{endgrent} procedures are implemented on top of this.
  1051. @end deffn
  1052. @deffn {Scheme Procedure} getgr [group]
  1053. @deffnx {C Function} scm_getgrgid (group)
  1054. Look up an entry in the group database. @var{group} can be an integer,
  1055. a string, or omitted, giving the behaviour of getgrgid, getgrnam
  1056. or getgrent respectively.
  1057. @end deffn
  1058. In addition to the accessor procedures for the user database, the
  1059. following shortcut procedure is also available.
  1060. @deffn {Scheme Procedure} getlogin
  1061. @deffnx {C Function} scm_getlogin ()
  1062. Return a string containing the name of the user logged in on
  1063. the controlling terminal of the process, or @code{#f} if this
  1064. information cannot be obtained.
  1065. @end deffn
  1066. @node Time
  1067. @subsection Time
  1068. @cindex time
  1069. @deffn {Scheme Procedure} current-time
  1070. @deffnx {C Function} scm_current_time ()
  1071. Return the number of seconds since 1970-01-01 00:00:00 @acronym{UTC},
  1072. excluding leap seconds.
  1073. @end deffn
  1074. @deffn {Scheme Procedure} gettimeofday
  1075. @deffnx {C Function} scm_gettimeofday ()
  1076. Return a pair containing the number of seconds and microseconds
  1077. since 1970-01-01 00:00:00 @acronym{UTC}, excluding leap seconds. Note:
  1078. whether true microsecond resolution is available depends on the
  1079. operating system.
  1080. @end deffn
  1081. The following procedures either accept an object representing a broken down
  1082. time and return a selected component, or accept an object representing
  1083. a broken down time and a value and set the component to the value.
  1084. The numbers in parentheses give the usual range.
  1085. @deffn {Scheme Procedure} tm:sec tm
  1086. @deffnx {Scheme Procedure} set-tm:sec tm val
  1087. Seconds (0-59).
  1088. @end deffn
  1089. @deffn {Scheme Procedure} tm:min tm
  1090. @deffnx {Scheme Procedure} set-tm:min tm val
  1091. Minutes (0-59).
  1092. @end deffn
  1093. @deffn {Scheme Procedure} tm:hour tm
  1094. @deffnx {Scheme Procedure} set-tm:hour tm val
  1095. Hours (0-23).
  1096. @end deffn
  1097. @deffn {Scheme Procedure} tm:mday tm
  1098. @deffnx {Scheme Procedure} set-tm:mday tm val
  1099. Day of the month (1-31).
  1100. @end deffn
  1101. @deffn {Scheme Procedure} tm:mon tm
  1102. @deffnx {Scheme Procedure} set-tm:mon tm val
  1103. Month (0-11).
  1104. @end deffn
  1105. @deffn {Scheme Procedure} tm:year tm
  1106. @deffnx {Scheme Procedure} set-tm:year tm val
  1107. Year (70-), the year minus 1900.
  1108. @end deffn
  1109. @deffn {Scheme Procedure} tm:wday tm
  1110. @deffnx {Scheme Procedure} set-tm:wday tm val
  1111. Day of the week (0-6) with Sunday represented as 0.
  1112. @end deffn
  1113. @deffn {Scheme Procedure} tm:yday tm
  1114. @deffnx {Scheme Procedure} set-tm:yday tm val
  1115. Day of the year (0-364, 365 in leap years).
  1116. @end deffn
  1117. @deffn {Scheme Procedure} tm:isdst tm
  1118. @deffnx {Scheme Procedure} set-tm:isdst tm val
  1119. Daylight saving indicator (0 for ``no'', greater than 0 for ``yes'', less than
  1120. 0 for ``unknown'').
  1121. @end deffn
  1122. @deffn {Scheme Procedure} tm:gmtoff tm
  1123. @deffnx {Scheme Procedure} set-tm:gmtoff tm val
  1124. Time zone offset in seconds west of @acronym{UTC} (-46800 to 43200).
  1125. For example on East coast USA (zone @samp{EST+5}) this would be 18000
  1126. (ie.@: @m{5\times60\times60,5*60*60}) in winter, or 14400
  1127. (ie.@: @m{4\times60\times60,4*60*60}) during daylight savings.
  1128. Note @code{tm:gmtoff} is not the same as @code{tm_gmtoff} in the C
  1129. @code{tm} structure. @code{tm_gmtoff} is seconds east and hence the
  1130. negative of the value here.
  1131. @end deffn
  1132. @deffn {Scheme Procedure} tm:zone tm
  1133. @deffnx {Scheme Procedure} set-tm:zone tm val
  1134. Time zone label (a string), not necessarily unique.
  1135. @end deffn
  1136. @sp 1
  1137. @deffn {Scheme Procedure} localtime time [zone]
  1138. @deffnx {C Function} scm_localtime (time, zone)
  1139. @cindex local time
  1140. Return an object representing the broken down components of
  1141. @var{time}, an integer like the one returned by
  1142. @code{current-time}. The time zone for the calculation is
  1143. optionally specified by @var{zone} (a string), otherwise the
  1144. @env{TZ} environment variable or the system default is used.
  1145. @end deffn
  1146. @deffn {Scheme Procedure} gmtime time
  1147. @deffnx {C Function} scm_gmtime (time)
  1148. Return an object representing the broken down components of
  1149. @var{time}, an integer like the one returned by
  1150. @code{current-time}. The values are calculated for @acronym{UTC}.
  1151. @end deffn
  1152. @deffn {Scheme Procedure} mktime sbd-time [zone]
  1153. @deffnx {C Function} scm_mktime (sbd_time, zone)
  1154. For a broken down time object @var{sbd-time}, return a pair the
  1155. @code{car} of which is an integer time like @code{current-time}, and
  1156. the @code{cdr} of which is a new broken down time with normalized
  1157. fields.
  1158. @var{zone} is a timezone string, or the default is the @env{TZ}
  1159. environment variable or the system default (@pxref{TZ Variable,,
  1160. Specifying the Time Zone with @env{TZ}, libc, GNU C Library Reference
  1161. Manual}). @var{sbd-time} is taken to be in that @var{zone}.
  1162. The following fields of @var{sbd-time} are used: @code{tm:year},
  1163. @code{tm:mon}, @code{tm:mday}, @code{tm:hour}, @code{tm:min},
  1164. @code{tm:sec}, @code{tm:isdst}. The values can be outside their usual
  1165. ranges. For example @code{tm:hour} normally goes up to 23, but a
  1166. value say 33 would mean 9 the following day.
  1167. @code{tm:isdst} in @var{sbd-time} says whether the time given is with
  1168. daylight savings or not. This is ignored if @var{zone} doesn't have
  1169. any daylight savings adjustment amount.
  1170. The broken down time in the return normalizes the values of
  1171. @var{sbd-time} by bringing them into their usual ranges, and using the
  1172. actual daylight savings rule for that time in @var{zone} (which may
  1173. differ from what @var{sbd-time} had). The easiest way to think of
  1174. this is that @var{sbd-time} plus @var{zone} converts to the integer
  1175. UTC time, then a @code{localtime} is applied to get the normal
  1176. presentation of that time, in @var{zone}.
  1177. @end deffn
  1178. @deffn {Scheme Procedure} tzset
  1179. @deffnx {C Function} scm_tzset ()
  1180. Initialize the timezone from the @env{TZ} environment variable
  1181. or the system default. It's not usually necessary to call this procedure
  1182. since it's done automatically by other procedures that depend on the
  1183. timezone.
  1184. @end deffn
  1185. @deffn {Scheme Procedure} strftime format tm
  1186. @deffnx {C Function} scm_strftime (format, tm)
  1187. @cindex time formatting
  1188. Return a string which is broken-down time structure @var{tm} formatted
  1189. according to the given @var{format} string.
  1190. @var{format} contains field specifications introduced by a @samp{%}
  1191. character. See @ref{Formatting Calendar Time,,, libc, The GNU C
  1192. Library Reference Manual}, or @samp{man 3 strftime}, for the available
  1193. formatting.
  1194. @lisp
  1195. (strftime "%c" (localtime (current-time)))
  1196. @result{} "Mon Mar 11 20:17:43 2002"
  1197. @end lisp
  1198. If @code{setlocale} has been called (@pxref{Locales}), month and day
  1199. names are from the current locale and in the locale character set.
  1200. @end deffn
  1201. @deffn {Scheme Procedure} strptime format string
  1202. @deffnx {C Function} scm_strptime (format, string)
  1203. @cindex time parsing
  1204. Performs the reverse action to @code{strftime}, parsing
  1205. @var{string} according to the specification supplied in
  1206. @var{format}. The interpretation of month and day names is
  1207. dependent on the current locale. The value returned is a pair.
  1208. The @acronym{CAR} has an object with time components
  1209. in the form returned by @code{localtime} or @code{gmtime},
  1210. but the time zone components
  1211. are not usefully set.
  1212. The @acronym{CDR} reports the number of characters from @var{string}
  1213. which were used for the conversion.
  1214. @end deffn
  1215. @defvar internal-time-units-per-second
  1216. The value of this variable is the number of time units per second
  1217. reported by the following procedures.
  1218. @end defvar
  1219. @deffn {Scheme Procedure} times
  1220. @deffnx {C Function} scm_times ()
  1221. Return an object with information about real and processor
  1222. time. The following procedures accept such an object as an
  1223. argument and return a selected component:
  1224. @deffn {Scheme Procedure} tms:clock tms
  1225. The current real time, expressed as time units relative to an
  1226. arbitrary base.
  1227. @end deffn
  1228. @deffn {Scheme Procedure} tms:utime tms
  1229. The CPU time units used by the calling process.
  1230. @end deffn
  1231. @deffn {Scheme Procedure} tms:stime tms
  1232. The CPU time units used by the system on behalf of the calling
  1233. process.
  1234. @end deffn
  1235. @deffn {Scheme Procedure} tms:cutime tms
  1236. The CPU time units used by terminated child processes of the
  1237. calling process, whose status has been collected (e.g., using
  1238. @code{waitpid}).
  1239. @end deffn
  1240. @deffn {Scheme Procedure} tms:cstime tms
  1241. Similarly, the CPU times units used by the system on behalf of
  1242. terminated child processes.
  1243. @end deffn
  1244. @end deffn
  1245. @deffn {Scheme Procedure} get-internal-real-time
  1246. @deffnx {C Function} scm_get_internal_real_time ()
  1247. Return the number of time units since the interpreter was
  1248. started.
  1249. @end deffn
  1250. @deffn {Scheme Procedure} get-internal-run-time
  1251. @deffnx {C Function} scm_get_internal_run_time ()
  1252. Return the number of time units of processor time used by the
  1253. interpreter. Both @emph{system} and @emph{user} time are
  1254. included but subprocesses are not.
  1255. @end deffn
  1256. @node Runtime Environment
  1257. @subsection Runtime Environment
  1258. @deffn {Scheme Procedure} program-arguments
  1259. @deffnx {Scheme Procedure} command-line
  1260. @deffnx {Scheme Procedure} set-program-arguments
  1261. @deffnx {C Function} scm_program_arguments ()
  1262. @deffnx {C Function} scm_set_program_arguments_scm (lst)
  1263. @cindex command line
  1264. @cindex program arguments
  1265. Get the command line arguments passed to Guile, or set new arguments.
  1266. The arguments are a list of strings, the first of which is the invoked
  1267. program name. This is just @nicode{"guile"} (or the executable path)
  1268. when run interactively, or it's the script name when running a script
  1269. with @option{-s} (@pxref{Invoking Guile}).
  1270. @example
  1271. guile -L /my/extra/dir -s foo.scm abc def
  1272. (program-arguments) @result{} ("foo.scm" "abc" "def")
  1273. @end example
  1274. @code{set-program-arguments} allows a library module or similar to
  1275. modify the arguments, for example to strip options it recognises,
  1276. leaving the rest for the mainline.
  1277. The argument list is held in a fluid, which means it's separate for
  1278. each thread. Neither the list nor the strings within it are copied at
  1279. any point and normally should not be mutated.
  1280. The two names @code{program-arguments} and @code{command-line} are an
  1281. historical accident, they both do exactly the same thing. The name
  1282. @code{scm_set_program_arguments_scm} has an extra @code{_scm} on the
  1283. end to avoid clashing with the C function below.
  1284. @end deffn
  1285. @deftypefn {C Function} void scm_set_program_arguments (int argc, char **argv, char *first)
  1286. @cindex command line
  1287. @cindex program arguments
  1288. Set the list of command line arguments for @code{program-arguments}
  1289. and @code{command-line} above.
  1290. @var{argv} is an array of null-terminated strings, as in a C
  1291. @code{main} function. @var{argc} is the number of strings in
  1292. @var{argv}, or if it's negative then a @code{NULL} in @var{argv} marks
  1293. its end.
  1294. @var{first} is an extra string put at the start of the arguments, or
  1295. @code{NULL} for no such extra. This is a convenient way to pass the
  1296. program name after advancing @var{argv} to strip option arguments.
  1297. Eg.@:
  1298. @example
  1299. @{
  1300. char *progname = argv[0];
  1301. for (argv++; argv[0] != NULL && argv[0][0] == '-'; argv++)
  1302. @{
  1303. /* munch option ... */
  1304. @}
  1305. /* remaining args for scheme level use */
  1306. scm_set_program_arguments (-1, argv, progname);
  1307. @}
  1308. @end example
  1309. This sort of thing is often done at startup under
  1310. @code{scm_boot_guile} with options handled at the C level removed.
  1311. The given strings are all copied, so the C data is not accessed again
  1312. once @code{scm_set_program_arguments} returns.
  1313. @end deftypefn
  1314. @deffn {Scheme Procedure} getenv name
  1315. @deffnx {C Function} scm_getenv (name)
  1316. @cindex environment
  1317. Looks up the string @var{name} in the current environment. The return
  1318. value is @code{#f} unless a string of the form @code{NAME=VALUE} is
  1319. found, in which case the string @code{VALUE} is returned.
  1320. @end deffn
  1321. @deffn {Scheme Procedure} setenv name value
  1322. Modifies the environment of the current process, which is
  1323. also the default environment inherited by child processes.
  1324. If @var{value} is @code{#f}, then @var{name} is removed from the
  1325. environment. Otherwise, the string @var{name}=@var{value} is added
  1326. to the environment, replacing any existing string with name matching
  1327. @var{name}.
  1328. The return value is unspecified.
  1329. @end deffn
  1330. @deffn {Scheme Procedure} unsetenv name
  1331. Remove variable @var{name} from the environment. The
  1332. name can not contain a @samp{=} character.
  1333. @end deffn
  1334. @deffn {Scheme Procedure} environ [env]
  1335. @deffnx {C Function} scm_environ (env)
  1336. If @var{env} is omitted, return the current environment (in the
  1337. Unix sense) as a list of strings. Otherwise set the current
  1338. environment, which is also the default environment for child
  1339. processes, to the supplied list of strings. Each member of
  1340. @var{env} should be of the form @var{name}=@var{value} and values of
  1341. @var{name} should not be duplicated. If @var{env} is supplied
  1342. then the return value is unspecified.
  1343. @end deffn
  1344. @deffn {Scheme Procedure} putenv str
  1345. @deffnx {C Function} scm_putenv (str)
  1346. Modifies the environment of the current process, which is
  1347. also the default environment inherited by child processes.
  1348. If @var{str} is of the form @code{NAME=VALUE} then it will be written
  1349. directly into the environment, replacing any existing environment string
  1350. with
  1351. name matching @code{NAME}. If @var{str} does not contain an equal
  1352. sign, then any existing string with name matching @var{str} will
  1353. be removed.
  1354. The return value is unspecified.
  1355. @end deffn
  1356. @node Processes
  1357. @subsection Processes
  1358. @cindex processes
  1359. @cindex child processes
  1360. @findex cd
  1361. @deffn {Scheme Procedure} chdir str
  1362. @deffnx {C Function} scm_chdir (str)
  1363. @cindex current directory
  1364. Change the current working directory to @var{str}.
  1365. The return value is unspecified.
  1366. @end deffn
  1367. @findex pwd
  1368. @deffn {Scheme Procedure} getcwd
  1369. @deffnx {C Function} scm_getcwd ()
  1370. Return the name of the current working directory.
  1371. @end deffn
  1372. @deffn {Scheme Procedure} umask [mode]
  1373. @deffnx {C Function} scm_umask (mode)
  1374. If @var{mode} is omitted, returns a decimal number representing the
  1375. current file creation mask. Otherwise the file creation mask is set
  1376. to @var{mode} and the previous value is returned. @xref{Setting
  1377. Permissions,,Assigning File Permissions,libc,The GNU C Library
  1378. Reference Manual}, for more on how to use umasks.
  1379. E.g., @code{(umask #o022)} sets the mask to octal 22/decimal 18.
  1380. @end deffn
  1381. @deffn {Scheme Procedure} chroot path
  1382. @deffnx {C Function} scm_chroot (path)
  1383. Change the root directory to that specified in @var{path}.
  1384. This directory will be used for path names beginning with
  1385. @file{/}. The root directory is inherited by all children
  1386. of the current process. Only the superuser may change the
  1387. root directory.
  1388. @end deffn
  1389. @deffn {Scheme Procedure} getpid
  1390. @deffnx {C Function} scm_getpid ()
  1391. Return an integer representing the current process ID.
  1392. @end deffn
  1393. @deffn {Scheme Procedure} getgroups
  1394. @deffnx {C Function} scm_getgroups ()
  1395. Return a vector of integers representing the current
  1396. supplementary group IDs.
  1397. @end deffn
  1398. @deffn {Scheme Procedure} getppid
  1399. @deffnx {C Function} scm_getppid ()
  1400. Return an integer representing the process ID of the parent
  1401. process.
  1402. @end deffn
  1403. @deffn {Scheme Procedure} getuid
  1404. @deffnx {C Function} scm_getuid ()
  1405. Return an integer representing the current real user ID.
  1406. @end deffn
  1407. @deffn {Scheme Procedure} getgid
  1408. @deffnx {C Function} scm_getgid ()
  1409. Return an integer representing the current real group ID.
  1410. @end deffn
  1411. @deffn {Scheme Procedure} geteuid
  1412. @deffnx {C Function} scm_geteuid ()
  1413. Return an integer representing the current effective user ID.
  1414. If the system does not support effective IDs, then the real ID
  1415. is returned. @code{(provided? 'EIDs)} reports whether the
  1416. system supports effective IDs.
  1417. @end deffn
  1418. @deffn {Scheme Procedure} getegid
  1419. @deffnx {C Function} scm_getegid ()
  1420. Return an integer representing the current effective group ID.
  1421. If the system does not support effective IDs, then the real ID
  1422. is returned. @code{(provided? 'EIDs)} reports whether the
  1423. system supports effective IDs.
  1424. @end deffn
  1425. @deffn {Scheme Procedure} setgroups vec
  1426. @deffnx {C Function} scm_setgroups (vec)
  1427. Set the current set of supplementary group IDs to the integers in the
  1428. given vector @var{vec}. The return value is unspecified.
  1429. Generally only the superuser can set the process group IDs
  1430. (@pxref{Setting Groups, Setting the Group IDs,, libc, The GNU C
  1431. Library Reference Manual}).
  1432. @end deffn
  1433. @deffn {Scheme Procedure} setuid id
  1434. @deffnx {C Function} scm_setuid (id)
  1435. Sets both the real and effective user IDs to the integer @var{id}, provided
  1436. the process has appropriate privileges.
  1437. The return value is unspecified.
  1438. @end deffn
  1439. @deffn {Scheme Procedure} setgid id
  1440. @deffnx {C Function} scm_setgid (id)
  1441. Sets both the real and effective group IDs to the integer @var{id}, provided
  1442. the process has appropriate privileges.
  1443. The return value is unspecified.
  1444. @end deffn
  1445. @deffn {Scheme Procedure} seteuid id
  1446. @deffnx {C Function} scm_seteuid (id)
  1447. Sets the effective user ID to the integer @var{id}, provided the process
  1448. has appropriate privileges. If effective IDs are not supported, the
  1449. real ID is set instead---@code{(provided? 'EIDs)} reports whether the
  1450. system supports effective IDs.
  1451. The return value is unspecified.
  1452. @end deffn
  1453. @deffn {Scheme Procedure} setegid id
  1454. @deffnx {C Function} scm_setegid (id)
  1455. Sets the effective group ID to the integer @var{id}, provided the process
  1456. has appropriate privileges. If effective IDs are not supported, the
  1457. real ID is set instead---@code{(provided? 'EIDs)} reports whether the
  1458. system supports effective IDs.
  1459. The return value is unspecified.
  1460. @end deffn
  1461. @deffn {Scheme Procedure} getpgrp
  1462. @deffnx {C Function} scm_getpgrp ()
  1463. Return an integer representing the current process group ID.
  1464. This is the @acronym{POSIX} definition, not @acronym{BSD}.
  1465. @end deffn
  1466. @deffn {Scheme Procedure} setpgid pid pgid
  1467. @deffnx {C Function} scm_setpgid (pid, pgid)
  1468. Move the process @var{pid} into the process group @var{pgid}. @var{pid} or
  1469. @var{pgid} must be integers: they can be zero to indicate the ID of the
  1470. current process.
  1471. Fails on systems that do not support job control.
  1472. The return value is unspecified.
  1473. @end deffn
  1474. @deffn {Scheme Procedure} setsid
  1475. @deffnx {C Function} scm_setsid ()
  1476. Creates a new session. The current process becomes the session leader
  1477. and is put in a new process group. The process will be detached
  1478. from its controlling terminal if it has one.
  1479. The return value is an integer representing the new process group ID.
  1480. @end deffn
  1481. @deffn {Scheme Procedure} getsid pid
  1482. @deffnx {C Function} scm_getsid (pid)
  1483. Returns the session ID of process @var{pid}. (The session
  1484. ID of a process is the process group ID of its session leader.)
  1485. @end deffn
  1486. @deffn {Scheme Procedure} waitpid pid [options]
  1487. @deffnx {C Function} scm_waitpid (pid, options)
  1488. This procedure collects status information from a child process which
  1489. has terminated or (optionally) stopped. Normally it will
  1490. suspend the calling process until this can be done. If more than one
  1491. child process is eligible then one will be chosen by the operating system.
  1492. The value of @var{pid} determines the behaviour:
  1493. @table @asis
  1494. @item @var{pid} greater than 0
  1495. Request status information from the specified child process.
  1496. @item @var{pid} equal to -1 or @code{WAIT_ANY}
  1497. @vindex WAIT_ANY
  1498. Request status information for any child process.
  1499. @item @var{pid} equal to 0 or @code{WAIT_MYPGRP}
  1500. @vindex WAIT_MYPGRP
  1501. Request status information for any child process in the current process
  1502. group.
  1503. @item @var{pid} less than -1
  1504. Request status information for any child process whose process group ID
  1505. is @minus{}@var{pid}.
  1506. @end table
  1507. The @var{options} argument, if supplied, should be the bitwise OR of the
  1508. values of zero or more of the following variables:
  1509. @defvar WNOHANG
  1510. Return immediately even if there are no child processes to be collected.
  1511. @end defvar
  1512. @defvar WUNTRACED
  1513. Report status information for stopped processes as well as terminated
  1514. processes.
  1515. @end defvar
  1516. The return value is a pair containing:
  1517. @enumerate
  1518. @item
  1519. The process ID of the child process, or 0 if @code{WNOHANG} was
  1520. specified and no process was collected.
  1521. @item
  1522. The integer status value (@pxref{Process Completion Status,,, libc, The
  1523. GNU C Library Reference Manual}).
  1524. @end enumerate
  1525. @end deffn
  1526. The following three
  1527. functions can be used to decode the integer status value returned by
  1528. @code{waitpid}.
  1529. @deffn {Scheme Procedure} status:exit-val status
  1530. @deffnx {C Function} scm_status_exit_val (status)
  1531. Return the exit status value, as would be set if a process
  1532. ended normally through a call to @code{exit} or @code{_exit},
  1533. if any, otherwise @code{#f}.
  1534. @end deffn
  1535. @deffn {Scheme Procedure} status:term-sig status
  1536. @deffnx {C Function} scm_status_term_sig (status)
  1537. Return the signal number which terminated the process, if any,
  1538. otherwise @code{#f}.
  1539. @end deffn
  1540. @deffn {Scheme Procedure} status:stop-sig status
  1541. @deffnx {C Function} scm_status_stop_sig (status)
  1542. Return the signal number which stopped the process, if any,
  1543. otherwise @code{#f}.
  1544. @end deffn
  1545. @deffn {Scheme Procedure} system [cmd]
  1546. @deffnx {C Function} scm_system (cmd)
  1547. Execute @var{cmd} using the operating system's ``command
  1548. processor''. Under Unix this is usually the default shell
  1549. @code{sh}. The value returned is @var{cmd}'s exit status as
  1550. returned by @code{waitpid}, which can be interpreted using the
  1551. functions above.
  1552. If @code{system} is called without arguments, return a boolean
  1553. indicating whether the command processor is available.
  1554. @end deffn
  1555. @deffn {Scheme Procedure} system* arg1 arg2 @dots{}
  1556. @deffnx {C Function} scm_system_star (args)
  1557. Execute the command indicated by @var{arg1} @var{arg2} @enddots{}. The
  1558. first element must be a string indicating the command to be executed,
  1559. and the remaining items must be strings representing each of the
  1560. arguments to that command.
  1561. This function returns the exit status of the command as provided by
  1562. @code{waitpid}. This value can be handled with @code{status:exit-val}
  1563. and the related functions.
  1564. @code{system*} is similar to @code{system}, but accepts only one
  1565. string per-argument, and performs no shell interpretation. The
  1566. command is executed using fork and execlp. Accordingly this function
  1567. may be safer than @code{system} in situations where shell
  1568. interpretation is not required.
  1569. Example: (system* "echo" "foo" "bar")
  1570. @end deffn
  1571. @deffn {Scheme Procedure} quit [status]
  1572. @deffnx {Scheme Procedure} exit [status]
  1573. Terminate the current process with proper unwinding of the Scheme stack.
  1574. The exit status zero if @var{status} is not supplied. If @var{status}
  1575. is supplied, and it is an integer, that integer is used as the exit
  1576. status. If @var{status} is @code{#t} or @code{#f}, the exit status is
  1577. @var{EXIT_SUCCESS} or @var{EXIT_FAILURE}, respectively.
  1578. The procedure @code{exit} is an alias of @code{quit}. They have the
  1579. same functionality.
  1580. @end deffn
  1581. @defvr {Scheme Variable} EXIT_SUCCESS
  1582. @defvrx {Scheme Variable} EXIT_FAILURE
  1583. These constants represent the standard exit codes for success (zero) or
  1584. failure (one.)
  1585. @end defvr
  1586. @deffn {Scheme Procedure} primitive-exit [status]
  1587. @deffnx {Scheme Procedure} primitive-_exit [status]
  1588. @deffnx {C Function} scm_primitive_exit (status)
  1589. @deffnx {C Function} scm_primitive__exit (status)
  1590. Terminate the current process without unwinding the Scheme stack. The
  1591. exit status is @var{status} if supplied, otherwise zero.
  1592. @code{primitive-exit} uses the C @code{exit} function and hence runs
  1593. usual C level cleanups (flush output streams, call @code{atexit}
  1594. functions, etc, see @ref{Normal Termination,,, libc, The GNU C Library
  1595. Reference Manual})).
  1596. @code{primitive-_exit} is the @code{_exit} system call
  1597. (@pxref{Termination Internals,,, libc, The GNU C Library Reference
  1598. Manual}). This terminates the program immediately, with neither
  1599. Scheme-level nor C-level cleanups.
  1600. The typical use for @code{primitive-_exit} is from a child process
  1601. created with @code{primitive-fork}. For example in a Gdk program the
  1602. child process inherits the X server connection and a C-level
  1603. @code{atexit} cleanup which will close that connection. But closing
  1604. in the child would upset the protocol in the parent, so
  1605. @code{primitive-_exit} should be used to exit without that.
  1606. @end deffn
  1607. @deffn {Scheme Procedure} execl filename arg @dots{}
  1608. @deffnx {C Function} scm_execl (filename, args)
  1609. Executes the file named by @var{filename} as a new process image.
  1610. The remaining arguments are supplied to the process; from a C program
  1611. they are accessible as the @code{argv} argument to @code{main}.
  1612. Conventionally the first @var{arg} is the same as @var{filename}.
  1613. All arguments must be strings.
  1614. If @var{arg} is missing, @var{filename} is executed with a null
  1615. argument list, which may have system-dependent side-effects.
  1616. This procedure is currently implemented using the @code{execv} system
  1617. call, but we call it @code{execl} because of its Scheme calling interface.
  1618. @end deffn
  1619. @deffn {Scheme Procedure} execlp filename arg @dots{}
  1620. @deffnx {C Function} scm_execlp (filename, args)
  1621. Similar to @code{execl}, however if
  1622. @var{filename} does not contain a slash
  1623. then the file to execute will be located by searching the
  1624. directories listed in the @code{PATH} environment variable.
  1625. This procedure is currently implemented using the @code{execvp} system
  1626. call, but we call it @code{execlp} because of its Scheme calling interface.
  1627. @end deffn
  1628. @deffn {Scheme Procedure} execle filename env arg @dots{}
  1629. @deffnx {C Function} scm_execle (filename, env, args)
  1630. Similar to @code{execl}, but the environment of the new process is
  1631. specified by @var{env}, which must be a list of strings as returned by the
  1632. @code{environ} procedure.
  1633. This procedure is currently implemented using the @code{execve} system
  1634. call, but we call it @code{execle} because of its Scheme calling interface.
  1635. @end deffn
  1636. @deffn {Scheme Procedure} primitive-fork
  1637. @deffnx {C Function} scm_fork ()
  1638. Creates a new ``child'' process by duplicating the current ``parent'' process.
  1639. In the child the return value is 0. In the parent the return value is
  1640. the integer process ID of the child.
  1641. Note that it is unsafe to fork a process that has multiple threads
  1642. running, as only the thread that calls @code{primitive-fork} will
  1643. persist in the child. Any resources that other threads held, such as
  1644. locked mutexes or open file descriptors, are lost. Indeed,
  1645. @acronym{POSIX} specifies that only async-signal-safe procedures are
  1646. safe to call after a multithreaded fork, which is a very limited set.
  1647. Guile issues a warning if it detects a fork from a multi-threaded
  1648. program.
  1649. If you are going to @code{exec} soon after forking, the procedures in
  1650. @code{(ice-9 popen)} may be useful to you, as they fork and exec within
  1651. an async-signal-safe function carefully written to ensure robust program
  1652. behavior, even in the presence of threads. @xref{Pipes}, for more.
  1653. This procedure has been renamed from @code{fork} to avoid a naming conflict
  1654. with the scsh fork.
  1655. @end deffn
  1656. @deffn {Scheme Procedure} nice incr
  1657. @deffnx {C Function} scm_nice (incr)
  1658. @cindex process priority
  1659. Increment the priority of the current process by @var{incr}. A higher
  1660. priority value means that the process runs less often.
  1661. The return value is unspecified.
  1662. @end deffn
  1663. @deffn {Scheme Procedure} setpriority which who prio
  1664. @deffnx {C Function} scm_setpriority (which, who, prio)
  1665. @vindex PRIO_PROCESS
  1666. @vindex PRIO_PGRP
  1667. @vindex PRIO_USER
  1668. Set the scheduling priority of the process, process group
  1669. or user, as indicated by @var{which} and @var{who}. @var{which}
  1670. is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
  1671. or @code{PRIO_USER}, and @var{who} is interpreted relative to
  1672. @var{which} (a process identifier for @code{PRIO_PROCESS},
  1673. process group identifier for @code{PRIO_PGRP}, and a user
  1674. identifier for @code{PRIO_USER}. A zero value of @var{who}
  1675. denotes the current process, process group, or user.
  1676. @var{prio} is a value in the range [@minus{}20,20]. The default
  1677. priority is 0; lower priorities (in numerical terms) cause more
  1678. favorable scheduling. Sets the priority of all of the specified
  1679. processes. Only the super-user may lower priorities. The return
  1680. value is not specified.
  1681. @end deffn
  1682. @deffn {Scheme Procedure} getpriority which who
  1683. @deffnx {C Function} scm_getpriority (which, who)
  1684. @vindex PRIO_PROCESS
  1685. @vindex PRIO_PGRP
  1686. @vindex PRIO_USER
  1687. Return the scheduling priority of the process, process group
  1688. or user, as indicated by @var{which} and @var{who}. @var{which}
  1689. is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
  1690. or @code{PRIO_USER}, and @var{who} should be interpreted depending on
  1691. @var{which} (a process identifier for @code{PRIO_PROCESS},
  1692. process group identifier for @code{PRIO_PGRP}, and a user
  1693. identifier for @code{PRIO_USER}). A zero value of @var{who}
  1694. denotes the current process, process group, or user. Return
  1695. the highest priority (lowest numerical value) of any of the
  1696. specified processes.
  1697. @end deffn
  1698. @cindex affinity, CPU
  1699. @deffn {Scheme Procedure} getaffinity pid
  1700. @deffnx {C Function} scm_getaffinity (pid)
  1701. Return a bitvector representing the CPU affinity mask for
  1702. process @var{pid}. Each CPU the process has affinity with
  1703. has its corresponding bit set in the returned bitvector.
  1704. The number of bits set is a good estimate of how many CPUs
  1705. Guile can use without stepping on other processes' toes.
  1706. Currently this procedure is only defined on GNU variants
  1707. (@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The
  1708. GNU C Library Reference Manual}).
  1709. @end deffn
  1710. @deffn {Scheme Procedure} setaffinity pid mask
  1711. @deffnx {C Function} scm_setaffinity (pid, mask)
  1712. Install the CPU affinity mask @var{mask}, a bitvector, for
  1713. the process or thread with ID @var{pid}. The return value
  1714. is unspecified.
  1715. Currently this procedure is only defined on GNU variants
  1716. (@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The
  1717. GNU C Library Reference Manual}).
  1718. @end deffn
  1719. @xref{Threads}, for information on how get the number of processors
  1720. available on a system.
  1721. @node Signals
  1722. @subsection Signals
  1723. @cindex signal
  1724. The following procedures raise, handle and wait for signals.
  1725. Scheme code signal handlers are run via an async (@pxref{Asyncs}), so
  1726. they're called in the handler's thread at the next safe opportunity.
  1727. Generally this is after any currently executing primitive procedure
  1728. finishes (which could be a long time for primitives that wait for an
  1729. external event).
  1730. @deffn {Scheme Procedure} kill pid sig
  1731. @deffnx {C Function} scm_kill (pid, sig)
  1732. Sends a signal to the specified process or group of processes.
  1733. @var{pid} specifies the processes to which the signal is sent:
  1734. @table @asis
  1735. @item @var{pid} greater than 0
  1736. The process whose identifier is @var{pid}.
  1737. @item @var{pid} equal to 0
  1738. All processes in the current process group.
  1739. @item @var{pid} less than -1
  1740. The process group whose identifier is -@var{pid}
  1741. @item @var{pid} equal to -1
  1742. If the process is privileged, all processes except for some special
  1743. system processes. Otherwise, all processes with the current effective
  1744. user ID.
  1745. @end table
  1746. @var{sig} should be specified using a variable corresponding to
  1747. the Unix symbolic name, e.g.,
  1748. @defvar SIGHUP
  1749. Hang-up signal.
  1750. @end defvar
  1751. @defvar SIGINT
  1752. Interrupt signal.
  1753. @end defvar
  1754. A full list of signals on the GNU system may be found in @ref{Standard
  1755. Signals,,,libc,The GNU C Library Reference Manual}.
  1756. @end deffn
  1757. @deffn {Scheme Procedure} raise sig
  1758. @deffnx {C Function} scm_raise (sig)
  1759. Sends a specified signal @var{sig} to the current process, where
  1760. @var{sig} is as described for the @code{kill} procedure.
  1761. @end deffn
  1762. @deffn {Scheme Procedure} sigaction signum [handler [flags [thread]]]
  1763. @deffnx {C Function} scm_sigaction (signum, handler, flags)
  1764. @deffnx {C Function} scm_sigaction_for_thread (signum, handler, flags, thread)
  1765. Install or report the signal handler for a specified signal.
  1766. @var{signum} is the signal number, which can be specified using the value
  1767. of variables such as @code{SIGINT}.
  1768. If @var{handler} is omitted, @code{sigaction} returns a pair: the
  1769. @acronym{CAR} is the current signal hander, which will be either an
  1770. integer with the value @code{SIG_DFL} (default action) or
  1771. @code{SIG_IGN} (ignore), or the Scheme procedure which handles the
  1772. signal, or @code{#f} if a non-Scheme procedure handles the signal.
  1773. The @acronym{CDR} contains the current @code{sigaction} flags for the
  1774. handler.
  1775. If @var{handler} is provided, it is installed as the new handler for
  1776. @var{signum}. @var{handler} can be a Scheme procedure taking one
  1777. argument, or the value of @code{SIG_DFL} (default action) or
  1778. @code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler
  1779. was installed before @code{sigaction} was first used. When a scheme
  1780. procedure has been specified, that procedure will run in the given
  1781. @var{thread}. When no thread has been given, the thread that made this
  1782. call to @code{sigaction} is used.
  1783. @var{flags} is a @code{logior} (@pxref{Bitwise Operations}) of the
  1784. following (where provided by the system), or @code{0} for none.
  1785. @defvar SA_NOCLDSTOP
  1786. By default, @code{SIGCHLD} is signalled when a child process stops
  1787. (ie.@: receives @code{SIGSTOP}), and when a child process terminates.
  1788. With the @code{SA_NOCLDSTOP} flag, @code{SIGCHLD} is only signalled
  1789. for termination, not stopping.
  1790. @code{SA_NOCLDSTOP} has no effect on signals other than
  1791. @code{SIGCHLD}.
  1792. @end defvar
  1793. @defvar SA_RESTART
  1794. If a signal occurs while in a system call, deliver the signal then
  1795. restart the system call (as opposed to returning an @code{EINTR} error
  1796. from that call).
  1797. @end defvar
  1798. Guile handles signals asynchronously. When it receives a signal, the
  1799. synchronous signal handler just records the fact that a signal was
  1800. received and sets a flag to tell the relevant Guile thread that it has a
  1801. pending signal. When the Guile thread checks the pending-interrupt
  1802. flag, it will arrange to run the asynchronous part of the signal
  1803. handler, which is the handler attached by @code{sigaction}.
  1804. This strategy has some perhaps-unexpected interactions with the
  1805. @code{SA_RESTART} flag, though: because the synchronous handler doesn't
  1806. do very much, and notably it doesn't run the Guile handler, it's
  1807. impossible to interrupt a thread stuck in a long-running system call via
  1808. a signal handler that is installed with @code{SA_RESTART}: the
  1809. synchronous handler just records the pending interrupt, but then the
  1810. system call resumes and Guile doesn't have a chance to actually check
  1811. the flag and run the asynchronous handler. That's just how it is.
  1812. The return value is a pair with information about the old handler as
  1813. described above.
  1814. This interface does not provide access to the ``signal blocking''
  1815. facility. Maybe this is not needed, since the thread support may
  1816. provide solutions to the problem of consistent access to data
  1817. structures.
  1818. @end deffn
  1819. @deffn {Scheme Procedure} restore-signals
  1820. @deffnx {C Function} scm_restore_signals ()
  1821. Return all signal handlers to the values they had before any call to
  1822. @code{sigaction} was made. The return value is unspecified.
  1823. @end deffn
  1824. @deffn {Scheme Procedure} alarm i
  1825. @deffnx {C Function} scm_alarm (i)
  1826. Set a timer to raise a @code{SIGALRM} signal after the specified
  1827. number of seconds (an integer). It's advisable to install a signal
  1828. handler for
  1829. @code{SIGALRM} beforehand, since the default action is to terminate
  1830. the process.
  1831. The return value indicates the time remaining for the previous alarm,
  1832. if any. The new value replaces the previous alarm. If there was
  1833. no previous alarm, the return value is zero.
  1834. @end deffn
  1835. @deffn {Scheme Procedure} pause
  1836. @deffnx {C Function} scm_pause ()
  1837. Pause the current process (thread?) until a signal arrives whose
  1838. action is to either terminate the current process or invoke a
  1839. handler procedure. The return value is unspecified.
  1840. @end deffn
  1841. @deffn {Scheme Procedure} sleep secs
  1842. @deffnx {Scheme Procedure} usleep usecs
  1843. @deffnx {C Function} scm_sleep (secs)
  1844. @deffnx {C Function} scm_usleep (usecs)
  1845. Wait the given period @var{secs} seconds or @var{usecs} microseconds
  1846. (both integers). If a signal arrives the wait stops and the return
  1847. value is the time remaining, in seconds or microseconds respectively.
  1848. If the period elapses with no signal the return is zero.
  1849. On most systems the process scheduler is not microsecond accurate and
  1850. the actual period slept by @code{usleep} might be rounded to a system
  1851. clock tick boundary, which might be 10 milliseconds for instance.
  1852. See @code{scm_std_sleep} and @code{scm_std_usleep} for equivalents at
  1853. the C level (@pxref{Blocking}).
  1854. @end deffn
  1855. @deffn {Scheme Procedure} getitimer which_timer
  1856. @deffnx {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds value_seconds value_microseconds
  1857. @deffnx {C Function} scm_getitimer (which_timer)
  1858. @deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, value_seconds, value_microseconds)
  1859. Get or set the periods programmed in certain system timers.
  1860. These timers have two settings. The first setting, the interval, is the
  1861. value at which the timer will be reset when the current timer expires.
  1862. The second is the current value of the timer, indicating when the next
  1863. expiry will be signalled.
  1864. @var{which_timer} is one of the following values:
  1865. @defvar ITIMER_REAL
  1866. A real-time timer, counting down elapsed real time. At zero it raises
  1867. @code{SIGALRM}. This is like @code{alarm} above, but with a higher
  1868. resolution period.
  1869. @end defvar
  1870. @defvar ITIMER_VIRTUAL
  1871. A virtual-time timer, counting down while the current process is
  1872. actually using CPU. At zero it raises @code{SIGVTALRM}.
  1873. @end defvar
  1874. @defvar ITIMER_PROF
  1875. A profiling timer, counting down while the process is running (like
  1876. @code{ITIMER_VIRTUAL}) and also while system calls are running on the
  1877. process's behalf. At zero it raises a @code{SIGPROF}.
  1878. This timer is intended for profiling where a program is spending its
  1879. time (by looking where it is when the timer goes off).
  1880. @end defvar
  1881. @code{getitimer} returns the restart timer value and its current value,
  1882. as a list containing two pairs. Each pair is a time in seconds and
  1883. microseconds: @code{((@var{interval_secs} . @var{interval_usecs})
  1884. (@var{value_secs} . @var{value_usecs}))}.
  1885. @code{setitimer} sets the timer values similarly, in seconds and
  1886. microseconds (which must be integers). The interval value can be zero
  1887. to have the timer run down just once. The return value is the timer's
  1888. previous setting, in the same form as @code{getitimer} returns.
  1889. @example
  1890. (setitimer ITIMER_REAL
  1891. 5 500000 ;; Raise SIGALRM every 5.5 seconds
  1892. 2 0) ;; with the first SIGALRM in 2 seconds
  1893. @end example
  1894. Although the timers are programmed in microseconds, the actual
  1895. accuracy might not be that high.
  1896. Note that @code{ITIMER_PROF} and @code{ITIMER_VIRTUAL} are not
  1897. functional on all platforms and may always error when called.
  1898. @code{(provided? 'ITIMER_PROF)} and @code{(provided? 'ITIMER_VIRTUAL)}
  1899. can be used to test if the those itimers are supported on the given
  1900. host. @code{ITIMER_REAL} is supported on all platforms that support
  1901. @code{setitimer}.
  1902. @end deffn
  1903. @node Terminals and Ptys
  1904. @subsection Terminals and Ptys
  1905. @deffn {Scheme Procedure} isatty? port
  1906. @deffnx {C Function} scm_isatty_p (port)
  1907. @cindex terminal
  1908. Return @code{#t} if @var{port} is using a serial non--file
  1909. device, otherwise @code{#f}.
  1910. @end deffn
  1911. @deffn {Scheme Procedure} ttyname port
  1912. @deffnx {C Function} scm_ttyname (port)
  1913. @cindex terminal
  1914. Return a string with the name of the serial terminal device
  1915. underlying @var{port}.
  1916. @end deffn
  1917. @deffn {Scheme Procedure} ctermid
  1918. @deffnx {C Function} scm_ctermid ()
  1919. @cindex terminal
  1920. Return a string containing the file name of the controlling
  1921. terminal for the current process.
  1922. @end deffn
  1923. @deffn {Scheme Procedure} tcgetpgrp port
  1924. @deffnx {C Function} scm_tcgetpgrp (port)
  1925. @cindex process group
  1926. Return the process group ID of the foreground process group
  1927. associated with the terminal open on the file descriptor
  1928. underlying @var{port}.
  1929. If there is no foreground process group, the return value is a
  1930. number greater than 1 that does not match the process group ID
  1931. of any existing process group. This can happen if all of the
  1932. processes in the job that was formerly the foreground job have
  1933. terminated, and no other job has yet been moved into the
  1934. foreground.
  1935. @end deffn
  1936. @deffn {Scheme Procedure} tcsetpgrp port pgid
  1937. @deffnx {C Function} scm_tcsetpgrp (port, pgid)
  1938. @cindex process group
  1939. Set the foreground process group ID for the terminal used by the file
  1940. descriptor underlying @var{port} to the integer @var{pgid}.
  1941. The calling process
  1942. must be a member of the same session as @var{pgid} and must have the same
  1943. controlling terminal. The return value is unspecified.
  1944. @end deffn
  1945. @node Pipes
  1946. @subsection Pipes
  1947. @cindex pipe
  1948. The following procedures are similar to the @code{popen} and
  1949. @code{pclose} system routines. The code is in a separate ``popen''
  1950. module@footnote{This module is only available on systems where the
  1951. @code{popen} feature is provided (@pxref{Common Feature Symbols}).}:
  1952. @lisp
  1953. (use-modules (ice-9 popen))
  1954. @end lisp
  1955. @findex popen
  1956. @deffn {Scheme Procedure} open-pipe command mode
  1957. @deffnx {Scheme Procedure} open-pipe* mode prog [args...]
  1958. Execute a command in a subprocess, with a pipe to it or from it, or
  1959. with pipes in both directions.
  1960. @code{open-pipe} runs the shell @var{command} using @samp{/bin/sh -c}.
  1961. @code{open-pipe*} executes @var{prog} directly, with the optional
  1962. @var{args} arguments (all strings).
  1963. @var{mode} should be one of the following values. @code{OPEN_READ} is
  1964. an input pipe, ie.@: to read from the subprocess. @code{OPEN_WRITE}
  1965. is an output pipe, ie.@: to write to it.
  1966. @defvar OPEN_READ
  1967. @defvarx OPEN_WRITE
  1968. @defvarx OPEN_BOTH
  1969. @end defvar
  1970. For an input pipe, the child's standard output is the pipe and
  1971. standard input is inherited from @code{current-input-port}. For an
  1972. output pipe, the child's standard input is the pipe and standard
  1973. output is inherited from @code{current-output-port}. In all cases
  1974. the child's standard error is inherited from
  1975. @code{current-error-port} (@pxref{Default Ports}).
  1976. If those @code{current-X-ports} are not files of some kind, and hence
  1977. don't have file descriptors for the child, then @file{/dev/null} is
  1978. used instead.
  1979. Care should be taken with @code{OPEN_BOTH}, a deadlock will occur if
  1980. both parent and child are writing, and waiting until the write completes
  1981. before doing any reading. Each direction has @code{PIPE_BUF} bytes of
  1982. buffering (@pxref{Buffering}), which will be enough for small writes,
  1983. but not for say putting a big file through a filter.
  1984. @end deffn
  1985. @deffn {Scheme Procedure} open-input-pipe command
  1986. Equivalent to @code{open-pipe} with mode @code{OPEN_READ}.
  1987. @lisp
  1988. (let* ((port (open-input-pipe "date --utc"))
  1989. (str (read-line port))) ; from (ice-9 rdelim)
  1990. (close-pipe port)
  1991. str)
  1992. @result{} "Mon Mar 11 20:10:44 UTC 2002"
  1993. @end lisp
  1994. @end deffn
  1995. @deffn {Scheme Procedure} open-output-pipe command
  1996. Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}.
  1997. @lisp
  1998. (let ((port (open-output-pipe "lpr")))
  1999. (display "Something for the line printer.\n" port)
  2000. (if (not (eqv? 0 (status:exit-val (close-pipe port))))
  2001. (error "Cannot print")))
  2002. @end lisp
  2003. @end deffn
  2004. @deffn {Scheme Procedure} open-input-output-pipe command
  2005. Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}.
  2006. @end deffn
  2007. @findex pclose
  2008. @deffn {Scheme Procedure} close-pipe port
  2009. Close a pipe created by @code{open-pipe}, wait for the process to
  2010. terminate, and return the wait status code. The status is as per
  2011. @code{waitpid} and can be decoded with @code{status:exit-val} etc
  2012. (@pxref{Processes})
  2013. @end deffn
  2014. @sp 1
  2015. @code{waitpid WAIT_ANY} should not be used when pipes are open, since
  2016. it can reap a pipe's child process, causing an error from a subsequent
  2017. @code{close-pipe}.
  2018. @code{close-port} (@pxref{Ports}) can close a pipe, but it doesn't reap
  2019. the child process.
  2020. The garbage collector will close a pipe no longer in use, and reap the
  2021. child process with @code{waitpid}. If the child hasn't yet terminated
  2022. the garbage collector doesn't block, but instead checks again in the
  2023. next GC.
  2024. Many systems have per-user and system-wide limits on the number of
  2025. processes, and a system-wide limit on the number of pipes, so pipes
  2026. should be closed explicitly when no longer needed, rather than letting
  2027. the garbage collector pick them up at some later time.
  2028. @deffn {Scheme Procedure} pipeline @var{commands}
  2029. Execute a pipeline of @var{commands}, where each command is a
  2030. list of a program and its arguments as strings, returning an input
  2031. port to the end of the pipeline, an output port to the beginning of the
  2032. pipeline and a list of PIDs of the processes executing the @var{commands}.
  2033. @example
  2034. (let ((commands '(("git" "ls-files")
  2035. ("tar" "-cf-" "-T-")
  2036. ("sha1sum" "-")))
  2037. (success? (lambda (pid)
  2038. (zero?
  2039. (status:exit-val (cdr (waitpid pid)))))))
  2040. (receive (from to pids) (pipeline commands)
  2041. (let* ((sha1 (read-delimited " " from))
  2042. (index (list-index (negate success?) (reverse pids))))
  2043. (close to)
  2044. (close from)
  2045. (if (not index)
  2046. sha1
  2047. (string-append "pipeline failed in command: "
  2048. (string-join (list-ref commands index)))))))
  2049. @result{} "52f99d234503fca8c84ef94b1005a3a28d8b3bc1"
  2050. @end example
  2051. @end deffn
  2052. @node Networking
  2053. @subsection Networking
  2054. @cindex network
  2055. @menu
  2056. * Network Address Conversion::
  2057. * Network Databases::
  2058. * Network Socket Address::
  2059. * Network Sockets and Communication::
  2060. * Internet Socket Examples::
  2061. @end menu
  2062. @node Network Address Conversion
  2063. @subsubsection Network Address Conversion
  2064. @cindex network address
  2065. This section describes procedures which convert internet addresses
  2066. between numeric and string formats.
  2067. @subsubheading IPv4 Address Conversion
  2068. @cindex IPv4
  2069. An IPv4 Internet address is a 4-byte value, represented in Guile as an
  2070. integer in host byte order, so that say ``0.0.0.1'' is 1, or
  2071. ``1.0.0.0'' is 16777216.
  2072. Some underlying C functions use network byte order for addresses,
  2073. Guile converts as necessary so that at the Scheme level its host byte
  2074. order everywhere.
  2075. @defvar INADDR_ANY
  2076. For a server, this can be used with @code{bind} (@pxref{Network
  2077. Sockets and Communication}) to allow connections from any interface on
  2078. the machine.
  2079. @end defvar
  2080. @defvar INADDR_BROADCAST
  2081. The broadcast address on the local network.
  2082. @end defvar
  2083. @defvar INADDR_LOOPBACK
  2084. The address of the local host using the loopback device, ie.@:
  2085. @samp{127.0.0.1}.
  2086. @end defvar
  2087. @c INADDR_NONE is defined in the code, but serves no purpose.
  2088. @c inet_addr() returns it as an error indication, but that function
  2089. @c isn't provided, for the good reason that inet_aton() does the same
  2090. @c job and gives an unambiguous error indication. (INADDR_NONE is a
  2091. @c valid 4-byte value, in glibc it's the same as INADDR_BROADCAST.)
  2092. @c
  2093. @c @defvar INADDR_NONE
  2094. @c No address.
  2095. @c @end defvar
  2096. @deffn {Scheme Procedure} inet-netof address
  2097. @deffnx {C Function} scm_inet_netof (address)
  2098. Return the network number part of the given IPv4
  2099. Internet address. E.g.,
  2100. @lisp
  2101. (inet-netof 2130706433) @result{} 127
  2102. @end lisp
  2103. @end deffn
  2104. @deffn {Scheme Procedure} inet-lnaof address
  2105. @deffnx {C Function} scm_lnaof (address)
  2106. Return the local-address-with-network part of the given
  2107. IPv4 Internet address, using the obsolete class A/B/C system.
  2108. E.g.,
  2109. @lisp
  2110. (inet-lnaof 2130706433) @result{} 1
  2111. @end lisp
  2112. @end deffn
  2113. @deffn {Scheme Procedure} inet-makeaddr net lna
  2114. @deffnx {C Function} scm_inet_makeaddr (net, lna)
  2115. Make an IPv4 Internet address by combining the network number
  2116. @var{net} with the local-address-within-network number
  2117. @var{lna}. E.g.,
  2118. @lisp
  2119. (inet-makeaddr 127 1) @result{} 2130706433
  2120. @end lisp
  2121. @end deffn
  2122. @subsubheading IPv6 Address Conversion
  2123. @cindex IPv6
  2124. An IPv6 Internet address is a 16-byte value, represented in Guile as
  2125. an integer in host byte order, so that say ``::1'' is 1.
  2126. @deffn {Scheme Procedure} inet-ntop family address
  2127. @deffnx {C Function} scm_inet_ntop (family, address)
  2128. Convert a network address from an integer to a printable string.
  2129. @var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,
  2130. @lisp
  2131. (inet-ntop AF_INET 2130706433) @result{} "127.0.0.1"
  2132. (inet-ntop AF_INET6 (- (expt 2 128) 1))
  2133. @result{} "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  2134. @end lisp
  2135. @end deffn
  2136. @deffn {Scheme Procedure} inet-pton family address
  2137. @deffnx {C Function} scm_inet_pton (family, address)
  2138. Convert a string containing a printable network address to an integer
  2139. address. @var{family} can be @code{AF_INET} or @code{AF_INET6}.
  2140. E.g.,
  2141. @lisp
  2142. (inet-pton AF_INET "127.0.0.1") @result{} 2130706433
  2143. (inet-pton AF_INET6 "::1") @result{} 1
  2144. @end lisp
  2145. @end deffn
  2146. @node Network Databases
  2147. @subsubsection Network Databases
  2148. @cindex network database
  2149. This section describes procedures which query various network databases.
  2150. Care should be taken when using the database routines since they are not
  2151. reentrant.
  2152. @subsubheading @code{getaddrinfo}
  2153. @cindex @code{addrinfo} object type
  2154. @cindex host name lookup
  2155. @cindex service name lookup
  2156. The @code{getaddrinfo} procedure maps host and service names to socket addresses
  2157. and associated information in a protocol-independent way.
  2158. @deffn {Scheme Procedure} getaddrinfo name service [hint_flags [hint_family [hint_socktype [hint_protocol]]]]
  2159. @deffnx {C Function} scm_getaddrinfo (name, service, hint_flags, hint_family, hint_socktype, hint_protocol)
  2160. Return a list of @code{addrinfo} structures containing
  2161. a socket address and associated information for host @var{name}
  2162. and/or @var{service} to be used in creating a socket with
  2163. which to address the specified service.
  2164. @example
  2165. (let* ((ai (car (getaddrinfo "www.gnu.org" "http")))
  2166. (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
  2167. (addrinfo:protocol ai))))
  2168. (connect s (addrinfo:addr ai))
  2169. s)
  2170. @end example
  2171. When @var{service} is omitted or is @code{#f}, return
  2172. network-level addresses for @var{name}. When @var{name}
  2173. is @code{#f} @var{service} must be provided and service
  2174. locations local to the caller are returned.
  2175. Additional hints can be provided. When specified,
  2176. @var{hint_flags} should be a bitwise-or of zero or more
  2177. constants among the following:
  2178. @table @code
  2179. @item AI_PASSIVE
  2180. Socket address is intended for @code{bind}.
  2181. @item AI_CANONNAME
  2182. Request for canonical host name, available via
  2183. @code{addrinfo:canonname}. This makes sense mainly when
  2184. DNS lookups are involved.
  2185. @item AI_NUMERICHOST
  2186. Specifies that @var{name} is a numeric host address string
  2187. (e.g., @code{"127.0.0.1"}), meaning that name resolution
  2188. will not be used.
  2189. @item AI_NUMERICSERV
  2190. Likewise, specifies that @var{service} is a numeric port
  2191. string (e.g., @code{"80"}).
  2192. @item AI_ADDRCONFIG
  2193. Return only addresses configured on the local system It is
  2194. highly recommended to provide this flag when the returned
  2195. socket addresses are to be used to make connections;
  2196. otherwise, some of the returned addresses could be unreachable
  2197. or use a protocol that is not supported.
  2198. @item AI_V4MAPPED
  2199. When looking up IPv6 addresses, return mapped IPv4 addresses if
  2200. there is no IPv6 address available at all.
  2201. @item AI_ALL
  2202. If this flag is set along with @code{AI_V4MAPPED} when looking up IPv6
  2203. addresses, return all IPv6 addresses as well as all IPv4 addresses, the latter
  2204. mapped to IPv6 format.
  2205. @end table
  2206. When given, @var{hint_family} should specify the requested
  2207. address family, e.g., @code{AF_INET6}. Similarly,
  2208. @var{hint_socktype} should specify the requested socket type
  2209. (e.g., @code{SOCK_DGRAM}), and @var{hint_protocol} should
  2210. specify the requested protocol (its value is interpreted
  2211. as in calls to @code{socket}).
  2212. On error, an exception with key @code{getaddrinfo-error} is
  2213. thrown, with an error code (an integer) as its argument:
  2214. @example
  2215. (catch 'getaddrinfo-error
  2216. (lambda ()
  2217. (getaddrinfo "www.gnu.org" "gopher"))
  2218. (lambda (key errcode)
  2219. (cond ((= errcode EAI_SERVICE)
  2220. (display "doesn't know about Gopher!\n"))
  2221. ((= errcode EAI_NONAME)
  2222. (display "www.gnu.org not found\\n"))
  2223. (else
  2224. (format #t "something wrong: ~a\n"
  2225. (gai-strerror errcode))))))
  2226. @end example
  2227. Error codes are:
  2228. @table @code
  2229. @item EAI_AGAIN
  2230. The name or service could not be resolved at this time. Future
  2231. attempts may succeed.
  2232. @item EAI_BADFLAGS
  2233. @var{hint_flags} contains an invalid value.
  2234. @item EAI_FAIL
  2235. A non-recoverable error occurred when attempting to
  2236. resolve the name.
  2237. @item EAI_FAMILY
  2238. @var{hint_family} was not recognized.
  2239. @item EAI_NONAME
  2240. Either @var{name} does not resolve for the supplied parameters,
  2241. or neither @var{name} nor @var{service} were supplied.
  2242. @item EAI_NODATA
  2243. This non-POSIX error code can be returned on some systems (GNU
  2244. and Darwin, at least), for example when @var{name} is known
  2245. but requests that were made turned out no data. Error handling
  2246. code should be prepared to handle it when it is defined.
  2247. @item EAI_SERVICE
  2248. @var{service} was not recognized for the specified socket type.
  2249. @item EAI_SOCKTYPE
  2250. @var{hint_socktype} was not recognized.
  2251. @item EAI_SYSTEM
  2252. A system error occurred. In C, the error code can be found in
  2253. @code{errno}; this value is not accessible from Scheme, but in
  2254. practice it provides little information about the actual error
  2255. cause.
  2256. @c See <http://bugs.gnu.org/13958>.
  2257. @end table
  2258. Users are encouraged to read the
  2259. @url{http://www.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html,
  2260. "POSIX specification} for more details.
  2261. @end deffn
  2262. The following procedures take an @code{addrinfo} object as returned by
  2263. @code{getaddrinfo}:
  2264. @deffn {Scheme Procedure} addrinfo:flags ai
  2265. Return flags for @var{ai} as a bitwise or of @code{AI_} values (see above).
  2266. @end deffn
  2267. @deffn {Scheme Procedure} addrinfo:fam ai
  2268. Return the address family of @var{ai} (a @code{AF_} value).
  2269. @end deffn
  2270. @deffn {Scheme Procedure} addrinfo:socktype ai
  2271. Return the socket type for @var{ai} (a @code{SOCK_} value).
  2272. @end deffn
  2273. @deffn {Scheme Procedure} addrinfo:protocol ai
  2274. Return the protocol of @var{ai}.
  2275. @end deffn
  2276. @deffn {Scheme Procedure} addrinfo:addr ai
  2277. Return the socket address associated with @var{ai} as a @code{sockaddr}
  2278. object (@pxref{Network Socket Address}).
  2279. @end deffn
  2280. @deffn {Scheme Procedure} addrinfo:canonname ai
  2281. Return a string for the canonical name associated with @var{ai} if
  2282. the @code{AI_CANONNAME} flag was supplied.
  2283. @end deffn
  2284. @subsubheading The Host Database
  2285. @cindex @file{/etc/hosts}
  2286. @cindex network database
  2287. A @dfn{host object} is a structure that represents what is known about a
  2288. network host, and is the usual way of representing a system's network
  2289. identity inside software.
  2290. The following functions accept a host object and return a selected
  2291. component:
  2292. @deffn {Scheme Procedure} hostent:name host
  2293. The ``official'' hostname for @var{host}.
  2294. @end deffn
  2295. @deffn {Scheme Procedure} hostent:aliases host
  2296. A list of aliases for @var{host}.
  2297. @end deffn
  2298. @deffn {Scheme Procedure} hostent:addrtype host
  2299. The host address type, one of the @code{AF} constants, such as
  2300. @code{AF_INET} or @code{AF_INET6}.
  2301. @end deffn
  2302. @deffn {Scheme Procedure} hostent:length host
  2303. The length of each address for @var{host}, in bytes.
  2304. @end deffn
  2305. @deffn {Scheme Procedure} hostent:addr-list host
  2306. The list of network addresses associated with @var{host}. For
  2307. @code{AF_INET} these are integer IPv4 address (@pxref{Network Address
  2308. Conversion}).
  2309. @end deffn
  2310. The following procedures can be used to search the host database. However,
  2311. @code{getaddrinfo} should be preferred over them since it's more generic and
  2312. thread-safe.
  2313. @deffn {Scheme Procedure} gethost [host]
  2314. @deffnx {Scheme Procedure} gethostbyname hostname
  2315. @deffnx {Scheme Procedure} gethostbyaddr address
  2316. @deffnx {C Function} scm_gethost (host)
  2317. Look up a host by name or address, returning a host object. The
  2318. @code{gethost} procedure will accept either a string name or an integer
  2319. address; if given no arguments, it behaves like @code{gethostent} (see
  2320. below). If a name or address is supplied but the address can not be
  2321. found, an error will be thrown to one of the keys:
  2322. @code{host-not-found}, @code{try-again}, @code{no-recovery} or
  2323. @code{no-data}, corresponding to the equivalent @code{h_error} values.
  2324. Unusual conditions may result in errors thrown to the
  2325. @code{system-error} or @code{misc_error} keys.
  2326. @lisp
  2327. (gethost "www.gnu.org")
  2328. @result{} #("www.gnu.org" () 2 4 (3353880842))
  2329. (gethostbyname "www.emacs.org")
  2330. @result{} #("emacs.org" ("www.emacs.org") 2 4 (1073448978))
  2331. @end lisp
  2332. @end deffn
  2333. The following procedures may be used to step through the host
  2334. database from beginning to end.
  2335. @deffn {Scheme Procedure} sethostent [stayopen]
  2336. Initialize an internal stream from which host objects may be read. This
  2337. procedure must be called before any calls to @code{gethostent}, and may
  2338. also be called afterward to reset the host entry stream. If
  2339. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2340. closed by subsequent @code{gethostbyname} or @code{gethostbyaddr} calls,
  2341. possibly giving an efficiency gain.
  2342. @end deffn
  2343. @deffn {Scheme Procedure} gethostent
  2344. Return the next host object from the host database, or @code{#f} if
  2345. there are no more hosts to be found (or an error has been encountered).
  2346. This procedure may not be used before @code{sethostent} has been called.
  2347. @end deffn
  2348. @deffn {Scheme Procedure} endhostent
  2349. Close the stream used by @code{gethostent}. The return value is unspecified.
  2350. @end deffn
  2351. @deffn {Scheme Procedure} sethost [stayopen]
  2352. @deffnx {C Function} scm_sethost (stayopen)
  2353. If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.
  2354. Otherwise it is equivalent to @code{sethostent stayopen}.
  2355. @end deffn
  2356. @subsubheading The Network Database
  2357. @cindex network database
  2358. The following functions accept an object representing a network
  2359. and return a selected component:
  2360. @deffn {Scheme Procedure} netent:name net
  2361. The ``official'' network name.
  2362. @end deffn
  2363. @deffn {Scheme Procedure} netent:aliases net
  2364. A list of aliases for the network.
  2365. @end deffn
  2366. @deffn {Scheme Procedure} netent:addrtype net
  2367. The type of the network number. Currently, this returns only
  2368. @code{AF_INET}.
  2369. @end deffn
  2370. @deffn {Scheme Procedure} netent:net net
  2371. The network number.
  2372. @end deffn
  2373. The following procedures are used to search the network database:
  2374. @deffn {Scheme Procedure} getnet [net]
  2375. @deffnx {Scheme Procedure} getnetbyname net-name
  2376. @deffnx {Scheme Procedure} getnetbyaddr net-number
  2377. @deffnx {C Function} scm_getnet (net)
  2378. Look up a network by name or net number in the network database. The
  2379. @var{net-name} argument must be a string, and the @var{net-number}
  2380. argument must be an integer. @code{getnet} will accept either type of
  2381. argument, behaving like @code{getnetent} (see below) if no arguments are
  2382. given.
  2383. @end deffn
  2384. The following procedures may be used to step through the network
  2385. database from beginning to end.
  2386. @deffn {Scheme Procedure} setnetent [stayopen]
  2387. Initialize an internal stream from which network objects may be read. This
  2388. procedure must be called before any calls to @code{getnetent}, and may
  2389. also be called afterward to reset the net entry stream. If
  2390. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2391. closed by subsequent @code{getnetbyname} or @code{getnetbyaddr} calls,
  2392. possibly giving an efficiency gain.
  2393. @end deffn
  2394. @deffn {Scheme Procedure} getnetent
  2395. Return the next entry from the network database.
  2396. @end deffn
  2397. @deffn {Scheme Procedure} endnetent
  2398. Close the stream used by @code{getnetent}. The return value is unspecified.
  2399. @end deffn
  2400. @deffn {Scheme Procedure} setnet [stayopen]
  2401. @deffnx {C Function} scm_setnet (stayopen)
  2402. If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.
  2403. Otherwise it is equivalent to @code{setnetent stayopen}.
  2404. @end deffn
  2405. @subsubheading The Protocol Database
  2406. @cindex @file{/etc/protocols}
  2407. @cindex protocols
  2408. @cindex network protocols
  2409. The following functions accept an object representing a protocol
  2410. and return a selected component:
  2411. @deffn {Scheme Procedure} protoent:name protocol
  2412. The ``official'' protocol name.
  2413. @end deffn
  2414. @deffn {Scheme Procedure} protoent:aliases protocol
  2415. A list of aliases for the protocol.
  2416. @end deffn
  2417. @deffn {Scheme Procedure} protoent:proto protocol
  2418. The protocol number.
  2419. @end deffn
  2420. The following procedures are used to search the protocol database:
  2421. @deffn {Scheme Procedure} getproto [protocol]
  2422. @deffnx {Scheme Procedure} getprotobyname name
  2423. @deffnx {Scheme Procedure} getprotobynumber number
  2424. @deffnx {C Function} scm_getproto (protocol)
  2425. Look up a network protocol by name or by number. @code{getprotobyname}
  2426. takes a string argument, and @code{getprotobynumber} takes an integer
  2427. argument. @code{getproto} will accept either type, behaving like
  2428. @code{getprotoent} (see below) if no arguments are supplied.
  2429. @end deffn
  2430. The following procedures may be used to step through the protocol
  2431. database from beginning to end.
  2432. @deffn {Scheme Procedure} setprotoent [stayopen]
  2433. Initialize an internal stream from which protocol objects may be read. This
  2434. procedure must be called before any calls to @code{getprotoent}, and may
  2435. also be called afterward to reset the protocol entry stream. If
  2436. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2437. closed by subsequent @code{getprotobyname} or @code{getprotobynumber} calls,
  2438. possibly giving an efficiency gain.
  2439. @end deffn
  2440. @deffn {Scheme Procedure} getprotoent
  2441. Return the next entry from the protocol database.
  2442. @end deffn
  2443. @deffn {Scheme Procedure} endprotoent
  2444. Close the stream used by @code{getprotoent}. The return value is unspecified.
  2445. @end deffn
  2446. @deffn {Scheme Procedure} setproto [stayopen]
  2447. @deffnx {C Function} scm_setproto (stayopen)
  2448. If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.
  2449. Otherwise it is equivalent to @code{setprotoent stayopen}.
  2450. @end deffn
  2451. @subsubheading The Service Database
  2452. @cindex @file{/etc/services}
  2453. @cindex services
  2454. @cindex network services
  2455. The following functions accept an object representing a service
  2456. and return a selected component:
  2457. @deffn {Scheme Procedure} servent:name serv
  2458. The ``official'' name of the network service.
  2459. @end deffn
  2460. @deffn {Scheme Procedure} servent:aliases serv
  2461. A list of aliases for the network service.
  2462. @end deffn
  2463. @deffn {Scheme Procedure} servent:port serv
  2464. The Internet port used by the service.
  2465. @end deffn
  2466. @deffn {Scheme Procedure} servent:proto serv
  2467. The protocol used by the service. A service may be listed many times
  2468. in the database under different protocol names.
  2469. @end deffn
  2470. The following procedures are used to search the service database:
  2471. @deffn {Scheme Procedure} getserv [name [protocol]]
  2472. @deffnx {Scheme Procedure} getservbyname name protocol
  2473. @deffnx {Scheme Procedure} getservbyport port protocol
  2474. @deffnx {C Function} scm_getserv (name, protocol)
  2475. Look up a network service by name or by service number, and return a
  2476. network service object. The @var{protocol} argument specifies the name
  2477. of the desired protocol; if the protocol found in the network service
  2478. database does not match this name, a system error is signalled.
  2479. The @code{getserv} procedure will take either a service name or number
  2480. as its first argument; if given no arguments, it behaves like
  2481. @code{getservent} (see below).
  2482. @lisp
  2483. (getserv "imap" "tcp")
  2484. @result{} #("imap2" ("imap") 143 "tcp")
  2485. (getservbyport 88 "udp")
  2486. @result{} #("kerberos" ("kerberos5" "krb5") 88 "udp")
  2487. @end lisp
  2488. @end deffn
  2489. The following procedures may be used to step through the service
  2490. database from beginning to end.
  2491. @deffn {Scheme Procedure} setservent [stayopen]
  2492. Initialize an internal stream from which service objects may be read. This
  2493. procedure must be called before any calls to @code{getservent}, and may
  2494. also be called afterward to reset the service entry stream. If
  2495. @var{stayopen} is supplied and is not @code{#f}, the database is not
  2496. closed by subsequent @code{getservbyname} or @code{getservbyport} calls,
  2497. possibly giving an efficiency gain.
  2498. @end deffn
  2499. @deffn {Scheme Procedure} getservent
  2500. Return the next entry from the services database.
  2501. @end deffn
  2502. @deffn {Scheme Procedure} endservent
  2503. Close the stream used by @code{getservent}. The return value is unspecified.
  2504. @end deffn
  2505. @deffn {Scheme Procedure} setserv [stayopen]
  2506. @deffnx {C Function} scm_setserv (stayopen)
  2507. If @var{stayopen} is omitted, this is equivalent to @code{endservent}.
  2508. Otherwise it is equivalent to @code{setservent stayopen}.
  2509. @end deffn
  2510. @node Network Socket Address
  2511. @subsubsection Network Socket Address
  2512. @cindex socket address
  2513. @cindex network socket address
  2514. @tpindex Socket address
  2515. A @dfn{socket address} object identifies a socket endpoint for
  2516. communication. In the case of @code{AF_INET} for instance, the socket
  2517. address object comprises the host address (or interface on the host)
  2518. and a port number which specifies a particular open socket in a
  2519. running client or server process. A socket address object can be
  2520. created with,
  2521. @deffn {Scheme Procedure} make-socket-address AF_INET ipv4addr port
  2522. @deffnx {Scheme Procedure} make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2523. @deffnx {Scheme Procedure} make-socket-address AF_UNIX path
  2524. @deffnx {C Function} scm_make_socket_address (family, address, arglist)
  2525. Return a new socket address object. The first argument is the address
  2526. family, one of the @code{AF} constants, then the arguments vary
  2527. according to the family.
  2528. For @code{AF_INET} the arguments are an IPv4 network address number
  2529. (@pxref{Network Address Conversion}), and a port number.
  2530. For @code{AF_INET6} the arguments are an IPv6 network address number
  2531. and a port number. Optional @var{flowinfo} and @var{scopeid}
  2532. arguments may be given (both integers, default 0).
  2533. For @code{AF_UNIX} the argument is a filename (a string).
  2534. The C function @code{scm_make_socket_address} takes the @var{family}
  2535. and @var{address} arguments directly, then @var{arglist} is a list of
  2536. further arguments, being the port for IPv4, port and optional flowinfo
  2537. and scopeid for IPv6, or the empty list @code{SCM_EOL} for Unix
  2538. domain.
  2539. @end deffn
  2540. @noindent
  2541. The following functions access the fields of a socket address object,
  2542. @deffn {Scheme Procedure} sockaddr:fam sa
  2543. Return the address family from socket address object @var{sa}. This
  2544. is one of the @code{AF} constants (e.g.@: @code{AF_INET}).
  2545. @end deffn
  2546. @deffn {Scheme Procedure} sockaddr:path sa
  2547. For an @code{AF_UNIX} socket address object @var{sa}, return the
  2548. filename.
  2549. @end deffn
  2550. @deffn {Scheme Procedure} sockaddr:addr sa
  2551. For an @code{AF_INET} or @code{AF_INET6} socket address object
  2552. @var{sa}, return the network address number.
  2553. @end deffn
  2554. @deffn {Scheme Procedure} sockaddr:port sa
  2555. For an @code{AF_INET} or @code{AF_INET6} socket address object
  2556. @var{sa}, return the port number.
  2557. @end deffn
  2558. @deffn {Scheme Procedure} sockaddr:flowinfo sa
  2559. For an @code{AF_INET6} socket address object @var{sa}, return the
  2560. flowinfo value.
  2561. @end deffn
  2562. @deffn {Scheme Procedure} sockaddr:scopeid sa
  2563. For an @code{AF_INET6} socket address object @var{sa}, return the
  2564. scope ID value.
  2565. @end deffn
  2566. @tpindex @code{struct sockaddr}
  2567. @tpindex @code{sockaddr}
  2568. The functions below convert to and from the C @code{struct sockaddr}
  2569. (@pxref{Address Formats,,, libc, The GNU C Library Reference Manual}).
  2570. That structure is a generic type, an application can cast to or from
  2571. @code{struct sockaddr_in}, @code{struct sockaddr_in6} or @code{struct
  2572. sockaddr_un} according to the address family.
  2573. In a @code{struct sockaddr} taken or returned, the byte ordering in
  2574. the fields follows the C conventions (@pxref{Byte Order,, Byte Order
  2575. Conversion, libc, The GNU C Library Reference Manual}). This means
  2576. network byte order for @code{AF_INET} host address
  2577. (@code{sin_addr.s_addr}) and port number (@code{sin_port}), and
  2578. @code{AF_INET6} port number (@code{sin6_port}). But at the Scheme
  2579. level these values are taken or returned in host byte order, so the
  2580. port is an ordinary integer, and the host address likewise is an
  2581. ordinary integer (as described in @ref{Network Address Conversion}).
  2582. @deftypefn {C Function} {struct sockaddr *} scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize)
  2583. Return a newly-@code{malloc}ed @code{struct sockaddr} created from
  2584. arguments like those taken by @code{scm_make_socket_address} above.
  2585. The size (in bytes) of the @code{struct sockaddr} return is stored
  2586. into @code{*@var{outsize}}. An application must call @code{free} to
  2587. release the returned structure when no longer required.
  2588. @end deftypefn
  2589. @deftypefn {C Function} SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size)
  2590. Return a Scheme socket address object from the C @var{address}
  2591. structure. @var{address_size} is the size in bytes of @var{address}.
  2592. @end deftypefn
  2593. @deftypefn {C Function} {struct sockaddr *} scm_to_sockaddr (SCM address, size_t *address_size)
  2594. Return a newly-@code{malloc}ed @code{struct sockaddr} from a Scheme
  2595. level socket address object.
  2596. The size (in bytes) of the @code{struct sockaddr} return is stored
  2597. into @code{*@var{outsize}}. An application must call @code{free} to
  2598. release the returned structure when no longer required.
  2599. @end deftypefn
  2600. @node Network Sockets and Communication
  2601. @subsubsection Network Sockets and Communication
  2602. @cindex socket
  2603. @cindex network socket
  2604. Socket ports can be created using @code{socket} and @code{socketpair}.
  2605. The ports are initially unbuffered, to make reading and writing to the
  2606. same port more reliable. A buffer can be added to the port using
  2607. @code{setvbuf} (@pxref{Buffering}).
  2608. Most systems have limits on how many files and sockets can be open, so
  2609. it's strongly recommended that socket ports be closed explicitly when
  2610. no longer required (@pxref{Ports}).
  2611. Some of the underlying C functions take values in network byte order,
  2612. but the convention in Guile is that at the Scheme level everything is
  2613. ordinary host byte order and conversions are made automatically where
  2614. necessary.
  2615. @deffn {Scheme Procedure} socket family style proto
  2616. @deffnx {C Function} scm_socket (family, style, proto)
  2617. Return a new socket port of the type specified by @var{family},
  2618. @var{style} and @var{proto}. All three parameters are integers. The
  2619. possible values for @var{family} are as follows, where supported by
  2620. the system,
  2621. @defvar PF_UNIX
  2622. @defvarx PF_INET
  2623. @defvarx PF_INET6
  2624. @end defvar
  2625. The possible values for @var{style} are as follows, again where
  2626. supported by the system,
  2627. @defvar SOCK_STREAM
  2628. @defvarx SOCK_DGRAM
  2629. @defvarx SOCK_RAW
  2630. @defvarx SOCK_RDM
  2631. @defvarx SOCK_SEQPACKET
  2632. @end defvar
  2633. @var{proto} can be obtained from a protocol name using
  2634. @code{getprotobyname} (@pxref{Network Databases}). A value of zero
  2635. means the default protocol, which is usually right.
  2636. A socket cannot by used for communication until it has been connected
  2637. somewhere, usually with either @code{connect} or @code{accept} below.
  2638. @end deffn
  2639. @deffn {Scheme Procedure} socketpair family style proto
  2640. @deffnx {C Function} scm_socketpair (family, style, proto)
  2641. Return a pair, the @code{car} and @code{cdr} of which are two unnamed
  2642. socket ports connected to each other. The connection is full-duplex,
  2643. so data can be transferred in either direction between the two.
  2644. @var{family}, @var{style} and @var{proto} are as per @code{socket}
  2645. above. But many systems only support socket pairs in the
  2646. @code{PF_UNIX} family. Zero is likely to be the only meaningful value
  2647. for @var{proto}.
  2648. @end deffn
  2649. @deffn {Scheme Procedure} getsockopt sock level optname
  2650. @deffnx {Scheme Procedure} setsockopt sock level optname value
  2651. @deffnx {C Function} scm_getsockopt (sock, level, optname)
  2652. @deffnx {C Function} scm_setsockopt (sock, level, optname, value)
  2653. Get or set an option on socket port @var{sock}. @code{getsockopt}
  2654. returns the current value. @code{setsockopt} sets a value and the
  2655. return is unspecified.
  2656. @var{level} is an integer specifying a protocol layer, either
  2657. @code{SOL_SOCKET} for socket level options, or a protocol number from
  2658. the @code{IPPROTO} constants or @code{getprotoent} (@pxref{Network
  2659. Databases}).
  2660. @defvar SOL_SOCKET
  2661. @defvarx IPPROTO_IP
  2662. @defvarx IPPROTO_TCP
  2663. @defvarx IPPROTO_UDP
  2664. @end defvar
  2665. @var{optname} is an integer specifying an option within the protocol
  2666. layer.
  2667. For @code{SOL_SOCKET} level the following @var{optname}s are defined
  2668. (when provided by the system). For their meaning see
  2669. @ref{Socket-Level Options,,, libc, The GNU C Library Reference
  2670. Manual}, or @command{man 7 socket}.
  2671. @defvar SO_DEBUG
  2672. @defvarx SO_REUSEADDR
  2673. @defvarx SO_STYLE
  2674. @defvarx SO_TYPE
  2675. @defvarx SO_ERROR
  2676. @defvarx SO_DONTROUTE
  2677. @defvarx SO_BROADCAST
  2678. @defvarx SO_SNDBUF
  2679. @defvarx SO_RCVBUF
  2680. @defvarx SO_KEEPALIVE
  2681. @defvarx SO_OOBINLINE
  2682. @defvarx SO_NO_CHECK
  2683. @defvarx SO_PRIORITY
  2684. @defvarx SO_REUSEPORT
  2685. The @var{value} taken or returned is an integer.
  2686. @end defvar
  2687. @defvar SO_LINGER
  2688. The @var{value} taken or returned is a pair of integers
  2689. @code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without timeout
  2690. support (ie.@: without @code{struct linger}), only @var{ENABLE} has an
  2691. effect but the value in Guile is always a pair.
  2692. @end defvar
  2693. @c Note that we refer only to ``man ip'' here. On GNU/Linux it's
  2694. @c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.
  2695. @c
  2696. For IP level (@code{IPPROTO_IP}) the following @var{optname}s are
  2697. defined (when provided by the system). See @command{man ip} for what
  2698. they mean.
  2699. @defvar IP_MULTICAST_IF
  2700. This sets the source interface used by multicast traffic.
  2701. @end defvar
  2702. @defvar IP_MULTICAST_TTL
  2703. This sets the default TTL for multicast traffic. This defaults
  2704. to 1 and should be increased to allow traffic to pass beyond the
  2705. local network.
  2706. @end defvar
  2707. @defvar IP_ADD_MEMBERSHIP
  2708. @defvarx IP_DROP_MEMBERSHIP
  2709. These can be used only with @code{setsockopt}, not @code{getsockopt}.
  2710. @var{value} is a pair @code{(@var{MULTIADDR} . @var{INTERFACEADDR})}
  2711. of integer IPv4 addresses (@pxref{Network Address Conversion}).
  2712. @var{MULTIADDR} is a multicast address to be added to or dropped from
  2713. the interface @var{INTERFACEADDR}. @var{INTERFACEADDR} can be
  2714. @code{INADDR_ANY} to have the system select the interface.
  2715. @var{INTERFACEADDR} can also be an interface index number, on systems
  2716. supporting that.
  2717. @end defvar
  2718. @end deffn
  2719. For @code{IPPROTO_TCP} level the following @var{optname}s are defined
  2720. (when provided by the system). For their meaning see @command{man 7
  2721. tcp}.
  2722. @defvar TCP_NODELAY
  2723. @defvarx TCP_CORK
  2724. The @var{value} taken or returned is an integer.
  2725. @end defvar
  2726. @deffn {Scheme Procedure} shutdown sock how
  2727. @deffnx {C Function} scm_shutdown (sock, how)
  2728. Sockets can be closed simply by using @code{close-port}. The
  2729. @code{shutdown} procedure allows reception or transmission on a
  2730. connection to be shut down individually, according to the parameter
  2731. @var{how}:
  2732. @table @asis
  2733. @item 0
  2734. Stop receiving data for this socket. If further data arrives, reject it.
  2735. @item 1
  2736. Stop trying to transmit data from this socket. Discard any
  2737. data waiting to be sent. Stop looking for acknowledgement of
  2738. data already sent; don't retransmit it if it is lost.
  2739. @item 2
  2740. Stop both reception and transmission.
  2741. @end table
  2742. The return value is unspecified.
  2743. @end deffn
  2744. @deffn {Scheme Procedure} connect sock sockaddr
  2745. @deffnx {Scheme Procedure} connect sock AF_INET ipv4addr port
  2746. @deffnx {Scheme Procedure} connect sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2747. @deffnx {Scheme Procedure} connect sock AF_UNIX path
  2748. @deffnx {C Function} scm_connect (sock, fam, address, args)
  2749. Initiate a connection on socket port @var{sock} to a given address. The
  2750. destination is either a socket address object, or arguments the same as
  2751. @code{make-socket-address} would take to make such an object
  2752. (@pxref{Network Socket Address}). Return true unless the socket was
  2753. configured as non-blocking and the connection could not be made
  2754. immediately.
  2755. @example
  2756. (connect sock AF_INET INADDR_LOOPBACK 23)
  2757. (connect sock (make-socket-address AF_INET INADDR_LOOPBACK 23))
  2758. @end example
  2759. @end deffn
  2760. @deffn {Scheme Procedure} bind sock sockaddr
  2761. @deffnx {Scheme Procedure} bind sock AF_INET ipv4addr port
  2762. @deffnx {Scheme Procedure} bind sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
  2763. @deffnx {Scheme Procedure} bind sock AF_UNIX path
  2764. @deffnx {C Function} scm_bind (sock, fam, address, args)
  2765. Bind socket port @var{sock} to the given address. The address is
  2766. either a socket address object, or arguments the same as
  2767. @code{make-socket-address} would take to make such an object
  2768. (@pxref{Network Socket Address}). The return value is unspecified.
  2769. Generally a socket is only explicitly bound to a particular address
  2770. when making a server, i.e.@: to listen on a particular port. For an
  2771. outgoing connection the system will assign a local address
  2772. automatically, if not already bound.
  2773. @example
  2774. (bind sock AF_INET INADDR_ANY 12345)
  2775. (bind sock (make-socket-address AF_INET INADDR_ANY 12345))
  2776. @end example
  2777. @end deffn
  2778. @deffn {Scheme Procedure} listen sock backlog
  2779. @deffnx {C Function} scm_listen (sock, backlog)
  2780. Enable @var{sock} to accept connection
  2781. requests. @var{backlog} is an integer specifying
  2782. the maximum length of the queue for pending connections.
  2783. If the queue fills, new clients will fail to connect until
  2784. the server calls @code{accept} to accept a connection from
  2785. the queue.
  2786. The return value is unspecified.
  2787. @end deffn
  2788. @deffn {Scheme Procedure} accept sock [flags]
  2789. @deffnx {C Function} scm_accept (sock)
  2790. Accept a connection from socket port @var{sock} which has been enabled
  2791. for listening with @code{listen} above.
  2792. If there are no incoming connections in the queue, there are two
  2793. possible behaviors, depending on whether @var{sock} has been configured
  2794. for non-blocking operation or not:
  2795. @itemize
  2796. @item
  2797. If there is no connection waiting and the socket was set to non-blocking
  2798. mode with the @code{O_NONBLOCK} port option (@pxref{Ports and File
  2799. Descriptors,@code{fcntl}}), return @code{#f} directly.
  2800. @item
  2801. Otherwise wait until a connection is available.
  2802. @end itemize
  2803. The return value is a pair. The @code{car} is a new socket port,
  2804. connected and ready to communicate. The @code{cdr} is a socket address
  2805. object (@pxref{Network Socket Address}) which is where the remote
  2806. connection is from (like @code{getpeername} below).
  2807. @var{flags}, if given, may include @code{SOCK_CLOEXEC} or
  2808. @code{SOCK_NONBLOCK}, which like @code{O_CLOEXEC} and @code{O_NONBLOCK}
  2809. apply to the newly accepted socket.
  2810. All communication takes place using the new socket returned. The
  2811. given @var{sock} remains bound and listening, and @code{accept} may be
  2812. called on it again to get another incoming connection when desired.
  2813. @end deffn
  2814. @deffn {Scheme Procedure} getsockname sock
  2815. @deffnx {C Function} scm_getsockname (sock)
  2816. Return a socket address object which is the where @var{sock} is bound
  2817. locally. @var{sock} may have obtained its local address from
  2818. @code{bind} (above), or if a @code{connect} is done with an otherwise
  2819. unbound socket (which is usual) then the system will have assigned an
  2820. address.
  2821. Note that on many systems the address of a socket in the
  2822. @code{AF_UNIX} namespace cannot be read.
  2823. @end deffn
  2824. @deffn {Scheme Procedure} getpeername sock
  2825. @deffnx {C Function} scm_getpeername (sock)
  2826. Return a socket address object which is where @var{sock} is connected
  2827. to, i.e.@: the remote endpoint.
  2828. Note that on many systems the address of a socket in the
  2829. @code{AF_UNIX} namespace cannot be read.
  2830. @end deffn
  2831. @deffn {Scheme Procedure} recv! sock buf [flags]
  2832. @deffnx {C Function} scm_recv (sock, buf, flags)
  2833. Receive data from a socket port.
  2834. @var{sock} must already
  2835. be bound to the address from which data is to be received.
  2836. @var{buf} is a bytevector into which
  2837. the data will be written. The size of @var{buf} limits
  2838. the amount of
  2839. data which can be received: in the case of packet
  2840. protocols, if a packet larger than this limit is encountered
  2841. then some data
  2842. will be irrevocably lost.
  2843. @vindex MSG_OOB
  2844. @vindex MSG_PEEK
  2845. @vindex MSG_DONTROUTE
  2846. The optional @var{flags} argument is a value or bitwise OR of
  2847. @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2848. The value returned is the number of bytes read from the
  2849. socket.
  2850. Note that the data is read directly from the socket file
  2851. descriptor:
  2852. any unread buffered port data is ignored.
  2853. @end deffn
  2854. @deffn {Scheme Procedure} send sock message [flags]
  2855. @deffnx {C Function} scm_send (sock, message, flags)
  2856. @vindex MSG_OOB
  2857. @vindex MSG_PEEK
  2858. @vindex MSG_DONTROUTE
  2859. Transmit bytevector @var{message} on socket port @var{sock}.
  2860. @var{sock} must already be bound to a destination address. The value
  2861. returned is the number of bytes transmitted---it's possible for this
  2862. to be less than the length of @var{message} if the socket is set to be
  2863. non-blocking. The optional @var{flags} argument is a value or bitwise
  2864. OR of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2865. Note that the data is written directly to the socket
  2866. file descriptor:
  2867. any unflushed buffered port data is ignored.
  2868. @end deffn
  2869. @deffn {Scheme Procedure} recvfrom! sock buf [flags [start [end]]]
  2870. @deffnx {C Function} scm_recvfrom (sock, buf, flags, start, end)
  2871. Receive data from socket port @var{sock}, returning the originating
  2872. address as well as the data. This function is usually for datagram
  2873. sockets, but can be used on stream-oriented sockets too.
  2874. The data received is stored in bytevector @var{buf}, using
  2875. either the whole bytevector or just the region between the optional
  2876. @var{start} and @var{end} positions. The size of @var{buf}
  2877. limits the amount of data that can be received. For datagram
  2878. protocols if a packet larger than this is received then excess
  2879. bytes are irrevocably lost.
  2880. The return value is a pair. The @code{car} is the number of bytes
  2881. read. The @code{cdr} is a socket address object (@pxref{Network
  2882. Socket Address}) which is where the data came from, or @code{#f} if
  2883. the origin is unknown.
  2884. @vindex MSG_OOB
  2885. @vindex MSG_PEEK
  2886. @vindex MSG_DONTROUTE
  2887. The optional @var{flags} argument is a or bitwise-OR (@code{logior})
  2888. of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2889. Data is read directly from the socket file descriptor, any buffered
  2890. port data is ignored.
  2891. @c This was linux kernel 2.6.15 and glibc 2.3.6, not sure what any
  2892. @c specs are supposed to say about recvfrom threading.
  2893. @c
  2894. On a GNU/Linux system @code{recvfrom!} is not multi-threading, all
  2895. threads stop while a @code{recvfrom!} call is in progress. An
  2896. application may need to use @code{select}, @code{O_NONBLOCK} or
  2897. @code{MSG_DONTWAIT} to avoid this.
  2898. @end deffn
  2899. @deffn {Scheme Procedure} sendto sock message sockaddr [flags]
  2900. @deffnx {Scheme Procedure} sendto sock message AF_INET ipv4addr port [flags]
  2901. @deffnx {Scheme Procedure} sendto sock message AF_INET6 ipv6addr port [flowinfo [scopeid [flags]]]
  2902. @deffnx {Scheme Procedure} sendto sock message AF_UNIX path [flags]
  2903. @deffnx {C Function} scm_sendto (sock, message, fam, address, args_and_flags)
  2904. Transmit bytevector @var{message} as a datagram socket port
  2905. @var{sock}. The destination is specified either as a socket address
  2906. object, or as arguments the same as would be taken by
  2907. @code{make-socket-address} to create such an object (@pxref{Network
  2908. Socket Address}).
  2909. The destination address may be followed by an optional @var{flags}
  2910. argument which is a @code{logior} (@pxref{Bitwise Operations}) of
  2911. @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
  2912. The value returned is the number of bytes transmitted --
  2913. it's possible for
  2914. this to be less than the length of @var{message} if the
  2915. socket is
  2916. set to be non-blocking.
  2917. Note that the data is written directly to the socket
  2918. file descriptor:
  2919. any unflushed buffered port data is ignored.
  2920. @end deffn
  2921. @node Internet Socket Examples
  2922. @subsubsection Network Socket Examples
  2923. @cindex network examples
  2924. @cindex socket examples
  2925. The following give examples of how to use network sockets.
  2926. @subsubheading Internet Socket Client Example
  2927. @cindex socket client example
  2928. The following example demonstrates an Internet socket client.
  2929. It connects to the HTTP daemon running on the local machine and
  2930. returns the contents of the root index URL.
  2931. @example
  2932. (let ((s (socket PF_INET SOCK_STREAM 0)))
  2933. (connect s AF_INET (inet-pton AF_INET "127.0.0.1") 80)
  2934. (display "GET / HTTP/1.0\r\n\r\n" s)
  2935. (do ((line (read-line s) (read-line s)))
  2936. ((eof-object? line))
  2937. (display line)
  2938. (newline)))
  2939. @end example
  2940. @subsubheading Internet Socket Server Example
  2941. @cindex socket server example
  2942. The following example shows a simple Internet server which listens on
  2943. port 2904 for incoming connections and sends a greeting back to the
  2944. client.
  2945. @example
  2946. (let ((s (socket PF_INET SOCK_STREAM 0)))
  2947. (setsockopt s SOL_SOCKET SO_REUSEADDR 1)
  2948. ;; @r{Specific address?}
  2949. ;; @r{(bind s AF_INET (inet-pton AF_INET "127.0.0.1") 2904)}
  2950. (bind s AF_INET INADDR_ANY 2904)
  2951. (listen s 5)
  2952. (simple-format #t "Listening for clients in pid: ~S" (getpid))
  2953. (newline)
  2954. (while #t
  2955. (let* ((client-connection (accept s))
  2956. (client-details (cdr client-connection))
  2957. (client (car client-connection)))
  2958. (simple-format #t "Got new client connection: ~S"
  2959. client-details)
  2960. (newline)
  2961. (simple-format #t "Client address: ~S"
  2962. (gethostbyaddr
  2963. (sockaddr:addr client-details)))
  2964. (newline)
  2965. ;; @r{Send back the greeting to the client port}
  2966. (display "Hello client\r\n" client)
  2967. (close client))))
  2968. @end example
  2969. @node System Identification
  2970. @subsection System Identification
  2971. @cindex system name
  2972. This section lists the various procedures Guile provides for accessing
  2973. information about the system it runs on.
  2974. @deffn {Scheme Procedure} uname
  2975. @deffnx {C Function} scm_uname ()
  2976. Return an object with some information about the computer
  2977. system the program is running on.
  2978. The following procedures accept an object as returned by @code{uname}
  2979. and return a selected component (all of which are strings).
  2980. @deffn {Scheme Procedure} utsname:sysname un
  2981. The name of the operating system.
  2982. @end deffn
  2983. @deffn {Scheme Procedure} utsname:nodename un
  2984. The network name of the computer.
  2985. @end deffn
  2986. @deffn {Scheme Procedure} utsname:release un
  2987. The current release level of the operating system implementation.
  2988. @end deffn
  2989. @deffn {Scheme Procedure} utsname:version un
  2990. The current version level within the release of the operating system.
  2991. @end deffn
  2992. @deffn {Scheme Procedure} utsname:machine un
  2993. A description of the hardware.
  2994. @end deffn
  2995. @end deffn
  2996. @deffn {Scheme Procedure} gethostname
  2997. @deffnx {C Function} scm_gethostname ()
  2998. @cindex host name
  2999. Return the host name of the current processor.
  3000. @end deffn
  3001. @deffn {Scheme Procedure} sethostname name
  3002. @deffnx {C Function} scm_sethostname (name)
  3003. Set the host name of the current processor to @var{name}. May
  3004. only be used by the superuser. The return value is not
  3005. specified.
  3006. @end deffn
  3007. @node Locales
  3008. @subsection Locales
  3009. @cindex locale
  3010. @deffn {Scheme Procedure} setlocale category [locale]
  3011. @deffnx {C Function} scm_setlocale (category, locale)
  3012. Get or set the current locale, used for various internationalizations.
  3013. Locales are strings, such as @samp{sv_SE}.
  3014. If @var{locale} is given then the locale for the given @var{category}
  3015. is set and the new value returned. If @var{locale} is not given then
  3016. the current value is returned. @var{category} should be one of the
  3017. following values (@pxref{Locale Categories, Categories of Activities
  3018. that Locales Affect,, libc, The GNU C Library Reference Manual}):
  3019. @defvar LC_ALL
  3020. @defvarx LC_COLLATE
  3021. @defvarx LC_CTYPE
  3022. @defvarx LC_MESSAGES
  3023. @defvarx LC_MONETARY
  3024. @defvarx LC_NUMERIC
  3025. @defvarx LC_TIME
  3026. @end defvar
  3027. @cindex @code{LANG}
  3028. A common usage is @samp{(setlocale LC_ALL "")}, which initializes all
  3029. categories based on standard environment variables (@code{LANG} etc).
  3030. For full details on categories and locale names @pxref{Locales,,
  3031. Locales and Internationalization, libc, The GNU C Library Reference
  3032. Manual}.
  3033. Note that @code{setlocale} affects locale settings for the whole
  3034. process. @xref{i18n Introduction, locale objects and
  3035. @code{make-locale}}, for a thread-safe alternative.
  3036. @end deffn
  3037. @node Encryption
  3038. @subsection Encryption
  3039. @cindex encryption
  3040. Please note that the procedures in this section are not suited for
  3041. strong encryption, they are only interfaces to the well-known and
  3042. common system library functions of the same name. They are just as good
  3043. (or bad) as the underlying functions, so you should refer to your system
  3044. documentation before using them (@pxref{crypt,, Encrypting Passwords,
  3045. libc, The GNU C Library Reference Manual}).
  3046. @deffn {Scheme Procedure} crypt key salt
  3047. @deffnx {C Function} scm_crypt (key, salt)
  3048. Encrypt @var{key}, with the addition of @var{salt} (both strings),
  3049. using the @code{crypt} C library call.
  3050. @end deffn
  3051. Although @code{getpass} is not an encryption procedure per se, it
  3052. appears here because it is often used in combination with @code{crypt}:
  3053. @deffn {Scheme Procedure} getpass prompt
  3054. @deffnx {C Function} scm_getpass (prompt)
  3055. @cindex password
  3056. Display @var{prompt} to the standard error output and read
  3057. a password from @file{/dev/tty}. If this file is not
  3058. accessible, it reads from standard input. The password may be
  3059. up to 127 characters in length. Additional characters and the
  3060. terminating newline character are discarded. While reading
  3061. the password, echoing and the generation of signals by special
  3062. characters is disabled.
  3063. @end deffn
  3064. @c Local Variables:
  3065. @c TeX-master: "guile.texi"
  3066. @c End: