Go to the first, previous, next, last section, table of contents.

Goonix reference

Goonix preliminaries

Goonix is a posix-compliant (John S. Quarterman and Susanne Willhelm: "UNIX, POSIX, and Open Systems", Addison-Wesley Publishing Co, Inc. 1993) library for systems programming in scheme. It allows the scheme programmer to use the same calls available to C programmers in a posix environment.

This chapter is written assuming that you are running the guile program directly. If you are writing a C program instead, and linking with libguile, you can follow the instructions in section Libguile reference to initialize goonix correctly, and then follow this section when you write scheme code.

Unlike the Tk package, no special scheme code is required to initialize goonix: all goonix procedures are primitive, and available as long as goonix has been initialized with the correct libguile calls.

When documenting the procedures in this chapter, I use the convention [arg] to indicate that the argument is optional, and [args ...] to indicate that the optional argument can be repeated.

You will notice that most of these procedures closely mirror the equivalent UNIX system or library calls. If you want to know more detail about how these procedures behave, you can read the relevant UNIX man page.

You will also notice that many of these procedures begin with a % character. There is a convention in goonix that puts a % in front of all procedures that might invoke the scheme (error ...) mechanism, thus escaping from the current continuation.

I/O extensions to standard scheme

These I/O Extensions to scheme provide many of the UNIX file system calls, thus allowing sophisticated file manipulation and directory traversal. This is mostly done using UNIX's stdio layer, rather than the raw open, read, write layer.

