Fix omissions and typos in previous commit.
[bpt/guile.git] / doc / ref / posix.texi
index d131f4f..34194fb 100644 (file)
@@ -1,6 +1,6 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Guile Reference Manual.
-@c Copyright (C)  1996, 1997, 2000, 2001, 2002, 2003, 2004
+@c Copyright (C)  1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007
 @c   Free Software Foundation, Inc.
 @c See the file guile.texi for copying conditions.
 
@@ -11,7 +11,7 @@
 @menu
 * Conventions::                 Conventions employed by the POSIX interface.
 * Ports and File Descriptors::  Scheme ``ports'' and Unix file descriptors
-                                 have different representations.
+                                  have different representations.
 * File System::                 stat, chown, chmod, etc.
 * User Information::            Retrieving a user's GECOS (/etc/passwd) entry.
 * Time::                        gettimeofday, localtime, strftime, etc.
@@ -472,38 +472,59 @@ If @var{size} is omitted, a default size will be used.
 @end defvar
 @end deffn
 
-@deffn {Scheme Procedure} fcntl object cmd [value]
+@deffn {Scheme Procedure} fcntl port/fd cmd [value]
 @deffnx {C Function} scm_fcntl (object, cmd, value)
-Apply @var{command} to the specified file descriptor or the underlying
-file descriptor of the specified port.  @var{value} is an optional
-integer argument.
+Apply @var{cmd} on @var{port/fd}, either a port or file descriptor.
+The @var{value} argument is used by the @code{SET} commands described
+below, it's an integer value.
 
-Values for @var{command} are:
+Values for @var{cmd} are:
 
 @defvar F_DUPFD
-Duplicate a file descriptor
+Duplicate the file descriptor, the same as @code{dup->fdes} above
+does.
 @end defvar
+
 @defvar F_GETFD
-Get flags associated with the file descriptor.
+@defvarx F_SETFD
+Get or set flags associated with the file descriptor.  The only flag
+is the following,
+
+@defvar FD_CLOEXEC
+``Close on exec'', meaning the file descriptor will be closed on an
+@code{exec} call (a successful such call).  For example to set that
+flag,
+
+@example
+(fcntl port F_SETFD FD_CLOEXEC)
+@end example
+
+Or better, set it but leave any other possible future flags unchanged,
+
+@example
+(fcntl port F_SETFD (logior FD_CLOEXEC
+                            (fcntl port F_GETFD)))
+@end example
 @end defvar
-@defvar F_SETFD
-Set flags associated with the file descriptor to @var{value}.
 @end defvar
+
 @defvar F_GETFL
-Get flags associated with the open file.
-@end defvar
-@defvar F_SETFL
-Set flags associated with the open file to @var{value}
+@defvarx F_SETFL
+Get or set flags associated with the open file.  These flags are
+@code{O_RDONLY} etc described under @code{open} above.
+
+A common use is to set @code{O_NONBLOCK} on a network socket.  The
+following sets that flag, and leaves other flags unchanged.
+
+@example
+(fcntl sock F_SETFL (logior O_NONBLOCK
+                            (fcntl sock F_GETFL)))
+@end example
 @end defvar
+
 @defvar F_GETOWN
-Get the process ID of a socket's owner, for @code{SIGIO} signals.
-@end defvar
-@defvar F_SETOWN
-Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.
-@end defvar
-@defvar FD_CLOEXEC
-The value used to indicate the ``close on exec'' flag with @code{F_GETFL} or
-@code{F_SETFL}.
+@defvarx F_SETOWN
+Get or set the process ID of a socket's owner, for @code{SIGIO} signals.
 @end defvar
 @end deffn
 
@@ -525,12 +546,16 @@ for a given file at a given time.
 Unlock the file.
 @end defvar
 @defvar LOCK_NB
-Don't block when locking.  May be specified by bitwise OR'ing
-it to one of the other operations.
+Don't block when locking.  This is combined with one of the other
+operations using @code{logior} (@pxref{Bitwise Operations}).  If
+@code{flock} would block an @code{EWOULDBLOCK} error is thrown
+(@pxref{Conventions}).
 @end defvar
 
 The return value is not specified. @var{file} may be an open
 file descriptor or an open file descriptor port.
+
+Note that @code{flock} does not lock files across NFS.
 @end deffn
 
 @deffn {Scheme Procedure} select reads writes excepts [secs [usecs]]
@@ -873,25 +898,42 @@ The return value is unspecified.
 @deffn {Scheme Procedure} tmpnam
 @deffnx {C Function} scm_tmpnam ()
 @cindex temporary file
-Return a name in the file system that does not match any
-existing file.  However there is no guarantee that another
-process will not create the file after @code{tmpnam} is called.
-Care should be taken if opening the file, e.g., use the
-@code{O_EXCL} open flag or use @code{mkstemp!} instead.
+Return an auto-generated name of a temporary file, a file which
+doesn't already exist.  The name includes a path, it's usually in
+@file{/tmp} but that's system dependent.
+
+Care must be taken when using @code{tmpnam}.  In between choosing the
+name and creating the file another program might use that name, or an
+attacker might even make it a symlink pointing at something important
+and causing you to overwrite that.
+
+The safe way is to create the file using @code{open} with
+@code{O_EXCL} to avoid any overwriting.  A loop can try again with
+another name if the file exists (error @code{EEXIST}).
+@code{mkstemp!} below does that.
 @end deffn
 
 @deffn {Scheme Procedure} mkstemp! tmpl
 @deffnx {C Function} scm_mkstemp (tmpl)
 @cindex temporary file
-Create a new unique file in the file system and returns a new
-buffered port open for reading and writing to the file.
+Create a new unique file in the file system and return a new buffered
+port open for reading and writing to the file.
+
+@var{tmpl} is a string specifying where the file should be created: it
+must end with @samp{XXXXXX} and those @samp{X}s will be changed in the
+string to return the name of the file.  (@code{port-filename} on the
+port also gives the name.)
 
-@var{tmpl} is a string specifying where the file should be
-created: it must end with @samp{XXXXXX} and will be changed in
-place to return the name of the temporary file.
+POSIX doesn't specify the permissions mode of the file, on GNU and
+most systems it's @code{#o600}.  An application can use @code{chmod}
+to relax that if desired.  For example @code{#o666} less @code{umask},
+which is usual for ordinary file creation,
 
-The file is created with mode @code{0600}, which means read and write
-for the owner only.  @code{chmod} can be used to change this.
+@example
+(let ((port (mkstemp! (string-copy "/tmp/myfile-XXXXXX"))))
+  (chmod port (logand #o666 (lognot (umask))))
+  ...)
+@end example
 @end deffn
 
 @deffn {Scheme Procedure} dirname filename
@@ -914,6 +956,11 @@ If @var{suffix} is provided, and is equal to the end of
 @end lisp
 @end deffn
 
+@deffn {Scheme Procedure} file-exists? filename
+Return @code{#t} if the file named @var{filename} exists, @code{#f} if
+not.
+@end deffn
+
 
 @node User Information
 @subsection User Information
@@ -966,8 +1013,8 @@ return value is unspecified.
 @end deffn
 
 @deffn {Scheme Procedure} getpwent
-Return the next entry in the user database, using the stream set by
-@code{setpwent}.
+Read the next entry in the user database stream.  The return is a
+passwd user object as above, or @code{#f} when no more entries.
 @end deffn
 
 @deffn {Scheme Procedure} endpwent
@@ -1051,6 +1098,10 @@ following shortcut procedures are also available.
 Return a string containing a user name associated with the
 effective user id of the process.  Return @code{#f} if this
 information cannot be obtained.
+
+This function has been removed from the latest POSIX specification,
+Guile provides it only if the system has it.  Using @code{(getpwuid
+(geteuid))} may be a better idea.
 @end deffn
 
 @deffn {Scheme Procedure} getlogin
@@ -1124,6 +1175,13 @@ Daylight saving indicator (0 for ``no'', greater than 0 for ``yes'', less than
 @deffn {Scheme Procedure} tm:gmtoff tm
 @deffnx {Scheme Procedure} set-tm:gmtoff tm val
 Time zone offset in seconds west of @acronym{UTC} (-46800 to 43200).
+For example on East coast USA (zone @samp{EST+5}) this would be 18000
+(ie.@: @m{5\times60\times60,5*60*60}) in winter, or 14400
+(ie.@: @m{4\times60\times60,4*60*60}) during daylight savings.
+
+Note @code{tm:gmtoff} is not the same as @code{tm_gmtoff} in the C
+@code{tm} structure.  @code{tm_gmtoff} is seconds east and hence the
+negative of the value here.
 @end deffn
 @deffn {Scheme Procedure} tm:zone tm
 @deffnx {Scheme Procedure} set-tm:zone tm val
@@ -1150,15 +1208,33 @@ Return an object representing the broken down components of
 
 @deffn {Scheme Procedure} mktime sbd-time [zone]
 @deffnx {C Function} scm_mktime (sbd_time, zone)
-@var{sbd-time} is an object representing broken down time and
-@code{zone} is an optional time zone specifier (otherwise the @env{TZ}
-environment variable or the system default is used).
-
-Returns a pair: the @acronym{CAR} is a corresponding integer time
-value like that returned by @code{current-time}; the @acronym{CDR} is
-a broken down time object, similar to @var{sbd-time} but with
-normalized values; i.e.@: with corrected @code{tm:wday} and
-@code{tm:yday} fields.
+For a broken down time object @var{sbd-time}, return a pair the
+@code{car} of which is an integer time like @code{current-time}, and
+the @code{cdr} of which is a new broken down time with normalized
+fields.
+
+@var{zone} is a timezone string, or the default is the @env{TZ}
+environment variable or the system default (@pxref{TZ Variable,,
+Specifying the Time Zone with @env{TZ}, libc, GNU C Library Reference
+Manual}).  @var{sbd-time} is taken to be in that @var{zone}.
+
+The following fields of @var{sbd-time} are used: @code{tm:year},
+@code{tm:mon}, @code{tm:mday}, @code{tm:hour}, @code{tm:min},
+@code{tm:sec}, @code{tm:isdst}.  The values can be outside their usual
+ranges.  For example @code{tm:hour} normally goes up to 23, but a
+value say 33 would mean 9 the following day.
+
+@code{tm:isdst} in @var{sbd-time} says whether the time given is with
+daylight savings or not.  This is ignored if @var{zone} doesn't have
+any daylight savings adjustment amount.
+
+The broken down time in the return normalizes the values of
+@var{sbd-time} by bringing them into their usual ranges, and using the
+actual daylight savings rule for that time in @var{zone} (which may
+differ from what @var{sbd-time} had).  The easiest way to think of
+this is that @var{sbd-time} plus @var{zone} converts to the integer
+UTC time, then a @code{localtime} is applied to get the normal
+presentation of that time, in @var{zone}.
 @end deffn
 
 @deffn {Scheme Procedure} tzset
@@ -1169,21 +1245,46 @@ since it's done automatically by other procedures that depend on the
 timezone.
 @end deffn
 
-@deffn {Scheme Procedure} strftime format stime
-@deffnx {C Function} scm_strftime (format, stime)
+@deffn {Scheme Procedure} strftime format tm
+@deffnx {C Function} scm_strftime (format, tm)
 @cindex time formatting
-Formats a time specification @var{time} using @var{template}.  @var{time}
-is an object with time components in the form returned by @code{localtime}
-or @code{gmtime}.  @var{template} is a string which can include formatting
-specifications introduced by a @samp{%} character.  The formatting of
-month and day names is dependent on the current locale.  The value returned
-is the formatted string.
-@xref{Formatting Calendar Time, , , libc, The GNU C Library Reference Manual}.
+Return a string which is broken-down time structure @var{tm} formatted
+according to the given @var{format} string.
+
+@var{format} contains field specifications introduced by a @samp{%}
+character.  See @ref{Formatting Calendar Time,,, libc, The GNU C
+Library Reference Manual}, or @samp{man 3 strftime}, for the available
+formatting.
 
 @lisp
 (strftime "%c" (localtime (current-time)))
 @result{} "Mon Mar 11 20:17:43 2002"
 @end lisp
+
+If @code{setlocale} has been called (@pxref{Locales}), month and day
+names are from the current locale and in the locale character set.
+
+Note that @samp{%Z} might print the @code{tm:zone} in @var{tm} or it
+might print just the current zone (@code{tzset} above).  A GNU system
+prints @code{tm:zone}, a strict C99 system like NetBSD prints the
+current zone.  Perhaps in the future Guile will try to get
+@code{tm:zone} used always.
+@c
+@c  The issue in the above is not just whether tm_zone exists in
+@c  struct tm, but whether libc feels it should read it.  Being a
+@c  non-C99 field, a strict C99 program won't know to set it, quite
+@c  likely leaving garbage there.  NetBSD, which has the field,
+@c  therefore takes the view that it mustn't read it.  See the PR
+@c  about this at
+@c
+@c      http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=21722
+@c
+@c  Uniformly making tm:zone used on all systems (all those which have
+@c  %Z at all of course) might be nice (either mung TZ and tzset, or
+@c  mung tzname[]).  On the other hand it would make us do more than
+@c  C99 says, and we really don't want to get intimate with the gory
+@c  details of libc time funcs, no more than can be helped.
+@c
 @end deffn
 
 @deffn {Scheme Procedure} strptime format string
@@ -1252,15 +1353,72 @@ included but subprocesses are not.
 
 @deffn {Scheme Procedure} program-arguments
 @deffnx {Scheme Procedure} command-line
+@deffnx {Scheme Procedure} set-program-arguments
 @deffnx {C Function} scm_program_arguments ()
+@deffnx {C Function} scm_set_program_arguments_scm (lst)
 @cindex command line
 @cindex program arguments
-Return the list of command line arguments passed to Guile, as a list of
-strings.  The list includes the invoked program name, which is usually
-@code{"guile"}, but excludes switches and parameters for command line
-options like @code{-e} and @code{-l}.
+Get the command line arguments passed to Guile, or set new arguments.
+
+The arguments are a list of strings, the first of which is the invoked
+program name.  This is just @nicode{"guile"} (or the executable path)
+when run interactively, or it's the script name when running a script
+with @option{-s} (@pxref{Invoking Guile}).
+
+@example
+guile -L /my/extra/dir -s foo.scm abc def
+
+(program-arguments) @result{} ("foo.scm" "abc" "def")
+@end example
+
+@code{set-program-arguments} allows a library module or similar to
+modify the arguments, for example to strip options it recognises,
+leaving the rest for the mainline.
+
+The argument list is held in a fluid, which means it's separate for
+each thread.  Neither the list nor the strings within it are copied at
+any point and normally should not be mutated.
+
+The two names @code{program-arguments} and @code{command-line} are an
+historical accident, they both do exactly the same thing.  The name
+@code{scm_set_program_arguments_scm} has an extra @code{_scm} on the
+end to avoid clashing with the C function below.
 @end deffn
 
+@deftypefn {C Function} void scm_set_program_arguments (int argc, char **argv, char *first)
+@cindex command line
+@cindex program arguments
+Set the list of command line arguments for @code{program-arguments}
+and @code{command-line} above.
+
+@var{argv} is an array of null-terminated strings, as in a C
+@code{main} function.  @var{argc} is the number of strings in
+@var{argv}, or if it's negative then a @code{NULL} in @var{argv} marks
+its end.
+
+@var{first} is an extra string put at the start of the arguments, or
+@code{NULL} for no such extra.  This is a convenient way to pass the
+program name after advancing @var{argv} to strip option arguments.
+Eg.@:
+
+@example
+@{
+  char *progname = argv[0];
+  for (argv++; argv[0] != NULL && argv[0][0] == '-'; argv++)
+    @{
+      /* munch option ... */
+    @}
+  /* remaining args for scheme level use */
+  scm_set_program_arguments (-1, argv, progname);
+@}
+@end example
+
+This sort of thing is often done at startup under
+@code{scm_boot_guile} with options handled at the C level removed.
+The given strings are all copied, so the C data is not accessed again
+once @code{scm_set_program_arguments} returns.
+@end deftypefn
+
 @deffn {Scheme Procedure} getenv nam
 @deffnx {C Function} scm_getenv (nam)
 @cindex environment
@@ -1563,10 +1721,28 @@ Example: (system* "echo" "foo" "bar")
 @end deffn
 
 @deffn {Scheme Procedure} primitive-exit [status]
+@deffnx {Scheme Procedure} primitive-_exit [status]
 @deffnx {C Function} scm_primitive_exit (status)
-Terminate the current process without unwinding the Scheme stack.
-This is would typically be useful after a fork.  The exit status
-is @var{status} if supplied, otherwise zero.
+@deffnx {C Function} scm_primitive__exit (status)
+Terminate the current process without unwinding the Scheme stack.  The
+exit status is @var{status} if supplied, otherwise zero.
+
+@code{primitive-exit} uses the C @code{exit} function and hence runs
+usual C level cleanups (flush output streams, call @code{atexit}
+functions, etc, see @ref{Normal Termination,,, libc, The GNU C Library
+Reference Manual})).
+
+@code{primitive-_exit} is the @code{_exit} system call
+(@pxref{Termination Internals,,, libc, The GNU C Library Reference
+Manual}).  This terminates the program immediately, with neither
+Scheme-level nor C-level cleanups.
+
+The typical use for @code{primitive-_exit} is from a child process
+created with @code{primitive-fork}.  For example in a Gdk program the
+child process inherits the X server connection and a C-level
+@code{atexit} cleanup which will close that connection.  But closing
+in the child would upset the protocol in the parent, so
+@code{primitive-_exit} should be used to exit without that.
 @end deffn
 
 @deffn {Scheme Procedure} execl filename . args
@@ -1665,7 +1841,13 @@ specified processes.
 @subsection Signals
 @cindex signal
 
-Procedures to raise, handle and wait for signals.
+The following procedures raise, handle and wait for signals.
+
+Scheme code signal handlers are run via a system async (@pxref{System
+asyncs}), so they're called in the handler's thread at the next safe
+opportunity.  Generally this is after any currently executing
+primitive procedure finishes (which could be a long time for
+primitives that wait for an external event).
 
 @deffn {Scheme Procedure} kill pid sig
 @deffnx {C Function} scm_kill (pid, sig)
@@ -1732,10 +1914,31 @@ procedure has been specified, that procedure will run in the given
 @var{thread}.  When no thread has been given, the thread that made this
 call to @code{sigaction} is used.
 
-Flags can optionally be specified for the new handler (@code{SA_RESTART}
-will always be added if it's available and the system is using
-restartable system calls.)  The return value is a pair with information
-about the old handler as described above.
+@var{flags} is a @code{logior} (@pxref{Bitwise Operations}) of the
+following (where provided by the system), or @code{0} for none.
+
+@defvar SA_NOCLDSTOP
+By default, @code{SIGCHLD} is signalled when a child process stops
+(ie.@: receives @code{SIGSTOP}), and when a child process terminates.
+With the @code{SA_NOCLDSTOP} flag, @code{SIGCHLD} is only signalled
+for termination, not stopping.
+
+@code{SA_NOCLDSTOP} has no effect on signals other than
+@code{SIGCHLD}.
+@end defvar
+
+@defvar SA_RESTART
+If a signal occurs while in a system call, deliver the signal then
+restart the system call (as opposed to returning an @code{EINTR} error
+from that call).
+
+Guile always enables this flag where available, no matter what
+@var{flags} are specified.  This avoids spurious error returns in low
+level operations.
+@end defvar
+
+The return value is a pair with information about the old handler as
+described above.
 
 This interface does not provide access to the ``signal blocking''
 facility.  Maybe this is not needed, since the thread support may
@@ -1769,47 +1972,72 @@ action is to either terminate the current process or invoke a
 handler procedure.  The return value is unspecified.
 @end deffn
 
-@deffn {Scheme Procedure} sleep i
-@deffnx {C Function} scm_sleep (i)
-Wait for the given number of seconds (an integer) or until a signal
-arrives.  The return value is zero if the time elapses or the number
-of seconds remaining otherwise.
-@end deffn
-
-@deffn {Scheme Procedure} usleep i
-@deffnx {C Function} scm_usleep (i)
-Sleep for @var{i} microseconds.  @code{usleep} is not available on
-all platforms. [FIXME: so what happens when it isn't?]
-@end deffn
-
-@deffn {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds value_seconds value_microseconds
-@deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, value_seconds, value_microseconds)
-Set the timer specified by @var{which_timer} according to the given
-@var{interval_seconds}, @var{interval_microseconds},
-@var{value_seconds}, and @var{value_microseconds} values.
-
-Return information about the timer's previous setting.
+@deffn {Scheme Procedure} sleep secs
+@deffnx {Scheme Procedure} usleep usecs
+@deffnx {C Function} scm_sleep (secs)
+@deffnx {C Function} scm_usleep (usecs)
+Wait the given period @var{secs} seconds or @var{usecs} microseconds
+(both integers).  If a signal arrives the wait stops and the return
+value is the time remaining, in seconds or microseconds respectively.
+If the period elapses with no signal the return is zero.
 
-The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},
-and @code{ITIMER_PROF}.
+On most systems the process scheduler is not microsecond accurate and
+the actual period slept by @code{usleep} might be rounded to a system
+clock tick boundary, which might be 10 milliseconds for instance.
 
-The return value will be a list of two cons pairs representing the
-current state of the given timer.  The first pair is the seconds and
-microseconds of the timer @code{it_interval}, and the second pair is
-the seconds and microseconds of the timer @code{it_value}.
+See @code{scm_std_sleep} and @code{scm_std_usleep} for equivalents at
+the C level (@pxref{Blocking}).
 @end deffn
 
 @deffn {Scheme Procedure} getitimer which_timer
+@deffnx {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds periodic_seconds periodic_microseconds
 @deffnx {C Function} scm_getitimer (which_timer)
-Return information about the timer specified by @var{which_timer}.
+@deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, periodic_seconds, periodic_microseconds)
+Get or set the periods programmed in certain system timers.  These
+timers have a current interval value which counts down and on reaching
+zero raises a signal.  An optional periodic value can be set to
+restart from there each time, for periodic operation.
+@var{which_timer} is one of the following values
+
+@defvar ITIMER_REAL
+A real-time timer, counting down elapsed real time.  At zero it raises
+@code{SIGALRM}.  This is like @code{alarm} above, but with a higher
+resolution period.
+@end defvar 
+
+@defvar ITIMER_VIRTUAL
+A virtual-time timer, counting down while the current process is
+actually using CPU.  At zero it raises @code{SIGVTALRM}.
+@end defvar 
+
+@defvar ITIMER_PROF
+A profiling timer, counting down while the process is running (like
+@code{ITIMER_VIRTUAL}) and also while system calls are running on the
+process's behalf.  At zero it raises a @code{SIGPROF}.
+
+This timer is intended for profiling where a program is spending its
+time (by looking where it is when the timer goes off).
+@end defvar 
+
+@code{getitimer} returns the current timer value and its programmed
+restart value, as a list containing two pairs.  Each pair is a time in
+seconds and microseconds: @code{((@var{interval_secs}
+. @var{interval_usecs}) (@var{periodic_secs}
+. @var{periodic_usecs}))}.
+
+@code{setitimer} sets the timer values similarly, in seconds and
+microseconds (which must be integers).  The periodic value can be zero
+to have the timer run down just once.  The return value is the timer's
+previous setting, in the same form as @code{getitimer} returns.
 
-The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},
-and @code{ITIMER_PROF}.
+@example
+(setitimer ITIMER_REAL
+           5 500000     ;; first SIGALRM in 5.5 seconds time
+           2 0)         ;; then repeat every 2 seconds
+@end example
 
-The return value will be a list of two cons pairs representing the
-current state of the given timer.  The first pair is the seconds and
-microseconds of the timer @code{it_interval}, and the second pair is
-the seconds and microseconds of the timer @code{it_value}.
+Although the timers are programmed in microseconds, the actual
+accuracy might not be that high.
 @end deffn
 
 
@@ -1971,10 +2199,11 @@ the garbage collector pick them up at some later time.
 @cindex network
 
 @menu
-* Network Address Conversion::
-* Network Databases::
-* Network Sockets and Communication::
-* Internet Socket Examples::
+* Network Address Conversion::  
+* Network Databases::           
+* Network Socket Address::      
+* Network Sockets and Communication::  
+* Internet Socket Examples::    
 @end menu
 
 @node Network Address Conversion
@@ -1988,18 +2217,28 @@ between numeric and string formats.
 @cindex IPv4
 
 An IPv4 Internet address is a 4-byte value, represented in Guile as an
-integer in network byte order (meaning the first byte is the most
-significant in the number).
+integer in host byte order, so that say ``0.0.0.1'' is 1, or
+``1.0.0.0'' is 16777216.
 
-@defvar INADDR_LOOPBACK
-The address of the local host using the loopback device, ie.@:
-@samp{127.0.0.1}.
+Some underlying C functions use network byte order for addresses,
+Guile converts as necessary so that at the Scheme level its host byte
+order everywhere.
+
+@defvar INADDR_ANY
+For a server, this can be used with @code{bind} (@pxref{Network
+Sockets and Communication}) to allow connections from any interface on
+the machine.
 @end defvar
 
 @defvar INADDR_BROADCAST
 The broadcast address on the local network.
 @end defvar
 
+@defvar INADDR_LOOPBACK
+The address of the local host using the loopback device, ie.@:
+@samp{127.0.0.1}.
+@end defvar
+
 @c  INADDR_NONE is defined in the code, but serves no purpose.
 @c  inet_addr() returns it as an error indication, but that function
 @c  isn't provided, for the good reason that inet_aton() does the same
@@ -2065,11 +2304,12 @@ Make an IPv4 Internet address by combining the network number
 @subsubheading IPv6 Address Conversion
 @cindex IPv6
 
+An IPv6 Internet address is a 16-byte value, represented in Guile as
+an integer in host byte order, so that say ``::1'' is 1.
+
 @deffn {Scheme Procedure} inet-ntop family address
 @deffnx {C Function} scm_inet_ntop (family, address)
-Convert a network address into a printable string.
-Note that unlike the C version of this function,
-the input is an integer with normal host byte ordering.
+Convert a network address from an integer to a printable string.
 @var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,
 
 @lisp
@@ -2081,11 +2321,9 @@ ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
 
 @deffn {Scheme Procedure} inet-pton family address
 @deffnx {C Function} scm_inet_pton (family, address)
-Convert a string containing a printable network address to
-an integer address.  Note that unlike the C version of this
-function,
-the result is an integer with normal host byte ordering.
-@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,
+Convert a string containing a printable network address to an integer
+address.  @var{family} can be @code{AF_INET} or @code{AF_INET6}.
+E.g.,
 
 @lisp
 (inet-pton AF_INET "127.0.0.1") @result{} 2130706433
@@ -2120,14 +2358,16 @@ The ``official'' hostname for @var{host}.
 A list of aliases for @var{host}.
 @end deffn
 @deffn {Scheme Procedure} hostent:addrtype host
-The host address type.  For hosts with Internet addresses, this will
-return @code{AF_INET}.
+The host address type, one of the @code{AF} constants, such as
+@code{AF_INET} or @code{AF_INET6}.
 @end deffn
 @deffn {Scheme Procedure} hostent:length host
 The length of each address for @var{host}, in bytes.
 @end deffn
 @deffn {Scheme Procedure} hostent:addr-list host
-The list of network addresses associated with @var{host}.
+The list of network addresses associated with @var{host}.  For
+@code{AF_INET} these are integer IPv4 address (@pxref{Network Address
+Conversion}).
 @end deffn
 
 The following procedures are used to search the host database:
@@ -2370,6 +2610,119 @@ If @var{stayopen} is omitted, this is equivalent to @code{endservent}.
 Otherwise it is equivalent to @code{setservent stayopen}.
 @end deffn
 
+
+@node Network Socket Address
+@subsubsection Network Socket Address
+@cindex socket address
+@cindex network socket address
+@tpindex Socket address
+
+A @dfn{socket address} object identifies a socket endpoint for
+communication.  In the case of @code{AF_INET} for instance, the socket
+address object comprises the host address (or interface on the host)
+and a port number which specifies a particular open socket in a
+running client or server process.  A socket address object can be
+created with,
+
+@deffn {Scheme Procedure} make-socket-address AF_INET ipv4addr port
+@deffnx {Scheme Procedure} make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]]
+@deffnx {Scheme Procedure} make-socket-address AF_UNIX path
+@deffnx {C Function} scm_make_socket_address family address arglist
+Return a new socket address object.  The first argument is the address
+family, one of the @code{AF} constants, then the arguments vary
+according to the family.
+
+For @code{AF_INET} the arguments are an IPv4 network address number
+(@pxref{Network Address Conversion}), and a port number.
+
+For @code{AF_INET6} the arguments are an IPv6 network address number
+and a port number.  Optional @var{flowinfo} and @var{scopeid}
+arguments may be given (both integers, default 0).
+
+For @code{AF_UNIX} the argument is a filename (a string).
+
+The C function @code{scm_make_socket_address} takes the @var{family}
+and @var{address} arguments directly, then @var{arglist} is a list of
+further arguments, being the port for IPv4, port and optional flowinfo
+and scopeid for IPv6, or the empty list @code{SCM_EOL} for Unix
+domain.
+@end deffn
+
+@noindent
+The following functions access the fields of a socket address object,
+
+@deffn {Scheme Procedure} sockaddr:fam sa
+Return the address family from socket address object @var{sa}.  This
+is one of the @code{AF} constants (eg. @code{AF_INET}).
+@end deffn
+
+@deffn {Scheme Procedure} sockaddr:path sa
+For an @code{AF_UNIX} socket address object @var{sa}, return the
+filename.
+@end deffn
+
+@deffn {Scheme Procedure} sockaddr:addr sa
+For an @code{AF_INET} or @code{AF_INET6} socket address object
+@var{sa}, return the network address number.
+@end deffn
+
+@deffn {Scheme Procedure} sockaddr:port sa
+For an @code{AF_INET} or @code{AF_INET6} socket address object
+@var{sa}, return the port number.
+@end deffn
+
+@deffn {Scheme Procedure} sockaddr:flowinfo sa
+For an @code{AF_INET6} socket address object @var{sa}, return the
+flowinfo value.
+@end deffn
+
+@deffn {Scheme Procedure} sockaddr:scopeid sa
+For an @code{AF_INET6} socket address object @var{sa}, return the
+scope ID value.
+@end deffn
+
+@tpindex @code{struct sockaddr}
+@tpindex @code{sockaddr}
+The functions below convert to and from the C @code{struct sockaddr}
+(@pxref{Address Formats,,, libc, The GNU C Library Reference Manual}).
+That structure is a generic type, an application can cast to or from
+@code{struct sockaddr_in}, @code{struct sockaddr_in6} or @code{struct
+sockaddr_un} according to the address family.
+
+In a @code{struct sockaddr} taken or returned, the byte ordering in
+the fields follows the C conventions (@pxref{Byte Order,, Byte Order
+Conversion, libc, The GNU C Library Reference Manual}).  This means
+network byte order for @code{AF_INET} host address
+(@code{sin_addr.s_addr}) and port number (@code{sin_port}), and
+@code{AF_INET6} port number (@code{sin6_port}).  But at the Scheme
+level these values are taken or returned in host byte order, so the
+port is an ordinary integer, and the host address likewise is an
+ordinary integer (as described in @ref{Network Address Conversion}).
+
+@deftypefn {C Function} {struct sockaddr *} scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize)
+Return a newly-@code{malloc}ed @code{struct sockaddr} created from
+arguments like those taken by @code{scm_make_socket_address} above.
+
+The size (in bytes) of the @code{struct sockaddr} return is stored
+into @code{*@var{outsize}}.  An application must call @code{free} to
+release the returned structure when no longer required.
+@end deftypefn
+
+@deftypefn {C Function} SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size)
+Return a Scheme socket address object from the C @var{address}
+structure.  @var{address_size} is the size in bytes of @var{address}.
+@end deftypefn
+
+@deftypefn {C Function} {struct sockaddr *} scm_to_sockaddr (SCM address, size_t *address_size)
+Return a newly-@code{malloc}ed @code{struct sockaddr} from a Scheme
+level socket address object.
+
+The size (in bytes) of the @code{struct sockaddr} return is stored
+into @code{*@var{outsize}}.  An application must call @code{free} to
+release the returned structure when no longer required.
+@end deftypefn
+
+
 @node Network Sockets and Communication
 @subsubsection Network Sockets and Communication
 @cindex socket
@@ -2384,11 +2737,10 @@ Most systems have limits on how many files and sockets can be open, so
 it's strongly recommended that socket ports be closed explicitly when
 no longer required (@pxref{Ports}).
 
-The convention used for ``host'' vs.@: ``network'' addresses is that
-addresses are always held in host order at the Scheme level.  The
-procedures in this section automatically convert between host and
-network order when required.  The arguments and return values are thus
-in host order.
+Some of the underlying C functions take values in network byte order,
+but the convention in Guile is that at the Scheme level everything is
+ordinary host byte order and conversions are made automatically where
+necessary.
 
 @deffn {Scheme Procedure} socket family style proto
 @deffnx {C Function} scm_socket (family, style, proto)
@@ -2433,43 +2785,85 @@ for @var{proto}.
 @end deffn
 
 @deffn {Scheme Procedure} getsockopt sock level optname
+@deffnx {Scheme Procedure} setsockopt sock level optname value
 @deffnx {C Function} scm_getsockopt (sock, level, optname)
-Return the value of a particular socket option for the socket
-port @var{sock}.  @var{level} is an integer code for type of
-option being requested, e.g., @code{SOL_SOCKET} for
-socket-level options.  @var{optname} is an integer code for the
-option required and should be specified using one of the
-symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
+@deffnx {C Function} scm_setsockopt (sock, level, optname, value)
+Get or set an option on socket port @var{sock}.  @code{getsockopt}
+returns the current value.  @code{setsockopt} sets a value and the
+return is unspecified.
+
+@var{level} is an integer specifying a protocol layer, either
+@code{SOL_SOCKET} for socket level options, or a protocol number from
+the @code{IPPROTO} constants or @code{getprotoent} (@pxref{Network
+Databases}).
+
+@defvar SOL_SOCKET
+@defvarx IPPROTO_IP
+@defvarx IPPROTO_TCP
+@defvarx IPPROTO_UDP
+@end defvar
 
-The returned value is typically an integer but @code{SO_LINGER}
-returns a pair of integers.
-@end deffn
+@var{optname} is an integer specifying an option within the protocol
+layer.
+
+For @code{SOL_SOCKET} level the following @var{optname}s are defined
+(when provided by the system).  For their meaning see
+@ref{Socket-Level Options,,, libc, The GNU C Library Reference
+Manual}, or @command{man 7 socket}.
+
+@defvar SO_DEBUG
+@defvarx SO_REUSEADDR
+@defvarx SO_STYLE
+@defvarx SO_TYPE
+@defvarx SO_ERROR
+@defvarx SO_DONTROUTE
+@defvarx SO_BROADCAST
+@defvarx SO_SNDBUF
+@defvarx SO_RCVBUF
+@defvarx SO_KEEPALIVE
+@defvarx SO_OOBINLINE
+@defvarx SO_NO_CHECK
+@defvarx SO_PRIORITY
+The @var{value} taken or returned is an integer.
+@end defvar
 
-@deffn {Scheme Procedure} setsockopt sock level optname value
-@deffnx {C Function} scm_setsockopt (sock, level, optname, value)
-Set the value of a particular socket option for the socket
-port @var{sock}.  @var{level} is an integer code for type of option
-being set, e.g., @code{SOL_SOCKET} for socket-level options.
-@var{optname} is an
-integer code for the option to set and should be specified using one of
-the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
-@var{value} is the value to which the option should be set.  For
-most options this must be an integer, but for @code{SO_LINGER} it must
-be a pair.
+@defvar SO_LINGER
+The @var{value} taken or returned is a pair of integers
+@code{(@var{ENABLE} . @var{TIMEOUT})}.  On old systems without timeout
+support (ie.@: without @code{struct linger}), only @var{ENABLE} has an
+effect but the value in Guile is always a pair.
+@end defvar
 
-The return value is unspecified.
+@c  Note that we refer only to ``man ip'' here.  On GNU/Linux it's
+@c  ``man 7 ip'' but on NetBSD it's ``man 4 ip''.
+@c 
+For IP level (@code{IPPROTO_IP}) the following @var{optname}s are
+defined (when provided by the system).  See @command{man ip} for what
+they mean.
+
+@defvar IP_ADD_MEMBERSHIP
+@defvarx IP_DROP_MEMBERSHIP
+These can be used only with @code{setsockopt}, not @code{getsockopt}.
+@var{value} is a pair @code{(@var{MULTIADDR} . @var{INTERFACEADDR})}
+of integer IPv4 addresses (@pxref{Network Address Conversion}).
+@var{MULTIADDR} is a multicast address to be added to or dropped from
+the interface @var{INTERFACEADDR}.  @var{INTERFACEADDR} can be
+@code{INADDR_ANY} to have the system select the interface.
+@var{INTERFACEADDR} can also be an interface index number, on systems
+supporting that.
+@end defvar
 @end deffn
 
 @deffn {Scheme Procedure} shutdown sock how
 @deffnx {C Function} scm_shutdown (sock, how)
-Sockets can be closed simply by using @code{close-port}. The
+Sockets can be closed simply by using @code{close-port}.  The
 @code{shutdown} procedure allows reception or transmission on a
 connection to be shut down individually, according to the parameter
 @var{how}:
 
 @table @asis
 @item 0
-Stop receiving data for this socket.  If further data arrives,  reject it.
+Stop receiving data for this socket.  If further data arrives, reject it.
 @item 1
 Stop trying to transmit data from this socket.  Discard any
 data waiting to be sent.  Stop looking for acknowledgement of
@@ -2481,76 +2875,41 @@ Stop both reception and transmission.
 The return value is unspecified.
 @end deffn
 
-@deffn {Scheme Procedure} connect sock fam address . args
+@deffn {Scheme Procedure} connect sock sockaddr
+@deffnx {Scheme Procedure} connect sock AF_INET ipv4addr port
+@deffnx {Scheme Procedure} connect sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
+@deffnx {Scheme Procedure} connect sock AF_UNIX path
 @deffnx {C Function} scm_connect (sock, fam, address, args)
-Initiate a connection from a socket using a specified address
-family to the address
-specified by @var{address} and possibly @var{args}.
-The format required for @var{address}
-and @var{args} depends on the family of the socket.
-
-For a socket of family @code{AF_UNIX},
-only @var{address} is specified and must be a string with the
-filename where the socket is to be created.
-
-For a socket of family @code{AF_INET},
-@var{address} must be an integer IPv4 host address and
-@var{args} must be a single integer port number.
-
-For a socket of family @code{AF_INET6},
-@var{address} must be an integer IPv6 host address and
-@var{args} may be up to three integers:
-port [flowinfo] [scope_id],
-where flowinfo and scope_id default to zero.
+Initiate a connection on socket port @var{sock} to a given address.
+The destination is either a socket address object, or arguments the
+same as @code{make-socket-address} would take to make such an object
+(@pxref{Network Socket Address}).  The return value is unspecified.
 
-The return value is unspecified.
+@example
+(connect sock AF_INET INADDR_LOCALHOST 23)
+(connect sock (make-socket-address AF_INET INADDR_LOCALHOST 23))
+@end example
 @end deffn
 
-@deffn {Scheme Procedure} bind sock fam address . args
+@deffn {Scheme Procedure} bind sock sockaddr
+@deffnx {Scheme Procedure} bind sock AF_INET ipv4addr port
+@deffnx {Scheme Procedure} bind sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
+@deffnx {Scheme Procedure} bind sock AF_UNIX path
 @deffnx {C Function} scm_bind (sock, fam, address, args)
-Assign an address to the socket port @var{sock}.
-Generally this only needs to be done for server sockets,
-so they know where to look for incoming connections.  A socket
-without an address will be assigned one automatically when it
-starts communicating.
+Bind socket port @var{sock} to the given address.  The address is
+either a socket address object, or arguments the same as
+@code{make-socket-address} would take to make such an object
+(@pxref{Network Socket Address}).  The return value is unspecified.
 
-The format of @var{address} and @var{args} depends
-on the family of the socket.
+Generally a socket is only explicitly bound to a particular address
+when making a server, ie. to listen on a particular port.  For an
+outgoing connection the system will assign a local address
+automatically, if not already bound.
 
-For a socket of family @code{AF_UNIX}, only @var{address}
-is specified and must be a string with the filename where
-the socket is to be created.
-
-For a socket of family @code{AF_INET}, @var{address}
-must be an integer IPv4 address and @var{args}
-must be a single integer port number.
-
-The values of the following variables can also be used for
-@var{address}:
-
-@defvar INADDR_ANY
-Allow connections from any address.
-@end defvar
-
-@defvar INADDR_LOOPBACK
-The address of the local host using the loopback device.
-@end defvar
-
-@defvar INADDR_BROADCAST
-The broadcast address on the local network.
-@end defvar
-
-@defvar INADDR_NONE
-No address.
-@end defvar
-
-For a socket of family @code{AF_INET6}, @var{address}
-must be an integer IPv6 address and @var{args}
-may be up to three integers:
-port [flowinfo] [scope_id],
-where flowinfo and scope_id default to zero.
-
-The return value is unspecified.
+@example
+(bind sock AF_INET INADDR_ANY 12345)
+(bind sock (make-socket-address AF_INET INADDR_ANY 12345))
+@end example
 @end deffn
 
 @deffn {Scheme Procedure} listen sock backlog
@@ -2567,55 +2926,41 @@ The return value is unspecified.
 
 @deffn {Scheme Procedure} accept sock
 @deffnx {C Function} scm_accept (sock)
-Accept a connection on a bound, listening socket.
-If there
-are no pending connections in the queue, wait until
-one is available unless the non-blocking option has been
-set on the socket.
+Accept a connection from socket port @var{sock} which has been enabled
+for listening with @code{listen} above.  If there are no incoming
+connections in the queue, wait until one is available (unless
+@code{O_NONBLOCK} has been set on the socket, @pxref{Ports and File
+Descriptors,@code{fcntl}}).
 
-The return value is a
-pair in which the @acronym{CAR} is a new socket port for the
-connection and
-the @acronym{CDR} is an object with address information about the
-client which initiated the connection.
+The return value is a pair.  The @code{car} is a new socket port,
+connected and ready to communicate.  The @code{cdr} is a socket
+address object (@pxref{Network Socket Address}) which is where the
+remote connection is from (like @code{getpeername} below).
 
-@var{sock} does not become part of the
-connection and will continue to accept new requests.
-@end deffn
-
-The following functions take a socket address object, as returned
-by @code{accept} and other procedures, and return a selected component.
-
-@deffn {Scheme Procedure} sockaddr:fam sa
-The socket family, typically equal to the value of @code{AF_UNIX} or
-@code{AF_INET}.
-@end deffn
-@deffn {Scheme Procedure} sockaddr:path sa
-If the socket family is @code{AF_UNIX}, returns the path of the
-filename the socket is based on.
-@end deffn
-@deffn {Scheme Procedure} sockaddr:addr sa
-If the socket family is @code{AF_INET}, returns the Internet host
-address.
-@end deffn
-@deffn {Scheme Procedure} sockaddr:port sa
-If the socket family is @code{AF_INET}, returns the Internet port
-number.
+All communication takes place using the new socket returned.  The
+given @var{sock} remains bound and listening, and @code{accept} may be
+called on it again to get another incoming connection when desired.
 @end deffn
 
 @deffn {Scheme Procedure} getsockname sock
 @deffnx {C Function} scm_getsockname (sock)
-Return the address of @var{sock}, in the same form as the
-object returned by @code{accept}.  On many systems the address
-of a socket in the @code{AF_FILE} namespace cannot be read.
+Return a socket address object which is the where @var{sock} is bound
+locally.  @var{sock} may have obtained its local address from
+@code{bind} (above), or if a @code{connect} is done with an otherwise
+unbound socket (which is usual) then the system will have assigned an
+address.
+
+Note that on many systems the address of a socket in the
+@code{AF_UNIX} namespace cannot be read.
 @end deffn
 
 @deffn {Scheme Procedure} getpeername sock
 @deffnx {C Function} scm_getpeername (sock)
-Return the address that @var{sock}
-is connected to, in the same form as the object returned by
-@code{accept}.  On many systems the address of a socket in the
-@code{AF_FILE} namespace cannot be read.
+Return a socket address object which is where @var{sock} is connected
+to, ie. the remote endpoint.
+
+Note that on many systems the address of a socket in the
+@code{AF_UNIX} namespace cannot be read.
 @end deffn
 
 @deffn {Scheme Procedure} recv! sock buf [flags]
@@ -2664,45 +3009,53 @@ any unflushed buffered port data is ignored.
 
 @deffn {Scheme Procedure} recvfrom! sock str [flags [start [end]]]
 @deffnx {C Function} scm_recvfrom (sock, str, flags, start, end)
-Return data from the socket port @var{sock} and also
-information about where the data was received from.
-@var{sock} must already be bound to the address from which
-data is to be received.  @code{str}, is a string into which the
-data will be written.  The size of @var{str} limits the amount
-of data which can be received: in the case of packet protocols,
-if a packet larger than this limit is encountered then some
-data will be irrevocably lost.
+Receive data from socket port @var{sock}, returning the originating
+address as well as the data.  This function is usually for datagram
+sockets, but can be used on stream-oriented sockets too.
+
+The data received is stored in the given @var{str}, the whole string
+or just the region between the optional @var{start} and @var{end}
+positions.  The size of @var{str} limits the amount of data which can
+be received.  For datagram protocols if a packet larger than this is
+received then excess bytes are irrevocably lost.
+
+The return value is a pair.  The @code{car} is the number of bytes
+read.  The @code{cdr} is a socket address object (@pxref{Network
+Socket Address}) which is where the data came from, or @code{#f} if
+the origin is unknown.
 
 @vindex MSG_OOB
 @vindex MSG_PEEK
 @vindex MSG_DONTROUTE
-The optional @var{flags} argument is a value or bitwise OR of
-@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
+The optional @var{flags} argument is a or bitwise-OR (@code{logior})
+of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
 
-The value returned is a pair: the @acronym{CAR} is the number of
-bytes read from the socket and the @acronym{CDR} an address object
-in the same form as returned by @code{accept}.  The address
-will given as @code{#f} if not available, as is usually the
-case for stream sockets.
+Data is read directly from the socket file descriptor, any buffered
+port data is ignored.
 
-The @var{start} and @var{end} arguments specify a substring of
-@var{str} to which the data should be written.
-
-Note that the data is read directly from the socket file
-descriptor: any unread buffered port data is ignored.
+@c  This was linux kernel 2.6.15 and glibc 2.3.6, not sure what any
+@c  specs are supposed to say about recvfrom threading.
+@c
+On a GNU/Linux system @code{recvfrom!} is not multi-threading, all
+threads stop while a @code{recvfrom!} call is in progress.  An
+application may need to use @code{select}, @code{O_NONBLOCK} or
+@code{MSG_DONTWAIT} to avoid this.
 @end deffn
 
-@deffn {Scheme Procedure} sendto sock message fam address . args_and_flags
+@deffn {Scheme Procedure} sendto sock message sockaddr [flags]
+@deffnx {Scheme Procedure} sendto sock message AF_INET ipv4addr port [flags]
+@deffnx {Scheme Procedure} sendto sock message AF_INET6 ipv6addr port [flowinfo [scopeid [flags]]]
+@deffnx {Scheme Procedure} sendto sock message AF_UNIX path [flags]
 @deffnx {C Function} scm_sendto (sock, message, fam, address, args_and_flags)
-Transmit the string @var{message} on the socket port
-@var{sock}.  The
-destination address is specified using the @var{fam},
-@var{address} and
-@var{args_and_flags} arguments, in a similar way to the
-@code{connect} procedure.  @var{args_and_flags} contains
-the usual connection arguments optionally followed by
-a flags argument, which is a value or
-bitwise OR of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
+Transmit the string @var{message} as a datagram on socket port
+@var{sock}.  The destination is specified either as a socket address
+object, or as arguments the same as would be taken by
+@code{make-socket-address} to create such an object (@pxref{Network
+Socket Address}).
+
+The destination address may be followed by an optional @var{flags}
+argument which is a @code{logior} (@pxref{Bitwise Operations}) of
+@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
 
 The value returned is the number of bytes transmitted --
 it's possible for
@@ -2809,19 +3162,19 @@ client.
   (newline)
 
   (while #t
-         (let* ((client-connection (accept s))
-                (client-details (cdr client-connection))
-                (client (car client-connection)))
-           (simple-format #t "Got new client connection: ~S"
-                          client-details)
-           (newline)
-           (simple-format #t "Client address: ~S"
-                          (gethostbyaddr
-                           (sockaddr:addr client-details)))
-           (newline)
-           ;; @r{Send back the greeting to the client port}
-           (display "Hello client\r\n" client)
-           (close client))))
+    (let* ((client-connection (accept s))
+           (client-details (cdr client-connection))
+           (client (car client-connection)))
+      (simple-format #t "Got new client connection: ~S"
+                     client-details)
+      (newline)
+      (simple-format #t "Client address: ~S"
+                     (gethostbyaddr
+                      (sockaddr:addr client-details)))
+      (newline)
+      ;; @r{Send back the greeting to the client port}
+      (display "Hello client\r\n" client)
+      (close client))))
 @end example
 
 
