Parse /proc

This commit is contained in:
Lassi Kortela 2021-09-20 13:09:39 +03:00
parent b29504c23e
commit f4842f6220
1 changed files with 15 additions and 3 deletions

View File

@ -30,6 +30,18 @@
(proc entry) (proc entry)
(loop))))) (loop)))))
(define (parse-null-terminated-strings bytes)
(let loop ((a 0) (b 0) (strings '()))
(if (= b (bytevector-length bytes))
(if (= a b)
(reverse strings)
(error "Missing final null terminator"))
(if (zero? (bytevector-u8-ref bytes b))
(loop (+ b 1) (+ b 1) (cons (utf8->string
(bytevector-copy bytes a b))
strings))
(loop a (+ b 1) strings)))))
(define (handle-the-scheme-implementation impl) (define (handle-the-scheme-implementation impl)
(let ((cmdline #f) (let ((cmdline #f)
(environ #f) (environ #f)
@ -46,11 +58,11 @@
(equal? "proc/self/fd/" name)) (equal? "proc/self/fd/" name))
#f) #f)
((equal? "proc/self/cmdline" name) ((equal? "proc/self/cmdline" name)
(set! cmdline bytes)) (set! cmdline (parse-null-terminated-strings bytes)))
((equal? "proc/self/environ" name) ((equal? "proc/self/environ" name)
(set! stdin bytes)) (set! stdin (parse-null-terminated-strings bytes)))
((equal? "proc/self/cwd" name) ((equal? "proc/self/cwd" name)
(set! stdin bytes)) (set! stdin (utf8->string bytes)))
((equal? "proc/self/fd/0" name) ((equal? "proc/self/fd/0" name)
(set! stdin bytes)) (set! stdin bytes))
((string-prefix? "proc/self/fd/" name) ((string-prefix? "proc/self/fd/" name)