posix.texi 139 KB

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