posix.texi 130 KB

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