Debian Codegraph @

%3a98/os-environment-variables.mzscheme.sls

raw
1 #!r6rs
2 ;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
3 ;; LICENSE from the original collection this file is distributed with.
4
5 ;; Inspired by Danny Yoo's get-environment PLaneT package.
6
7 (library (srfi :98 os-environment-variables)
8 (export
9 (rename (getenv get-environment-variable))
10 get-environment-variables)
11 (import
12 (rnrs base)
13 (only (scheme base) getenv)
14 (scheme foreign))
15
16 (unsafe!)
17
18 (define environ (get-ffi-obj "environ" (ffi-lib #F) _pointer))
19
20 (define (get-environment-variables)
21 (let loop ((i 0) (accum '()))
22 (let ((next (ptr-ref environ _string/locale i)))
23 (if next
24 (loop (+ 1 i)
25 (cons (let loop ((i 0) (len (string-length next)))
26 (if (< i len)
27 (if (char=? #\= (string-ref next i))
28 (cons (substring next 0 i)
29 (substring next (+ 1 i) len))
30 (loop (+ 1 i) len))
31 (cons next #F)))
32 accum))
33 accum))))
34 )