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