posix.texi 130 KB

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