posix.texi 128 KB

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