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