* Added the primitive (environ):

returns an alist of (string x string) as obtained from the
    system environ.
* Added the primitive env:
  (env "key") retrieves the env value associated with key
  (env "key" "val") sets the env value assocuated with key to val
  (env "key" "val" overwrite?) same as above but it does not
  overwrite an existing value of overwrite? is #f.
This commit is contained in:
Abdulaziz Ghuloum 2007-01-13 00:42:37 -05:00
parent 6359ca9ee4
commit 212224b4cf
6 changed files with 85 additions and 5 deletions

Binary file not shown.

View File

@ -18,6 +18,9 @@
int total_allocated_pages = 0;
extern char **environ;
#define segment_size (pagesize*pagesize/wordsize)
#define segment_shift (pageshift+pageshift-wordshift)
#define segment_index(x) (((unsigned int)(x)) >> segment_shift)
@ -918,3 +921,51 @@ ikrt_waitpid(ikp pid){
pid_t t = waitpid(unfix(pid), &status, 0);
return fix(status);
}
ikp
ikrt_getenv(ikp str, ikpcb* pcb){
char* v = getenv(string_data(str));
if(v){
int n = strlen(v);
ikp s = ik_alloc(pcb, align(n+disp_string_data+1)) + string_tag;
ref(s, -string_tag) = fix(n);
memcpy(s+off_string_data, v, n+1);
return s;
}
else {
ikp s = ik_alloc(pcb, align(disp_string_data+1)) + string_tag;
ref(s, -string_tag) = fix(0);
ref(s, off_string_data) = 0;
return s;
}
}
ikp
ikrt_setenv(ikp key, ikp val, ikp overwrite){
int err = setenv(string_data(key), string_data(val),
overwrite!=false_object);
if(err){
return false_object;
} else {
return true_object;
}
}
ikp
ikrt_environ(ikpcb* pcb){
char** es = environ;
int i; char* e;
ikp ac = null_object;
for(i=0; (e=es[i]); i++){
int n = strlen(e);
ikp s = ik_alloc(pcb, align(n+disp_string_data+1)) + string_tag;
ref(s, -string_tag) = fix(n);
memcpy(s+off_string_data, e, n+1);
ikp p = ik_alloc(pcb, pair_size) + pair_tag;
ref(p, off_cdr) = ac;
ref(p, off_car) = s;
ac = p;
}
return ac;
}

Binary file not shown.

View File

@ -69,6 +69,35 @@
[else "Unknown error while deleting ~s"])
x)]))))
(primitive-set! 'env
(let ()
(define env
(case-lambda
[(key)
(if (string? key)
(foreign-call "ikrt_getenv" key)
(error 'env "the key: ~s is not a string" key))]
[(key val) (env key val #t)]
[(key val overwrite?)
(if (string? key)
(if (string? val)
(unless (foreign-call "ikrt_setenv" key val overwrite?)
(error 'env "failed to set ~s to ~s" key val))
(error 'env "the value: ~s is not a string" val))
(error 'env "the key: ~s is not a string" key))]))
env))
(primitive-set! 'environ
(lambda ()
(map
(lambda (s)
(define (loc= s i n)
(cond
[(fx= i n) i]
[(char=? (string-ref s i) #\=) i]
[else (loc= s (fx+ i 1) n)]))
(let ([n (string-length s)])
(let ([i (loc= s 0 n)])
(cons (substring s 0 i)
(substring s (fxadd1 i) n)))))
(foreign-call "ikrt_environ"))))

View File

@ -86,7 +86,7 @@
make-guardian weak-cons collect
interrupt-handler
time-it
posix-fork fork waitpid
posix-fork fork waitpid env environ
))
(define system-primitives
@ -266,7 +266,7 @@
(when (eq? who (caddr x))
(compile-library (car x) (cadr x))))
scheme-library-files))
; (define (time x) x)
(define (time x) x)
(fork
(lambda (pid)
(time (compile-all 'p1))