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