summaryrefslogtreecommitdiff
path: root/doc/sources/unix.texi
blob: e8a189c5bee5eb67c003f26cda9d4a56c2354606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
@node Low level Unix
@chapter Low level Unix interfaces

The low level Unix interfaces are currently available by
default in the Guile top level.  However in the future they will probably
be placed in a module and @code{use-modules} or something similar will
be required to make them available.

@menu
* Unix conventions::            Conventions followed by the low level Unix
                                interfaces.
* Ports and descriptors::       Ports, file descriptors and how they
                                interact.
* Extended I/O::                Reading and writing to ports.
* File system::                 Working in a hierarchical filesystem.
* User database::               Information about users from system databases.
* Processes::                   Information and control of Unix processes.
* Terminals::                   Terminals and pseudo-terminals.
* Network databases::           Network address conversion and information
                                from system databases.
* Network sockets::             An interface to the BSD socket library.
* Miscellaneous Unix::          Miscellaneous Unix interfaces.
@end menu

@node Unix conventions
@section Low level Unix conventions

The low-level interfaces are designed to give Scheme programs
access to as much functionality as possible from the underlying
Unix system.  They can be used to implement higher level
intefaces such as the Scheme shell @ref{scsh}.

Generally there is a single procedure for each corresponding Unix
facility.  However some of the procedures are implemented for
speed and convenience in Scheme and have no Unix equivalent
(e.g., @code{read-delimited}, @code{copy-file}.)

This interface is intended as far as possible to be portable across
different versions of Unix, so that Scheme programmers don't need to be
concerned with implementation differences.  In some cases procedures
which can't be implemented (or reimplemented) on particular systems may
become no-ops, or perform limited actions.  In other cases they may
throw errors.  It should be possible to use the feature system to
determine what functionality is available.

General naming conventions are as follows:

@itemize @bullet
@item
The Scheme name is often identical to the name of the underlying Unix
facility.
@item
Underscores in Unix names are converted to hyphens.
@item
Procedures which destructively modify Scheme data gain postpended
exclaimation marks, e.g., @code{recv!}.
@item
Predicates are postpended with question marks, e.g., @code{access?}.
@item
Some names are changed to avoid conflict with dissimilar interfaces
defined by scsh.
@item
Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted
to Scheme variables of the same name (underscores are not replaced
with hyphens)
@end itemize

Most of the Unix interface procedures can be relied on to return a
well-specified value.  Unexpected conditions are handled by raising
exceptions.

There are a few procedures which return a special
value if they don't succeed, e.g., @code{getenv} returns @code{#f}
if it the requested string is not found in the environment.  These
cases will be noted in the documentation.

For ways to deal with exceptions, @ref{Exceptions}.

Errors which the C-library would report by returning a NULL
pointer or through some other means cause a @code{system-error} exception
to be raised.  The value of the Unix @code{errno} variable is available
in the data passed by the exception, so there is no need to access the
global errno value (doing so would be unreliable in the presence of
continuations or multiple threads).

@deffn procedure errno [n]
@end deffn
@deffn procedure perror string
@end deffn

@node Ports and descriptors
@section Ports and file descriptors

@deffn procedure move->fdes port fd
@end deffn
@deffn procedure release-port-handle port
@end deffn
@deffn procedure set-port-revealed! @var{port} count
@end deffn
@deffn procedure fdes->ports fdes
@end deffn
@deffn procedure fileno port
@end deffn
@deffn procedure fdopen fdes modes
@end deffn
@deffn procedure duplicate-port port modes
@end deffn
@deffn procedure redirect-port into-port from-port
@end deffn
@deffn procedure freopen filename modes port
@end deffn

@node Extended I/O
@section Extended I/O

Extended I/O procedures are available which read or write lines of text,
read text delimited by a specified set of characters, or report or
set the current position of a port.

@findex fwrite
@findex fread
Interfaces to @code{read}/@code{fread} and @code{write}/@code{fwrite} are
also available, as @code{uniform-array-read!} and @code{uniform-array-write!},
@ref{Uniform arrays}.

@deffn procedure read-line [port] [handle-delim]
Return a line of text from @var{port} if specified, otherwise from the
value returned by @code{(current-input-port)}.  Under Unix, a line of text
is terminated by the first end-of-line character or by end-of-file.

