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