2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(library (ikarus posix)
|
|
|
|
(export posix-fork fork waitpid system file-exists? delete-file
|
2008-04-11 05:36:54 -04:00
|
|
|
nanosleep getenv env environ file-ctime)
|
2007-05-05 22:14:06 -04:00
|
|
|
(import
|
2007-10-12 00:33:19 -04:00
|
|
|
(rnrs bytevectors)
|
2007-05-05 22:14:06 -04:00
|
|
|
(except (ikarus)
|
2008-04-11 05:36:54 -04:00
|
|
|
nanosleep
|
2007-05-05 22:14:06 -04:00
|
|
|
posix-fork fork waitpid system file-exists? delete-file
|
2008-02-18 21:58:11 -05:00
|
|
|
getenv env environ file-ctime))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(define posix-fork
|
|
|
|
(lambda ()
|
|
|
|
(foreign-call "ikrt_fork")))
|
|
|
|
|
|
|
|
(define fork
|
|
|
|
(lambda (parent-proc child-proc)
|
|
|
|
(let ([pid (posix-fork)])
|
|
|
|
(cond
|
|
|
|
[(fx= pid 0) (child-proc)]
|
|
|
|
[(fx= pid -1)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fork "failed")]
|
2007-05-05 22:14:06 -04:00
|
|
|
[else (parent-proc pid)]))))
|
|
|
|
|
|
|
|
(define waitpid
|
|
|
|
(lambda (pid)
|
|
|
|
(unless (fixnum? pid)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'waitpid "not a fixnum" pid))
|
2007-05-05 22:14:06 -04:00
|
|
|
(foreign-call "ikrt_waitpid" pid)))
|
|
|
|
|
|
|
|
(define system
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'system "not a string" x))
|
2007-05-18 22:15:52 -04:00
|
|
|
(let ([rv (foreign-call "ik_system"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 x))])
|
2007-05-05 22:14:06 -04:00
|
|
|
(if (fx= rv -1)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'system "failed")
|
2007-05-05 22:14:06 -04:00
|
|
|
rv))))
|
|
|
|
|
|
|
|
(define file-exists?
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'file-exists? "filename is not a string" x))
|
2007-05-18 22:18:51 -04:00
|
|
|
(let ([v (foreign-call "ikrt_file_exists"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 x))])
|
2007-05-05 22:14:06 -04:00
|
|
|
(cond
|
|
|
|
[(boolean? v) v]
|
|
|
|
[else
|
2008-02-03 17:11:53 -05:00
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-who-condition 'file-exists?)
|
|
|
|
(make-message-condition
|
|
|
|
(case v
|
|
|
|
[(1) "file path contains a non-directory"]
|
|
|
|
[(2) "file path is too long"]
|
|
|
|
[(3) "file path is not accessible"]
|
|
|
|
[(4) "file path contains too many symbolic links"]
|
|
|
|
[(5) "internal access error while accessing file"]
|
|
|
|
[(6) "IO error encountered while accessing file"]
|
|
|
|
[else "Unknown error while testing file"]))
|
|
|
|
(make-i/o-filename-error x)))]))))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
|
|
|
(define delete-file
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'delete-file "filename is not a string" x))
|
2007-05-18 22:21:36 -04:00
|
|
|
(let ([v (foreign-call "ikrt_delete_file"
|
2007-10-12 00:33:19 -04:00
|
|
|
(string->utf8 x))])
|
2007-05-05 22:14:06 -04:00
|
|
|
(case v
|
|
|
|
[(0) (void)]
|
|
|
|
[else
|
2008-02-03 17:11:53 -05:00
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-who-condition 'delete-file)
|
|
|
|
(make-message-condition
|
|
|
|
(case v
|
|
|
|
[(1) "file path contains a non-directory"]
|
|
|
|
[(2) "file path is too long"]
|
|
|
|
[(3) "file does not exist"]
|
|
|
|
[(4) "file path is not accessible"]
|
|
|
|
[(5) "file path contains too many symbolic links"]
|
|
|
|
[(6) "you do not have permissions to delete file"]
|
|
|
|
[(7) "device is busy"]
|
|
|
|
[(8) "IO error encountered while deleting"]
|
|
|
|
[(9) "file is in a read-only file system"]
|
|
|
|
[(10) "internal access error while deleting"]
|
|
|
|
[else "Unknown error while deleting file"]))
|
|
|
|
(make-i/o-filename-error x)))]))))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2008-02-18 21:58:11 -05:00
|
|
|
(define (file-ctime x)
|
|
|
|
(define who 'file-ctime)
|
|
|
|
(unless (string? x)
|
|
|
|
(die who "not a string" x))
|
|
|
|
(let ([p (cons #f #f)])
|
|
|
|
(let ([v (foreign-call "ikrt_file_ctime" (string->utf8 x) p)])
|
|
|
|
(case v
|
|
|
|
[(0) (+ (* (car p) #e1e9) (cdr p))]
|
|
|
|
[else
|
|
|
|
(raise
|
|
|
|
(condition
|
|
|
|
(make-who-condition who)
|
|
|
|
(make-message-condition "cannot stat a file")
|
|
|
|
(make-i/o-filename-error x)))]))))
|
|
|
|
|
|
|
|
|
2007-11-19 12:57:50 -05:00
|
|
|
(define ($getenv-bv key)
|
|
|
|
(foreign-call "ikrt_getenv" key))
|
|
|
|
(define ($getenv-str key)
|
|
|
|
(utf8->string ($getenv-bv (string->utf8 key))))
|
|
|
|
|
|
|
|
(define (getenv key)
|
|
|
|
(if (string? key)
|
|
|
|
($getenv-str key)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'getenv "the key is not a string" key)))
|
2007-11-19 12:57:50 -05:00
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
(define env
|
|
|
|
(let ()
|
|
|
|
(define env
|
|
|
|
(case-lambda
|
|
|
|
[(key)
|
|
|
|
(if (string? key)
|
|
|
|
(foreign-call "ikrt_getenv" key)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'env "the key is not a string" key))]
|
2007-05-05 22:14:06 -04:00
|
|
|
[(key val) (env key val #t)]
|
|
|
|
[(key val overwrite?)
|
|
|
|
(if (string? key)
|
|
|
|
(if (string? val)
|
|
|
|
(unless (foreign-call "ikrt_setenv" key val overwrite?)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'env "failed" key val))
|
|
|
|
(die 'env "the value is not a string" val))
|
|
|
|
(die 'env "the key is not a string" key))]))
|
|
|
|
(define busted (lambda args (die 'env "BUG: busted!")))
|
2007-05-18 22:26:22 -04:00
|
|
|
busted))
|
2007-05-05 22:14:06 -04:00
|
|
|
|
2007-05-18 22:26:22 -04:00
|
|
|
|
2007-12-15 08:22:49 -05:00
|
|
|
(define environ (lambda args (die 'environ "busted!")))
|
2007-05-18 22:26:22 -04:00
|
|
|
(define environ^
|
2007-05-05 22:14:06 -04:00
|
|
|
(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)
|
|
|
|
(if (fx< (fxadd1 i) n)
|
|
|
|
(substring s (fxadd1 i) n)
|
|
|
|
"")))))
|
|
|
|
(foreign-call "ikrt_environ"))))
|
2008-04-11 05:36:54 -04:00
|
|
|
|
|
|
|
(define (nanosleep secs nsecs)
|
|
|
|
(import (ikarus system $fx))
|
|
|
|
(unless (cond
|
|
|
|
[(fixnum? secs) ($fx>= secs 0)]
|
|
|
|
[(bignum? secs) (<= 0 secs (- (expt 2 32) 1))]
|
|
|
|
[else (die 'nanosleep "not an exact integer" secs)])
|
|
|
|
(die 'nanosleep "seconds must be a nonnegative integer <=" secs))
|
|
|
|
(unless (cond
|
|
|
|
[(fixnum? nsecs) ($fx>= nsecs 0)]
|
|
|
|
[(bignum? nsecs) (<= 0 nsecs 999999999)]
|
|
|
|
[else (die 'nanosleep "not an exact integer" nsecs)])
|
|
|
|
(die 'nanosleep "nanoseconds must be an integer \
|
|
|
|
in the range 0..999999999" nsecs))
|
|
|
|
(let ([rv (foreign-call "ikrt_nanosleep" secs nsecs)])
|
|
|
|
(unless (eq? rv 0)
|
|
|
|
(error 'nanosleep "failed"))))
|
|
|
|
|
2007-05-05 22:14:06 -04:00
|
|
|
)
|