@@ -2838,7 +3191,7 @@ Return an object with some information about the computer
 system the program is running on.
 
 The following procedures accept an object as returned by @code{uname}
-and return a selected component.
+and return a selected component (all of which are strings).
 
 @deffn {Scheme Procedure} utsname:sysname un
 The name of the operating system.
@@ -2879,10 +3232,11 @@ specified.
 Get or set the current locale, used for various internationalizations.
 Locales are strings, such as @samp{sv_SE}.
 
-If @var{locale} is given then the locale for the given @var{category} is set
-and the new value returned.  If @var{locale} is not given then the
-current value is returned.  @var{category} should be one of the
-following values
+If @var{locale} is given then the locale for the given @var{category}
+is set and the new value returned.  If @var{locale} is not given then
+the current value is returned.  @var{category} should be one of the
+following values (@pxref{Locale Categories, Categories of Activities
+that Locales Affect,, libc, The GNU C Library Reference Manual}):
 
 @defvar LC_ALL
 @defvarx LC_COLLATE
@@ -2899,6 +3253,10 @@ categories based on standard environment variables (@code{LANG} etc).
 For full details on categories and locale names @pxref{Locales,,
 Locales and Internationalization, libc, The GNU C Library Reference
 Manual}.
+
+Note that @code{setlocale} affects locale settings for the whole
+process.  @xref{i18n Introduction, locale objects and
+@code{make-locale}}, for a thread-safe alternative.
 @end deffn
 
 @node Encryption
@@ -2909,12 +3267,13 @@ Please note that the procedures in this section are not suited for
 strong encryption, they are only interfaces to the well-known and
 common system library functions of the same name.  They are just as good
 (or bad) as the underlying functions, so you should refer to your system
-documentation before using them.
+documentation before using them (@pxref{crypt,, Encrypting Passwords,
+libc, The GNU C Library Reference Manual}).
 
 @deffn {Scheme Procedure} crypt key salt
 @deffnx {C Function} scm_crypt (key, salt)
-Encrypt @var{key} using @var{salt} as the salt value to the
-crypt(3) library call.
+Encrypt @var{key}, with the addition of @var{salt} (both strings),
+using the @code{crypt} C library call.
 @end deffn
 
 Although @code{getpass} is not an encryption procedure per se, it