Skip to content

Instantly share code, notes, and snippets.

@dhess
Created February 10, 2009 09:11
Show Gist options
  • Select an option

  • Save dhess/61318 to your computer and use it in GitHub Desktop.

Select an option

Save dhess/61318 to your computer and use it in GitHub Desktop.
A Chicken Scheme macro for temporarily changing the current working directory
;;; Preserve the current working directory, then change to a new directory
;;; DIR and evaluate THUNK. Finally, restore the working directory and
;;; return THUNK's value.
;;;
;;; Uses dynamic-wind, so it's signal- and continuation-safe.
;;;
;;; Works with Chicken Scheme.
(use posix)
(define (with-current-directory dirname thunk)
(let ((cwd (current-directory)))
(dynamic-wind
(lambda () (change-directory dirname))
thunk
(lambda () (change-directory cwd)))))
;;; Example: should print "/" and return 5.
(with-current-directory "/"
(lambda ()
(print (current-directory))
3
4
5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment