2 @chapter Low level Unix interfaces
4 The low level Unix interfaces are currently available by
5 default in the Guile top level. However in the future they will probably
6 be placed in a module and @code{use-modules} or something similar will
7 be required to make them available.
10 * Unix conventions:: Conventions followed by the low level Unix
12 * Ports and descriptors:: Ports, file descriptors and how they
14 * Extended I/O:: Reading and writing to ports.
15 * File system:: Working in a hierarchical filesystem.
16 * User database:: Information about users from system databases.
17 * Processes:: Information and control of Unix processes.
18 * Terminals:: Terminals and pseudo-terminals.
19 * Network databases:: Network address conversion and information
20 from system databases.
21 * Network sockets:: An interface to the BSD socket library.
22 * Miscellaneous Unix:: Miscellaneous Unix interfaces.
25 @node Unix conventions
26 @section Low level Unix conventions
28 The low-level interfaces are designed to give Scheme programs
29 access to as much functionality as possible from the underlying
30 Unix system. They can be used to implement higher level
31 intefaces such as the Scheme shell @ref{scsh}.
33 Generally there is a single procedure for each corresponding Unix
34 facility. However some of the procedures are implemented for
35 speed and convenience in Scheme and have no Unix equivalent
36 (e.g., @code{read-delimited}, @code{copy-file}.)
38 This interface is intended as far as possible to be portable across
39 different versions of Unix, so that Scheme programmers don't need to be
40 concerned with implementation differences. In some cases procedures
41 which can't be implemented (or reimplemented) on particular systems may
42 become no-ops, or perform limited actions. In other cases they may
43 throw errors. It should be possible to use the feature system to
44 determine what functionality is available.
46 General naming conventions are as follows:
50 The Scheme name is often identical to the name of the underlying Unix
53 Underscores in Unix names are converted to hyphens.
55 Procedures which destructively modify Scheme data gain postpended
56 exclaimation marks, e.g., @code{recv!}.
58 Predicates are postpended with question marks, e.g., @code{access?}.
60 Some names are changed to avoid conflict with dissimilar interfaces
63 Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted
64 to Scheme variables of the same name (underscores are not replaced
68 Most of the Unix interface procedures can be relied on to return a
69 well-specified value. Unexpected conditions are handled by raising
72 There are a few procedures which return a special
73 value if they don't succeed, e.g., @code{getenv} returns @code{#f}
74 if it the requested string is not found in the environment. These
75 cases will be noted in the documentation.
77 For ways to deal with exceptions, @ref{Exceptions}.
79 Errors which the C-library would report by returning a NULL
80 pointer or through some other means cause a @code{system-error} exception
81 to be raised. The value of the Unix @code{errno} variable is available
82 in the data passed by the exception, so there is no need to access the
83 global errno value (doing so would be unreliable in the presence of
84 continuations or multiple threads).
86 @deffn procedure errno [n]
88 @deffn procedure perror string
91 @node Ports and descriptors
92 @section Ports and file descriptors
94 @deffn procedure move->fdes port fd
96 @deffn procedure release-port-handle port
98 @deffn procedure set-port-revealed! @var{port} count
100 @deffn procedure fdes->ports fdes
102 @deffn procedure fileno port
104 @deffn procedure fdopen fdes modes
106 @deffn procedure duplicate-port port modes
108 @deffn procedure redirect-port into-port from-port
110 @deffn procedure freopen filename modes port
114 @section Extended I/O
116 Extended I/O procedures are available which read or write lines of text,
117 read text delimited by a specified set of characters, or report or
118 set the current position of a port.
122 Interfaces to @code{read}/@code{fread} and @code{write}/@code{fwrite} are
123 also available, as @code{uniform-array-read!} and @code{uniform-array-write!},
124 @ref{Uniform arrays}.
126 @deffn procedure read-line [port] [handle-delim]
127 Return a line of text from @var{port} if specified, otherwise from the
128 value returned by @code{(current-input-port)}. Under Unix, a line of text
129 is terminated by the first end-of-line character or by end-of-file.
131 If @var{handle-delim} is specified, it should be one of the following
135 Discard the terminating delimiter. This is the default, but it will
136 be impossible to tell whether the read terminated with a delimiter or
139 Append the terminating delimiter (if any) to the returned string.
141 Push the terminating delimiter (if any) back on to the port.
143 Return a pair containing the string read from the port and the
144 terminating delimiter or end-of-file object.
146 NOTE: if the scsh module is loaded then
147 multiple values are returned instead of a pair.
150 @deffn procedure read-line! buf [port]
151 Read a line of text into the supplied string @var{buf} and return the
152 number of characters added to @var{buf}. If @var{buf} is filled, then
153 @code{#f} is returned.
154 Read from @var{port} if
155 specified, otherwise from the value returned by @code{(current-input-port)}.
157 @deffn procedure read-delimited delims [port] [handle-delim]
158 Read text until one of the characters in the string @var{delims} is found
159 or end-of-file is reached. Read from @var{port} if supplied, otherwise
160 from the value returned by @code{(current-input-port)}.
161 @var{handle-delim} takes the same values as described for @code{read-line}.
163 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
164 char-set, not a string.
166 @deffn procedure read-delimited! delims buf [port] [handle-delim] [start] [end]
167 Read text into the supplied string @var{buf} and return the number of
168 characters added to @var{buf} (subject to @var{handle-delim}, which takes
169 the same values specified for @code{read-line}. If @var{buf} is filled,
170 @code{#f} is returned for both the number of characters read and the
171 delimiter. Also terminates if one of the characters in the string
172 @var{delims} is found
173 or end-of-file is reached. Read from @var{port} if supplied, otherwise
174 from the value returned by @code{(current-input-port)}.
176 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
177 char-set, not a string.
179 @deffn procedure write-line obj [port]
180 Display @var{obj} and a new-line character to @var{port} if specified,
182 value returned by @code{(current-output-port)}; equivalent to:
189 @deffn procedure ftell port
190 Returns an integer representing the current position of @var{port},
191 measured from the beginning.
193 @deffn procedure fseek port offset whence
194 Sets the current position of @var{port} to the integer @var{offset},
195 which is interpreted according to the value of @var{whence}.
197 One of the following variables should be supplied
200 Seek from the beginning of the file.
203 Seek from the current position.
206 Seek from the end of the file.
213 These procedures query and set file system attributes (such as owner,
214 permissions, sizes and types of files); deleting, copying, renaming and
215 linking files; creating and removing directories and querying their
216 contents; and the @code{sync} interface.
218 @deffn procedure access? path how
219 Evaluates to @code{#t} if @var{path} corresponds to an existing
220 file and the current process
221 has the type of access specified by @var{how}, otherwise
223 @var{how} should be specified
224 using the values of the variables listed below. Multiple values can
225 be combined using a bitwise or, in which case @code{#t} will only
226 be returned if all accesses are granted.
228 Permissions are checked using the real id of the current process,
229 not the effective id, although it's the effective id which determines
230 whether the access would actually be granted.
233 test for read permission.
236 test for write permission.
239 test for execute permission.
242 test for existence of the file.
246 @deffn procedure stat obj
247 Evaluates to an object containing various information
248 about the file determined by @var{obj}.
249 @var{obj} can be a string containing a file name or a port or file
250 descriptor which is open on a file (in which case @code{fstat} is used
251 as the underlying system call).
253 The object returned by @code{stat} can be passed as a single parameter
254 to the following procedures, all of which return integers:
258 The device containing the file.
260 The file serial number, which distinguishes this file from all other
261 files on the same device.
263 The mode of the file. This includes file type information
264 and the file permission bits. See @code{stat:type} and @code{stat:perms}
267 The number of hard links to the file.
269 The user ID of the file's owner.
271 The group ID of the file.
273 Device ID; this entry is defined only for character or block
276 The size of a regular file in bytes.
278 The last access time for the file.
280 The last modification time for the file.
282 The last modification time for the attributes of the file.
284 The optimal block size for reading or writing the file, in bytes.
286 The amount of disk space that the file occupies measured in units of
290 In addition, the following procedures return the information
291 from stat:mode in a more convenient form:
295 A symbol representing the type of file. Possible values are
296 currently: regular, directory, symlink, block-special, char-special,
297 fifo, socket, unknown
299 An integer representing the access permission bits.
302 @deffn procedure lstat path
303 Similar to @code{stat}, but does not follow symbolic links, i.e.,
304 it will return information about a symbolic link itself, not the
305 file it points to. @var{path} must be a string.
307 @deffn procedure readlink path
309 @deffn procedure chown path owner group
311 @deffn procedure chmod port-or-path mode
313 @deffn procedure utime path [actime] [modtime]
315 @deffn procedure delete-file path
317 @deffn procedure copy-file path-from path-to
319 @deffn procedure rename-file path-from path-to
321 @deffn procedure link path-from path-to
323 @deffn procedure symlink path-from path-to
325 @deffn procedure mkdir path [mode]
327 @deffn procedure rmdir path
329 @deffn procedure opendir path
331 @deffn procedure readdir port
333 @deffn procedure rewinddir port
335 @deffn procedure closedir port
337 @deffn procedure sync
341 @section User database
343 @deffn procedure getpwuid uid
345 @deffn procedure getpwnam name
347 @deffn procedure getpwent
349 @deffn procedure setpwent port
351 @deffn procedure endpwent
353 @deffn procedure getgrgid uid
355 @deffn procedure getgrnam name
357 @deffn procedure getgrent
359 @deffn procedure setgrent port
361 @deffn procedure endgrent
367 @deffn procedure chdir path
369 @deffn procedure getcwd
371 @deffn procedure umask [mode]
373 @deffn procedure getpid
375 @deffn procedure getgroups
377 @deffn procedure kill pid sig
379 @var{sig} should be specified using a variable corresponding to
380 the Unix symbolic name, e.g,
388 @deffn procedure waitpid pid options
398 @deffn procedure getppid
400 @deffn procedure getuid
402 @deffn procedure getgid
404 @deffn procedure geteuid
406 @deffn procedure getegid
408 @deffn procedure setuid id
410 @deffn procedure setgid id
412 @deffn procedure seteuid id
414 @deffn procedure setegid id
416 @deffn procedure getpgrp
418 @deffn procedure setpgid pid pgid
420 @deffn procedure setsid
422 @deffn procedure execl arg ...
424 @deffn procedure execlp arg ...
426 @deffn procedure primitive-fork
428 @deffn procedure environ [env]
430 @deffn procedure putenv string
432 @deffn procedure nice incr
436 @section Terminals and pseudo-terminals
438 @deffn procedure isatty? port
440 @deffn procedure ttyname port
442 @deffn procedure ctermid
444 @deffn procedure tcgetpgrp port
446 @deffn procedure tcsetpgrp port pgid
449 @node Network databases
450 @section Network address conversion and system databases
452 @deffn procedure inet-aton address
454 @deffn procedure inet-ntoa number
456 @deffn procedure inet-netof address
458 @deffn procedure inet-lnaof address
460 @deffn procedure inet-makeaddr net lna
462 @deffn procedure gethostbyname name
464 @deffn procedure gethostbyaddr address
466 @deffn procedure gethostent
468 @deffn procedure sethostent port
470 @deffn procedure endhostent
472 @deffn procedure getnetbyname name
474 @deffn procedure getnetbyaddr address
476 @deffn procedure getnetent
478 @deffn procedure setnetent port
480 @deffn procedure endnetent
482 @deffn procedure getprotobyname name
484 @deffn procedure getprotobynumber number
486 @deffn procedure getprotoent
488 @deffn procedure setprotoent port
490 @deffn procedure endprotoent
492 @deffn procedure getservbyname name protocol
494 @deffn procedure getservbyport port protocol
496 @deffn procedure getservent
498 @deffn procedure setservent port
500 @deffn procedure endservent
503 @node Network sockets
504 @section BSD socket library interface
506 @deffn procedure socket family style protocol
508 @deffn procedure socketpair family style protocol
510 @deffn procedure getsockopt socket level optname
512 @deffn procedure setsockopt socket level optname value
514 @deffn procedure shutdown socket how
516 @deffn procedure connect socket family address arg ...
518 @deffn procedure bind socket family address arg ...
520 @deffn procedure listen socket backlog
522 @deffn procedure accept socket
524 @deffn procedure getsockname socket
526 @deffn procedure getpeername socket
528 @deffn procedure recv! socket buf [flags]
530 @deffn procedure send socket message [flags]
532 @deffn procedure recvfrom! socket buf [flags] [start] [end]
534 @deffn procedure sendto socket message family address args ... [flags]
537 @node Miscellaneous Unix
538 @section Miscellaneous Unix interfaces
540 Things which haven't been classified elsewhere (yet?).
542 @deffn procedure open path flags [mode]
566 @deffn procedure select reads writes excepts secs msecs
568 @deffn procedure uname
570 @deffn procedure pipe
572 @deffn procedure open-pipe command modes
574 @deffn procedure open-input-pipe command
576 @deffn procedure open-output-pipe command
578 @deffn procedure setlocale category [locale]
594 @deffn procedure strftime format stime
596 @deffn procedure strptime format string
598 @deffn procedure mknod
602 @chapter The Scheme shell (scsh)
604 Guile includes an incomplete port of the Scheme shell (scsh) 0.4.4.
606 For information about scsh on the Web see
607 @url{http://www-swiss.ai.mit.edu/scsh/scsh.html}.
608 The original scsh is available by ftp from
609 @url{ftp://swiss-ftp.ai.mit.edu:/pub/su}.
611 This port of scsh does not currently use the Guile module system, but
612 can be initialized using:
614 (load-from-path "scsh/init")
617 Note that SLIB must be installed before scsh can be initialized, see
618 @ref{SLIB} for details.
621 @chapter Programming Threads.