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