Next: , Previous: Pathnames, Up: Operating-System Interface


15.2 Working Directory

When MIT/GNU Scheme is started, the current working directory (or simply, working directory) is initialized in an operating-system dependent manner; usually, it is the directory in which Scheme was invoked. The working directory can be determined from within Scheme by calling the pwd procedure, and changed by calling the cd procedure. Each REP loop has its own working directory, and inferior REP loops initialize their working directory from the value in effect in their superior at the time they are created.

— procedure: working-directory-pathname
— procedure: pwd

Returns the current working directory as a pathname that has no name, type, or version components, just host, device, and directory components. pwd is an alias for working-directory-pathname; the long name is intended for programs and the short name for interactive use.

— procedure: set-working-directory-pathname! filename
— procedure: cd filename

Makes filename the current working directory and returns the new current working directory as a pathname. Filename is coerced to a pathname using pathname-as-directory. cd is an alias for set-working-directory-pathname!; the long name is intended for programs and the short name for interactive use.

Additionally, set-working-directory-pathname! modifies the value of
*default-pathname-defaults* by merging the new working directory into it.

When this procedure is executed in the top-level REP loop, it changes the working directory of the running Scheme executable.

          (set-working-directory-pathname! "/usr/morris/blisp")
               =>  #[pathname "/usr/morris/blisp/"]
          (set-working-directory-pathname! "~")
               =>  #[pathname "/usr/morris/"]
     

This procedure signals an error if filename does not refer to an existing directory.

If filename describes a relative rather than absolute pathname, this procedure interprets it as relative to the current working directory, before changing the working directory.

          (working-directory-pathname)
               =>  #[pathname "/usr/morris/"]
          (set-working-directory-pathname! "foo")
               =>  #[pathname "/usr/morris/foo/"]
     
— procedure: with-working-directory-pathname filename thunk

This procedure temporarily rebinds the current working directory to filename, invokes thunk (a procedure of no arguments), then restores the previous working directory and returns the value yielded by thunk. Filename is coerced to a pathname using pathname-as-directory. In addition to binding the working directory, with-working-directory-pathname also binds the variable *default-pathname-defaults*, merging the old value of that variable with the new working directory pathname. Both bindings are performed in exactly the same way as dynamic binding of a variable (see Dynamic Binding).