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