If @var{handle-delim} is specified, it should be one of the following
symbols:
@table @code
@item trim
Discard the terminating delimiter.  This is the default, but it will
be impossible to tell whether the read terminated with a delimiter or
end-of-file.
@item concat
Append the terminating delimiter (if any) to the returned string.
@item peek
Push the terminating delimiter (if any) back on to the port.
@item split
Return a pair containing the string read from the port and the 
terminating delimiter or end-of-file object.

NOTE: if the scsh module is loaded then
multiple values are returned instead of a pair.
@end table
@end deffn
@deffn procedure read-line! buf [port]
Read a line of text into the supplied string @var{buf} and return the
number of characters added to @var{buf}.  If @var{buf} is filled, then
@code{#f} is returned.
Read from @var{port} if
specified, otherwise from the value returned by @code{(current-input-port)}.
@end deffn
@deffn procedure read-delimited delims [port] [handle-delim]
Read text until one of the characters in the string @var{delims} is found
or end-of-file is reached.  Read from @var{port} if supplied, otherwise
from the value returned by @code{(current-input-port)}.
@var{handle-delim} takes the same values as described for @code{read-line}.

NOTE: if the scsh module is loaded then @var{delims} must be an scsh
char-set, not a string.
@end deffn
@deffn procedure read-delimited! delims buf [port] [handle-delim] [start] [end]
Read text into the supplied string @var{buf} and return the number of
characters added to @var{buf} (subject to @var{handle-delim}, which takes
the same values specified for @code{read-line}.  If @var{buf} is filled,
@code{#f} is returned for both the number of characters read and the
delimiter.  Also terminates if one of the characters in the string
@var{delims} is found
or end-of-file is reached.  Read from @var{port} if supplied, otherwise
from the value returned by @code{(current-input-port)}.

NOTE: if the scsh module is loaded then @var{delims} must be an scsh
char-set, not a string.
@end deffn
@deffn procedure write-line obj [port]
Display @var{obj} and a new-line character to @var{port} if specified,
otherwise to the
value returned by @code{(current-output-port)}; equivalent to:

@smalllisp
(display obj [port])
(newline [port])
@end smalllisp
@end deffn
@deffn procedure ftell port
Returns an integer representing the current position of @var{port},
measured from the beginning.
@end deffn
@deffn procedure fseek port offset whence
Sets the current position of @var{port} to the integer @var{offset},
which is interpreted according to the value of @var{whence}.

One of the following variables should be supplied
for @var{whence}:
@defvar SEEK_SET
Seek from the beginning of the file.
@end defvar
@defvar SEEK_CUR
Seek from the current position.
@end defvar
@defvar SEEK_END
Seek from the end of the file.
@end defvar
@end deffn

@node File system
@section File system

These procedures query and set file system attributes (such as owner,
permissions, sizes and types of files); deleting, copying, renaming and
linking files; creating and removing directories and querying their
contents; and the @code{sync} interface.

@deffn procedure access? path how
Evaluates to @code{#t} if @var{path} corresponds to an existing
file and the current process
has the type of access specified by @var{how}, otherwise 
@code{#f}.
@var{how} should be specified
using the values of the variables listed below.  Multiple values can
be combined using a bitwise or, in which case @code{#t} will only
be returned if all accesses are granted.

Permissions are checked using the real id of the current process,
not the effective id, although it's the effective id which determines
whether the access would actually be granted.

@defvar R_OK
test for read permission.
@end defvar
@defvar W_OK
test for write permission.
@end defvar
@defvar X_OK
test for execute permission.
@end defvar
@defvar F_OK
test for existence of the file.
@end defvar
@end deffn
@findex fstat
@deffn procedure stat obj
Evaluates to an object containing various information
about the file determined by @var{obj}.
@var{obj} can be a string containing a file name or a port or file
descriptor which is open on a file (in which case @code{fstat} is used
as the underlying system call).

The object returned by @code{stat} can be passed as a single parameter
to the following procedures, all of which return integers:

@table @r
@item stat:dev
The device containing the file.
@item stat:ino
The file serial number, which distinguishes this file from all other
files on the same device.
@item stat:mode
The mode of the file.  This includes file type information
and the file permission bits.  See @code{stat:type} and @code{stat:perms}
below.
@item stat:nlink
The number of hard links to the file.
@item stat:uid
The user ID of the file's owner.
@item stat:gid
The group ID of the file.
@item stat:rdev
Device ID; this entry is defined only for character or block
special files.
@item stat:size
The size of a regular file in bytes.
@item stat:atime
The last access time for the file.
@item stat:mtime
The last modification time for the file.
@item stat:ctime
The last modification time for the attributes of the file.
@item stat:blksize
The optimal block size for reading or writing the file, in bytes.
@item stat:blocks
The amount of disk space that the file occupies measured in units of
512 byte blocks.
@end table

In addition, the following procedures return the information
from stat:mode in a more convenient form:

@table @r
@item stat:type
A symbol representing the type of file.  Possible values are
currently: regular, directory, symlink, block-special, char-special,
fifo, socket, unknown
@item stat:perms
An integer representing the access permission bits.
@end table
@end deffn
@deffn procedure lstat path
Similar to @code{stat}, but does not follow symbolic links, i.e.,
it will return information about a symbolic link itself, not the 
file it points to.  @var{path} must be a string.
@end deffn
@deffn procedure readlink path
@end deffn
@deffn procedure chown path owner group
@end deffn
@deffn procedure chmod port-or-path mode
@end deffn
@deffn procedure utime path [actime] [modtime]
@end deffn
@deffn procedure delete-file path
@end deffn
@deffn procedure copy-file path-from path-to
@end deffn
@deffn procedure rename-file path-from path-to
@end deffn
@deffn procedure link path-from path-to
@end deffn
@deffn procedure symlink path-from path-to
@end deffn
@deffn procedure mkdir path [mode]
@end deffn
@deffn procedure rmdir path
@end deffn
@deffn procedure opendir path
@end deffn
@deffn procedure readdir port
@end deffn
@deffn procedure rewinddir port
@end deffn
@deffn procedure closedir port
@end deffn
@deffn procedure sync
@end deffn

@node User database
@section User database

@deffn procedure getpwuid uid
@end deffn
@deffn procedure getpwnam name
@end deffn
@deffn procedure getpwent
@end deffn
@deffn procedure setpwent port
@end deffn
@deffn procedure endpwent
@end deffn
@deffn procedure getgrgid uid
@end deffn
@deffn procedure getgrnam name
@end deffn
@deffn procedure getgrent
@end deffn
@deffn procedure setgrent port
@end deffn
@deffn procedure endgrent
@end deffn

@node Processes
@section Processes

@deffn procedure chdir path
@end deffn
@deffn procedure getcwd
@end deffn
@deffn procedure umask [mode]
@end deffn
@deffn procedure getpid
@end deffn
@deffn procedure getgroups
@end deffn
@deffn procedure kill pid sig

@var{sig} should be specified using a variable corresponding to
the Unix symbolic name, e.g,
@defvar SIGHUP
Hang-up signal.
@end defvar
@defvar SIGINT
Interrupt signal.
@end defvar
@end deffn
@deffn procedure waitpid pid options
@defvar WAIT_ANY
@end defvar
@defvar WAIT_MYPGRP
@end defvar
@defvar WNOHANG
@end defvar
@defvar WUNTRACED
@end defvar
@end deffn
@deffn procedure getppid
@end deffn
@deffn procedure getuid
@end deffn
@deffn procedure getgid
@end deffn
@deffn procedure geteuid
@end deffn
@deffn procedure getegid
@end deffn
@deffn procedure setuid id
@end deffn
@deffn procedure setgid id
@end deffn
@deffn procedure seteuid id
@end deffn
@deffn procedure setegid id
@end deffn
@deffn procedure getpgrp
@end deffn
@deffn procedure setpgid pid pgid
@end deffn
@deffn procedure setsid
@end deffn
@deffn procedure execl arg ...
@end deffn
@deffn procedure execlp arg ...
@end deffn
@deffn procedure primitive-fork
@end deffn
@deffn procedure environ [env]
@end deffn
@deffn procedure putenv string
@end deffn
@deffn procedure nice incr
@end deffn

@node Terminals
@section Terminals and pseudo-terminals

@deffn procedure isatty? port
@end deffn
@deffn procedure ttyname port
@end deffn
@deffn procedure ctermid
@end deffn
@deffn procedure tcgetpgrp port
@end deffn
@deffn procedure tcsetpgrp port pgid
@end deffn

@node Network databases
@section Network address conversion and system databases

@deffn procedure inet-aton address
@end deffn
@deffn procedure inet-ntoa number
@end deffn
@deffn procedure inet-netof address
@end deffn
@deffn procedure inet-lnaof address
@end deffn
@deffn procedure inet-makeaddr net lna
@end deffn
@deffn procedure gethostbyname name
@end deffn
@deffn procedure gethostbyaddr address
@end deffn
@deffn procedure gethostent
@end deffn
@deffn procedure sethostent port
@end deffn
@deffn procedure endhostent
@end deffn
@deffn procedure getnetbyname name
@end deffn
@deffn procedure getnetbyaddr address
@end deffn
@deffn procedure getnetent
@end deffn
@deffn procedure setnetent port
@end deffn
@deffn procedure endnetent
@end deffn
@deffn procedure getprotobyname name
@end deffn
@deffn procedure getprotobynumber number
@end deffn
@deffn procedure getprotoent
@end deffn
@deffn procedure setprotoent port
@end deffn
@deffn procedure endprotoent
@end deffn
@deffn procedure getservbyname name protocol
@end deffn
@deffn procedure getservbyport port protocol
@end deffn
@deffn procedure getservent
@end deffn
@deffn procedure setservent port
@end deffn
@deffn procedure endservent
@end deffn

@node Network sockets
@section BSD socket library interface

@deffn procedure socket family style protocol
@end deffn
@deffn procedure socketpair family style protocol
@end deffn
@deffn procedure getsockopt socket level optname
@end deffn
@deffn procedure setsockopt socket level optname value
@end deffn
@deffn procedure shutdown socket how
@end deffn
@deffn procedure connect socket family address arg ...
@end deffn
@deffn procedure bind socket family address arg ...
@end deffn
@deffn procedure listen socket backlog
@end deffn
@deffn procedure accept socket
@end deffn
@deffn procedure getsockname socket
@end deffn
@deffn procedure getpeername socket
@end deffn
@deffn procedure recv! socket buf [flags]
@end deffn
@deffn procedure send socket message [flags]
@end deffn
@deffn procedure recvfrom! socket buf [flags] [start] [end]
@end deffn
@deffn procedure sendto socket message family address args ... [flags]
@end deffn

@node Miscellaneous Unix
@section Miscellaneous Unix interfaces

Things which haven't been classified elsewhere (yet?).

@deffn procedure open path flags [mode]
@defvar O_RDONLY
@end defvar
@defvar O_WRONLY
@end defvar
@defvar O_RDWR
@end defvar
@defvar O_CREAT
@end defvar
@defvar O_EXCL
@end defvar
@defvar O_NOCTTY
@end defvar
@defvar O_TRUNC
@end defvar
@defvar O_APPEND
@end defvar
@defvar O_NONBLOCK
@end defvar
@defvar O_NDELAY
@end defvar
@defvar O_SYNC
@end defvar
@end deffn
@deffn procedure select reads writes excepts secs msecs
@end deffn
@deffn procedure uname
@end deffn
@deffn procedure pipe
@end deffn
@deffn procedure open-pipe command modes
@end deffn
@deffn procedure open-input-pipe command
@end deffn
@deffn procedure open-output-pipe command
@end deffn
@deffn procedure setlocale category [locale]
@defvar LC_COLLATE
@end defvar
@defvar LC_CTYPE
@end defvar
@defvar LC_MONETARY
@end defvar
@defvar LC_NUMERIC
@end defvar
@defvar LC_TIME
@end defvar
@defvar LC_MESSAGES
@end defvar
@defvar LC_ALL
@end defvar
@end deffn
@deffn procedure strftime format stime
@end deffn
@deffn procedure strptime format string
@end deffn
@deffn procedure mknod
@end deffn

@node scsh
@chapter The Scheme shell (scsh) 

Guile includes an incomplete port of the Scheme shell (scsh) 0.4.4.

For information about scsh on the Web see
@url{http://www-swiss.ai.mit.edu/scsh/scsh.html}.
The original scsh is available by ftp from
@url{ftp://swiss-ftp.ai.mit.edu:/pub/su}.

This port of scsh does not currently use the Guile module system, but
can be initialized using:
@smalllisp
(load-from-path "scsh/init")
@end smalllisp

Note that SLIB must be installed before scsh can be initialized, see
@ref{SLIB} for details.

@node Threads
@chapter Programming Threads.