Replace $letrec with $rec
[bpt/guile.git] / doc / ref / posix.texi
CommitLineData
2da09c3f
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
1ba05158 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007,
b9b88351 4@c 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
2da09c3f
MV
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
7381c1de 14 have different representations.
a0e07ba4
NJ
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
64de6db5
BT
214@deffn {Scheme Procedure} fdes->ports fdes
215@deffnx {C Function} scm_fdes_to_ports (fdes)
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
64de6db5
BT
233@deffn {Scheme Procedure} primitive-move->fdes port fdes
234@deffnx {C Function} scm_primitive_move_to_fdes (port, fdes)
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
64de6db5
BT
255@deffn {Scheme Procedure} fsync port_or_fd
256@deffnx {C Function} scm_fsync (port_or_fd)
a0e07ba4 257Copies any unwritten data for the specified output file descriptor to disk.
64de6db5 258If @var{port_or_fd} is a port, its buffer is flushed before the underlying
a0e07ba4
NJ
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
64de6db5
BT
405@deffn {Scheme Procedure} redirect-port old_port new_port
406@deffnx {C Function} scm_redirect_port (old_port, new_port)
a0e07ba4 407This procedure takes two ports and duplicates the underlying file
64de6db5
BT
408descriptor from @var{old_port} into @var{new_port}. The
409current file descriptor in @var{new_port} will be closed.
a0e07ba4
NJ
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
122f24cc
LC
473
474Only certain types of ports are supported, most importantly
475file ports.
a0e07ba4
NJ
476@end deffn
477
23f2b9a3 478@deffn {Scheme Procedure} fcntl port/fd cmd [value]
8f85c0c6 479@deffnx {C Function} scm_fcntl (object, cmd, value)
23f2b9a3
KR
480Apply @var{cmd} on @var{port/fd}, either a port or file descriptor.
481The @var{value} argument is used by the @code{SET} commands described
482below, it's an integer value.
a0e07ba4 483
1b09b607 484Values for @var{cmd} are:
a0e07ba4 485
2ce02471 486@defvar F_DUPFD
1b09b607
KR
487Duplicate the file descriptor, the same as @code{dup->fdes} above
488does.
2ce02471 489@end defvar
1b09b607 490
2ce02471 491@defvar F_GETFD
1b09b607
KR
492@defvarx F_SETFD
493Get or set flags associated with the file descriptor. The only flag
494is the following,
495
496@defvar FD_CLOEXEC
497``Close on exec'', meaning the file descriptor will be closed on an
498@code{exec} call (a successful such call). For example to set that
499flag,
500
501@example
502(fcntl port F_SETFD FD_CLOEXEC)
503@end example
23f2b9a3
KR
504
505Or better, set it but leave any other possible future flags unchanged,
506
507@example
508(fcntl port F_SETFD (logior FD_CLOEXEC
509 (fcntl port F_GETFD)))
510@end example
2ce02471 511@end defvar
2ce02471 512@end defvar
1b09b607 513
2ce02471 514@defvar F_GETFL
1b09b607
KR
515@defvarx F_SETFL
516Get or set flags associated with the open file. These flags are
517@code{O_RDONLY} etc described under @code{open} above.
518
519A common use is to set @code{O_NONBLOCK} on a network socket. The
520following sets that flag, and leaves other flags unchanged.
521
522@example
23f2b9a3
KR
523(fcntl sock F_SETFL (logior O_NONBLOCK
524 (fcntl sock F_GETFL)))
1b09b607 525@end example
2ce02471 526@end defvar
1b09b607 527
2ce02471 528@defvar F_GETOWN
1b09b607
KR
529@defvarx F_SETOWN
530Get or set the process ID of a socket's owner, for @code{SIGIO} signals.
2ce02471 531@end defvar
a0e07ba4
NJ
532@end deffn
533
8f85c0c6
NJ
534@deffn {Scheme Procedure} flock file operation
535@deffnx {C Function} scm_flock (file, operation)
f3dfb8ac 536@cindex file locking
a0e07ba4
NJ
537Apply or remove an advisory lock on an open file.
538@var{operation} specifies the action to be done:
2ce02471
NJ
539
540@defvar LOCK_SH
a0e07ba4
NJ
541Shared lock. More than one process may hold a shared lock
542for a given file at a given time.
2ce02471
NJ
543@end defvar
544@defvar LOCK_EX
a0e07ba4
NJ
545Exclusive lock. Only one process may hold an exclusive lock
546for a given file at a given time.
2ce02471
NJ
547@end defvar
548@defvar LOCK_UN
a0e07ba4 549Unlock the file.
2ce02471
NJ
550@end defvar
551@defvar LOCK_NB
67bcd110
KR
552Don't block when locking. This is combined with one of the other
553operations using @code{logior} (@pxref{Bitwise Operations}). If
554@code{flock} would block an @code{EWOULDBLOCK} error is thrown
555(@pxref{Conventions}).
2ce02471
NJ
556@end defvar
557
a0e07ba4 558The return value is not specified. @var{file} may be an open
85a9b4ed 559file descriptor or an open file descriptor port.
67bcd110
KR
560
561Note that @code{flock} does not lock files across NFS.
a0e07ba4
NJ
562@end deffn
563
8f85c0c6
NJ
564@deffn {Scheme Procedure} select reads writes excepts [secs [usecs]]
565@deffnx {C Function} scm_select (reads, writes, excepts, secs, usecs)
a0e07ba4 566This procedure has a variety of uses: waiting for the ability
85a9b4ed 567to provide input, accept output, or the existence of
a0e07ba4
NJ
568exceptional conditions on a collection of ports or file
569descriptors, or waiting for a timeout to occur.
d1447c71
LC
570
571When an error occurs, of if it is interrupted by a signal, this
572procedure throws a @code{system-error} exception
573(@pxref{Conventions, @code{system-error}}). In case of an
574interruption, the associated error number is @var{EINTR}.
a0e07ba4
NJ
575
576@var{reads}, @var{writes} and @var{excepts} can be lists or
577vectors, with each member a port or a file descriptor.
578The value returned is a list of three corresponding
579lists or vectors containing only the members which meet the
580specified requirement. The ability of port buffers to
581provide input or accept output is taken into account.
582Ordering of the input lists or vectors is not preserved.
583
584The optional arguments @var{secs} and @var{usecs} specify the
585timeout. Either @var{secs} can be specified alone, as
586either an integer or a real number, or both @var{secs} and
587@var{usecs} can be specified as integers, in which case
588@var{usecs} is an additional timeout expressed in
589microseconds. If @var{secs} is omitted or is @code{#f} then
590select will wait for as long as it takes for one of the other
591conditions to be satisfied.
592
593The scsh version of @code{select} differs as follows:
594Only vectors are accepted for the first three arguments.
595The @var{usecs} argument is not supported.
596Multiple values are returned instead of a list.
597Duplicates in the input vectors appear only once in output.
598An additional @code{select!} interface is provided.
599@end deffn
600
601@node File System
3229f68b 602@subsection File System
f3dfb8ac 603@cindex file system
a0e07ba4
NJ
604
605These procedures allow querying and setting file system attributes
606(such as owner,
607permissions, sizes and types of files); deleting, copying, renaming and
608linking files; creating and removing directories and querying their
609contents; syncing the file system and creating special files.
610
8f85c0c6
NJ
611@deffn {Scheme Procedure} access? path how
612@deffnx {C Function} scm_access (path, how)
ad1c1f18
KR
613Test accessibility of a file under the real UID and GID of the calling
614process. The return is @code{#t} if @var{path} exists and the
615permissions requested by @var{how} are all allowed, or @code{#f} if
616not.
a0e07ba4 617
ad1c1f18
KR
618@var{how} is an integer which is one of the following values, or a
619bitwise-OR (@code{logior}) of multiple values.
a0e07ba4
NJ
620
621@defvar R_OK
ad1c1f18 622Test for read permission.
a0e07ba4
NJ
623@end defvar
624@defvar W_OK
ad1c1f18 625Test for write permission.
a0e07ba4
NJ
626@end defvar
627@defvar X_OK
ad1c1f18 628Test for execute permission.
a0e07ba4
NJ
629@end defvar
630@defvar F_OK
ad1c1f18
KR
631Test for existence of the file. This is implied by each of the other
632tests, so there's no need to combine it with them.
a0e07ba4 633@end defvar
ad1c1f18
KR
634
635It's important to note that @code{access?} does not simply indicate
636what will happen on attempting to read or write a file. In normal
637circumstances it does, but in a set-UID or set-GID program it doesn't
638because @code{access?} tests the real ID, whereas an open or execute
639attempt uses the effective ID.
640
641A program which will never run set-UID/GID can ignore the difference
642between real and effective IDs, but for maximum generality, especially
1cd9ea69
KR
643in library functions, it's best not to use @code{access?} to predict
644the result of an open or execute, instead simply attempt that and
645catch any exception.
ad1c1f18
KR
646
647The main use for @code{access?} is to let a set-UID/GID program
648determine what the invoking user would have been allowed to do,
649without the greater (or perhaps lesser) privileges afforded by the
650effective ID. For more on this, see @ref{Testing File Access,,, libc,
651The GNU C Library Reference Manual}.
a0e07ba4
NJ
652@end deffn
653
654@findex fstat
8f85c0c6
NJ
655@deffn {Scheme Procedure} stat object
656@deffnx {C Function} scm_stat (object)
a0e07ba4 657Return an object containing various information about the file
64de6db5 658determined by @var{object}. @var{object} can be a string containing
a0e07ba4
NJ
659a file name or a port or integer file descriptor which is open
660on a file (in which case @code{fstat} is used as the underlying
661system call).
662
663The object returned by @code{stat} can be passed as a single
664parameter to the following procedures, all of which return
665integers:
666
2ce02471 667@deffn {Scheme Procedure} stat:dev st
5c3917e7 668The device number containing the file.
2ce02471
NJ
669@end deffn
670@deffn {Scheme Procedure} stat:ino st
a0e07ba4
NJ
671The file serial number, which distinguishes this file from all
672other files on the same device.
2ce02471
NJ
673@end deffn
674@deffn {Scheme Procedure} stat:mode st
5c3917e7
KR
675The mode of the file. This is an integer which incorporates file type
676information and file permission bits. See also @code{stat:type} and
a0e07ba4 677@code{stat:perms} below.
2ce02471
NJ
678@end deffn
679@deffn {Scheme Procedure} stat:nlink st
a0e07ba4 680The number of hard links to the file.
2ce02471
NJ
681@end deffn
682@deffn {Scheme Procedure} stat:uid st
a0e07ba4 683The user ID of the file's owner.
2ce02471
NJ
684@end deffn
685@deffn {Scheme Procedure} stat:gid st
a0e07ba4 686The group ID of the file.
2ce02471
NJ
687@end deffn
688@deffn {Scheme Procedure} stat:rdev st
f5f7888d
KR
689Device ID; this entry is defined only for character or block special
690files. On some systems this field is not available at all, in which
691case @code{stat:rdev} returns @code{#f}.
2ce02471
NJ
692@end deffn
693@deffn {Scheme Procedure} stat:size st
a0e07ba4 694The size of a regular file in bytes.
2ce02471
NJ
695@end deffn
696@deffn {Scheme Procedure} stat:atime st
06bfe276 697The last access time for the file, in seconds.
2ce02471
NJ
698@end deffn
699@deffn {Scheme Procedure} stat:mtime st
06bfe276 700The last modification time for the file, in seconds.
2ce02471
NJ
701@end deffn
702@deffn {Scheme Procedure} stat:ctime st
06bfe276
AW
703The last modification time for the attributes of the file, in seconds.
704@end deffn
705@deffn {Scheme Procedure} stat:atimensec st
706@deffnx {Scheme Procedure} stat:mtimensec st
707@deffnx {Scheme Procedure} stat:ctimensec st
708The fractional part of a file's access, modification, or attribute modification
709time, in nanoseconds. Nanosecond timestamps are only available on some operating
477e4219 710systems and file systems. If Guile cannot retrieve nanosecond-level timestamps
06bfe276 711for a file, these fields will be set to 0.
2ce02471
NJ
712@end deffn
713@deffn {Scheme Procedure} stat:blksize st
f5f7888d
KR
714The optimal block size for reading or writing the file, in bytes. On
715some systems this field is not available, in which case
716@code{stat:blksize} returns a sensible suggested block size.
2ce02471
NJ
717@end deffn
718@deffn {Scheme Procedure} stat:blocks st
f5f7888d
KR
719The amount of disk space that the file occupies measured in units of
720512 byte blocks. On some systems this field is not available, in
721which case @code{stat:blocks} returns @code{#f}.
2ce02471 722@end deffn
a0e07ba4
NJ
723
724In addition, the following procedures return the information
5c3917e7 725from @code{stat:mode} in a more convenient form:
a0e07ba4 726
2ce02471 727@deffn {Scheme Procedure} stat:type st
a0e07ba4 728A symbol representing the type of file. Possible values are
7403e409
NJ
729@samp{regular}, @samp{directory}, @samp{symlink},
730@samp{block-special}, @samp{char-special}, @samp{fifo}, @samp{socket},
731and @samp{unknown}.
2ce02471
NJ
732@end deffn
733@deffn {Scheme Procedure} stat:perms st
a0e07ba4 734An integer representing the access permission bits.
2ce02471 735@end deffn
a0e07ba4
NJ
736@end deffn
737
64de6db5
BT
738@deffn {Scheme Procedure} lstat path
739@deffnx {C Function} scm_lstat (path)
a0e07ba4
NJ
740Similar to @code{stat}, but does not follow symbolic links, i.e.,
741it will return information about a symbolic link itself, not the
742file it points to. @var{path} must be a string.
743@end deffn
744
8f85c0c6
NJ
745@deffn {Scheme Procedure} readlink path
746@deffnx {C Function} scm_readlink (path)
a0e07ba4
NJ
747Return the value of the symbolic link named by @var{path} (a
748string), i.e., the file that the link points to.
749@end deffn
750
751@findex fchown
752@findex lchown
8f85c0c6
NJ
753@deffn {Scheme Procedure} chown object owner group
754@deffnx {C Function} scm_chown (object, owner, group)
7403e409
NJ
755Change the ownership and group of the file referred to by @var{object}
756to the integer values @var{owner} and @var{group}. @var{object} can
757be a string containing a file name or, if the platform supports
758@code{fchown} (@pxref{File Owner,,,libc,The GNU C Library Reference
759Manual}), a port or integer file descriptor which is open on the file.
760The return value is unspecified.
a0e07ba4
NJ
761
762If @var{object} is a symbolic link, either the
763ownership of the link or the ownership of the referenced file will be
764changed depending on the operating system (lchown is
765unsupported at present). If @var{owner} or @var{group} is specified
766as @code{-1}, then that ID is not changed.
767@end deffn
768
769@findex fchmod
8f85c0c6
NJ
770@deffn {Scheme Procedure} chmod object mode
771@deffnx {C Function} scm_chmod (object, mode)
64de6db5
BT
772Changes the permissions of the file referred to by @var{object}.
773@var{object} can be a string containing a file name or a port or integer file
a0e07ba4
NJ
774descriptor which is open on a file (in which case @code{fchmod} is used
775as the underlying system call).
776@var{mode} specifies
777the new permissions as a decimal number, e.g., @code{(chmod "foo" #o755)}.
778The return value is unspecified.
779@end deffn
780
06bfe276
AW
781@deffn {Scheme Procedure} utime pathname [actime [modtime [actimens [modtimens [flags]]]]]
782@deffnx {C Function} scm_utime (pathname, actime, modtime, actimens, modtimens, flags)
a0e07ba4 783@code{utime} sets the access and modification times for the
64de6db5 784file named by @var{pathname}. If @var{actime} or @var{modtime} is
a0e07ba4
NJ
785not supplied, then the current time is used. @var{actime} and
786@var{modtime} must be integer time values as returned by the
787@code{current-time} procedure.
06bfe276
AW
788
789The optional @var{actimens} and @var{modtimens} are nanoseconds
790to add @var{actime} and @var{modtime}. Nanosecond precision is
477e4219 791only supported on some combinations of file systems and operating
06bfe276 792systems.
a0e07ba4
NJ
793@lisp
794(utime "foo" (- (current-time) 3600))
795@end lisp
796will set the access time to one hour in the past and the
797modification time to the current time.
798@end deffn
799
800@findex unlink
8f85c0c6
NJ
801@deffn {Scheme Procedure} delete-file str
802@deffnx {C Function} scm_delete_file (str)
7403e409
NJ
803Deletes (or ``unlinks'') the file whose path is specified by
804@var{str}.
a0e07ba4
NJ
805@end deffn
806
8f85c0c6
NJ
807@deffn {Scheme Procedure} copy-file oldfile newfile
808@deffnx {C Function} scm_copy_file (oldfile, newfile)
7403e409 809Copy the file specified by @var{oldfile} to @var{newfile}.
a0e07ba4
NJ
810The return value is unspecified.
811@end deffn
812
fbac7c61
LC
813@deffn {Scheme Procedure} sendfile out in count [offset]
814@deffnx {C Function} scm_sendfile (out, in, count, offset)
815Send @var{count} bytes from @var{in} to @var{out}, both of which
e0886e07 816must be either open file ports or file descriptors. When
fbac7c61 817@var{offset} is omitted, start reading from @var{in}'s current
e0886e07
LC
818position; otherwise, start reading at @var{offset}. Return
819the number of bytes actually sent.
fbac7c61
LC
820
821When @var{in} is a port, it is often preferable to specify @var{offset},
822because @var{in}'s offset as a port may be different from the offset of
823its underlying file descriptor.
824
825On systems that support it, such as GNU/Linux, this procedure uses the
826@code{sendfile} libc function, which usually corresponds to a system
827call. This is faster than doing a series of @code{read} and
828@code{write} system calls. A typical application is to send a file over
829a socket.
830
831In some cases, the @code{sendfile} libc function may return
832@code{EINVAL} or @code{ENOSYS}. In that case, Guile's @code{sendfile}
833procedure automatically falls back to doing a series of @code{read} and
834@code{write} calls.
e0886e07
LC
835
836In other cases, the libc function may send fewer bytes than
837@var{count}---for instance because @var{out} is a slow or limited
838device, such as a pipe. When that happens, Guile's @code{sendfile}
839automatically retries until exactly @var{count} bytes were sent or an
840error occurs.
fbac7c61
LC
841@end deffn
842
a0e07ba4 843@findex rename
8f85c0c6
NJ
844@deffn {Scheme Procedure} rename-file oldname newname
845@deffnx {C Function} scm_rename (oldname, newname)
a0e07ba4
NJ
846Renames the file specified by @var{oldname} to @var{newname}.
847The return value is unspecified.
848@end deffn
849
8f85c0c6
NJ
850@deffn {Scheme Procedure} link oldpath newpath
851@deffnx {C Function} scm_link (oldpath, newpath)
a0e07ba4
NJ
852Creates a new name @var{newpath} in the file system for the
853file named by @var{oldpath}. If @var{oldpath} is a symbolic
854link, the link may or may not be followed depending on the
855system.
856@end deffn
857
8f85c0c6
NJ
858@deffn {Scheme Procedure} symlink oldpath newpath
859@deffnx {C Function} scm_symlink (oldpath, newpath)
7403e409
NJ
860Create a symbolic link named @var{newpath} with the value (i.e., pointing to)
861@var{oldpath}. The return value is unspecified.
a0e07ba4
NJ
862@end deffn
863
8f85c0c6
NJ
864@deffn {Scheme Procedure} mkdir path [mode]
865@deffnx {C Function} scm_mkdir (path, mode)
a0e07ba4
NJ
866Create a new directory named by @var{path}. If @var{mode} is omitted
867then the permissions of the directory file are set using the current
7403e409
NJ
868umask (@pxref{Processes}). Otherwise they are set to the decimal
869value specified with @var{mode}. The return value is unspecified.
a0e07ba4
NJ
870@end deffn
871
8f85c0c6
NJ
872@deffn {Scheme Procedure} rmdir path
873@deffnx {C Function} scm_rmdir (path)
a0e07ba4
NJ
874Remove the existing directory named by @var{path}. The directory must
875be empty for this to succeed. The return value is unspecified.
876@end deffn
877
8f85c0c6
NJ
878@deffn {Scheme Procedure} opendir dirname
879@deffnx {C Function} scm_opendir (dirname)
f3dfb8ac 880@cindex directory contents
7403e409 881Open the directory specified by @var{dirname} and return a directory
a0e07ba4 882stream.
46e78202
LC
883
884Before using this and the procedures below, make sure to see the
885higher-level procedures for directory traversal that are available
886(@pxref{File Tree Walk}).
a0e07ba4
NJ
887@end deffn
888
7403e409
NJ
889@deffn {Scheme Procedure} directory-stream? object
890@deffnx {C Function} scm_directory_stream_p (object)
a0e07ba4
NJ
891Return a boolean indicating whether @var{object} is a directory
892stream as returned by @code{opendir}.
893@end deffn
894
7403e409
NJ
895@deffn {Scheme Procedure} readdir stream
896@deffnx {C Function} scm_readdir (stream)
a0e07ba4
NJ
897Return (as a string) the next directory entry from the directory stream
898@var{stream}. If there is no remaining entry to be read then the
899end of file object is returned.
900@end deffn
901
7403e409
NJ
902@deffn {Scheme Procedure} rewinddir stream
903@deffnx {C Function} scm_rewinddir (stream)
a0e07ba4
NJ
904Reset the directory port @var{stream} so that the next call to
905@code{readdir} will return the first directory entry.
906@end deffn
907
7403e409
NJ
908@deffn {Scheme Procedure} closedir stream
909@deffnx {C Function} scm_closedir (stream)
a0e07ba4
NJ
910Close the directory stream @var{stream}.
911The return value is unspecified.
912@end deffn
913
bcf009c3
NJ
914Here is an example showing how to display all the entries in a
915directory:
916
917@lisp
918(define dir (opendir "/usr/lib"))
919(do ((entry (readdir dir) (readdir dir)))
920 ((eof-object? entry))
921 (display entry)(newline))
922(closedir dir)
923@end lisp
924
8f85c0c6
NJ
925@deffn {Scheme Procedure} sync
926@deffnx {C Function} scm_sync ()
a0e07ba4
NJ
927Flush the operating system disk buffers.
928The return value is unspecified.
929@end deffn
930
8f85c0c6
NJ
931@deffn {Scheme Procedure} mknod path type perms dev
932@deffnx {C Function} scm_mknod (path, type, perms, dev)
f3dfb8ac 933@cindex device file
a0e07ba4 934Creates a new special file, such as a file corresponding to a device.
7403e409
NJ
935@var{path} specifies the name of the file. @var{type} should be one
936of the following symbols: @samp{regular}, @samp{directory},
937@samp{symlink}, @samp{block-special}, @samp{char-special},
938@samp{fifo}, or @samp{socket}. @var{perms} (an integer) specifies the
939file permissions. @var{dev} (an integer) specifies which device the
940special file refers to. Its exact interpretation depends on the kind
941of special file being created.
a0e07ba4
NJ
942
943E.g.,
944@lisp
945(mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2))
946@end lisp
947
948The return value is unspecified.
949@end deffn
950
8f85c0c6
NJ
951@deffn {Scheme Procedure} tmpnam
952@deffnx {C Function} scm_tmpnam ()
f3dfb8ac 953@cindex temporary file
0b0715f1
KR
954Return an auto-generated name of a temporary file, a file which
955doesn't already exist. The name includes a path, it's usually in
956@file{/tmp} but that's system dependent.
957
958Care must be taken when using @code{tmpnam}. In between choosing the
959name and creating the file another program might use that name, or an
960attacker might even make it a symlink pointing at something important
961and causing you to overwrite that.
962
963The safe way is to create the file using @code{open} with
964@code{O_EXCL} to avoid any overwriting. A loop can try again with
965another name if the file exists (error @code{EEXIST}).
966@code{mkstemp!} below does that.
a0e07ba4
NJ
967@end deffn
968
8f85c0c6
NJ
969@deffn {Scheme Procedure} mkstemp! tmpl
970@deffnx {C Function} scm_mkstemp (tmpl)
f3dfb8ac 971@cindex temporary file
0b0715f1
KR
972Create a new unique file in the file system and return a new buffered
973port open for reading and writing to the file.
24ec486c 974
0b0715f1
KR
975@var{tmpl} is a string specifying where the file should be created: it
976must end with @samp{XXXXXX} and those @samp{X}s will be changed in the
977string to return the name of the file. (@code{port-filename} on the
978port also gives the name.)
24ec486c 979
0b0715f1
KR
980POSIX doesn't specify the permissions mode of the file, on GNU and
981most systems it's @code{#o600}. An application can use @code{chmod}
982to relax that if desired. For example @code{#o666} less @code{umask},
983which is usual for ordinary file creation,
61fdb557
KR
984
985@example
0b0715f1 986(let ((port (mkstemp! (string-copy "/tmp/myfile-XXXXXX"))))
61fdb557
KR
987 (chmod port (logand #o666 (lognot (umask))))
988 ...)
989@end example
a0e07ba4
NJ
990@end deffn
991
cd53bd36 992@deffn {Scheme Procedure} tmpfile
5f6ffd66 993@deffnx {C Function} scm_tmpfile ()
cd53bd36
TTN
994Return an input/output port to a unique temporary file
995named using the path prefix @code{P_tmpdir} defined in
996@file{stdio.h}.
997The file is automatically deleted when the port is closed
998or the program terminates.
999@end deffn
1000
8f85c0c6
NJ
1001@deffn {Scheme Procedure} dirname filename
1002@deffnx {C Function} scm_dirname (filename)
a0e07ba4
NJ
1003Return the directory name component of the file name
1004@var{filename}. If @var{filename} does not contain a directory
1005component, @code{.} is returned.
1006@end deffn
1007
8f85c0c6
NJ
1008@deffn {Scheme Procedure} basename filename [suffix]
1009@deffnx {C Function} scm_basename (filename, suffix)
a0e07ba4
NJ
1010Return the base name of the file name @var{filename}. The
1011base name is the file name without any directory components.
85a9b4ed 1012If @var{suffix} is provided, and is equal to the end of
a0e07ba4 1013@var{basename}, it is removed also.
bcf009c3
NJ
1014
1015@lisp
1016(basename "/tmp/test.xml" ".xml")
1017@result{} "test"
1018@end lisp
a0e07ba4
NJ
1019@end deffn
1020
839e6326
NJ
1021@deffn {Scheme Procedure} file-exists? filename
1022Return @code{#t} if the file named @var{filename} exists, @code{#f} if
1023not.
1024@end deffn
1025
66750b78
LC
1026@cindex file name separator
1027@cindex absolute file name
1028
1029Many operating systems, such as GNU, use @code{/} (forward slash) to
1030separate the components of a file name; any file name starting with
1031@code{/} is considered an @dfn{absolute file name}. These conventions
1032are specified by the POSIX Base Definitions, which refer to conforming
1033file names as ``pathnames''. Some operating systems use a different
1034convention; in particular, Windows uses @code{\} (backslash) as the file
1035name separator, and also has the notion of @dfn{volume names} like
1036@code{C:\} for absolute file names. The following procedures and
1037variables provide support for portable file name manipulations.
1038
1039@deffn {Scheme Procedure} system-file-name-convention
1040Return either @code{posix} or @code{windows}, depending on
1041what kind of system this Guile is running on.
1042@end deffn
1043
1044@deffn {Scheme Procedure} file-name-separator? c
1045Return true if character @var{c} is a file name separator on the host
1046platform.
1047@end deffn
1048
1049@deffn {Scheme Procedure} absolute-file-name? file-name
1050Return true if @var{file-name} denotes an absolute file name on the host
1051platform.
1052@end deffn
1053
1054@defvr {Scheme Variable} file-name-separator-string
7d39b488
LC
1055The preferred file name separator.
1056
1057Note that on MinGW builds for Windows, both @code{/} and @code{\} are
1058valid separators. Thus, programs should not assume that
1059@code{file-name-separator-string} is the @emph{only} file name
1060separator---e.g., when extracting the components of a file name.
66750b78
LC
1061@end defvr
1062
a0e07ba4
NJ
1063
1064@node User Information
3229f68b 1065@subsection User Information
f3dfb8ac
KR
1066@cindex user information
1067@cindex password file
1068@cindex group file
a0e07ba4
NJ
1069
1070The facilities in this section provide an interface to the user and
1071group database.
1072They should be used with care since they are not reentrant.
1073
1074The following functions accept an object representing user information
1075and return a selected component:
1076
2ce02471 1077@deffn {Scheme Procedure} passwd:name pw
a0e07ba4 1078The name of the userid.
2ce02471
NJ
1079@end deffn
1080@deffn {Scheme Procedure} passwd:passwd pw
a0e07ba4 1081The encrypted passwd.
2ce02471
NJ
1082@end deffn
1083@deffn {Scheme Procedure} passwd:uid pw
a0e07ba4 1084The user id number.
2ce02471
NJ
1085@end deffn
1086@deffn {Scheme Procedure} passwd:gid pw
a0e07ba4 1087The group id number.
2ce02471
NJ
1088@end deffn
1089@deffn {Scheme Procedure} passwd:gecos pw
a0e07ba4 1090The full name.
2ce02471
NJ
1091@end deffn
1092@deffn {Scheme Procedure} passwd:dir pw
a0e07ba4 1093The home directory.
2ce02471
NJ
1094@end deffn
1095@deffn {Scheme Procedure} passwd:shell pw
a0e07ba4 1096The login shell.
2ce02471
NJ
1097@end deffn
1098@sp 1
a0e07ba4 1099
8f85c0c6 1100@deffn {Scheme Procedure} getpwuid uid
a0e07ba4
NJ
1101Look up an integer userid in the user database.
1102@end deffn
1103
8f85c0c6 1104@deffn {Scheme Procedure} getpwnam name
a0e07ba4
NJ
1105Look up a user name string in the user database.
1106@end deffn
1107
8f85c0c6 1108@deffn {Scheme Procedure} setpwent
a0e07ba4
NJ
1109Initializes a stream used by @code{getpwent} to read from the user database.
1110The next use of @code{getpwent} will return the first entry. The
1111return value is unspecified.
1112@end deffn
1113
8f85c0c6 1114@deffn {Scheme Procedure} getpwent
40296bab
KR
1115Read the next entry in the user database stream. The return is a
1116passwd user object as above, or @code{#f} when no more entries.
a0e07ba4
NJ
1117@end deffn
1118
8f85c0c6 1119@deffn {Scheme Procedure} endpwent
a0e07ba4
NJ
1120Closes the stream used by @code{getpwent}. The return value is unspecified.
1121@end deffn
1122
8f85c0c6
NJ
1123@deffn {Scheme Procedure} setpw [arg]
1124@deffnx {C Function} scm_setpwent (arg)
a0e07ba4
NJ
1125If called with a true argument, initialize or reset the password data
1126stream. Otherwise, close the stream. The @code{setpwent} and
1127@code{endpwent} procedures are implemented on top of this.
1128@end deffn
1129
8f85c0c6
NJ
1130@deffn {Scheme Procedure} getpw [user]
1131@deffnx {C Function} scm_getpwuid (user)
64de6db5 1132Look up an entry in the user database. @var{user} can be an integer,
a0e07ba4
NJ
1133a string, or omitted, giving the behaviour of getpwuid, getpwnam
1134or getpwent respectively.
1135@end deffn
1136
1137The following functions accept an object representing group information
1138and return a selected component:
1139
2ce02471 1140@deffn {Scheme Procedure} group:name gr
a0e07ba4 1141The group name.
2ce02471
NJ
1142@end deffn
1143@deffn {Scheme Procedure} group:passwd gr
a0e07ba4 1144The encrypted group password.
2ce02471
NJ
1145@end deffn
1146@deffn {Scheme Procedure} group:gid gr
a0e07ba4 1147The group id number.
2ce02471
NJ
1148@end deffn
1149@deffn {Scheme Procedure} group:mem gr
85a9b4ed 1150A list of userids which have this group as a supplementary group.
2ce02471
NJ
1151@end deffn
1152@sp 1
a0e07ba4 1153
8f85c0c6 1154@deffn {Scheme Procedure} getgrgid gid
85a9b4ed 1155Look up an integer group id in the group database.
a0e07ba4
NJ
1156@end deffn
1157
8f85c0c6 1158@deffn {Scheme Procedure} getgrnam name
a0e07ba4
NJ
1159Look up a group name in the group database.
1160@end deffn
1161
8f85c0c6 1162@deffn {Scheme Procedure} setgrent
a0e07ba4
NJ
1163Initializes a stream used by @code{getgrent} to read from the group database.
1164The next use of @code{getgrent} will return the first entry.
1165The return value is unspecified.
1166@end deffn
1167
8f85c0c6 1168@deffn {Scheme Procedure} getgrent
a0e07ba4
NJ
1169Return the next entry in the group database, using the stream set by
1170@code{setgrent}.
1171@end deffn
1172
8f85c0c6 1173@deffn {Scheme Procedure} endgrent
a0e07ba4
NJ
1174Closes the stream used by @code{getgrent}.
1175The return value is unspecified.
1176@end deffn
1177
8f85c0c6
NJ
1178@deffn {Scheme Procedure} setgr [arg]
1179@deffnx {C Function} scm_setgrent (arg)
a0e07ba4
NJ
1180If called with a true argument, initialize or reset the group data
1181stream. Otherwise, close the stream. The @code{setgrent} and
1182@code{endgrent} procedures are implemented on top of this.
1183@end deffn
1184
64de6db5
BT
1185@deffn {Scheme Procedure} getgr [group]
1186@deffnx {C Function} scm_getgrgid (group)
1187Look up an entry in the group database. @var{group} can be an integer,
a0e07ba4
NJ
1188a string, or omitted, giving the behaviour of getgrgid, getgrnam
1189or getgrent respectively.
1190@end deffn
1191
1192In addition to the accessor procedures for the user database, the
cd28785f 1193following shortcut procedure is also available.
a0e07ba4 1194
8f85c0c6
NJ
1195@deffn {Scheme Procedure} getlogin
1196@deffnx {C Function} scm_getlogin ()
a0e07ba4
NJ
1197Return a string containing the name of the user logged in on
1198the controlling terminal of the process, or @code{#f} if this
1199information cannot be obtained.
1200@end deffn
1201
1202
1203@node Time
3229f68b 1204@subsection Time
f3dfb8ac 1205@cindex time
a0e07ba4 1206
8f85c0c6
NJ
1207@deffn {Scheme Procedure} current-time
1208@deffnx {C Function} scm_current_time ()
7403e409 1209Return the number of seconds since 1970-01-01 00:00:00 @acronym{UTC},
a0e07ba4
NJ
1210excluding leap seconds.
1211@end deffn
1212
8f85c0c6
NJ
1213@deffn {Scheme Procedure} gettimeofday
1214@deffnx {C Function} scm_gettimeofday ()
a0e07ba4 1215Return a pair containing the number of seconds and microseconds
7403e409 1216since 1970-01-01 00:00:00 @acronym{UTC}, excluding leap seconds. Note:
a0e07ba4
NJ
1217whether true microsecond resolution is available depends on the
1218operating system.
1219@end deffn
1220
1221The following procedures either accept an object representing a broken down
1222time and return a selected component, or accept an object representing
1223a broken down time and a value and set the component to the value.
1224The numbers in parentheses give the usual range.
1225
2ce02471
NJ
1226@deffn {Scheme Procedure} tm:sec tm
1227@deffnx {Scheme Procedure} set-tm:sec tm val
a0e07ba4 1228Seconds (0-59).
2ce02471
NJ
1229@end deffn
1230@deffn {Scheme Procedure} tm:min tm
1231@deffnx {Scheme Procedure} set-tm:min tm val
a0e07ba4 1232Minutes (0-59).
2ce02471
NJ
1233@end deffn
1234@deffn {Scheme Procedure} tm:hour tm
1235@deffnx {Scheme Procedure} set-tm:hour tm val
a0e07ba4 1236Hours (0-23).
2ce02471
NJ
1237@end deffn
1238@deffn {Scheme Procedure} tm:mday tm
1239@deffnx {Scheme Procedure} set-tm:mday tm val
a0e07ba4 1240Day of the month (1-31).
2ce02471
NJ
1241@end deffn
1242@deffn {Scheme Procedure} tm:mon tm
1243@deffnx {Scheme Procedure} set-tm:mon tm val
a0e07ba4 1244Month (0-11).
2ce02471
NJ
1245@end deffn
1246@deffn {Scheme Procedure} tm:year tm
1247@deffnx {Scheme Procedure} set-tm:year tm val
a0e07ba4 1248Year (70-), the year minus 1900.
2ce02471
NJ
1249@end deffn
1250@deffn {Scheme Procedure} tm:wday tm
1251@deffnx {Scheme Procedure} set-tm:wday tm val
a0e07ba4 1252Day of the week (0-6) with Sunday represented as 0.
2ce02471
NJ
1253@end deffn
1254@deffn {Scheme Procedure} tm:yday tm
1255@deffnx {Scheme Procedure} set-tm:yday tm val
a0e07ba4 1256Day of the year (0-364, 365 in leap years).
2ce02471
NJ
1257@end deffn
1258@deffn {Scheme Procedure} tm:isdst tm
1259@deffnx {Scheme Procedure} set-tm:isdst tm val
7403e409
NJ
1260Daylight saving indicator (0 for ``no'', greater than 0 for ``yes'', less than
12610 for ``unknown'').
2ce02471
NJ
1262@end deffn
1263@deffn {Scheme Procedure} tm:gmtoff tm
1264@deffnx {Scheme Procedure} set-tm:gmtoff tm val
7403e409 1265Time zone offset in seconds west of @acronym{UTC} (-46800 to 43200).
40296bab
KR
1266For example on East coast USA (zone @samp{EST+5}) this would be 18000
1267(ie.@: @m{5\times60\times60,5*60*60}) in winter, or 14400
1268(ie.@: @m{4\times60\times60,4*60*60}) during daylight savings.
1269
1270Note @code{tm:gmtoff} is not the same as @code{tm_gmtoff} in the C
1271@code{tm} structure. @code{tm_gmtoff} is seconds east and hence the
1272negative of the value here.
2ce02471
NJ
1273@end deffn
1274@deffn {Scheme Procedure} tm:zone tm
1275@deffnx {Scheme Procedure} set-tm:zone tm val
a0e07ba4 1276Time zone label (a string), not necessarily unique.
2ce02471
NJ
1277@end deffn
1278@sp 1
a0e07ba4 1279
8f85c0c6
NJ
1280@deffn {Scheme Procedure} localtime time [zone]
1281@deffnx {C Function} scm_localtime (time, zone)
f3dfb8ac 1282@cindex local time
a0e07ba4
NJ
1283Return an object representing the broken down components of
1284@var{time}, an integer like the one returned by
1285@code{current-time}. The time zone for the calculation is
1286optionally specified by @var{zone} (a string), otherwise the
7403e409 1287@env{TZ} environment variable or the system default is used.
a0e07ba4
NJ
1288@end deffn
1289
8f85c0c6
NJ
1290@deffn {Scheme Procedure} gmtime time
1291@deffnx {C Function} scm_gmtime (time)
a0e07ba4
NJ
1292Return an object representing the broken down components of
1293@var{time}, an integer like the one returned by
7403e409 1294@code{current-time}. The values are calculated for @acronym{UTC}.
a0e07ba4
NJ
1295@end deffn
1296
82512be0 1297@deffn {Scheme Procedure} mktime sbd-time [zone]
8f85c0c6 1298@deffnx {C Function} scm_mktime (sbd_time, zone)
b0fb2306
KR
1299For a broken down time object @var{sbd-time}, return a pair the
1300@code{car} of which is an integer time like @code{current-time}, and
1301the @code{cdr} of which is a new broken down time with normalized
1302fields.
1303
1304@var{zone} is a timezone string, or the default is the @env{TZ}
1305environment variable or the system default (@pxref{TZ Variable,,
1306Specifying the Time Zone with @env{TZ}, libc, GNU C Library Reference
1307Manual}). @var{sbd-time} is taken to be in that @var{zone}.
1308
1309The following fields of @var{sbd-time} are used: @code{tm:year},
1310@code{tm:mon}, @code{tm:mday}, @code{tm:hour}, @code{tm:min},
1311@code{tm:sec}, @code{tm:isdst}. The values can be outside their usual
1312ranges. For example @code{tm:hour} normally goes up to 23, but a
1313value say 33 would mean 9 the following day.
1314
1315@code{tm:isdst} in @var{sbd-time} says whether the time given is with
1316daylight savings or not. This is ignored if @var{zone} doesn't have
1317any daylight savings adjustment amount.
1318
1319The broken down time in the return normalizes the values of
1320@var{sbd-time} by bringing them into their usual ranges, and using the
1321actual daylight savings rule for that time in @var{zone} (which may
1322differ from what @var{sbd-time} had). The easiest way to think of
1323this is that @var{sbd-time} plus @var{zone} converts to the integer
1324UTC time, then a @code{localtime} is applied to get the normal
1325presentation of that time, in @var{zone}.
a0e07ba4
NJ
1326@end deffn
1327
8f85c0c6
NJ
1328@deffn {Scheme Procedure} tzset
1329@deffnx {C Function} scm_tzset ()
7403e409 1330Initialize the timezone from the @env{TZ} environment variable
a0e07ba4
NJ
1331or the system default. It's not usually necessary to call this procedure
1332since it's done automatically by other procedures that depend on the
1333timezone.
1334@end deffn
1335
4b08cab6
KR
1336@deffn {Scheme Procedure} strftime format tm
1337@deffnx {C Function} scm_strftime (format, tm)
f3dfb8ac 1338@cindex time formatting
4b08cab6
KR
1339Return a string which is broken-down time structure @var{tm} formatted
1340according to the given @var{format} string.
1341
1342@var{format} contains field specifications introduced by a @samp{%}
1343character. See @ref{Formatting Calendar Time,,, libc, The GNU C
1344Library Reference Manual}, or @samp{man 3 strftime}, for the available
1345formatting.
bcf009c3
NJ
1346
1347@lisp
1348(strftime "%c" (localtime (current-time)))
1349@result{} "Mon Mar 11 20:17:43 2002"
1350@end lisp
4b08cab6
KR
1351
1352If @code{setlocale} has been called (@pxref{Locales}), month and day
1353names are from the current locale and in the locale character set.
a0e07ba4
NJ
1354@end deffn
1355
8f85c0c6
NJ
1356@deffn {Scheme Procedure} strptime format string
1357@deffnx {C Function} scm_strptime (format, string)
f3dfb8ac 1358@cindex time parsing
a0e07ba4
NJ
1359Performs the reverse action to @code{strftime}, parsing
1360@var{string} according to the specification supplied in
64de6db5 1361@var{format}. The interpretation of month and day names is
a0e07ba4 1362dependent on the current locale. The value returned is a pair.
7403e409 1363The @acronym{CAR} has an object with time components
a0e07ba4
NJ
1364in the form returned by @code{localtime} or @code{gmtime},
1365but the time zone components
1366are not usefully set.
7403e409 1367The @acronym{CDR} reports the number of characters from @var{string}
a0e07ba4
NJ
1368which were used for the conversion.
1369@end deffn
1370
1371@defvar internal-time-units-per-second
1372The value of this variable is the number of time units per second
1373reported by the following procedures.
1374@end defvar
1375
8f85c0c6
NJ
1376@deffn {Scheme Procedure} times
1377@deffnx {C Function} scm_times ()
a0e07ba4
NJ
1378Return an object with information about real and processor
1379time. The following procedures accept such an object as an
1380argument and return a selected component:
1381
2ce02471 1382@deffn {Scheme Procedure} tms:clock tms
a0e07ba4
NJ
1383The current real time, expressed as time units relative to an
1384arbitrary base.
2ce02471
NJ
1385@end deffn
1386@deffn {Scheme Procedure} tms:utime tms
a0e07ba4 1387The CPU time units used by the calling process.
2ce02471
NJ
1388@end deffn
1389@deffn {Scheme Procedure} tms:stime tms
a0e07ba4
NJ
1390The CPU time units used by the system on behalf of the calling
1391process.
2ce02471
NJ
1392@end deffn
1393@deffn {Scheme Procedure} tms:cutime tms
a0e07ba4
NJ
1394The CPU time units used by terminated child processes of the
1395calling process, whose status has been collected (e.g., using
1396@code{waitpid}).
2ce02471
NJ
1397@end deffn
1398@deffn {Scheme Procedure} tms:cstime tms
a0e07ba4
NJ
1399Similarly, the CPU times units used by the system on behalf of
1400terminated child processes.
2ce02471 1401@end deffn
a0e07ba4
NJ
1402@end deffn
1403
8f85c0c6
NJ
1404@deffn {Scheme Procedure} get-internal-real-time
1405@deffnx {C Function} scm_get_internal_real_time ()
a0e07ba4
NJ
1406Return the number of time units since the interpreter was
1407started.
1408@end deffn
1409
8f85c0c6
NJ
1410@deffn {Scheme Procedure} get-internal-run-time
1411@deffnx {C Function} scm_get_internal_run_time ()
a0e07ba4
NJ
1412Return the number of time units of processor time used by the
1413interpreter. Both @emph{system} and @emph{user} time are
1414included but subprocesses are not.
1415@end deffn
1416
1417@node Runtime Environment
3229f68b 1418@subsection Runtime Environment
a0e07ba4 1419
8f85c0c6
NJ
1420@deffn {Scheme Procedure} program-arguments
1421@deffnx {Scheme Procedure} command-line
9a18d8d4 1422@deffnx {Scheme Procedure} set-program-arguments
8f85c0c6 1423@deffnx {C Function} scm_program_arguments ()
9a18d8d4 1424@deffnx {C Function} scm_set_program_arguments_scm (lst)
f3dfb8ac
KR
1425@cindex command line
1426@cindex program arguments
9a18d8d4
KR
1427Get the command line arguments passed to Guile, or set new arguments.
1428
1429The arguments are a list of strings, the first of which is the invoked
1430program name. This is just @nicode{"guile"} (or the executable path)
1431when run interactively, or it's the script name when running a script
1432with @option{-s} (@pxref{Invoking Guile}).
1433
1434@example
1435guile -L /my/extra/dir -s foo.scm abc def
1436
1437(program-arguments) @result{} ("foo.scm" "abc" "def")
1438@end example
1439
1440@code{set-program-arguments} allows a library module or similar to
1441modify the arguments, for example to strip options it recognises,
1442leaving the rest for the mainline.
1443
1444The argument list is held in a fluid, which means it's separate for
1445each thread. Neither the list nor the strings within it are copied at
1446any point and normally should not be mutated.
1447
1448The two names @code{program-arguments} and @code{command-line} are an
1449historical accident, they both do exactly the same thing. The name
1450@code{scm_set_program_arguments_scm} has an extra @code{_scm} on the
1451end to avoid clashing with the C function below.
a0e07ba4
NJ
1452@end deffn
1453
9a18d8d4
KR
1454@deftypefn {C Function} void scm_set_program_arguments (int argc, char **argv, char *first)
1455@cindex command line
1456@cindex program arguments
1457Set the list of command line arguments for @code{program-arguments}
1458and @code{command-line} above.
1459
1460@var{argv} is an array of null-terminated strings, as in a C
1461@code{main} function. @var{argc} is the number of strings in
bf5df489
KR
1462@var{argv}, or if it's negative then a @code{NULL} in @var{argv} marks
1463its end.
9a18d8d4
KR
1464
1465@var{first} is an extra string put at the start of the arguments, or
1466@code{NULL} for no such extra. This is a convenient way to pass the
1467program name after advancing @var{argv} to strip option arguments.
bf5df489 1468Eg.@:
9a18d8d4
KR
1469
1470@example
1471@{
1472 char *progname = argv[0];
9a18d8d4
KR
1473 for (argv++; argv[0] != NULL && argv[0][0] == '-'; argv++)
1474 @{
1475 /* munch option ... */
1476 @}
1477 /* remaining args for scheme level use */
1478 scm_set_program_arguments (-1, argv, progname);
1479@}
1480@end example
1481
1482This sort of thing is often done at startup under
bf5df489 1483@code{scm_boot_guile} with options handled at the C level removed.
9a18d8d4
KR
1484The given strings are all copied, so the C data is not accessed again
1485once @code{scm_set_program_arguments} returns.
1486@end deftypefn
1487
64de6db5
BT
1488@deffn {Scheme Procedure} getenv name
1489@deffnx {C Function} scm_getenv (name)
f3dfb8ac 1490@cindex environment
a0e07ba4
NJ
1491Looks up the string @var{name} in the current environment. The return
1492value is @code{#f} unless a string of the form @code{NAME=VALUE} is
1493found, in which case the string @code{VALUE} is returned.
1494@end deffn
1495
8f85c0c6 1496@deffn {Scheme Procedure} setenv name value
a0e07ba4
NJ
1497Modifies the environment of the current process, which is
1498also the default environment inherited by child processes.
1499
1500If @var{value} is @code{#f}, then @var{name} is removed from the
1501environment. Otherwise, the string @var{name}=@var{value} is added
1502to the environment, replacing any existing string with name matching
1503@var{name}.
1504
1505The return value is unspecified.
1506@end deffn
1507
395b0a34
NJ
1508@deffn {Scheme Procedure} unsetenv name
1509Remove variable @var{name} from the environment. The
1510name can not contain a @samp{=} character.
1511@end deffn
1512
8f85c0c6
NJ
1513@deffn {Scheme Procedure} environ [env]
1514@deffnx {C Function} scm_environ (env)
a0e07ba4
NJ
1515If @var{env} is omitted, return the current environment (in the
1516Unix sense) as a list of strings. Otherwise set the current
1517environment, which is also the default environment for child
1518processes, to the supplied list of strings. Each member of
64de6db5
BT
1519@var{env} should be of the form @var{name}=@var{value} and values of
1520@var{name} should not be duplicated. If @var{env} is supplied
a0e07ba4
NJ
1521then the return value is unspecified.
1522@end deffn
1523
8f85c0c6
NJ
1524@deffn {Scheme Procedure} putenv str
1525@deffnx {C Function} scm_putenv (str)
a0e07ba4
NJ
1526Modifies the environment of the current process, which is
1527also the default environment inherited by child processes.
1528
64de6db5 1529If @var{str} is of the form @code{NAME=VALUE} then it will be written
a0e07ba4
NJ
1530directly into the environment, replacing any existing environment string
1531with
64de6db5
BT
1532name matching @code{NAME}. If @var{str} does not contain an equal
1533sign, then any existing string with name matching @var{str} will
a0e07ba4
NJ
1534be removed.
1535
1536The return value is unspecified.
1537@end deffn
1538
1539
1540@node Processes
3229f68b 1541@subsection Processes
f3dfb8ac
KR
1542@cindex processes
1543@cindex child processes
a0e07ba4
NJ
1544
1545@findex cd
8f85c0c6
NJ
1546@deffn {Scheme Procedure} chdir str
1547@deffnx {C Function} scm_chdir (str)
f3dfb8ac 1548@cindex current directory
64de6db5 1549Change the current working directory to @var{str}.
a0e07ba4
NJ
1550The return value is unspecified.
1551@end deffn
1552
1553@findex pwd
8f85c0c6
NJ
1554@deffn {Scheme Procedure} getcwd
1555@deffnx {C Function} scm_getcwd ()
a0e07ba4
NJ
1556Return the name of the current working directory.
1557@end deffn
1558
8f85c0c6
NJ
1559@deffn {Scheme Procedure} umask [mode]
1560@deffnx {C Function} scm_umask (mode)
7403e409
NJ
1561If @var{mode} is omitted, returns a decimal number representing the
1562current file creation mask. Otherwise the file creation mask is set
1563to @var{mode} and the previous value is returned. @xref{Setting
1564Permissions,,Assigning File Permissions,libc,The GNU C Library
1565Reference Manual}, for more on how to use umasks.
a0e07ba4 1566
7403e409 1567E.g., @code{(umask #o022)} sets the mask to octal 22/decimal 18.
a0e07ba4
NJ
1568@end deffn
1569
8f85c0c6
NJ
1570@deffn {Scheme Procedure} chroot path
1571@deffnx {C Function} scm_chroot (path)
a0e07ba4
NJ
1572Change the root directory to that specified in @var{path}.
1573This directory will be used for path names beginning with
1574@file{/}. The root directory is inherited by all children
1575of the current process. Only the superuser may change the
1576root directory.
1577@end deffn
1578
8f85c0c6
NJ
1579@deffn {Scheme Procedure} getpid
1580@deffnx {C Function} scm_getpid ()
a0e07ba4
NJ
1581Return an integer representing the current process ID.
1582@end deffn
1583
8f85c0c6
NJ
1584@deffn {Scheme Procedure} getgroups
1585@deffnx {C Function} scm_getgroups ()
a0e07ba4 1586Return a vector of integers representing the current
85a9b4ed 1587supplementary group IDs.
a0e07ba4
NJ
1588@end deffn
1589
8f85c0c6
NJ
1590@deffn {Scheme Procedure} getppid
1591@deffnx {C Function} scm_getppid ()
a0e07ba4
NJ
1592Return an integer representing the process ID of the parent
1593process.
1594@end deffn
1595
8f85c0c6
NJ
1596@deffn {Scheme Procedure} getuid
1597@deffnx {C Function} scm_getuid ()
a0e07ba4
NJ
1598Return an integer representing the current real user ID.
1599@end deffn
1600
8f85c0c6
NJ
1601@deffn {Scheme Procedure} getgid
1602@deffnx {C Function} scm_getgid ()
a0e07ba4
NJ
1603Return an integer representing the current real group ID.
1604@end deffn
1605
8f85c0c6
NJ
1606@deffn {Scheme Procedure} geteuid
1607@deffnx {C Function} scm_geteuid ()
a0e07ba4
NJ
1608Return an integer representing the current effective user ID.
1609If the system does not support effective IDs, then the real ID
66add4eb 1610is returned. @code{(provided? 'EIDs)} reports whether the
a0e07ba4
NJ
1611system supports effective IDs.
1612@end deffn
1613
8f85c0c6
NJ
1614@deffn {Scheme Procedure} getegid
1615@deffnx {C Function} scm_getegid ()
a0e07ba4
NJ
1616Return an integer representing the current effective group ID.
1617If the system does not support effective IDs, then the real ID
66add4eb 1618is returned. @code{(provided? 'EIDs)} reports whether the
a0e07ba4
NJ
1619system supports effective IDs.
1620@end deffn
1621
ef048324
KR
1622@deffn {Scheme Procedure} setgroups vec
1623@deffnx {C Function} scm_setgroups (vec)
1624Set the current set of supplementary group IDs to the integers in the
1625given vector @var{vec}. The return value is unspecified.
1626
1627Generally only the superuser can set the process group IDs
1628(@pxref{Setting Groups, Setting the Group IDs,, libc, The GNU C
1629Library Reference Manual}).
1630@end deffn
1631
8f85c0c6
NJ
1632@deffn {Scheme Procedure} setuid id
1633@deffnx {C Function} scm_setuid (id)
a0e07ba4
NJ
1634Sets both the real and effective user IDs to the integer @var{id}, provided
1635the process has appropriate privileges.
1636The return value is unspecified.
1637@end deffn
1638
8f85c0c6
NJ
1639@deffn {Scheme Procedure} setgid id
1640@deffnx {C Function} scm_setgid (id)
a0e07ba4
NJ
1641Sets both the real and effective group IDs to the integer @var{id}, provided
1642the process has appropriate privileges.
1643The return value is unspecified.
1644@end deffn
1645
8f85c0c6
NJ
1646@deffn {Scheme Procedure} seteuid id
1647@deffnx {C Function} scm_seteuid (id)
a0e07ba4
NJ
1648Sets the effective user ID to the integer @var{id}, provided the process
1649has appropriate privileges. If effective IDs are not supported, the
7403e409 1650real ID is set instead---@code{(provided? 'EIDs)} reports whether the
a0e07ba4
NJ
1651system supports effective IDs.
1652The return value is unspecified.
1653@end deffn
1654
8f85c0c6
NJ
1655@deffn {Scheme Procedure} setegid id
1656@deffnx {C Function} scm_setegid (id)
a0e07ba4
NJ
1657Sets the effective group ID to the integer @var{id}, provided the process
1658has appropriate privileges. If effective IDs are not supported, the
7403e409 1659real ID is set instead---@code{(provided? 'EIDs)} reports whether the
a0e07ba4
NJ
1660system supports effective IDs.
1661The return value is unspecified.
1662@end deffn
1663
8f85c0c6
NJ
1664@deffn {Scheme Procedure} getpgrp
1665@deffnx {C Function} scm_getpgrp ()
a0e07ba4 1666Return an integer representing the current process group ID.
7403e409 1667This is the @acronym{POSIX} definition, not @acronym{BSD}.
a0e07ba4
NJ
1668@end deffn
1669
8f85c0c6
NJ
1670@deffn {Scheme Procedure} setpgid pid pgid
1671@deffnx {C Function} scm_setpgid (pid, pgid)
a0e07ba4
NJ
1672Move the process @var{pid} into the process group @var{pgid}. @var{pid} or
1673@var{pgid} must be integers: they can be zero to indicate the ID of the
1674current process.
1675Fails on systems that do not support job control.
1676The return value is unspecified.
1677@end deffn
1678
8f85c0c6
NJ
1679@deffn {Scheme Procedure} setsid
1680@deffnx {C Function} scm_setsid ()
a0e07ba4
NJ
1681Creates a new session. The current process becomes the session leader
1682and is put in a new process group. The process will be detached
1683from its controlling terminal if it has one.
1684The return value is an integer representing the new process group ID.
1685@end deffn
1686
211a5b04
NJ
1687@deffn {Scheme Procedure} getsid pid
1688@deffnx {C Function} scm_getsid (pid)
1689Returns the session ID of process @var{pid}. (The session
1690ID of a process is the process group ID of its session leader.)
1691@end deffn
1692
8f85c0c6
NJ
1693@deffn {Scheme Procedure} waitpid pid [options]
1694@deffnx {C Function} scm_waitpid (pid, options)
a0e07ba4
NJ
1695This procedure collects status information from a child process which
1696has terminated or (optionally) stopped. Normally it will
1697suspend the calling process until this can be done. If more than one
1698child process is eligible then one will be chosen by the operating system.
1699
1700The value of @var{pid} determines the behaviour:
1701
7403e409 1702@table @asis
a0e07ba4
NJ
1703@item @var{pid} greater than 0
1704Request status information from the specified child process.
7403e409 1705@item @var{pid} equal to -1 or @code{WAIT_ANY}
2ce02471 1706@vindex WAIT_ANY
a0e07ba4 1707Request status information for any child process.
7403e409 1708@item @var{pid} equal to 0 or @code{WAIT_MYPGRP}
2ce02471 1709@vindex WAIT_MYPGRP
a0e07ba4
NJ
1710Request status information for any child process in the current process
1711group.
1712@item @var{pid} less than -1
1713Request status information for any child process whose process group ID
7403e409 1714is @minus{}@var{pid}.
a0e07ba4
NJ
1715@end table
1716
1717The @var{options} argument, if supplied, should be the bitwise OR of the
1718values of zero or more of the following variables:
1719
1720@defvar WNOHANG
1721Return immediately even if there are no child processes to be collected.
1722@end defvar
1723
1724@defvar WUNTRACED
1725Report status information for stopped processes as well as terminated
1726processes.
1727@end defvar
1728
1729The return value is a pair containing:
1730
1731@enumerate
1732@item
1733The process ID of the child process, or 0 if @code{WNOHANG} was
1734specified and no process was collected.
1735@item
1736The integer status value.
1737@end enumerate
1738@end deffn
1739
1740The following three
1741functions can be used to decode the process status code returned
1742by @code{waitpid}.
1743
8f85c0c6
NJ
1744@deffn {Scheme Procedure} status:exit-val status
1745@deffnx {C Function} scm_status_exit_val (status)
a0e07ba4
NJ
1746Return the exit status value, as would be set if a process
1747ended normally through a call to @code{exit} or @code{_exit},
1748if any, otherwise @code{#f}.
1749@end deffn
1750
8f85c0c6
NJ
1751@deffn {Scheme Procedure} status:term-sig status
1752@deffnx {C Function} scm_status_term_sig (status)
a0e07ba4
NJ
1753Return the signal number which terminated the process, if any,
1754otherwise @code{#f}.
1755@end deffn
1756
8f85c0c6
NJ
1757@deffn {Scheme Procedure} status:stop-sig status
1758@deffnx {C Function} scm_status_stop_sig (status)
a0e07ba4
NJ
1759Return the signal number which stopped the process, if any,
1760otherwise @code{#f}.
1761@end deffn
1762
8f85c0c6
NJ
1763@deffn {Scheme Procedure} system [cmd]
1764@deffnx {C Function} scm_system (cmd)
7403e409
NJ
1765Execute @var{cmd} using the operating system's ``command
1766processor''. Under Unix this is usually the default shell
a0e07ba4
NJ
1767@code{sh}. The value returned is @var{cmd}'s exit status as
1768returned by @code{waitpid}, which can be interpreted using the
1769functions above.
1770
1771If @code{system} is called without arguments, return a boolean
1772indicating whether the command processor is available.
1773@end deffn
1774
df0a1002 1775@deffn {Scheme Procedure} system* arg1 arg2 @dots{}
8141bd98 1776@deffnx {C Function} scm_system_star (args)
df0a1002
BT
1777Execute the command indicated by @var{arg1} @var{arg2} @enddots{}. The
1778first element must be a string indicating the command to be executed,
1779and the remaining items must be strings representing each of the
1780arguments to that command.
8141bd98
RB
1781
1782This function returns the exit status of the command as provided by
1783@code{waitpid}. This value can be handled with @code{status:exit-val}
1784and the related functions.
1785
1786@code{system*} is similar to @code{system}, but accepts only one
1787string per-argument, and performs no shell interpretation. The
1788command is executed using fork and execlp. Accordingly this function
1789may be safer than @code{system} in situations where shell
1790interpretation is not required.
1791
1792Example: (system* "echo" "foo" "bar")
1793@end deffn
1794
14ae4725
MG
1795@deffn {Scheme Procedure} quit [status]
1796@deffnx {Scheme Procedure} exit [status]
1797Terminate the current process with proper unwinding of the Scheme stack.
1798The exit status zero if @var{status} is not supplied. If @var{status}
1799is supplied, and it is an integer, that integer is used as the exit
447af515
LC
1800status. If @var{status} is @code{#t} or @code{#f}, the exit status is
1801@var{EXIT_SUCCESS} or @var{EXIT_FAILURE}, respectively.
14ae4725
MG
1802
1803The procedure @code{exit} is an alias of @code{quit}. They have the
1804same functionality.
1805@end deffn
1806
447af515
LC
1807@defvr {Scheme Variable} EXIT_SUCCESS
1808@defvrx {Scheme Variable} EXIT_FAILURE
1809These constants represent the standard exit codes for success (zero) or
1810failure (one.)
1811@end defvr
1812
8f85c0c6 1813@deffn {Scheme Procedure} primitive-exit [status]
23f2b9a3 1814@deffnx {Scheme Procedure} primitive-_exit [status]
8f85c0c6 1815@deffnx {C Function} scm_primitive_exit (status)
23f2b9a3
KR
1816@deffnx {C Function} scm_primitive__exit (status)
1817Terminate the current process without unwinding the Scheme stack. The
1818exit status is @var{status} if supplied, otherwise zero.
1819
1820@code{primitive-exit} uses the C @code{exit} function and hence runs
1821usual C level cleanups (flush output streams, call @code{atexit}
1822functions, etc, see @ref{Normal Termination,,, libc, The GNU C Library
1823Reference Manual})).
1824
1825@code{primitive-_exit} is the @code{_exit} system call
1826(@pxref{Termination Internals,,, libc, The GNU C Library Reference
1827Manual}). This terminates the program immediately, with neither
1828Scheme-level nor C-level cleanups.
1829
1830The typical use for @code{primitive-_exit} is from a child process
1831created with @code{primitive-fork}. For example in a Gdk program the
1832child process inherits the X server connection and a C-level
1833@code{atexit} cleanup which will close that connection. But closing
1834in the child would upset the protocol in the parent, so
1835@code{primitive-_exit} should be used to exit without that.
a0e07ba4
NJ
1836@end deffn
1837
df0a1002 1838@deffn {Scheme Procedure} execl filename arg @dots{}
8f85c0c6 1839@deffnx {C Function} scm_execl (filename, args)
64de6db5 1840Executes the file named by @var{filename} as a new process image.
a0e07ba4 1841The remaining arguments are supplied to the process; from a C program
85a9b4ed 1842they are accessible as the @code{argv} argument to @code{main}.
64de6db5 1843Conventionally the first @var{arg} is the same as @var{filename}.
a0e07ba4
NJ
1844All arguments must be strings.
1845
64de6db5 1846If @var{arg} is missing, @var{filename} is executed with a null
a0e07ba4
NJ
1847argument list, which may have system-dependent side-effects.
1848
1849This procedure is currently implemented using the @code{execv} system
1850call, but we call it @code{execl} because of its Scheme calling interface.
1851@end deffn
1852
df0a1002 1853@deffn {Scheme Procedure} execlp filename arg @dots{}
8f85c0c6 1854@deffnx {C Function} scm_execlp (filename, args)
a0e07ba4
NJ
1855Similar to @code{execl}, however if
1856@var{filename} does not contain a slash
1857then the file to execute will be located by searching the
1858directories listed in the @code{PATH} environment variable.
1859
1860This procedure is currently implemented using the @code{execvp} system
1861call, but we call it @code{execlp} because of its Scheme calling interface.
1862@end deffn
1863
df0a1002 1864@deffn {Scheme Procedure} execle filename env arg @dots{}
8f85c0c6 1865@deffnx {C Function} scm_execle (filename, env, args)
a0e07ba4
NJ
1866Similar to @code{execl}, but the environment of the new process is
1867specified by @var{env}, which must be a list of strings as returned by the
1868@code{environ} procedure.
1869
1870This procedure is currently implemented using the @code{execve} system
1871call, but we call it @code{execle} because of its Scheme calling interface.
1872@end deffn
1873
8f85c0c6
NJ
1874@deffn {Scheme Procedure} primitive-fork
1875@deffnx {C Function} scm_fork ()
7403e409 1876Creates a new ``child'' process by duplicating the current ``parent'' process.
a0e07ba4
NJ
1877In the child the return value is 0. In the parent the return value is
1878the integer process ID of the child.
1879
22cdf986
AW
1880Note that it is unsafe to fork a process that has multiple threads
1881running, as only the thread that calls @code{primitive-fork} will
1882persist in the child. Any resources that other threads held, such as
1883locked mutexes or open file descriptors, are lost. Indeed,
1884@acronym{POSIX} specifies that only async-signal-safe procedures are
1885safe to call after a multithreaded fork, which is a very limited set.
1886Guile issues a warning if it detects a fork from a multi-threaded
1887program.
1888
1889If you are going to @code{exec} soon after forking, the procedures in
1890@code{(ice-9 popen)} may be useful to you, as they fork and exec within
1891an async-signal-safe function carefully written to ensure robust program
1892behavior, even in the presence of threads. @xref{Pipes}, for more.
1893
a0e07ba4
NJ
1894This procedure has been renamed from @code{fork} to avoid a naming conflict
1895with the scsh fork.
1896@end deffn
1897
8f85c0c6
NJ
1898@deffn {Scheme Procedure} nice incr
1899@deffnx {C Function} scm_nice (incr)
f3dfb8ac 1900@cindex process priority
a0e07ba4
NJ
1901Increment the priority of the current process by @var{incr}. A higher
1902priority value means that the process runs less often.
1903The return value is unspecified.
1904@end deffn
1905
8f85c0c6
NJ
1906@deffn {Scheme Procedure} setpriority which who prio
1907@deffnx {C Function} scm_setpriority (which, who, prio)
2ce02471
NJ
1908@vindex PRIO_PROCESS
1909@vindex PRIO_PGRP
1910@vindex PRIO_USER
a0e07ba4
NJ
1911Set the scheduling priority of the process, process group
1912or user, as indicated by @var{which} and @var{who}. @var{which}
1913is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
1914or @code{PRIO_USER}, and @var{who} is interpreted relative to
1915@var{which} (a process identifier for @code{PRIO_PROCESS},
004fe2c8 1916process group identifier for @code{PRIO_PGRP}, and a user
a0e07ba4
NJ
1917identifier for @code{PRIO_USER}. A zero value of @var{who}
1918denotes the current process, process group, or user.
7403e409
NJ
1919@var{prio} is a value in the range [@minus{}20,20]. The default
1920priority is 0; lower priorities (in numerical terms) cause more
1921favorable scheduling. Sets the priority of all of the specified
1922processes. Only the super-user may lower priorities. The return
1923value is not specified.
a0e07ba4
NJ
1924@end deffn
1925
8f85c0c6
NJ
1926@deffn {Scheme Procedure} getpriority which who
1927@deffnx {C Function} scm_getpriority (which, who)
2ce02471
NJ
1928@vindex PRIO_PROCESS
1929@vindex PRIO_PGRP
1930@vindex PRIO_USER
a0e07ba4
NJ
1931Return the scheduling priority of the process, process group
1932or user, as indicated by @var{which} and @var{who}. @var{which}
1933is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}
7403e409 1934or @code{PRIO_USER}, and @var{who} should be interpreted depending on
a0e07ba4
NJ
1935@var{which} (a process identifier for @code{PRIO_PROCESS},
1936process group identifier for @code{PRIO_PGRP}, and a user
7403e409 1937identifier for @code{PRIO_USER}). A zero value of @var{who}
a0e07ba4
NJ
1938denotes the current process, process group, or user. Return
1939the highest priority (lowest numerical value) of any of the
1940specified processes.
1941@end deffn
1942
fe613fe2
LC
1943@cindex affinity, CPU
1944
1945@deffn {Scheme Procedure} getaffinity pid
1946@deffnx {C Function} scm_getaffinity (pid)
1947Return a bitvector representing the CPU affinity mask for
1948process @var{pid}. Each CPU the process has affinity with
1949has its corresponding bit set in the returned bitvector.
1950The number of bits set is a good estimate of how many CPUs
1951Guile can use without stepping on other processes' toes.
1952
3ae78cac
LC
1953Currently this procedure is only defined on GNU variants
1954(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The
1955GNU C Library Reference Manual}).
fe613fe2
LC
1956@end deffn
1957
1958@deffn {Scheme Procedure} setaffinity pid mask
1959@deffnx {C Function} scm_setaffinity (pid, mask)
1960Install the CPU affinity mask @var{mask}, a bitvector, for
1961the process or thread with ID @var{pid}. The return value
1962is unspecified.
1963
3ae78cac
LC
1964Currently this procedure is only defined on GNU variants
1965(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The
1966GNU C Library Reference Manual}).
fe613fe2
LC
1967@end deffn
1968
f0c0141f
LC
1969@deffn {Scheme Procedure} total-processor-count
1970@deffnx {C Function} scm_total_processor_count ()
1971Return the total number of processors of the machine, which
1972is guaranteed to be at least 1. A ``processor'' here is a
1973thread execution unit, which can be either:
1974
1975@itemize
1976@item an execution core in a (possibly multi-core) chip, in a
1977 (possibly multi- chip) module, in a single computer, or
1978@item a thread execution unit inside a core in the case of
1979 @dfn{hyper-threaded} CPUs.
1980@end itemize
1981
1982Which of the two definitions is used, is unspecified.
1983@end deffn
1984
1985@deffn {Scheme Procedure} current-processor-count
1986@deffnx {C Function} scm_current_processor_count ()
1987Like @code{total-processor-count}, but return the number of
1988processors available to the current process. See
1989@code{setaffinity} and @code{getaffinity} for more
1990information.
1991@end deffn
1992
a0e07ba4
NJ
1993
1994@node Signals
3229f68b 1995@subsection Signals
f3dfb8ac 1996@cindex signal
a0e07ba4 1997
bf5df489
KR
1998The following procedures raise, handle and wait for signals.
1999
2000Scheme code signal handlers are run via a system async (@pxref{System
2001asyncs}), so they're called in the handler's thread at the next safe
2002opportunity. Generally this is after any currently executing
2003primitive procedure finishes (which could be a long time for
2004primitives that wait for an external event).
a0e07ba4 2005
8f85c0c6
NJ
2006@deffn {Scheme Procedure} kill pid sig
2007@deffnx {C Function} scm_kill (pid, sig)
a0e07ba4
NJ
2008Sends a signal to the specified process or group of processes.
2009
2010@var{pid} specifies the processes to which the signal is sent:
2011
7403e409 2012@table @asis
a0e07ba4
NJ
2013@item @var{pid} greater than 0
2014The process whose identifier is @var{pid}.
2015@item @var{pid} equal to 0
2016All processes in the current process group.
2017@item @var{pid} less than -1
2018The process group whose identifier is -@var{pid}
2019@item @var{pid} equal to -1
2020If the process is privileged, all processes except for some special
2021system processes. Otherwise, all processes with the current effective
2022user ID.
2023@end table
2024
2025@var{sig} should be specified using a variable corresponding to
2026the Unix symbolic name, e.g.,
2027
2028@defvar SIGHUP
2029Hang-up signal.
2030@end defvar
2031
2032@defvar SIGINT
2033Interrupt signal.
2034@end defvar
7403e409
NJ
2035
2036A full list of signals on the GNU system may be found in @ref{Standard
2037Signals,,,libc,The GNU C Library Reference Manual}.
a0e07ba4
NJ
2038@end deffn
2039
8f85c0c6
NJ
2040@deffn {Scheme Procedure} raise sig
2041@deffnx {C Function} scm_raise (sig)
a0e07ba4 2042Sends a specified signal @var{sig} to the current process, where
7403e409 2043@var{sig} is as described for the @code{kill} procedure.
a0e07ba4
NJ
2044@end deffn
2045
b6506f45 2046@deffn {Scheme Procedure} sigaction signum [handler [flags [thread]]]
8f85c0c6 2047@deffnx {C Function} scm_sigaction (signum, handler, flags)
b6506f45 2048@deffnx {C Function} scm_sigaction_for_thread (signum, handler, flags, thread)
a0e07ba4
NJ
2049Install or report the signal handler for a specified signal.
2050
2051@var{signum} is the signal number, which can be specified using the value
2052of variables such as @code{SIGINT}.
2053
b6506f45 2054If @var{handler} is omitted, @code{sigaction} returns a pair: the
7403e409
NJ
2055@acronym{CAR} is the current signal hander, which will be either an
2056integer with the value @code{SIG_DFL} (default action) or
2057@code{SIG_IGN} (ignore), or the Scheme procedure which handles the
2058signal, or @code{#f} if a non-Scheme procedure handles the signal.
2059The @acronym{CDR} contains the current @code{sigaction} flags for the
2060handler.
a0e07ba4 2061
b6506f45 2062If @var{handler} is provided, it is installed as the new handler for
0a50eeaa
NJ
2063@var{signum}. @var{handler} can be a Scheme procedure taking one
2064argument, or the value of @code{SIG_DFL} (default action) or
a0e07ba4 2065@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler
b6506f45
MV
2066was installed before @code{sigaction} was first used. When a scheme
2067procedure has been specified, that procedure will run in the given
2068@var{thread}. When no thread has been given, the thread that made this
2069call to @code{sigaction} is used.
2070
91f5e9f7
KR
2071@var{flags} is a @code{logior} (@pxref{Bitwise Operations}) of the
2072following (where provided by the system), or @code{0} for none.
2073
2074@defvar SA_NOCLDSTOP
2075By default, @code{SIGCHLD} is signalled when a child process stops
2076(ie.@: receives @code{SIGSTOP}), and when a child process terminates.
2077With the @code{SA_NOCLDSTOP} flag, @code{SIGCHLD} is only signalled
2078for termination, not stopping.
2079
2080@code{SA_NOCLDSTOP} has no effect on signals other than
2081@code{SIGCHLD}.
2082@end defvar
2083
2084@defvar SA_RESTART
2085If a signal occurs while in a system call, deliver the signal then
2086restart the system call (as opposed to returning an @code{EINTR} error
2087from that call).
91f5e9f7
KR
2088@end defvar
2089
2090The return value is a pair with information about the old handler as
2091described above.
a0e07ba4 2092
7403e409 2093This interface does not provide access to the ``signal blocking''
a0e07ba4
NJ
2094facility. Maybe this is not needed, since the thread support may
2095provide solutions to the problem of consistent access to data
2096structures.
2097@end deffn
2098
8f85c0c6
NJ
2099@deffn {Scheme Procedure} restore-signals
2100@deffnx {C Function} scm_restore_signals ()
a0e07ba4
NJ
2101Return all signal handlers to the values they had before any call to
2102@code{sigaction} was made. The return value is unspecified.
2103@end deffn
2104
8f85c0c6
NJ
2105@deffn {Scheme Procedure} alarm i
2106@deffnx {C Function} scm_alarm (i)
a0e07ba4
NJ
2107Set a timer to raise a @code{SIGALRM} signal after the specified
2108number of seconds (an integer). It's advisable to install a signal
2109handler for
2110@code{SIGALRM} beforehand, since the default action is to terminate
2111the process.
2112
2113The return value indicates the time remaining for the previous alarm,
2114if any. The new value replaces the previous alarm. If there was
2115no previous alarm, the return value is zero.
2116@end deffn
2117
8f85c0c6
NJ
2118@deffn {Scheme Procedure} pause
2119@deffnx {C Function} scm_pause ()
a0e07ba4
NJ
2120Pause the current process (thread?) until a signal arrives whose
2121action is to either terminate the current process or invoke a
2122handler procedure. The return value is unspecified.
2123@end deffn
2124
bf5df489
KR
2125@deffn {Scheme Procedure} sleep secs
2126@deffnx {Scheme Procedure} usleep usecs
2127@deffnx {C Function} scm_sleep (secs)
2128@deffnx {C Function} scm_usleep (usecs)
2129Wait the given period @var{secs} seconds or @var{usecs} microseconds
2130(both integers). If a signal arrives the wait stops and the return
2131value is the time remaining, in seconds or microseconds respectively.
2132If the period elapses with no signal the return is zero.
9401323e 2133
bf5df489
KR
2134On most systems the process scheduler is not microsecond accurate and
2135the actual period slept by @code{usleep} might be rounded to a system
2136clock tick boundary, which might be 10 milliseconds for instance.
9401323e 2137
bf5df489
KR
2138See @code{scm_std_sleep} and @code{scm_std_usleep} for equivalents at
2139the C level (@pxref{Blocking}).
a0e07ba4
NJ
2140@end deffn
2141
8f85c0c6 2142@deffn {Scheme Procedure} getitimer which_timer
966d4bdd 2143@deffnx {Scheme Procedure} setitimer which_timer interval_seconds interval_microseconds value_seconds value_microseconds
8f85c0c6 2144@deffnx {C Function} scm_getitimer (which_timer)
966d4bdd
AW
2145@deffnx {C Function} scm_setitimer (which_timer, interval_seconds, interval_microseconds, value_seconds, value_microseconds)
2146Get or set the periods programmed in certain system timers.
2147
2148These timers have two settings. The first setting, the interval, is the
2149value at which the timer will be reset when the current timer expires.
2150The second is the current value of the timer, indicating when the next
2151expiry will be signalled.
2152
2153@var{which_timer} is one of the following values:
bf5df489
KR
2154
2155@defvar ITIMER_REAL
2156A real-time timer, counting down elapsed real time. At zero it raises
2157@code{SIGALRM}. This is like @code{alarm} above, but with a higher
2158resolution period.
2159@end defvar
2160
2161@defvar ITIMER_VIRTUAL
2162A virtual-time timer, counting down while the current process is
2163actually using CPU. At zero it raises @code{SIGVTALRM}.
2164@end defvar
2165
2166@defvar ITIMER_PROF
2167A profiling timer, counting down while the process is running (like
2168@code{ITIMER_VIRTUAL}) and also while system calls are running on the
2169process's behalf. At zero it raises a @code{SIGPROF}.
2170
2171This timer is intended for profiling where a program is spending its
2172time (by looking where it is when the timer goes off).
2173@end defvar
2174
966d4bdd
AW
2175@code{getitimer} returns the restart timer value and its current value,
2176as a list containing two pairs. Each pair is a time in seconds and
2177microseconds: @code{((@var{interval_secs} . @var{interval_usecs})
2178(@var{value_secs} . @var{value_usecs}))}.
bf5df489
KR
2179
2180@code{setitimer} sets the timer values similarly, in seconds and
966d4bdd 2181microseconds (which must be integers). The interval value can be zero
bf5df489
KR
2182to have the timer run down just once. The return value is the timer's
2183previous setting, in the same form as @code{getitimer} returns.
9401323e 2184
bf5df489
KR
2185@example
2186(setitimer ITIMER_REAL
966d4bdd
AW
2187 5 500000 ;; Raise SIGALRM every 5.5 seconds
2188 2 0) ;; with the first SIGALRM in 2 seconds
bf5df489 2189@end example
9401323e 2190
bf5df489
KR
2191Although the timers are programmed in microseconds, the actual
2192accuracy might not be that high.
a0e07ba4
NJ
2193@end deffn
2194
2195
2196@node Terminals and Ptys
3229f68b 2197@subsection Terminals and Ptys
a0e07ba4 2198
8f85c0c6
NJ
2199@deffn {Scheme Procedure} isatty? port
2200@deffnx {C Function} scm_isatty_p (port)
f3dfb8ac 2201@cindex terminal
a0e07ba4
NJ
2202Return @code{#t} if @var{port} is using a serial non--file
2203device, otherwise @code{#f}.
2204@end deffn
2205
8f85c0c6
NJ
2206@deffn {Scheme Procedure} ttyname port
2207@deffnx {C Function} scm_ttyname (port)
f3dfb8ac 2208@cindex terminal
a0e07ba4
NJ
2209Return a string with the name of the serial terminal device
2210underlying @var{port}.
2211@end deffn
2212
8f85c0c6
NJ
2213@deffn {Scheme Procedure} ctermid
2214@deffnx {C Function} scm_ctermid ()
f3dfb8ac 2215@cindex terminal
a0e07ba4
NJ
2216Return a string containing the file name of the controlling
2217terminal for the current process.
2218@end deffn
2219
8f85c0c6
NJ
2220@deffn {Scheme Procedure} tcgetpgrp port
2221@deffnx {C Function} scm_tcgetpgrp (port)
f3dfb8ac 2222@cindex process group
a0e07ba4
NJ
2223Return the process group ID of the foreground process group
2224associated with the terminal open on the file descriptor
2225underlying @var{port}.
2226
2227If there is no foreground process group, the return value is a
2228number greater than 1 that does not match the process group ID
2229of any existing process group. This can happen if all of the
2230processes in the job that was formerly the foreground job have
2231terminated, and no other job has yet been moved into the
2232foreground.
2233@end deffn
2234
8f85c0c6
NJ
2235@deffn {Scheme Procedure} tcsetpgrp port pgid
2236@deffnx {C Function} scm_tcsetpgrp (port, pgid)
f3dfb8ac 2237@cindex process group
a0e07ba4
NJ
2238Set the foreground process group ID for the terminal used by the file
2239descriptor underlying @var{port} to the integer @var{pgid}.
2240The calling process
2241must be a member of the same session as @var{pgid} and must have the same
2242controlling terminal. The return value is unspecified.
2243@end deffn
2244
2245@node Pipes
3229f68b 2246@subsection Pipes
f3dfb8ac 2247@cindex pipe
a0e07ba4 2248
cb62d8e5 2249The following procedures are similar to the @code{popen} and
7403e409 2250@code{pclose} system routines. The code is in a separate ``popen''
df3d365a
LC
2251module@footnote{This module is only available on systems where the
2252@code{fork} feature is provided (@pxref{Common Feature Symbols}).}:
a0e07ba4 2253
aba0dff5 2254@lisp
a0e07ba4 2255(use-modules (ice-9 popen))
aba0dff5 2256@end lisp
a0e07ba4
NJ
2257
2258@findex popen
cb62d8e5
KR
2259@deffn {Scheme Procedure} open-pipe command mode
2260@deffnx {Scheme Procedure} open-pipe* mode prog [args...]
2261Execute a command in a subprocess, with a pipe to it or from it, or
2262with pipes in both directions.
2263
2264@code{open-pipe} runs the shell @var{command} using @samp{/bin/sh -c}.
2265@code{open-pipe*} executes @var{prog} directly, with the optional
2266@var{args} arguments (all strings).
2267
2268@var{mode} should be one of the following values. @code{OPEN_READ} is
2269an input pipe, ie.@: to read from the subprocess. @code{OPEN_WRITE}
2270is an output pipe, ie.@: to write to it.
2271
2272@defvar OPEN_READ
2273@defvarx OPEN_WRITE
2274@defvarx OPEN_BOTH
2275@end defvar
2276
2277For an input pipe, the child's standard output is the pipe and
2278standard input is inherited from @code{current-input-port}. For an
2279output pipe, the child's standard input is the pipe and standard
2280output is inherited from @code{current-output-port}. In all cases
2281cases the child's standard error is inherited from
2282@code{current-error-port} (@pxref{Default Ports}).
2283
2284If those @code{current-X-ports} are not files of some kind, and hence
2285don't have file descriptors for the child, then @file{/dev/null} is
2286used instead.
7064e449 2287
cb62d8e5
KR
2288Care should be taken with @code{OPEN_BOTH}, a deadlock will occur if
2289both parent and child are writing, and waiting until the write
2290completes before doing any reading. Each direction has
2291@code{PIPE_BUF} bytes of buffering (@pxref{Ports and File
2292Descriptors}), which will be enough for small writes, but not for say
2293putting a big file through a filter.
a0e07ba4
NJ
2294@end deffn
2295
8f85c0c6 2296@deffn {Scheme Procedure} open-input-pipe command
a0e07ba4 2297Equivalent to @code{open-pipe} with mode @code{OPEN_READ}.
bcf009c3
NJ
2298
2299@lisp
cb62d8e5
KR
2300(let* ((port (open-input-pipe "date --utc"))
2301 (str (read-line port)))
2302 (close-pipe port)
2303 str)
2304@result{} "Mon Mar 11 20:10:44 UTC 2002"
bcf009c3 2305@end lisp
a0e07ba4
NJ
2306@end deffn
2307
8f85c0c6 2308@deffn {Scheme Procedure} open-output-pipe command
a0e07ba4 2309Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}.
cb62d8e5
KR
2310
2311@lisp
2312(let ((port (open-output-pipe "lpr")))
2313 (display "Something for the line printer.\n" port)
2314 (if (not (eqv? 0 (status:exit-val (close-pipe port))))
2315 (error "Cannot print")))
2316@end lisp
a0e07ba4
NJ
2317@end deffn
2318
7064e449
MV
2319@deffn {Scheme Procedure} open-input-output-pipe command
2320Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}.
2321@end deffn
2322
a0e07ba4 2323@findex pclose
8f85c0c6 2324@deffn {Scheme Procedure} close-pipe port
cb62d8e5
KR
2325Close a pipe created by @code{open-pipe}, wait for the process to
2326terminate, and return the wait status code. The status is as per
2327@code{waitpid} and can be decoded with @code{status:exit-val} etc
2328(@pxref{Processes})
a0e07ba4
NJ
2329@end deffn
2330
cb62d8e5
KR
2331@sp 1
2332@code{waitpid WAIT_ANY} should not be used when pipes are open, since
2333it can reap a pipe's child process, causing an error from a subsequent
2334@code{close-pipe}.
2335
2336@code{close-port} (@pxref{Closing}) can close a pipe, but it doesn't
2337reap the child process.
2338
2339The garbage collector will close a pipe no longer in use, and reap the
2340child process with @code{waitpid}. If the child hasn't yet terminated
2341the garbage collector doesn't block, but instead checks again in the
2342next GC.
2343
2344Many systems have per-user and system-wide limits on the number of
2345processes, and a system-wide limit on the number of pipes, so pipes
2346should be closed explicitly when no longer needed, rather than letting
2347the garbage collector pick them up at some later time.
2348
2349
a0e07ba4 2350@node Networking
3229f68b 2351@subsection Networking
f3dfb8ac 2352@cindex network
a0e07ba4
NJ
2353
2354@menu
13ed23db
KR
2355* Network Address Conversion::
2356* Network Databases::
2357* Network Socket Address::
2358* Network Sockets and Communication::
2359* Internet Socket Examples::
a0e07ba4
NJ
2360@end menu
2361
2362@node Network Address Conversion
3229f68b 2363@subsubsection Network Address Conversion
f3dfb8ac 2364@cindex network address
a0e07ba4
NJ
2365
2366This section describes procedures which convert internet addresses
2367between numeric and string formats.
2368
3229f68b 2369@subsubheading IPv4 Address Conversion
f3dfb8ac 2370@cindex IPv4
a0e07ba4 2371
957f9f62 2372An IPv4 Internet address is a 4-byte value, represented in Guile as an
99d16776
KR
2373integer in host byte order, so that say ``0.0.0.1'' is 1, or
2374``1.0.0.0'' is 16777216.
2375
2376Some underlying C functions use network byte order for addresses,
2377Guile converts as necessary so that at the Scheme level its host byte
2378order everywhere.
957f9f62 2379
13ed23db
KR
2380@defvar INADDR_ANY
2381For a server, this can be used with @code{bind} (@pxref{Network
2382Sockets and Communication}) to allow connections from any interface on
2383the machine.
957f9f62
KR
2384@end defvar
2385
2386@defvar INADDR_BROADCAST
2387The broadcast address on the local network.
2388@end defvar
2389
13ed23db
KR
2390@defvar INADDR_LOOPBACK
2391The address of the local host using the loopback device, ie.@:
2392@samp{127.0.0.1}.
2393@end defvar
2394
957f9f62
KR
2395@c INADDR_NONE is defined in the code, but serves no purpose.
2396@c inet_addr() returns it as an error indication, but that function
2397@c isn't provided, for the good reason that inet_aton() does the same
2398@c job and gives an unambiguous error indication. (INADDR_NONE is a
2399@c valid 4-byte value, in glibc it's the same as INADDR_BROADCAST.)
2400@c
2401@c @defvar INADDR_NONE
2402@c No address.
2403@c @end defvar
2404
8f85c0c6
NJ
2405@deffn {Scheme Procedure} inet-aton address
2406@deffnx {C Function} scm_inet_aton (address)
3452e666
LC
2407This function is deprecated in favor of @code{inet-pton}.
2408
a0e07ba4
NJ
2409Convert an IPv4 Internet address from printable string
2410(dotted decimal notation) to an integer. E.g.,
2411
2412@lisp
2413(inet-aton "127.0.0.1") @result{} 2130706433
2414@end lisp
2415@end deffn
2416
8f85c0c6
NJ
2417@deffn {Scheme Procedure} inet-ntoa inetid
2418@deffnx {C Function} scm_inet_ntoa (inetid)
3452e666
LC
2419This function is deprecated in favor of @code{inet-ntop}.
2420
a0e07ba4
NJ
2421Convert an IPv4 Internet address to a printable
2422(dotted decimal notation) string. E.g.,
2423
2424@lisp
2425(inet-ntoa 2130706433) @result{} "127.0.0.1"
2426@end lisp
2427@end deffn
2428
8f85c0c6
NJ
2429@deffn {Scheme Procedure} inet-netof address
2430@deffnx {C Function} scm_inet_netof (address)
a0e07ba4
NJ
2431Return the network number part of the given IPv4
2432Internet address. E.g.,
2433
2434@lisp
2435(inet-netof 2130706433) @result{} 127
2436@end lisp
2437@end deffn
2438
8f85c0c6
NJ
2439@deffn {Scheme Procedure} inet-lnaof address
2440@deffnx {C Function} scm_lnaof (address)
a0e07ba4
NJ
2441Return the local-address-with-network part of the given
2442IPv4 Internet address, using the obsolete class A/B/C system.
2443E.g.,
2444
2445@lisp
2446(inet-lnaof 2130706433) @result{} 1
2447@end lisp
2448@end deffn
2449
8f85c0c6
NJ
2450@deffn {Scheme Procedure} inet-makeaddr net lna
2451@deffnx {C Function} scm_inet_makeaddr (net, lna)
a0e07ba4
NJ
2452Make an IPv4 Internet address by combining the network number
2453@var{net} with the local-address-within-network number
2454@var{lna}. E.g.,
2455
2456@lisp
2457(inet-makeaddr 127 1) @result{} 2130706433
2458@end lisp
2459@end deffn
2460
3229f68b 2461@subsubheading IPv6 Address Conversion
f3dfb8ac 2462@cindex IPv6
a0e07ba4 2463
99d16776
KR
2464An IPv6 Internet address is a 16-byte value, represented in Guile as
2465an integer in host byte order, so that say ``::1'' is 1.
2466
8f85c0c6
NJ
2467@deffn {Scheme Procedure} inet-ntop family address
2468@deffnx {C Function} scm_inet_ntop (family, address)
99d16776 2469Convert a network address from an integer to a printable string.
a0e07ba4
NJ
2470@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,
2471
2472@lisp
2473(inet-ntop AF_INET 2130706433) @result{} "127.0.0.1"
187a4390
NJ
2474(inet-ntop AF_INET6 (- (expt 2 128) 1))
2475 @result{} "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
a0e07ba4
NJ
2476@end lisp
2477@end deffn
2478
8f85c0c6
NJ
2479@deffn {Scheme Procedure} inet-pton family address
2480@deffnx {C Function} scm_inet_pton (family, address)
99d16776
KR
2481Convert a string containing a printable network address to an integer
2482address. @var{family} can be @code{AF_INET} or @code{AF_INET6}.
2483E.g.,
a0e07ba4
NJ
2484
2485@lisp
2486(inet-pton AF_INET "127.0.0.1") @result{} 2130706433
2487(inet-pton AF_INET6 "::1") @result{} 1
2488@end lisp
2489@end deffn
2490
2491
2492@node Network Databases
3229f68b 2493@subsubsection Network Databases
f3dfb8ac 2494@cindex network database
a0e07ba4
NJ
2495
2496This section describes procedures which query various network databases.
2497Care should be taken when using the database routines since they are not
2498reentrant.
2499
55ae00ea
LC
2500@subsubheading @code{getaddrinfo}
2501
2502@cindex @code{addrinfo} object type
2503@cindex host name lookup
2504@cindex service name lookup
2505
2506The @code{getaddrinfo} procedure maps host and service names to socket addresses
2507and associated information in a protocol-independent way.
2508
2509@deffn {Scheme Procedure} getaddrinfo name service [hint_flags [hint_family [hint_socktype [hint_protocol]]]]
2510@deffnx {C Function} scm_getaddrinfo (name, service, hint_flags, hint_family, hint_socktype, hint_protocol)
2511Return a list of @code{addrinfo} structures containing
2512a socket address and associated information for host @var{name}
2513and/or @var{service} to be used in creating a socket with
2514which to address the specified service.
2515
2516@example
2517(let* ((ai (car (getaddrinfo "www.gnu.org" "http")))
2518 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
d2add8ae 2519 (addrinfo:protocol ai))))
55ae00ea
LC
2520 (connect s (addrinfo:addr ai))
2521 s)
2522@end example
2523
2524When @var{service} is omitted or is @code{#f}, return
2525network-level addresses for @var{name}. When @var{name}
2526is @code{#f} @var{service} must be provided and service
2527locations local to the caller are returned.
2528
2529Additional hints can be provided. When specified,
2530@var{hint_flags} should be a bitwise-or of zero or more
2531constants among the following:
2532
2533@table @code
2534@item AI_PASSIVE
2535Socket address is intended for @code{bind}.
2536
2537@item AI_CANONNAME
2538Request for canonical host name, available via
2539@code{addrinfo:canonname}. This makes sense mainly when
2540DNS lookups are involved.
2541
2542@item AI_NUMERICHOST
2543Specifies that @var{name} is a numeric host address string
2544(e.g., @code{"127.0.0.1"}), meaning that name resolution
2545will not be used.
2546
2547@item AI_NUMERICSERV
2548Likewise, specifies that @var{service} is a numeric port
2549string (e.g., @code{"80"}).
2550
2551@item AI_ADDRCONFIG
2552Return only addresses configured on the local system It is
2553highly recommended to provide this flag when the returned
2554socket addresses are to be used to make connections;
2555otherwise, some of the returned addresses could be unreachable
2556or use a protocol that is not supported.
2557
2558@item AI_V4MAPPED
2559When looking up IPv6 addresses, return mapped IPv4 addresses if
2560there is no IPv6 address available at all.
2561
2562@item AI_ALL
2563If this flag is set along with @code{AI_V4MAPPED} when looking up IPv6
2564addresses, return all IPv6 addresses as well as all IPv4 addresses, the latter
2565mapped to IPv6 format.
2566@end table
2567
2568When given, @var{hint_family} should specify the requested
2569address family, e.g., @code{AF_INET6}. Similarly,
2570@var{hint_socktype} should specify the requested socket type
2571(e.g., @code{SOCK_DGRAM}), and @var{hint_protocol} should
ecb87335 2572specify the requested protocol (its value is interpreted
55ae00ea
LC
2573as in calls to @code{socket}).
2574
2575On error, an exception with key @code{getaddrinfo-error} is
2576thrown, with an error code (an integer) as its argument:
2577
2578@example
2579(catch 'getaddrinfo-error
2580 (lambda ()
2581 (getaddrinfo "www.gnu.org" "gopher"))
2582 (lambda (key errcode)
2583 (cond ((= errcode EAI_SERVICE)
2584 (display "doesn't know about Gopher!\n"))
2585 ((= errcode EAI_NONAME)
2586 (display "www.gnu.org not found\\n"))
2587 (else
2588 (format #t "something wrong: ~a\n"
2589 (gai-strerror errcode))))))
2590@end example
2591
2592Error codes are:
2593
2594@table @code
2595@item EAI_AGAIN
2596The name or service could not be resolved at this time. Future
2597attempts may succeed.
2598
2599@item EAI_BADFLAGS
2600@var{hint_flags} contains an invalid value.
2601
2602@item EAI_FAIL
2603A non-recoverable error occurred when attempting to
2604resolve the name.
2605
2606@item EAI_FAMILY
2607@var{hint_family} was not recognized.
2608
2609@item EAI_NONAME
2610Either @var{name} does not resolve for the supplied parameters,
2611or neither @var{name} nor @var{service} were supplied.
2612
66d86131 2613@item EAI_NODATA
1ba05158
LC
2614This non-POSIX error code can be returned on some systems (GNU
2615and Darwin, at least), for example when @var{name} is known
2616but requests that were made turned out no data. Error handling
66d86131
LC
2617code should be prepared to handle it when it is defined.
2618
55ae00ea
LC
2619@item EAI_SERVICE
2620@var{service} was not recognized for the specified socket type.
2621
2622@item EAI_SOCKTYPE
2623@var{hint_socktype} was not recognized.
2624
2625@item EAI_SYSTEM
5bb40f9d
LC
2626A system error occurred. In C, the error code can be found in
2627@code{errno}; this value is not accessible from Scheme, but in
2628practice it provides little information about the actual error
2629cause.
2630@c See <http://bugs.gnu.org/13958>.
55ae00ea
LC
2631@end table
2632
2633Users are encouraged to read the
2634@url{http://www.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html,
2635"POSIX specification} for more details.
2636@end deffn
2637
2638The following procedures take an @code{addrinfo} object as returned by
2639@code{getaddrinfo}:
2640
2641@deffn {Scheme Procedure} addrinfo:flags ai
2642Return flags for @var{ai} as a bitwise or of @code{AI_} values (see above).
2643@end deffn
2644
2645@deffn {Scheme Procedure} addrinfo:fam ai
2646Return the address family of @var{ai} (a @code{AF_} value).
2647@end deffn
2648
2649@deffn {Scheme Procedure} addrinfo:socktype ai
2650Return the socket type for @var{ai} (a @code{SOCK_} value).
2651@end deffn
2652
2653@deffn {Scheme Procedure} addrinfo:protocol ai
2654Return the protocol of @var{ai}.
2655@end deffn
2656
2657@deffn {Scheme Procedure} addrinfo:addr ai
2658Return the socket address associated with @var{ai} as a @code{sockaddr}
2659object (@pxref{Network Socket Address}).
2660@end deffn
2661
2662@deffn {Scheme Procedure} addrinfo:canonname ai
2663Return a string for the canonical name associated with @var{ai} if
2664the @code{AI_CANONNAME} flag was supplied.
2665@end deffn
2666
3229f68b 2667@subsubheading The Host Database
f3dfb8ac
KR
2668@cindex @file{/etc/hosts}
2669@cindex network database
a0e07ba4
NJ
2670
2671A @dfn{host object} is a structure that represents what is known about a
2672network host, and is the usual way of representing a system's network
2673identity inside software.
2674
2675The following functions accept a host object and return a selected
2676component:
2677
8f85c0c6 2678@deffn {Scheme Procedure} hostent:name host
7403e409 2679The ``official'' hostname for @var{host}.
a0e07ba4 2680@end deffn
8f85c0c6 2681@deffn {Scheme Procedure} hostent:aliases host
a0e07ba4
NJ
2682A list of aliases for @var{host}.
2683@end deffn
8f85c0c6 2684@deffn {Scheme Procedure} hostent:addrtype host
99d16776
KR
2685The host address type, one of the @code{AF} constants, such as
2686@code{AF_INET} or @code{AF_INET6}.
a0e07ba4 2687@end deffn
8f85c0c6 2688@deffn {Scheme Procedure} hostent:length host
a0e07ba4
NJ
2689The length of each address for @var{host}, in bytes.
2690@end deffn
8f85c0c6 2691@deffn {Scheme Procedure} hostent:addr-list host
99d16776
KR
2692The list of network addresses associated with @var{host}. For
2693@code{AF_INET} these are integer IPv4 address (@pxref{Network Address
2694Conversion}).
a0e07ba4
NJ
2695@end deffn
2696
55ae00ea
LC
2697The following procedures can be used to search the host database. However,
2698@code{getaddrinfo} should be preferred over them since it's more generic and
2699thread-safe.
a0e07ba4 2700
8f85c0c6
NJ
2701@deffn {Scheme Procedure} gethost [host]
2702@deffnx {Scheme Procedure} gethostbyname hostname
2703@deffnx {Scheme Procedure} gethostbyaddr address
2704@deffnx {C Function} scm_gethost (host)
a0e07ba4
NJ
2705Look up a host by name or address, returning a host object. The
2706@code{gethost} procedure will accept either a string name or an integer
2707address; if given no arguments, it behaves like @code{gethostent} (see
2708below). If a name or address is supplied but the address can not be
2709found, an error will be thrown to one of the keys:
2710@code{host-not-found}, @code{try-again}, @code{no-recovery} or
2711@code{no-data}, corresponding to the equivalent @code{h_error} values.
2712Unusual conditions may result in errors thrown to the
2713@code{system-error} or @code{misc_error} keys.
bcf009c3
NJ
2714
2715@lisp
2716(gethost "www.gnu.org")
2717@result{} #("www.gnu.org" () 2 4 (3353880842))
2718
2719(gethostbyname "www.emacs.org")
2720@result{} #("emacs.org" ("www.emacs.org") 2 4 (1073448978))
2721@end lisp
a0e07ba4
NJ
2722@end deffn
2723
2724The following procedures may be used to step through the host
2725database from beginning to end.
2726
8f85c0c6 2727@deffn {Scheme Procedure} sethostent [stayopen]
a0e07ba4
NJ
2728Initialize an internal stream from which host objects may be read. This
2729procedure must be called before any calls to @code{gethostent}, and may
2730also be called afterward to reset the host entry stream. If
2731@var{stayopen} is supplied and is not @code{#f}, the database is not
2732closed by subsequent @code{gethostbyname} or @code{gethostbyaddr} calls,
2733possibly giving an efficiency gain.
2734@end deffn
2735
8f85c0c6 2736@deffn {Scheme Procedure} gethostent
a0e07ba4
NJ
2737Return the next host object from the host database, or @code{#f} if
2738there are no more hosts to be found (or an error has been encountered).
2739This procedure may not be used before @code{sethostent} has been called.
2740@end deffn
2741
8f85c0c6 2742@deffn {Scheme Procedure} endhostent
a0e07ba4
NJ
2743Close the stream used by @code{gethostent}. The return value is unspecified.
2744@end deffn
2745
8f85c0c6
NJ
2746@deffn {Scheme Procedure} sethost [stayopen]
2747@deffnx {C Function} scm_sethost (stayopen)
a0e07ba4
NJ
2748If @var{stayopen} is omitted, this is equivalent to @code{endhostent}.
2749Otherwise it is equivalent to @code{sethostent stayopen}.
2750@end deffn
3229f68b
MV
2751
2752@subsubheading The Network Database
f3dfb8ac 2753@cindex network database
a0e07ba4
NJ
2754
2755The following functions accept an object representing a network
2756and return a selected component:
2757
8f85c0c6 2758@deffn {Scheme Procedure} netent:name net
7403e409 2759The ``official'' network name.
a0e07ba4 2760@end deffn
8f85c0c6 2761@deffn {Scheme Procedure} netent:aliases net
a0e07ba4
NJ
2762A list of aliases for the network.
2763@end deffn
8f85c0c6 2764@deffn {Scheme Procedure} netent:addrtype net
a0e07ba4
NJ
2765The type of the network number. Currently, this returns only
2766@code{AF_INET}.
2767@end deffn
8f85c0c6 2768@deffn {Scheme Procedure} netent:net net
a0e07ba4
NJ
2769The network number.
2770@end deffn
2771
2772The following procedures are used to search the network database:
2773
8f85c0c6
NJ
2774@deffn {Scheme Procedure} getnet [net]
2775@deffnx {Scheme Procedure} getnetbyname net-name
2776@deffnx {Scheme Procedure} getnetbyaddr net-number
2777@deffnx {C Function} scm_getnet (net)
a0e07ba4
NJ
2778Look up a network by name or net number in the network database. The
2779@var{net-name} argument must be a string, and the @var{net-number}
2780argument must be an integer. @code{getnet} will accept either type of
2781argument, behaving like @code{getnetent} (see below) if no arguments are
2782given.
2783@end deffn
2784
2785The following procedures may be used to step through the network
2786database from beginning to end.
2787
8f85c0c6 2788@deffn {Scheme Procedure} setnetent [stayopen]
a0e07ba4
NJ
2789Initialize an internal stream from which network objects may be read. This
2790procedure must be called before any calls to @code{getnetent}, and may
2791also be called afterward to reset the net entry stream. If
2792@var{stayopen} is supplied and is not @code{#f}, the database is not
2793closed by subsequent @code{getnetbyname} or @code{getnetbyaddr} calls,
2794possibly giving an efficiency gain.
2795@end deffn
2796
8f85c0c6 2797@deffn {Scheme Procedure} getnetent
a0e07ba4
NJ
2798Return the next entry from the network database.
2799@end deffn
2800
8f85c0c6 2801@deffn {Scheme Procedure} endnetent
a0e07ba4
NJ
2802Close the stream used by @code{getnetent}. The return value is unspecified.
2803@end deffn
2804
8f85c0c6
NJ
2805@deffn {Scheme Procedure} setnet [stayopen]
2806@deffnx {C Function} scm_setnet (stayopen)
a0e07ba4
NJ
2807If @var{stayopen} is omitted, this is equivalent to @code{endnetent}.
2808Otherwise it is equivalent to @code{setnetent stayopen}.
2809@end deffn
2810
3229f68b 2811@subsubheading The Protocol Database
f3dfb8ac
KR
2812@cindex @file{/etc/protocols}
2813@cindex protocols
2814@cindex network protocols
a0e07ba4
NJ
2815
2816The following functions accept an object representing a protocol
2817and return a selected component:
2818
8f85c0c6 2819@deffn {Scheme Procedure} protoent:name protocol
7403e409 2820The ``official'' protocol name.
a0e07ba4 2821@end deffn
8f85c0c6 2822@deffn {Scheme Procedure} protoent:aliases protocol
a0e07ba4
NJ
2823A list of aliases for the protocol.
2824@end deffn
8f85c0c6 2825@deffn {Scheme Procedure} protoent:proto protocol
a0e07ba4
NJ
2826The protocol number.
2827@end deffn
2828
2829The following procedures are used to search the protocol database:
2830
8f85c0c6
NJ
2831@deffn {Scheme Procedure} getproto [protocol]
2832@deffnx {Scheme Procedure} getprotobyname name
2833@deffnx {Scheme Procedure} getprotobynumber number
2834@deffnx {C Function} scm_getproto (protocol)
a0e07ba4
NJ
2835Look up a network protocol by name or by number. @code{getprotobyname}
2836takes a string argument, and @code{getprotobynumber} takes an integer
2837argument. @code{getproto} will accept either type, behaving like
2838@code{getprotoent} (see below) if no arguments are supplied.
2839@end deffn
2840
2841The following procedures may be used to step through the protocol
2842database from beginning to end.
2843
8f85c0c6 2844@deffn {Scheme Procedure} setprotoent [stayopen]
a0e07ba4
NJ
2845Initialize an internal stream from which protocol objects may be read. This
2846procedure must be called before any calls to @code{getprotoent}, and may
2847also be called afterward to reset the protocol entry stream. If
2848@var{stayopen} is supplied and is not @code{#f}, the database is not
2849closed by subsequent @code{getprotobyname} or @code{getprotobynumber} calls,
2850possibly giving an efficiency gain.
2851@end deffn
2852
8f85c0c6 2853@deffn {Scheme Procedure} getprotoent
a0e07ba4
NJ
2854Return the next entry from the protocol database.
2855@end deffn
2856
8f85c0c6 2857@deffn {Scheme Procedure} endprotoent
a0e07ba4
NJ
2858Close the stream used by @code{getprotoent}. The return value is unspecified.
2859@end deffn
2860
8f85c0c6
NJ
2861@deffn {Scheme Procedure} setproto [stayopen]
2862@deffnx {C Function} scm_setproto (stayopen)
a0e07ba4
NJ
2863If @var{stayopen} is omitted, this is equivalent to @code{endprotoent}.
2864Otherwise it is equivalent to @code{setprotoent stayopen}.
2865@end deffn
2866
3229f68b 2867@subsubheading The Service Database
f3dfb8ac
KR
2868@cindex @file{/etc/services}
2869@cindex services
2870@cindex network services
a0e07ba4
NJ
2871
2872The following functions accept an object representing a service
2873and return a selected component:
2874
8f85c0c6 2875@deffn {Scheme Procedure} servent:name serv
7403e409 2876The ``official'' name of the network service.
a0e07ba4 2877@end deffn
8f85c0c6 2878@deffn {Scheme Procedure} servent:aliases serv
a0e07ba4
NJ
2879A list of aliases for the network service.
2880@end deffn
8f85c0c6 2881@deffn {Scheme Procedure} servent:port serv
a0e07ba4
NJ
2882The Internet port used by the service.
2883@end deffn
8f85c0c6 2884@deffn {Scheme Procedure} servent:proto serv
a0e07ba4
NJ
2885The protocol used by the service. A service may be listed many times
2886in the database under different protocol names.
2887@end deffn
2888
2889The following procedures are used to search the service database:
2890
8f85c0c6
NJ
2891@deffn {Scheme Procedure} getserv [name [protocol]]
2892@deffnx {Scheme Procedure} getservbyname name protocol
2893@deffnx {Scheme Procedure} getservbyport port protocol
2894@deffnx {C Function} scm_getserv (name, protocol)
a0e07ba4
NJ
2895Look up a network service by name or by service number, and return a
2896network service object. The @var{protocol} argument specifies the name
2897of the desired protocol; if the protocol found in the network service
2898database does not match this name, a system error is signalled.
2899
2900The @code{getserv} procedure will take either a service name or number
2901as its first argument; if given no arguments, it behaves like
2902@code{getservent} (see below).
bcf009c3
NJ
2903
2904@lisp
2905(getserv "imap" "tcp")
2906@result{} #("imap2" ("imap") 143 "tcp")
2907
2908(getservbyport 88 "udp")
2909@result{} #("kerberos" ("kerberos5" "krb5") 88 "udp")
2910@end lisp
a0e07ba4
NJ
2911@end deffn
2912
2913The following procedures may be used to step through the service
2914database from beginning to end.
2915
8f85c0c6 2916@deffn {Scheme Procedure} setservent [stayopen]
a0e07ba4
NJ
2917Initialize an internal stream from which service objects may be read. This
2918procedure must be called before any calls to @code{getservent}, and may
2919also be called afterward to reset the service entry stream. If
2920@var{stayopen} is supplied and is not @code{#f}, the database is not
2921closed by subsequent @code{getservbyname} or @code{getservbyport} calls,
2922possibly giving an efficiency gain.
2923@end deffn
2924
8f85c0c6 2925@deffn {Scheme Procedure} getservent
a0e07ba4
NJ
2926Return the next entry from the services database.
2927@end deffn
2928
8f85c0c6 2929@deffn {Scheme Procedure} endservent
a0e07ba4
NJ
2930Close the stream used by @code{getservent}. The return value is unspecified.
2931@end deffn
2932
8f85c0c6
NJ
2933@deffn {Scheme Procedure} setserv [stayopen]
2934@deffnx {C Function} scm_setserv (stayopen)
a0e07ba4
NJ
2935If @var{stayopen} is omitted, this is equivalent to @code{endservent}.
2936Otherwise it is equivalent to @code{setservent stayopen}.
2937@end deffn
2938
13ed23db
KR
2939
2940@node Network Socket Address
2941@subsubsection Network Socket Address
32ff7370
KR
2942@cindex socket address
2943@cindex network socket address
2944@tpindex Socket address
2945
2946A @dfn{socket address} object identifies a socket endpoint for
2947communication. In the case of @code{AF_INET} for instance, the socket
2948address object comprises the host address (or interface on the host)
2949and a port number which specifies a particular open socket in a
2950running client or server process. A socket address object can be
2951created with,
2952
2953@deffn {Scheme Procedure} make-socket-address AF_INET ipv4addr port
2954@deffnx {Scheme Procedure} make-socket-address AF_INET6 ipv6addr port [flowinfo [scopeid]]
2955@deffnx {Scheme Procedure} make-socket-address AF_UNIX path
5f6ffd66 2956@deffnx {C Function} scm_make_socket_address (family, address, arglist)
13ed23db
KR
2957Return a new socket address object. The first argument is the address
2958family, one of the @code{AF} constants, then the arguments vary
2959according to the family.
2960
2961For @code{AF_INET} the arguments are an IPv4 network address number
32ff7370 2962(@pxref{Network Address Conversion}), and a port number.
13ed23db
KR
2963
2964For @code{AF_INET6} the arguments are an IPv6 network address number
2965and a port number. Optional @var{flowinfo} and @var{scopeid}
2966arguments may be given (both integers, default 0).
2967
2968For @code{AF_UNIX} the argument is a filename (a string).
32ff7370
KR
2969
2970The C function @code{scm_make_socket_address} takes the @var{family}
2971and @var{address} arguments directly, then @var{arglist} is a list of
2972further arguments, being the port for IPv4, port and optional flowinfo
2973and scopeid for IPv6, or the empty list @code{SCM_EOL} for Unix
2974domain.
2975@end deffn
13ed23db
KR
2976
2977@noindent
2978The following functions access the fields of a socket address object,
2979
2980@deffn {Scheme Procedure} sockaddr:fam sa
2981Return the address family from socket address object @var{sa}. This
679cceed 2982is one of the @code{AF} constants (e.g.@: @code{AF_INET}).
13ed23db
KR
2983@end deffn
2984
2985@deffn {Scheme Procedure} sockaddr:path sa
2986For an @code{AF_UNIX} socket address object @var{sa}, return the
2987filename.
2988@end deffn
2989
2990@deffn {Scheme Procedure} sockaddr:addr sa
2991For an @code{AF_INET} or @code{AF_INET6} socket address object
2992@var{sa}, return the network address number.
2993@end deffn
2994
2995@deffn {Scheme Procedure} sockaddr:port sa
2996For an @code{AF_INET} or @code{AF_INET6} socket address object
2997@var{sa}, return the port number.
2998@end deffn
2999
3000@deffn {Scheme Procedure} sockaddr:flowinfo sa
3001For an @code{AF_INET6} socket address object @var{sa}, return the
3002flowinfo value.
3003@end deffn
3004
3005@deffn {Scheme Procedure} sockaddr:scopeid sa
3006For an @code{AF_INET6} socket address object @var{sa}, return the
3007scope ID value.
3008@end deffn
3009
32ff7370
KR
3010@tpindex @code{struct sockaddr}
3011@tpindex @code{sockaddr}
3012The functions below convert to and from the C @code{struct sockaddr}
3013(@pxref{Address Formats,,, libc, The GNU C Library Reference Manual}).
3014That structure is a generic type, an application can cast to or from
3015@code{struct sockaddr_in}, @code{struct sockaddr_in6} or @code{struct
3016sockaddr_un} according to the address family.
3017
3018In a @code{struct sockaddr} taken or returned, the byte ordering in
3019the fields follows the C conventions (@pxref{Byte Order,, Byte Order
3020Conversion, libc, The GNU C Library Reference Manual}). This means
3021network byte order for @code{AF_INET} host address
3022(@code{sin_addr.s_addr}) and port number (@code{sin_port}), and
3023@code{AF_INET6} port number (@code{sin6_port}). But at the Scheme
3024level these values are taken or returned in host byte order, so the
3025port is an ordinary integer, and the host address likewise is an
3026ordinary integer (as described in @ref{Network Address Conversion}).
3027
3028@deftypefn {C Function} {struct sockaddr *} scm_c_make_socket_address (SCM family, SCM address, SCM args, size_t *outsize)
3029Return a newly-@code{malloc}ed @code{struct sockaddr} created from
3030arguments like those taken by @code{scm_make_socket_address} above.
3031
3032The size (in bytes) of the @code{struct sockaddr} return is stored
3033into @code{*@var{outsize}}. An application must call @code{free} to
3034release the returned structure when no longer required.
3035@end deftypefn
3036
3037@deftypefn {C Function} SCM scm_from_sockaddr (const struct sockaddr *address, unsigned address_size)
3038Return a Scheme socket address object from the C @var{address}
3039structure. @var{address_size} is the size in bytes of @var{address}.
3040@end deftypefn
3041
3042@deftypefn {C Function} {struct sockaddr *} scm_to_sockaddr (SCM address, size_t *address_size)
3043Return a newly-@code{malloc}ed @code{struct sockaddr} from a Scheme
3044level socket address object.
3045
3046The size (in bytes) of the @code{struct sockaddr} return is stored
3047into @code{*@var{outsize}}. An application must call @code{free} to
3048release the returned structure when no longer required.
3049@end deftypefn
3050
13ed23db 3051
a0e07ba4 3052@node Network Sockets and Communication
3229f68b 3053@subsubsection Network Sockets and Communication
f3dfb8ac
KR
3054@cindex socket
3055@cindex network socket
a0e07ba4
NJ
3056
3057Socket ports can be created using @code{socket} and @code{socketpair}.
3058The ports are initially unbuffered, to make reading and writing to the
3059same port more reliable. A buffer can be added to the port using
7403e409 3060@code{setvbuf}; see @ref{Ports and File Descriptors}.
a0e07ba4 3061
9e996fb1
KR
3062Most systems have limits on how many files and sockets can be open, so
3063it's strongly recommended that socket ports be closed explicitly when
3064no longer required (@pxref{Ports}).
3065
99d16776
KR
3066Some of the underlying C functions take values in network byte order,
3067but the convention in Guile is that at the Scheme level everything is
3068ordinary host byte order and conversions are made automatically where
3069necessary.
a0e07ba4 3070
8f85c0c6
NJ
3071@deffn {Scheme Procedure} socket family style proto
3072@deffnx {C Function} scm_socket (family, style, proto)
a0e07ba4 3073Return a new socket port of the type specified by @var{family},
3dba2dd9
KR
3074@var{style} and @var{proto}. All three parameters are integers. The
3075possible values for @var{family} are as follows, where supported by
3076the system,
3077
3078@defvar PF_UNIX
3079@defvarx PF_INET
3080@defvarx PF_INET6
3081@end defvar
3082
3083The possible values for @var{style} are as follows, again where
3084supported by the system,
3085
3086@defvar SOCK_STREAM
3087@defvarx SOCK_DGRAM
3088@defvarx SOCK_RAW
0bd094c2
KR
3089@defvarx SOCK_RDM
3090@defvarx SOCK_SEQPACKET
3dba2dd9 3091@end defvar
a0e07ba4
NJ
3092
3093@var{proto} can be obtained from a protocol name using
3dba2dd9
KR
3094@code{getprotobyname} (@pxref{Network Databases}). A value of zero
3095means the default protocol, which is usually right.
a0e07ba4 3096
3dba2dd9
KR
3097A socket cannot by used for communication until it has been connected
3098somewhere, usually with either @code{connect} or @code{accept} below.
a0e07ba4
NJ
3099@end deffn
3100
8f85c0c6
NJ
3101@deffn {Scheme Procedure} socketpair family style proto
3102@deffnx {C Function} scm_socketpair (family, style, proto)
497cbe20
KR
3103Return a pair, the @code{car} and @code{cdr} of which are two unnamed
3104socket ports connected to each other. The connection is full-duplex,
3105so data can be transferred in either direction between the two.
3106
3107@var{family}, @var{style} and @var{proto} are as per @code{socket}
3108above. But many systems only support socket pairs in the
3109@code{PF_UNIX} family. Zero is likely to be the only meaningful value
3110for @var{proto}.
a0e07ba4
NJ
3111@end deffn
3112
8f85c0c6 3113@deffn {Scheme Procedure} getsockopt sock level optname
8b6b6ce5 3114@deffnx {Scheme Procedure} setsockopt sock level optname value
8f85c0c6 3115@deffnx {C Function} scm_getsockopt (sock, level, optname)
8b6b6ce5
KR
3116@deffnx {C Function} scm_setsockopt (sock, level, optname, value)
3117Get or set an option on socket port @var{sock}. @code{getsockopt}
3118returns the current value. @code{setsockopt} sets a value and the
3119return is unspecified.
3120
3121@var{level} is an integer specifying a protocol layer, either
3122@code{SOL_SOCKET} for socket level options, or a protocol number from
3123the @code{IPPROTO} constants or @code{getprotoent} (@pxref{Network
3124Databases}).
3125
3126@defvar SOL_SOCKET
3127@defvarx IPPROTO_IP
3128@defvarx IPPROTO_TCP
3129@defvarx IPPROTO_UDP
3130@end defvar
a0e07ba4 3131
8b6b6ce5
KR
3132@var{optname} is an integer specifying an option within the protocol
3133layer.
3134
3135For @code{SOL_SOCKET} level the following @var{optname}s are defined
3136(when provided by the system). For their meaning see
3137@ref{Socket-Level Options,,, libc, The GNU C Library Reference
3138Manual}, or @command{man 7 socket}.
3139
3140@defvar SO_DEBUG
3141@defvarx SO_REUSEADDR
3142@defvarx SO_STYLE
3143@defvarx SO_TYPE
3144@defvarx SO_ERROR
3145@defvarx SO_DONTROUTE
3146@defvarx SO_BROADCAST
3147@defvarx SO_SNDBUF
3148@defvarx SO_RCVBUF
3149@defvarx SO_KEEPALIVE
3150@defvarx SO_OOBINLINE
3151@defvarx SO_NO_CHECK
3152@defvarx SO_PRIORITY
5f24f1b5 3153@defvarx SO_REUSEPORT
8b6b6ce5
KR
3154The @var{value} taken or returned is an integer.
3155@end defvar
a0e07ba4 3156
8b6b6ce5
KR
3157@defvar SO_LINGER
3158The @var{value} taken or returned is a pair of integers
3159@code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without timeout
3160support (ie.@: without @code{struct linger}), only @var{ENABLE} has an
3161effect but the value in Guile is always a pair.
3162@end defvar
a0e07ba4 3163
8b6b6ce5
KR
3164@c Note that we refer only to ``man ip'' here. On GNU/Linux it's
3165@c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.
3166@c
3167For IP level (@code{IPPROTO_IP}) the following @var{optname}s are
3168defined (when provided by the system). See @command{man ip} for what
3169they mean.
3170
60905b80
AW
3171@defvar IP_MULTICAST_IF
3172This sets the source interface used by multicast traffic.
3173@end defvar
3174
3175@defvar IP_MULTICAST_TTL
3176This sets the default TTL for multicast traffic. This defaults
3177to 1 and should be increased to allow traffic to pass beyond the
3178local network.
3179@end defvar
3180
8b6b6ce5
KR
3181@defvar IP_ADD_MEMBERSHIP
3182@defvarx IP_DROP_MEMBERSHIP
3183These can be used only with @code{setsockopt}, not @code{getsockopt}.
3184@var{value} is a pair @code{(@var{MULTIADDR} . @var{INTERFACEADDR})}
99d16776 3185of integer IPv4 addresses (@pxref{Network Address Conversion}).
8b6b6ce5
KR
3186@var{MULTIADDR} is a multicast address to be added to or dropped from
3187the interface @var{INTERFACEADDR}. @var{INTERFACEADDR} can be
3188@code{INADDR_ANY} to have the system select the interface.
3189@var{INTERFACEADDR} can also be an interface index number, on systems
3190supporting that.
3191@end defvar
a0e07ba4
NJ
3192@end deffn
3193
8f85c0c6
NJ
3194@deffn {Scheme Procedure} shutdown sock how
3195@deffnx {C Function} scm_shutdown (sock, how)
99d16776 3196Sockets can be closed simply by using @code{close-port}. The
85a9b4ed 3197@code{shutdown} procedure allows reception or transmission on a
a0e07ba4
NJ
3198connection to be shut down individually, according to the parameter
3199@var{how}:
3200
3201@table @asis
3202@item 0
99d16776 3203Stop receiving data for this socket. If further data arrives, reject it.
a0e07ba4
NJ
3204@item 1
3205Stop trying to transmit data from this socket. Discard any
3206data waiting to be sent. Stop looking for acknowledgement of
3207data already sent; don't retransmit it if it is lost.
3208@item 2
3209Stop both reception and transmission.
3210@end table
3211
3212The return value is unspecified.
3213@end deffn
3214
13ed23db
KR
3215@deffn {Scheme Procedure} connect sock sockaddr
3216@deffnx {Scheme Procedure} connect sock AF_INET ipv4addr port
3217@deffnx {Scheme Procedure} connect sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
3218@deffnx {Scheme Procedure} connect sock AF_UNIX path
8f85c0c6 3219@deffnx {C Function} scm_connect (sock, fam, address, args)
13ed23db
KR
3220Initiate a connection on socket port @var{sock} to a given address.
3221The destination is either a socket address object, or arguments the
3222same as @code{make-socket-address} would take to make such an object
3223(@pxref{Network Socket Address}). The return value is unspecified.
a0e07ba4 3224
13ed23db 3225@example
32bc9257
NJ
3226(connect sock AF_INET INADDR_LOOPBACK 23)
3227(connect sock (make-socket-address AF_INET INADDR_LOOPBACK 23))
13ed23db 3228@end example
a0e07ba4
NJ
3229@end deffn
3230
13ed23db
KR
3231@deffn {Scheme Procedure} bind sock sockaddr
3232@deffnx {Scheme Procedure} bind sock AF_INET ipv4addr port
3233@deffnx {Scheme Procedure} bind sock AF_INET6 ipv6addr port [flowinfo [scopeid]]
3234@deffnx {Scheme Procedure} bind sock AF_UNIX path
8f85c0c6 3235@deffnx {C Function} scm_bind (sock, fam, address, args)
13ed23db
KR
3236Bind socket port @var{sock} to the given address. The address is
3237either a socket address object, or arguments the same as
3238@code{make-socket-address} would take to make such an object
3239(@pxref{Network Socket Address}). The return value is unspecified.
a0e07ba4 3240
13ed23db 3241Generally a socket is only explicitly bound to a particular address
679cceed 3242when making a server, i.e.@: to listen on a particular port. For an
13ed23db
KR
3243outgoing connection the system will assign a local address
3244automatically, if not already bound.
a0e07ba4 3245
13ed23db
KR
3246@example
3247(bind sock AF_INET INADDR_ANY 12345)
41185bfe 3248(bind sock (make-socket-address AF_INET INADDR_ANY 12345))
13ed23db 3249@end example
a0e07ba4
NJ
3250@end deffn
3251
8f85c0c6
NJ
3252@deffn {Scheme Procedure} listen sock backlog
3253@deffnx {C Function} scm_listen (sock, backlog)
a0e07ba4
NJ
3254Enable @var{sock} to accept connection
3255requests. @var{backlog} is an integer specifying
3256the maximum length of the queue for pending connections.
3257If the queue fills, new clients will fail to connect until
3258the server calls @code{accept} to accept a connection from
3259the queue.
3260
3261The return value is unspecified.
3262@end deffn
3263
8f85c0c6
NJ
3264@deffn {Scheme Procedure} accept sock
3265@deffnx {C Function} scm_accept (sock)
13ed23db
KR
3266Accept a connection from socket port @var{sock} which has been enabled
3267for listening with @code{listen} above. If there are no incoming
1b09b607
KR
3268connections in the queue, wait until one is available (unless
3269@code{O_NONBLOCK} has been set on the socket, @pxref{Ports and File
3270Descriptors,@code{fcntl}}).
a0e07ba4 3271
13ed23db
KR
3272The return value is a pair. The @code{car} is a new socket port,
3273connected and ready to communicate. The @code{cdr} is a socket
3274address object (@pxref{Network Socket Address}) which is where the
3275remote connection is from (like @code{getpeername} below).
a0e07ba4 3276
13ed23db
KR
3277All communication takes place using the new socket returned. The
3278given @var{sock} remains bound and listening, and @code{accept} may be
3279called on it again to get another incoming connection when desired.
2ce02471 3280@end deffn
a0e07ba4 3281
8f85c0c6
NJ
3282@deffn {Scheme Procedure} getsockname sock
3283@deffnx {C Function} scm_getsockname (sock)
13ed23db
KR
3284Return a socket address object which is the where @var{sock} is bound
3285locally. @var{sock} may have obtained its local address from
3286@code{bind} (above), or if a @code{connect} is done with an otherwise
3287unbound socket (which is usual) then the system will have assigned an
3288address.
3289
3290Note that on many systems the address of a socket in the
3291@code{AF_UNIX} namespace cannot be read.
a0e07ba4
NJ
3292@end deffn
3293
8f85c0c6
NJ
3294@deffn {Scheme Procedure} getpeername sock
3295@deffnx {C Function} scm_getpeername (sock)
13ed23db 3296Return a socket address object which is where @var{sock} is connected
679cceed 3297to, i.e.@: the remote endpoint.
13ed23db
KR
3298
3299Note that on many systems the address of a socket in the
3300@code{AF_UNIX} namespace cannot be read.
a0e07ba4
NJ
3301@end deffn
3302
8f85c0c6
NJ
3303@deffn {Scheme Procedure} recv! sock buf [flags]
3304@deffnx {C Function} scm_recv (sock, buf, flags)
a0e07ba4
NJ
3305Receive data from a socket port.
3306@var{sock} must already
3307be bound to the address from which data is to be received.
d21a1dc8 3308@var{buf} is a bytevector into which
a0e07ba4
NJ
3309the data will be written. The size of @var{buf} limits
3310the amount of
3311data which can be received: in the case of packet
3312protocols, if a packet larger than this limit is encountered
3313then some data
3314will be irrevocably lost.
3315
2ce02471
NJ
3316@vindex MSG_OOB
3317@vindex MSG_PEEK
3318@vindex MSG_DONTROUTE
7403e409
NJ
3319The optional @var{flags} argument is a value or bitwise OR of
3320@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
a0e07ba4
NJ
3321
3322The value returned is the number of bytes read from the
3323socket.
3324
3325Note that the data is read directly from the socket file
3326descriptor:
3327any unread buffered port data is ignored.
3328@end deffn
3329
8f85c0c6
NJ
3330@deffn {Scheme Procedure} send sock message [flags]
3331@deffnx {C Function} scm_send (sock, message, flags)
2ce02471
NJ
3332@vindex MSG_OOB
3333@vindex MSG_PEEK
3334@vindex MSG_DONTROUTE
d21a1dc8 3335Transmit bytevector @var{message} on socket port @var{sock}.
7403e409
NJ
3336@var{sock} must already be bound to a destination address. The value
3337returned is the number of bytes transmitted---it's possible for this
3338to be less than the length of @var{message} if the socket is set to be
3339non-blocking. The optional @var{flags} argument is a value or bitwise
3340OR of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
a0e07ba4
NJ
3341
3342Note that the data is written directly to the socket
3343file descriptor:
3344any unflushed buffered port data is ignored.
3345@end deffn
3346
d21a1dc8
LC
3347@deffn {Scheme Procedure} recvfrom! sock buf [flags [start [end]]]
3348@deffnx {C Function} scm_recvfrom (sock, buf, flags, start, end)
40296bab
KR
3349Receive data from socket port @var{sock}, returning the originating
3350address as well as the data. This function is usually for datagram
3351sockets, but can be used on stream-oriented sockets too.
3352
d21a1dc8
LC
3353The data received is stored in bytevector @var{buf}, using
3354either the whole bytevector or just the region between the optional
3355@var{start} and @var{end} positions. The size of @var{buf}
3356limits the amount of data that can be received. For datagram
3357protocols if a packet larger than this is received then excess
3358bytes are irrevocably lost.
40296bab
KR
3359
3360The return value is a pair. The @code{car} is the number of bytes
3361read. The @code{cdr} is a socket address object (@pxref{Network
3362Socket Address}) which is where the data came from, or @code{#f} if
3363the origin is unknown.
a0e07ba4 3364
2ce02471
NJ
3365@vindex MSG_OOB
3366@vindex MSG_PEEK
3367@vindex MSG_DONTROUTE
40296bab
KR
3368The optional @var{flags} argument is a or bitwise-OR (@code{logior})
3369of @code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
a0e07ba4 3370
40296bab
KR
3371Data is read directly from the socket file descriptor, any buffered
3372port data is ignored.
a0e07ba4 3373
40296bab
KR
3374@c This was linux kernel 2.6.15 and glibc 2.3.6, not sure what any
3375@c specs are supposed to say about recvfrom threading.
3376@c
3377On a GNU/Linux system @code{recvfrom!} is not multi-threading, all
3378threads stop while a @code{recvfrom!} call is in progress. An
3379application may need to use @code{select}, @code{O_NONBLOCK} or
3380@code{MSG_DONTWAIT} to avoid this.
a0e07ba4
NJ
3381@end deffn
3382
13ed23db
KR
3383@deffn {Scheme Procedure} sendto sock message sockaddr [flags]
3384@deffnx {Scheme Procedure} sendto sock message AF_INET ipv4addr port [flags]
3385@deffnx {Scheme Procedure} sendto sock message AF_INET6 ipv6addr port [flowinfo [scopeid [flags]]]
3386@deffnx {Scheme Procedure} sendto sock message AF_UNIX path [flags]
8f85c0c6 3387@deffnx {C Function} scm_sendto (sock, message, fam, address, args_and_flags)
d21a1dc8 3388Transmit bytevector @var{message} as a datagram socket port
13ed23db
KR
3389@var{sock}. The destination is specified either as a socket address
3390object, or as arguments the same as would be taken by
3391@code{make-socket-address} to create such an object (@pxref{Network
3392Socket Address}).
3393
3394The destination address may be followed by an optional @var{flags}
3395argument which is a @code{logior} (@pxref{Bitwise Operations}) of
3396@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.
a0e07ba4
NJ
3397
3398The value returned is the number of bytes transmitted --
3399it's possible for
3400this to be less than the length of @var{message} if the
3401socket is
3402set to be non-blocking.
3403Note that the data is written directly to the socket
3404file descriptor:
3405any unflushed buffered port data is ignored.
3406@end deffn
3407
bcf009c3
NJ
3408
3409@node Internet Socket Examples
3229f68b 3410@subsubsection Network Socket Examples
f3dfb8ac
KR
3411@cindex network examples
3412@cindex socket examples
bcf009c3 3413
3229f68b 3414The following give examples of how to use network sockets.
bcf009c3 3415
3229f68b 3416@subsubheading Internet Socket Client Example
bcf009c3
NJ
3417
3418@cindex socket client example
3419The following example demonstrates an Internet socket client.
3420It connects to the HTTP daemon running on the local machine and
3421returns the contents of the root index URL.
3422
3423@example
a8d0313f 3424(let ((s (socket PF_INET SOCK_STREAM 0)))
3452e666 3425 (connect s AF_INET (inet-pton AF_INET "127.0.0.1") 80)
bcf009c3
NJ
3426 (display "GET / HTTP/1.0\r\n\r\n" s)
3427
3428 (do ((line (read-line s) (read-line s)))
3429 ((eof-object? line))
3430 (display line)
3431 (newline)))
3432@end example
3433
3434
3229f68b 3435@subsubheading Internet Socket Server Example
bcf009c3
NJ
3436
3437@cindex socket server example
3438The following example shows a simple Internet server which listens on
3439port 2904 for incoming connections and sends a greeting back to the
3440client.
3441
3442@example
a8d0313f 3443(let ((s (socket PF_INET SOCK_STREAM 0)))
bcf009c3 3444 (setsockopt s SOL_SOCKET SO_REUSEADDR 1)
7403e409 3445 ;; @r{Specific address?}
3452e666 3446 ;; @r{(bind s AF_INET (inet-pton AF_INET "127.0.0.1") 2904)}
bcf009c3
NJ
3447 (bind s AF_INET INADDR_ANY 2904)
3448 (listen s 5)
3449
3450 (simple-format #t "Listening for clients in pid: ~S" (getpid))
3451 (newline)
3452
3453 (while #t
99d16776
KR
3454 (let* ((client-connection (accept s))
3455 (client-details (cdr client-connection))
3456 (client (car client-connection)))
3457 (simple-format #t "Got new client connection: ~S"
3458 client-details)
3459 (newline)
3460 (simple-format #t "Client address: ~S"
3461 (gethostbyaddr
3462 (sockaddr:addr client-details)))
3463 (newline)
3464 ;; @r{Send back the greeting to the client port}
3465 (display "Hello client\r\n" client)
3466 (close client))))
bcf009c3
NJ
3467@end example
3468
3469
a0e07ba4 3470@node System Identification
3229f68b 3471@subsection System Identification
f3dfb8ac 3472@cindex system name
a0e07ba4
NJ
3473
3474This section lists the various procedures Guile provides for accessing
3475information about the system it runs on.
3476
8f85c0c6
NJ
3477@deffn {Scheme Procedure} uname
3478@deffnx {C Function} scm_uname ()
a0e07ba4
NJ
3479Return an object with some information about the computer
3480system the program is running on.
a0e07ba4
NJ
3481
3482The following procedures accept an object as returned by @code{uname}
99d16776 3483and return a selected component (all of which are strings).
a0e07ba4 3484
2ce02471 3485@deffn {Scheme Procedure} utsname:sysname un
a0e07ba4 3486The name of the operating system.
2ce02471
NJ
3487@end deffn
3488@deffn {Scheme Procedure} utsname:nodename un
a0e07ba4 3489The network name of the computer.
2ce02471
NJ
3490@end deffn
3491@deffn {Scheme Procedure} utsname:release un
a0e07ba4 3492The current release level of the operating system implementation.
2ce02471
NJ
3493@end deffn
3494@deffn {Scheme Procedure} utsname:version un
a0e07ba4 3495The current version level within the release of the operating system.
2ce02471
NJ
3496@end deffn
3497@deffn {Scheme Procedure} utsname:machine un
a0e07ba4 3498A description of the hardware.
2ce02471
NJ
3499@end deffn
3500@end deffn
a0e07ba4 3501
8f85c0c6
NJ
3502@deffn {Scheme Procedure} gethostname
3503@deffnx {C Function} scm_gethostname ()
f3dfb8ac 3504@cindex host name
a0e07ba4
NJ
3505Return the host name of the current processor.
3506@end deffn
3507
8f85c0c6
NJ
3508@deffn {Scheme Procedure} sethostname name
3509@deffnx {C Function} scm_sethostname (name)
a0e07ba4
NJ
3510Set the host name of the current processor to @var{name}. May
3511only be used by the superuser. The return value is not
3512specified.
3513@end deffn
3514
a0e07ba4 3515@node Locales
3229f68b 3516@subsection Locales
f3dfb8ac 3517@cindex locale
a0e07ba4 3518
8f85c0c6
NJ
3519@deffn {Scheme Procedure} setlocale category [locale]
3520@deffnx {C Function} scm_setlocale (category, locale)
74f76d62
KR
3521Get or set the current locale, used for various internationalizations.
3522Locales are strings, such as @samp{sv_SE}.
3523
b89c4943
LC
3524If @var{locale} is given then the locale for the given @var{category}
3525is set and the new value returned. If @var{locale} is not given then
3526the current value is returned. @var{category} should be one of the
3527following values (@pxref{Locale Categories, Categories of Activities
3528that Locales Affect,, libc, The GNU C Library Reference Manual}):
74f76d62
KR
3529
3530@defvar LC_ALL
3531@defvarx LC_COLLATE
3532@defvarx LC_CTYPE
3533@defvarx LC_MESSAGES
3534@defvarx LC_MONETARY
3535@defvarx LC_NUMERIC
3536@defvarx LC_TIME
3537@end defvar
3538
f3dfb8ac 3539@cindex @code{LANG}
74f76d62
KR
3540A common usage is @samp{(setlocale LC_ALL "")}, which initializes all
3541categories based on standard environment variables (@code{LANG} etc).
3542For full details on categories and locale names @pxref{Locales,,
3543Locales and Internationalization, libc, The GNU C Library Reference
3544Manual}.
b89c4943
LC
3545
3546Note that @code{setlocale} affects locale settings for the whole
a2f00b9b 3547process. @xref{i18n Introduction, locale objects and
b89c4943 3548@code{make-locale}}, for a thread-safe alternative.
a0e07ba4
NJ
3549@end deffn
3550
3551@node Encryption
3229f68b 3552@subsection Encryption
f3dfb8ac 3553@cindex encryption
a0e07ba4
NJ
3554
3555Please note that the procedures in this section are not suited for
3556strong encryption, they are only interfaces to the well-known and
3557common system library functions of the same name. They are just as good
3558(or bad) as the underlying functions, so you should refer to your system
9a18d8d4
KR
3559documentation before using them (@pxref{crypt,, Encrypting Passwords,
3560libc, The GNU C Library Reference Manual}).
a0e07ba4 3561
8f85c0c6
NJ
3562@deffn {Scheme Procedure} crypt key salt
3563@deffnx {C Function} scm_crypt (key, salt)
9a18d8d4
KR
3564Encrypt @var{key}, with the addition of @var{salt} (both strings),
3565using the @code{crypt} C library call.
a0e07ba4
NJ
3566@end deffn
3567
5f378d17
TTN
3568Although @code{getpass} is not an encryption procedure per se, it
3569appears here because it is often used in combination with @code{crypt}:
a0e07ba4 3570
8f85c0c6
NJ
3571@deffn {Scheme Procedure} getpass prompt
3572@deffnx {C Function} scm_getpass (prompt)
f3dfb8ac 3573@cindex password
a0e07ba4
NJ
3574Display @var{prompt} to the standard error output and read
3575a password from @file{/dev/tty}. If this file is not
3576accessible, it reads from standard input. The password may be
3577up to 127 characters in length. Additional characters and the
3578terminating newline character are discarded. While reading
3579the password, echoing and the generation of signals by special
3580characters is disabled.
3581@end deffn
5982a8e0
KR
3582
3583
3584@c Local Variables:
3585@c TeX-master: "guile.texi"
3586@c End: