unix.texi 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. @node Low level Unix
  2. @chapter Low level Unix interfaces
  3. The low level Unix interfaces are currently available by
  4. default in the Guile top level. However in the future they will probably
  5. be placed in a module and @code{use-modules} or something similar will
  6. be required to make them available.
  7. @menu
  8. * Unix conventions:: Conventions followed by the low level Unix
  9. interfaces.
  10. * Ports and descriptors:: Ports, file descriptors and how they
  11. interact.
  12. * Extended I/O:: Reading and writing to ports.
  13. * File system:: Working in a hierarchical filesystem.
  14. * User database:: Information about users from system databases.
  15. * Processes:: Information and control of Unix processes.
  16. * Terminals:: Terminals and pseudo-terminals.
  17. * Network databases:: Network address conversion and information
  18. from system databases.
  19. * Network sockets:: An interface to the BSD socket library.
  20. * Miscellaneous Unix:: Miscellaneous Unix interfaces.
  21. @end menu
  22. @node Unix conventions
  23. @section Low level Unix conventions
  24. The low-level interfaces are designed to give Scheme programs
  25. access to as much functionality as possible from the underlying
  26. Unix system. They can be used to implement higher level
  27. intefaces such as the Scheme shell @ref{scsh}.
  28. Generally there is a single procedure for each corresponding Unix
  29. facility. However some of the procedures are implemented for
  30. speed and convenience in Scheme and have no Unix equivalent
  31. (e.g., @code{read-delimited}, @code{copy-file}.)
  32. This interface is intended as far as possible to be portable across
  33. different versions of Unix, so that Scheme programmers don't need to be
  34. concerned with implementation differences. In some cases procedures
  35. which can't be implemented (or reimplemented) on particular systems may
  36. become no-ops, or perform limited actions. In other cases they may
  37. throw errors. It should be possible to use the feature system to
  38. determine what functionality is available.
  39. General naming conventions are as follows:
  40. @itemize @bullet
  41. @item
  42. The Scheme name is often identical to the name of the underlying Unix
  43. facility.
  44. @item
  45. Underscores in Unix names are converted to hyphens.
  46. @item
  47. Procedures which destructively modify Scheme data gain postpended
  48. exclaimation marks, e.g., @code{recv!}.
  49. @item
  50. Predicates are postpended with question marks, e.g., @code{access?}.
  51. @item
  52. Some names are changed to avoid conflict with dissimilar interfaces
  53. defined by scsh.
  54. @item
  55. Unix preprocessor names such as @code{EPERM} or @code{R_OK} are converted
  56. to Scheme variables of the same name (underscores are not replaced
  57. with hyphens)
  58. @end itemize
  59. Most of the Unix interface procedures can be relied on to return a
  60. well-specified value. Unexpected conditions are handled by raising
  61. exceptions.
  62. There are a few procedures which return a special
  63. value if they don't succeed, e.g., @code{getenv} returns @code{#f}
  64. if it the requested string is not found in the environment. These
  65. cases will be noted in the documentation.
  66. For ways to deal with exceptions, @ref{Exceptions}.
  67. Errors which the C-library would report by returning a NULL
  68. pointer or through some other means cause a @code{system-error} exception
  69. to be raised. The value of the Unix @code{errno} variable is available
  70. in the data passed by the exception, so there is no need to access the
  71. global errno value (doing so would be unreliable in the presence of
  72. continuations or multiple threads).
  73. @deffn procedure errno [n]
  74. @end deffn
  75. @deffn procedure perror string
  76. @end deffn
  77. @node Ports and descriptors
  78. @section Ports and file descriptors
  79. @deffn procedure move->fdes port fd
  80. @end deffn
  81. @deffn procedure release-port-handle port
  82. @end deffn
  83. @deffn procedure set-port-revealed! @var{port} count
  84. @end deffn
  85. @deffn procedure fdes->ports fdes
  86. @end deffn
  87. @deffn procedure fileno port
  88. @end deffn
  89. @deffn procedure fdopen fdes modes
  90. @end deffn
  91. @deffn procedure duplicate-port port modes
  92. @end deffn
  93. @deffn procedure redirect-port into-port from-port
  94. @end deffn
  95. @deffn procedure freopen filename modes port
  96. @end deffn
  97. @node Extended I/O
  98. @section Extended I/O
  99. Extended I/O procedures are available which read or write lines of text,
  100. read text delimited by a specified set of characters, or report or
  101. set the current position of a port.
  102. @findex fwrite
  103. @findex fread
  104. Interfaces to @code{read}/@code{fread} and @code{write}/@code{fwrite} are
  105. also available, as @code{uniform-array-read!} and @code{uniform-array-write!},
  106. @ref{Uniform arrays}.
  107. @deffn procedure read-line [port] [handle-delim]
  108. Return a line of text from @var{port} if specified, otherwise from the
  109. value returned by @code{(current-input-port)}. Under Unix, a line of text
  110. is terminated by the first end-of-line character or by end-of-file.
  111. If @var{handle-delim} is specified, it should be one of the following
  112. symbols:
  113. @table @code
  114. @item trim
  115. Discard the terminating delimiter. This is the default, but it will
  116. be impossible to tell whether the read terminated with a delimiter or
  117. end-of-file.
  118. @item concat
  119. Append the terminating delimiter (if any) to the returned string.
  120. @item peek
  121. Push the terminating delimiter (if any) back on to the port.
  122. @item split
  123. Return a pair containing the string read from the port and the
  124. terminating delimiter or end-of-file object.
  125. NOTE: if the scsh module is loaded then
  126. multiple values are returned instead of a pair.
  127. @end table
  128. @end deffn
  129. @deffn procedure read-line! buf [port]
  130. Read a line of text into the supplied string @var{buf} and return the
  131. number of characters added to @var{buf}. If @var{buf} is filled, then
  132. @code{#f} is returned.
  133. Read from @var{port} if
  134. specified, otherwise from the value returned by @code{(current-input-port)}.
  135. @end deffn
  136. @deffn procedure read-delimited delims [port] [handle-delim]
  137. Read text until one of the characters in the string @var{delims} is found
  138. or end-of-file is reached. Read from @var{port} if supplied, otherwise
  139. from the value returned by @code{(current-input-port)}.
  140. @var{handle-delim} takes the same values as described for @code{read-line}.
  141. NOTE: if the scsh module is loaded then @var{delims} must be an scsh
  142. char-set, not a string.
  143. @end deffn
  144. @deffn procedure read-delimited! delims buf [port] [handle-delim] [start] [end]
  145. Read text into the supplied string @var{buf} and return the number of
  146. characters added to @var{buf} (subject to @var{handle-delim}, which takes
  147. the same values specified for @code{read-line}. If @var{buf} is filled,
  148. @code{#f} is returned for both the number of characters read and the
  149. delimiter. Also terminates if one of the characters in the string
  150. @var{delims} is found
  151. or end-of-file is reached. Read from @var{port} if supplied, otherwise
  152. from the value returned by @code{(current-input-port)}.
  153. NOTE: if the scsh module is loaded then @var{delims} must be an scsh
  154. char-set, not a string.
  155. @end deffn
  156. @deffn procedure write-line obj [port]
  157. Display @var{obj} and a new-line character to @var{port} if specified,
  158. otherwise to the
  159. value returned by @code{(current-output-port)}; equivalent to:
  160. @smalllisp
  161. (display obj [port])
  162. (newline [port])
  163. @end smalllisp
  164. @end deffn
  165. @deffn procedure ftell port
  166. Returns an integer representing the current position of @var{port},
  167. measured from the beginning.
  168. @end deffn
  169. @deffn procedure fseek port offset whence
  170. Sets the current position of @var{port} to the integer @var{offset},
  171. which is interpreted according to the value of @var{whence}.
  172. One of the following variables should be supplied
  173. for @var{whence}:
  174. @defvar SEEK_SET
  175. Seek from the beginning of the file.
  176. @end defvar
  177. @defvar SEEK_CUR
  178. Seek from the current position.
  179. @end defvar
  180. @defvar SEEK_END
  181. Seek from the end of the file.
  182. @end defvar
  183. @end deffn
  184. @node File system
  185. @section File system
  186. These procedures query and set file system attributes (such as owner,
  187. permissions, sizes and types of files); deleting, copying, renaming and
  188. linking files; creating and removing directories and querying their
  189. contents; and the @code{sync} interface.
  190. @deffn procedure access? path how
  191. Evaluates to @code{#t} if @var{path} corresponds to an existing
  192. file and the current process
  193. has the type of access specified by @var{how}, otherwise
  194. @code{#f}.
  195. @var{how} should be specified
  196. using the values of the variables listed below. Multiple values can
  197. be combined using a bitwise or, in which case @code{#t} will only
  198. be returned if all accesses are granted.
  199. Permissions are checked using the real id of the current process,
  200. not the effective id, although it's the effective id which determines
  201. whether the access would actually be granted.
  202. @defvar R_OK
  203. test for read permission.
  204. @end defvar
  205. @defvar W_OK
  206. test for write permission.
  207. @end defvar
  208. @defvar X_OK
  209. test for execute permission.
  210. @end defvar
  211. @defvar F_OK
  212. test for existence of the file.
  213. @end defvar
  214. @end deffn
  215. @findex fstat
  216. @deffn procedure stat obj
  217. Evaluates to an object containing various information
  218. about the file determined by @var{obj}.
  219. @var{obj} can be a string containing a file name or a port or file
  220. descriptor which is open on a file (in which case @code{fstat} is used
  221. as the underlying system call).
  222. The object returned by @code{stat} can be passed as a single parameter
  223. to the following procedures, all of which return integers:
  224. @table @r
  225. @item stat:dev
  226. The device containing the file.
  227. @item stat:ino
  228. The file serial number, which distinguishes this file from all other
  229. files on the same device.
  230. @item stat:mode
  231. The mode of the file. This includes file type information
  232. and the file permission bits. See @code{stat:type} and @code{stat:perms}
  233. below.
  234. @item stat:nlink
  235. The number of hard links to the file.
  236. @item stat:uid
  237. The user ID of the file's owner.
  238. @item stat:gid
  239. The group ID of the file.
  240. @item stat:rdev
  241. Device ID; this entry is defined only for character or block
  242. special files.
  243. @item stat:size
  244. The size of a regular file in bytes.
  245. @item stat:atime
  246. The last access time for the file.
  247. @item stat:mtime
  248. The last modification time for the file.
  249. @item stat:ctime
  250. The last modification time for the attributes of the file.
  251. @item stat:blksize
  252. The optimal block size for reading or writing the file, in bytes.
  253. @item stat:blocks
  254. The amount of disk space that the file occupies measured in units of
  255. 512 byte blocks.
  256. @end table
  257. In addition, the following procedures return the information
  258. from stat:mode in a more convenient form:
  259. @table @r
  260. @item stat:type
  261. A symbol representing the type of file. Possible values are
  262. currently: regular, directory, symlink, block-special, char-special,
  263. fifo, socket, unknown
  264. @item stat:perms
  265. An integer representing the access permission bits.
  266. @end table
  267. @end deffn
  268. @deffn procedure lstat path
  269. Similar to @code{stat}, but does not follow symbolic links, i.e.,
  270. it will return information about a symbolic link itself, not the
  271. file it points to. @var{path} must be a string.
  272. @end deffn
  273. @deffn procedure readlink path
  274. @end deffn
  275. @deffn procedure chown path owner group
  276. @end deffn
  277. @deffn procedure chmod port-or-path mode
  278. @end deffn
  279. @deffn procedure utime path [actime] [modtime]
  280. @end deffn
  281. @deffn procedure delete-file path
  282. @end deffn
  283. @deffn procedure copy-file path-from path-to
  284. @end deffn
  285. @deffn procedure rename-file path-from path-to
  286. @end deffn
  287. @deffn procedure link path-from path-to
  288. @end deffn
  289. @deffn procedure symlink path-from path-to
  290. @end deffn
  291. @deffn procedure mkdir path [mode]
  292. @end deffn
  293. @deffn procedure rmdir path
  294. @end deffn
  295. @deffn procedure opendir path
  296. @end deffn
  297. @deffn procedure readdir port
  298. @end deffn
  299. @deffn procedure rewinddir port
  300. @end deffn
  301. @deffn procedure closedir port
  302. @end deffn
  303. @deffn procedure sync
  304. @end deffn
  305. @node User database
  306. @section User database
  307. @deffn procedure getpwuid uid
  308. @end deffn
  309. @deffn procedure getpwnam name
  310. @end deffn
  311. @deffn procedure getpwent
  312. @end deffn
  313. @deffn procedure setpwent port
  314. @end deffn
  315. @deffn procedure endpwent
  316. @end deffn
  317. @deffn procedure getgrgid uid
  318. @end deffn
  319. @deffn procedure getgrnam name
  320. @end deffn
  321. @deffn procedure getgrent
  322. @end deffn
  323. @deffn procedure setgrent port
  324. @end deffn
  325. @deffn procedure endgrent
  326. @end deffn
  327. @node Processes
  328. @section Processes
  329. @deffn procedure chdir path
  330. @end deffn
  331. @deffn procedure getcwd
  332. @end deffn
  333. @deffn procedure umask [mode]
  334. @end deffn
  335. @deffn procedure getpid
  336. @end deffn
  337. @deffn procedure getgroups
  338. @end deffn
  339. @deffn procedure kill pid sig
  340. @var{sig} should be specified using a variable corresponding to
  341. the Unix symbolic name, e.g,
  342. @defvar SIGHUP
  343. Hang-up signal.
  344. @end defvar
  345. @defvar SIGINT
  346. Interrupt signal.
  347. @end defvar
  348. @end deffn
  349. @deffn procedure waitpid pid options
  350. @defvar WAIT_ANY
  351. @end defvar
  352. @defvar WAIT_MYPGRP
  353. @end defvar
  354. @defvar WNOHANG
  355. @end defvar
  356. @defvar WUNTRACED
  357. @end defvar
  358. @end deffn
  359. @deffn procedure getppid
  360. @end deffn
  361. @deffn procedure getuid
  362. @end deffn
  363. @deffn procedure getgid
  364. @end deffn
  365. @deffn procedure geteuid
  366. @end deffn
  367. @deffn procedure getegid
  368. @end deffn
  369. @deffn procedure setuid id
  370. @end deffn
  371. @deffn procedure setgid id
  372. @end deffn
  373. @deffn procedure seteuid id
  374. @end deffn
  375. @deffn procedure setegid id
  376. @end deffn
  377. @deffn procedure getpgrp
  378. @end deffn
  379. @deffn procedure setpgid pid pgid
  380. @end deffn
  381. @deffn procedure setsid
  382. @end deffn
  383. @deffn procedure execl arg ...
  384. @end deffn
  385. @deffn procedure execlp arg ...
  386. @end deffn
  387. @deffn procedure primitive-fork
  388. @end deffn
  389. @deffn procedure environ [env]
  390. @end deffn
  391. @deffn procedure putenv string
  392. @end deffn
  393. @deffn procedure nice incr
  394. @end deffn
  395. @node Terminals
  396. @section Terminals and pseudo-terminals
  397. @deffn procedure isatty? port
  398. @end deffn
  399. @deffn procedure ttyname port
  400. @end deffn
  401. @deffn procedure ctermid
  402. @end deffn
  403. @deffn procedure tcgetpgrp port
  404. @end deffn
  405. @deffn procedure tcsetpgrp port pgid
  406. @end deffn
  407. @node Network databases
  408. @section Network address conversion and system databases
  409. @deffn procedure inet-aton address
  410. @end deffn
  411. @deffn procedure inet-ntoa number
  412. @end deffn
  413. @deffn procedure inet-netof address
  414. @end deffn
  415. @deffn procedure inet-lnaof address
  416. @end deffn
  417. @deffn procedure inet-makeaddr net lna
  418. @end deffn
  419. @deffn procedure gethostbyname name
  420. @end deffn
  421. @deffn procedure gethostbyaddr address
  422. @end deffn
  423. @deffn procedure gethostent
  424. @end deffn
  425. @deffn procedure sethostent port
  426. @end deffn
  427. @deffn procedure endhostent
  428. @end deffn
  429. @deffn procedure getnetbyname name
  430. @end deffn
  431. @deffn procedure getnetbyaddr address
  432. @end deffn
  433. @deffn procedure getnetent
  434. @end deffn
  435. @deffn procedure setnetent port
  436. @end deffn
  437. @deffn procedure endnetent
  438. @end deffn
  439. @deffn procedure getprotobyname name
  440. @end deffn
  441. @deffn procedure getprotobynumber number
  442. @end deffn
  443. @deffn procedure getprotoent
  444. @end deffn
  445. @deffn procedure setprotoent port
  446. @end deffn
  447. @deffn procedure endprotoent
  448. @end deffn
  449. @deffn procedure getservbyname name protocol
  450. @end deffn
  451. @deffn procedure getservbyport port protocol
  452. @end deffn
  453. @deffn procedure getservent
  454. @end deffn
  455. @deffn procedure setservent port
  456. @end deffn
  457. @deffn procedure endservent
  458. @end deffn
  459. @node Network sockets
  460. @section BSD socket library interface
  461. @deffn procedure socket family style protocol
  462. @end deffn
  463. @deffn procedure socketpair family style protocol
  464. @end deffn
  465. @deffn procedure getsockopt socket level optname
  466. @end deffn
  467. @deffn procedure setsockopt socket level optname value
  468. @end deffn
  469. @deffn procedure shutdown socket how
  470. @end deffn
  471. @deffn procedure connect socket family address arg ...
  472. @end deffn
  473. @deffn procedure bind socket family address arg ...
  474. @end deffn
  475. @deffn procedure listen socket backlog
  476. @end deffn
  477. @deffn procedure accept socket
  478. @end deffn
  479. @deffn procedure getsockname socket
  480. @end deffn
  481. @deffn procedure getpeername socket
  482. @end deffn
  483. @deffn procedure recv! socket buf [flags]
  484. @end deffn
  485. @deffn procedure send socket message [flags]
  486. @end deffn
  487. @deffn procedure recvfrom! socket buf [flags] [start] [end]
  488. @end deffn
  489. @deffn procedure sendto socket message family address args ... [flags]
  490. @end deffn
  491. @node Miscellaneous Unix
  492. @section Miscellaneous Unix interfaces
  493. Things which haven't been classified elsewhere (yet?).
  494. @deffn procedure open path flags [mode]
  495. @defvar O_RDONLY
  496. @end defvar
  497. @defvar O_WRONLY
  498. @end defvar
  499. @defvar O_RDWR
  500. @end defvar
  501. @defvar O_CREAT
  502. @end defvar
  503. @defvar O_EXCL
  504. @end defvar
  505. @defvar O_NOCTTY
  506. @end defvar
  507. @defvar O_TRUNC
  508. @end defvar
  509. @defvar O_APPEND
  510. @end defvar
  511. @defvar O_NONBLOCK
  512. @end defvar
  513. @defvar O_NDELAY
  514. @end defvar
  515. @defvar O_SYNC
  516. @end defvar
  517. @end deffn
  518. @deffn procedure select reads writes excepts secs msecs
  519. @end deffn
  520. @deffn procedure uname
  521. @end deffn
  522. @deffn procedure pipe
  523. @end deffn
  524. @deffn procedure open-pipe command modes
  525. @end deffn
  526. @deffn procedure open-input-pipe command
  527. @end deffn
  528. @deffn procedure open-output-pipe command
  529. @end deffn
  530. @deffn procedure setlocale category [locale]
  531. @defvar LC_COLLATE
  532. @end defvar
  533. @defvar LC_CTYPE
  534. @end defvar
  535. @defvar LC_MONETARY
  536. @end defvar
  537. @defvar LC_NUMERIC
  538. @end defvar
  539. @defvar LC_TIME
  540. @end defvar
  541. @defvar LC_MESSAGES
  542. @end defvar
  543. @defvar LC_ALL
  544. @end defvar
  545. @end deffn
  546. @deffn procedure strftime format stime
  547. @end deffn
  548. @deffn procedure strptime format string
  549. @end deffn
  550. @deffn procedure mknod
  551. @end deffn
  552. @node scsh
  553. @chapter The Scheme shell (scsh)
  554. Guile includes an incomplete port of the Scheme shell (scsh) 0.4.4.
  555. For information about scsh on the Web see
  556. @url{http://www-swiss.ai.mit.edu/scsh/scsh.html}.
  557. The original scsh is available by ftp from
  558. @url{ftp://swiss-ftp.ai.mit.edu:/pub/su}.
  559. This port of scsh does not currently use the Guile module system, but
  560. can be initialized using:
  561. @smalllisp
  562. (load-from-path "scsh/init")
  563. @end smalllisp
  564. Note that SLIB must be installed before scsh can be initialized, see
  565. @ref{SLIB} for details.
  566. @node Threads
  567. @chapter Programming Threads.