Standard I/O file pointers are directly translated to scheme ports. Ports are described in great detail in all the scheme references (see section `Where to find more Guile/scheme resources' in Guile User Manual), so I will simply remind you that an input port is a scheme object which can deliver characters, and an output port is a scheme object which can accept characters. It is OK to think of ports as file pointers, even though there are some subtelties in ports related to scheme's unique control structure.

In the following read/write routines, if port is omitted, standard input (or standard output) is assumed.

I/O Extensions: read-line [port]
Reads a line of text from the given input port, and returns the string. If port is not specified, standard input is assumed.

I/O Extensions: read-line! str [port]
A destructive version of the above: puts the string in str; return value is unspecified.

I/O Extensions: write-line obj [port]
Writes a representation of obj to port. If port is not specified, it is assumed to be standard output.

I/O Extensions: %ftell port
Returns the current seek pointer of the given port.

I/O Extensions: %fseek port offset whence
Sets the seek pointer for port to the position offset. If whence is 0, that offset is with respect to the beginning of the file. If whence is 1, the offset is with respect to the current position. If whence is 2, the offset is with respect to the end of the file.

I/O Extensions: %freopen filename modes port
Opens filename with the specified modes, and associates port with that file.

I/O Extensions: %duplicate-port old-port modes
Returns a new port having the same open file as old-port.

I/O Extensions: %redirect-port into-port from-port
Redirects I/O from from-port to into-port: from here on, into-port will refer to the same file as from-port. If into-port already refers to an open file, it will be closed.

I/O Extensions: %opendir dirname
Opens the directory dirname, returning a directory port which can be used with %readdir and closedir.

I/O Extensions: %readdir dirp
Gets the next file name from the directory port dirp.

I/O Extensions: rewinddir dirp
Repositions the directory port dirp to the beginning of the stream.

I/O Extensions: %closedir dirp
Closes the directory port dirp.

I/O Extensions: %mkdir dirname [mode]
Makes a new directory with name dirname. The directory permissions are given by the optional argument mode, or by the current umask if mode is not specified.

I/O Extensions: %rmdir dirname
Removes the directory with path dirname.

I/O Extensions: %chdir path
Changes the current working directory to path.

I/O Extensions: %getcwd
Returns a string with the current working directory.

I/O Extensions: %chmod filename mode
Sets the permission mode on filename to mode.

I/O Extensions: %utime path [actime [modtime]]
Sets the access and modification times of the file at path. The time values actime and modtime are given in standard UNIX time: the number of seconds since 00:00:00 GMT, Jan. 1 1970. If actime and modtime are not given, the current time is used.

I/O Extensions: umask [mask-value]
Sets the user's umask to be the specified value mask-value. The new setting is returned. If no mask-value is given, umask will return the current setting.

Notice that the UNIX umask command interprets the mask-value to be an octal integer, whereas the goonix umask call does not make any such conversion, so you have to explicitly tell scheme to use an octal radix, or think about permissions in decimal!

I/O Extensions: %rename old-fname new-fname
Renames the file from old-fname to new-fname. This is analogous to the UNIX mv command and the rename system call.

I/O Extensions: %fileno port
Returns the integer file descriptor associated with port. In case of error, #f is returned.

I/O Extensions: %isatty port
Returns #t if the port is associated with a terminal device, and #f otherwise.

I/O Extensions: %fdopen fd type
Returns an I/O port associated with the numeric file descriptor fd (which is assumed to be already open). The type argument is a standard I/O character string specifying whether the file should be opened for reading, writing, or appending:
          r         open for reading

          w         truncate or create for writing

          a         append: open for writing at end of  file,  or
                    create for writing

          r+        open for update (reading and writing)

          w+        truncate or create for update

          a+        append; open or create for update at EOF

I/O Extensions: %primitive-move->fdes port fd
Moves the file descriptor that underlies port to the given value fd.

Returns #f for error, 0 if the file descriptor is already equal to fd, and 1 if the file descriptor is successfully moved.

I/O Extensions: %access path how
Determines the accessibility of the file in path. The how variable can take on any of the following values:
          R_OK           test for read permission

          W_OK           test for write permission

          X_OK           test for execute or search permission

     The following value may also be supplied for mode:

          F_OK           test whether the directories leading  to
                         the  file  can  be searched and the file
                         exists.

I/O Extensions: %stat filename
Gets typical UNIX stat information on filename, and returns that information in a scheme vector, where the UNIX stat structure values are stored in the following order: #(st_dev st_ino st_mode st_nlink st_uid st_gid st_rdev st_size st_atime st_mtime st_ctime st_blksize st_blocks)

I/O Extensions: getpid
Returns the current process ID.

I/O Extensions: %putenv str
Takes a string str of the form "VARIABLE=value" and adds that to the user's environment.

Posix system and library calls

POSIX Calls: %chown path owner group
Changes the ownership of path to the specified owner and group.

POSIX Calls: %link oldpath newpath
Adds newpath as a hard link to oldpath.

POSIX Calls: %pipe
Creates a pipe, and returns a scheme pair with the read port in its car and the write port in its cdr. If the pipe creation fails, %pipe returns #f.

POSIX Calls: open-pipe pipe-str mode
Opens a pipe to a new process by running pipe-str, and returns a port which can be used to write to the process (if mode is "w") or read from it (if mode is "r").

POSIX Calls: open-input-pipe pipe-str
A shortcut for running (open-pipe pipe-str "r").

POSIX Calls: open-output-pipe pipe-str
A shortcut for running (open-pipe pipe-str "w").

POSIX Calls: %getgroups
Returns a vector with all the supplementary group IDs of the current user process.

POSIX Calls: %getpwuid [name]
Given the login-name or uid in name, %getpwuid returns a vector with the following data: #(login-name password uid gid GEGOS home-dir shell).

If name is not specified, the first entry in the password file is returned (or the next entry, upon successive invocations).

POSIX Calls: setpwent [arg]
If called with an argument, setpwent rewinds the password file so the next call to %getpwuid will start from the beginning.

Without arguments, setpwent will close the password file; it can be used when processing is complete.

POSIX Calls: %getgrgid [which-group]
If the argument which-group (either the group name or the gid) is given, %getgrgid returns the group file entry for that group. If which-group is not given, the first entry in the group file is returned (or the next entry, upon successive invocations).

POSIX Calls: setgrent [arg]
This is analogous to setpwent, but for the group file.

If called with an argument, setgrent rewinds the group file so the next call to %getgrgid will start from the beginning.

Without arguments, setgrent will close the group file; it can be used when processing is complete.

POSIX Calls: %kill pid sig
Sends a signal sig to the process pid. The possible values for sig are the usual UNIX signal types. Notice the order of arguments: this is the order of the UNIX kill system call, not the order of the kill command usually typed at the shell.

POSIX Calls: %waitpid pid [options]
Waits for some or all child processes to exit or return a status.

If pid is -1, %waitpid will wait on any of its children. This is equivalent to the traditional UNIX wait system call.

If pid is greater than 0, %waitpid will wait on that particular child process.

If pid is equal to 0, %waitpid will wait on any children in its own process group.

If pid is less than -1, %waitpid will wait on any children in the process group of the particular child abs(pid).

The options argument is a bitwise OR of any of the flags:

WNOHANG Tells %waitpid to not suspend if status is not immediately available for one of the relevant child processes.
WUNTRACED Also reports status of children that are stopped and have not yet been reported.

The value returned by %waitpid is a scheme pair of the child pid and the status returned for that child.

POSIX Calls: getppid
Returns the process id of the current process's parent.

POSIX Calls: getuid
POSIX Calls: geteuid
POSIX Calls: getgid
POSIX Calls: getegid
Returns the real or effective user or group id of the current process.

POSIX Calls: %setuid id
POSIX Calls: %seteuid id
POSIX Calls: %setgid id
POSIX Calls: %setegid id
Sets the real or effective user or group id of the current process.

POSIX Calls: ttyname port
Returns a string with the path of the terminal device associated with the port, or #f if the device is not a terminal. Notice how this also serves the function of the UNIX isatty() call.

POSIX Calls: %execl command-str [exec-args ...]
POSIX Calls: %execlp command-str [exec-args ...]
Similar to execl, except that if filename does not contain a slash it searches for the file in the directories listed in the PATH environment variable.

%execlp is similar, but returns #f if an error occurs.

POSIX Calls: %fork
This is a raw interface to the UNIX fork() system call. It creates a new process with the same core image as the current process. %fork returns #f upon failure; otherwise the child gets a return value of 0, and the parent gets the child's pid.

See the UNIX fork man page for some of the subtelties of %fork (open file descriptors, directory streams, semaphores, tms_ values, file locks, ...).

POSIX Calls: %select reads writes excepts secs msecs
The %select call is used to examine a set of file descriptors (passed as scheme lists of non-negative integers in readfds, writefds and exceptfds).

The purpose of this examination is to determine if the descriptors are ready for the requested operation (reading or writing or treatment of exception). The return value is a list of 3 lists; the three lists are the original lists stripped down to only the ready file descriptors.

A timeout can be specified (in seconds plus miliseconds) with the secs and msecs are used.

POSIX Calls: %uname
This is an interface to the UNIX uname() system call. Upon successful completion it returns a list of 5 strings describing the system name and OS version: (list OSname nodename OSrelease OSversion MachineType). Upon failure it returns #f.

POSIX Calls: environ [env-list]
With no argument, returns the current environment as a scheme list of "VAR=val" strings. It can be called with a list of "VAR=val" strings, and then the environment will be set to those values.

UNIX system and library calls

UNIX Calls: %mknod path mode dev
Creates a new file named by the string path. The file permissions are specified in mode, and some special file type bits can also be OR-ed in (see the mknod() man page for details on the file type bits).

The dev argument is ignored unless mode is a block or character special file, in which case dev is a configuration-dependent specification of a character or block I/O device.

UNIX Calls: %acct path
Turns on or off process accounting. The accounting information will be reported in the file named by the string path. If path is #f, accounting will be turned off.

Must be super user to use this call.

UNIX Calls: %nice increment
Changes the nice value of a UNIX process by increment (remember: a large nice value means less priority). Only the super user can set a negative increment.

The value of increment should be between -20 and 20; if it is outside these bounds it will be silently adjusted to the extreme value.

UNIX Calls: sync
Schedules a write to disk of all information in core memory that should be on disk, such as super blocks, inodes and buffered disk I/O.

UNIX Calls: %symlink oldpath newpath
Makes newpath be a symbolic link to oldpath.

UNIX Calls: %readlink path
Returns a string with the contents of the symlink at path. The contents is the path to the real file. Returs #f on any error condition.

UNIX Calls: %lstat path
Like %stat except that if path is a symlink, %lstat will return information about the symlink, whereas %stat will look at the referenced file. The rationale behind this is that symlinks should be transparent to the traditional UNIX file system calls, but extra calls (like %lstat and %readlink) are provided to get more information.

Go to the first, previous, next, last section, table of contents.