]> git.donarmstrong.com Git - lilypond.git/blob - guile18/doc/sources/unix.texi
New upstream version 2.19.65
[lilypond.git] / guile18 / doc / sources / unix.texi
1 @node Low level Unix
2 @chapter Low level Unix interfaces
3
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.
8
9 @menu
10 * Unix conventions::            Conventions followed by the low level Unix
11                                 interfaces.
12 * Ports and descriptors::       Ports, file descriptors and how they
13                                 interact.
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.
23 @end menu
24
25 @node Unix conventions
26 @section Low level Unix conventions
27
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}.
32
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}.)
37
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.
45
46 General naming conventions are as follows:
47
48 @itemize @bullet
49 @item
50 The Scheme name is often identical to the name of the underlying Unix
51 facility.
52 @item
53 Underscores in Unix names are converted to hyphens.
54 @item
55 Procedures which destructively modify Scheme data gain postpended
56 exclaimation marks, e.g., @code{recv!}.
57 @item
58 Predicates are postpended with question marks, e.g., @code{access?}.
59 @item
60 Some names are changed to avoid conflict with dissimilar interfaces
61 defined by scsh.
62 @item
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
65 with hyphens)
66 @end itemize
67
68 Most of the Unix interface procedures can be relied on to return a
69 well-specified value.  Unexpected conditions are handled by raising
70 exceptions.
71
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.
76
77 For ways to deal with exceptions, @ref{Exceptions}.
78
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).
85
86 @deffn procedure errno [n]
87 @end deffn
88 @deffn procedure perror string
89 @end deffn
90
91 @node Ports and descriptors
92 @section Ports and file descriptors
93
94 @deffn procedure move->fdes port fd
95 @end deffn
96 @deffn procedure release-port-handle port
97 @end deffn
98 @deffn procedure set-port-revealed! @var{port} count
99 @end deffn
100 @deffn procedure fdes->ports fdes
101 @end deffn
102 @deffn procedure fileno port
103 @end deffn
104 @deffn procedure fdopen fdes modes
105 @end deffn
106 @deffn procedure duplicate-port port modes
107 @end deffn
108 @deffn procedure redirect-port into-port from-port
109 @end deffn
110 @deffn procedure freopen filename modes port
111 @end deffn
112
113 @node Extended I/O
114 @section Extended I/O
115
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.
119
120 @findex fwrite
121 @findex fread
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}.
125
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.
130
131 If @var{handle-delim} is specified, it should be one of the following
132 symbols:
133 @table @code
134 @item trim
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
137 end-of-file.
138 @item concat
139 Append the terminating delimiter (if any) to the returned string.
140 @item peek
141 Push the terminating delimiter (if any) back on to the port.
142 @item split
143 Return a pair containing the string read from the port and the 
144 terminating delimiter or end-of-file object.
145
146 NOTE: if the scsh module is loaded then
147 multiple values are returned instead of a pair.
148 @end table
149 @end deffn
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)}.
156 @end deffn
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}.
162
163 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
164 char-set, not a string.
165 @end deffn
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)}.
175
176 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
177 char-set, not a string.
178 @end deffn
179 @deffn procedure write-line obj [port]
180 Display @var{obj} and a new-line character to @var{port} if specified,
181 otherwise to the
182 value returned by @code{(current-output-port)}; equivalent to:
183
184 @smalllisp
185 (display obj [port])
186 (newline [port])
187 @end smalllisp
188 @end deffn
189 @deffn procedure ftell port
190 Returns an integer representing the current position of @var{port},
191 measured from the beginning.
192 @end deffn
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}.
196
197 One of the following variables should be supplied
198 for @var{whence}:
199 @defvar SEEK_SET
200 Seek from the beginning of the file.
201 @end defvar
202 @defvar SEEK_CUR
203 Seek from the current position.
204 @end defvar
205 @defvar SEEK_END
206 Seek from the end of the file.
207 @end defvar
208 @end deffn
209
210 @node File system
211 @section File system
212
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.
217
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 
222 @code{#f}.
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.
227
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.
231
232 @defvar R_OK
233 test for read permission.
234 @end defvar
235 @defvar W_OK
236 test for write permission.
237 @end defvar
238 @defvar X_OK
239 test for execute permission.
240 @end defvar
241 @defvar F_OK
242 test for existence of the file.
243 @end defvar
244 @end deffn
245 @findex fstat
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).
252
253 The object returned by @code{stat} can be passed as a single parameter
254 to the following procedures, all of which return integers:
255
256 @table @r
257 @item stat:dev
258 The device containing the file.
259 @item stat:ino
260 The file serial number, which distinguishes this file from all other
261 files on the same device.
262 @item stat:mode
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}
265 below.
266 @item stat:nlink
267 The number of hard links to the file.
268 @item stat:uid
269 The user ID of the file's owner.
270 @item stat:gid
271 The group ID of the file.
272 @item stat:rdev
273 Device ID; this entry is defined only for character or block
274 special files.
275 @item stat:size
276 The size of a regular file in bytes.
277 @item stat:atime
278 The last access time for the file.
279 @item stat:mtime
280 The last modification time for the file.
281 @item stat:ctime
282 The last modification time for the attributes of the file.
283 @item stat:blksize
284 The optimal block size for reading or writing the file, in bytes.
285 @item stat:blocks
286 The amount of disk space that the file occupies measured in units of
287 512 byte blocks.
288 @end table
289
290 In addition, the following procedures return the information
291 from stat:mode in a more convenient form:
292
293 @table @r
294 @item stat:type
295 A symbol representing the type of file.  Possible values are
296 currently: regular, directory, symlink, block-special, char-special,
297 fifo, socket, unknown
298 @item stat:perms
299 An integer representing the access permission bits.
300 @end table
301 @end deffn
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.
306 @end deffn
307 @deffn procedure readlink path
308 @end deffn
309 @deffn procedure chown path owner group
310 @end deffn
311 @deffn procedure chmod port-or-path mode
312 @end deffn
313 @deffn procedure utime path [actime] [modtime]
314 @end deffn
315 @deffn procedure delete-file path
316 @end deffn
317 @deffn procedure copy-file path-from path-to
318 @end deffn
319 @deffn procedure rename-file path-from path-to
320 @end deffn
321 @deffn procedure link path-from path-to
322 @end deffn
323 @deffn procedure symlink path-from path-to
324 @end deffn
325 @deffn procedure mkdir path [mode]
326 @end deffn
327 @deffn procedure rmdir path
328 @end deffn
329 @deffn procedure opendir path
330 @end deffn
331 @deffn procedure readdir port
332 @end deffn
333 @deffn procedure rewinddir port
334 @end deffn
335 @deffn procedure closedir port
336 @end deffn
337 @deffn procedure sync
338 @end deffn
339
340 @node User database
341 @section User database
342
343 @deffn procedure getpwuid uid
344 @end deffn
345 @deffn procedure getpwnam name
346 @end deffn
347 @deffn procedure getpwent
348 @end deffn
349 @deffn procedure setpwent port
350 @end deffn
351 @deffn procedure endpwent
352 @end deffn
353 @deffn procedure getgrgid uid
354 @end deffn
355 @deffn procedure getgrnam name
356 @end deffn
357 @deffn procedure getgrent
358 @end deffn
359 @deffn procedure setgrent port
360 @end deffn
361 @deffn procedure endgrent
362 @end deffn
363
364 @node Processes
365 @section Processes
366
367 @deffn procedure chdir path
368 @end deffn
369 @deffn procedure getcwd
370 @end deffn
371 @deffn procedure umask [mode]
372 @end deffn
373 @deffn procedure getpid
374 @end deffn
375 @deffn procedure getgroups
376 @end deffn
377 @deffn procedure kill pid sig
378
379 @var{sig} should be specified using a variable corresponding to
380 the Unix symbolic name, e.g,
381 @defvar SIGHUP
382 Hang-up signal.
383 @end defvar
384 @defvar SIGINT
385 Interrupt signal.
386 @end defvar
387 @end deffn
388 @deffn procedure waitpid pid options
389 @defvar WAIT_ANY
390 @end defvar
391 @defvar WAIT_MYPGRP
392 @end defvar
393 @defvar WNOHANG
394 @end defvar
395 @defvar WUNTRACED
396 @end defvar
397 @end deffn
398 @deffn procedure getppid
399 @end deffn
400 @deffn procedure getuid
401 @end deffn
402 @deffn procedure getgid
403 @end deffn
404 @deffn procedure geteuid
405 @end deffn
406 @deffn procedure getegid
407 @end deffn
408 @deffn procedure setuid id
409 @end deffn
410 @deffn procedure setgid id
411 @end deffn
412 @deffn procedure seteuid id
413 @end deffn
414 @deffn procedure setegid id
415 @end deffn
416 @deffn procedure getpgrp
417 @end deffn
418 @deffn procedure setpgid pid pgid
419 @end deffn
420 @deffn procedure setsid
421 @end deffn
422 @deffn procedure execl arg ...
423 @end deffn
424 @deffn procedure execlp arg ...
425 @end deffn
426 @deffn procedure primitive-fork
427 @end deffn
428 @deffn procedure environ [env]
429 @end deffn
430 @deffn procedure putenv string
431 @end deffn
432 @deffn procedure nice incr
433 @end deffn
434
435 @node Terminals
436 @section Terminals and pseudo-terminals
437
438 @deffn procedure isatty? port
439 @end deffn
440 @deffn procedure ttyname port
441 @end deffn
442 @deffn procedure ctermid
443 @end deffn
444 @deffn procedure tcgetpgrp port
445 @end deffn
446 @deffn procedure tcsetpgrp port pgid
447 @end deffn
448
449 @node Network databases
450 @section Network address conversion and system databases
451
452 @deffn procedure inet-aton address
453 @end deffn
454 @deffn procedure inet-ntoa number
455 @end deffn
456 @deffn procedure inet-netof address
457 @end deffn
458 @deffn procedure inet-lnaof address
459 @end deffn
460 @deffn procedure inet-makeaddr net lna
461 @end deffn
462 @deffn procedure gethostbyname name
463 @end deffn
464 @deffn procedure gethostbyaddr address
465 @end deffn
466 @deffn procedure gethostent
467 @end deffn
468 @deffn procedure sethostent port
469 @end deffn
470 @deffn procedure endhostent
471 @end deffn
472 @deffn procedure getnetbyname name
473 @end deffn
474 @deffn procedure getnetbyaddr address
475 @end deffn
476 @deffn procedure getnetent
477 @end deffn
478 @deffn procedure setnetent port
479 @end deffn
480 @deffn procedure endnetent
481 @end deffn
482 @deffn procedure getprotobyname name
483 @end deffn
484 @deffn procedure getprotobynumber number
485 @end deffn
486 @deffn procedure getprotoent
487 @end deffn
488 @deffn procedure setprotoent port
489 @end deffn
490 @deffn procedure endprotoent
491 @end deffn
492 @deffn procedure getservbyname name protocol
493 @end deffn
494 @deffn procedure getservbyport port protocol
495 @end deffn
496 @deffn procedure getservent
497 @end deffn
498 @deffn procedure setservent port
499 @end deffn
500 @deffn procedure endservent
501 @end deffn
502
503 @node Network sockets
504 @section BSD socket library interface
505
506 @deffn procedure socket family style protocol
507 @end deffn
508 @deffn procedure socketpair family style protocol
509 @end deffn
510 @deffn procedure getsockopt socket level optname
511 @end deffn
512 @deffn procedure setsockopt socket level optname value
513 @end deffn
514 @deffn procedure shutdown socket how
515 @end deffn
516 @deffn procedure connect socket family address arg ...
517 @end deffn
518 @deffn procedure bind socket family address arg ...
519 @end deffn
520 @deffn procedure listen socket backlog
521 @end deffn
522 @deffn procedure accept socket
523 @end deffn
524 @deffn procedure getsockname socket
525 @end deffn
526 @deffn procedure getpeername socket
527 @end deffn
528 @deffn procedure recv! socket buf [flags]
529 @end deffn
530 @deffn procedure send socket message [flags]
531 @end deffn
532 @deffn procedure recvfrom! socket buf [flags] [start] [end]
533 @end deffn
534 @deffn procedure sendto socket message family address args ... [flags]
535 @end deffn
536
537 @node Miscellaneous Unix
538 @section Miscellaneous Unix interfaces
539
540 Things which haven't been classified elsewhere (yet?).
541
542 @deffn procedure open path flags [mode]
543 @defvar O_RDONLY
544 @end defvar
545 @defvar O_WRONLY
546 @end defvar
547 @defvar O_RDWR
548 @end defvar
549 @defvar O_CREAT
550 @end defvar
551 @defvar O_EXCL
552 @end defvar
553 @defvar O_NOCTTY
554 @end defvar
555 @defvar O_TRUNC
556 @end defvar
557 @defvar O_APPEND
558 @end defvar
559 @defvar O_NONBLOCK
560 @end defvar
561 @defvar O_NDELAY
562 @end defvar
563 @defvar O_SYNC
564 @end defvar
565 @end deffn
566 @deffn procedure select reads writes excepts secs msecs
567 @end deffn
568 @deffn procedure uname
569 @end deffn
570 @deffn procedure pipe
571 @end deffn
572 @deffn procedure open-pipe command modes
573 @end deffn
574 @deffn procedure open-input-pipe command
575 @end deffn
576 @deffn procedure open-output-pipe command
577 @end deffn
578 @deffn procedure setlocale category [locale]
579 @defvar LC_COLLATE
580 @end defvar
581 @defvar LC_CTYPE
582 @end defvar
583 @defvar LC_MONETARY
584 @end defvar
585 @defvar LC_NUMERIC
586 @end defvar
587 @defvar LC_TIME
588 @end defvar
589 @defvar LC_MESSAGES
590 @end defvar
591 @defvar LC_ALL
592 @end defvar
593 @end deffn
594 @deffn procedure strftime format stime
595 @end deffn
596 @deffn procedure strptime format string
597 @end deffn
598 @deffn procedure mknod
599 @end deffn
600
601 @node scsh
602 @chapter The Scheme shell (scsh) 
603
604 Guile includes an incomplete port of the Scheme shell (scsh) 0.4.4.
605
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}.
610
611 This port of scsh does not currently use the Guile module system, but
612 can be initialized using:
613 @smalllisp
614 (load-from-path "scsh/init")
615 @end smalllisp
616
617 Note that SLIB must be installed before scsh can be initialized, see
618 @ref{SLIB} for details.
619
620 @node Threads
621 @chapter Programming Threads.
622