This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathposixprelude.bn
68 lines (59 loc) · 2.57 KB
/
posixprelude.bn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
;;;; posixprelude.bn -- Wrappers for POSIX functions -*- bone -*-
;;;; Copyright (C) 2016 Dov Murik
;;;;
;;;; Permission to use, copy, modify, and/or distribute this software for any
;;;; purpose with or without fee is hereby granted, provided that the above
;;;; copyright notice and this permission notice appear in all copies.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;;;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;;;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;;;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;;;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;;;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;;;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
(internsub (_posix-err name)
(err "Error calling POSIX " name ": " (sys.strerror (sys.errno))))
(defsub (gettimeofday)
"Return a list `(seconds microseconds)` of the time since epoch."
(aif (sys.gettimeofday?)
it
(_posix-err "gettimeofday")))
(defsub (timeofday-diff t2 t1)
"Return the number of microseconds between `t1` and `t2`, which are both
two-element lists of the form `(sec usec)` (as returned from `gettimeofday`)."
(let ((t1-sec (car t1))
(t1-usec (cadr t1))
(t2-sec (car t2))
(t2-usec (cadr t2)))
(+ (* 1000000 (- t2-sec t1-sec)) (- t2-usec t1-usec))))
(defsub (str-now)
"Return a str describung the current time."
(with t (sys.time?)
(aif (and t (sys.ctime? t))
(str-dropr 1 it)
(_posix-err "time or ctime"))))
(defsub (wait-for pid)
"Wait for child `pid` to terminate.
Returns a negative number for termination by signals, otherwise the exit status."
(with loop (lambda ()
(with status (cadr? (sys.waitpid? pid 0))
(acond ((not status) (_posix-err "waitpid"))
((sys.exitstatus? status) it)
((sys.termsig? status) (- 0 it))
(#t (loop)))))
(loop)))
(defsub (exec prog . args)
"Replace current program by calling `prog` with `args`."
(or (sys.execvp? prog (cons prog args))
(_posix-err "execvp")))
(defsub (call prog . args)
"Call the program `prog` with `args` and returns its exit status or (negative) termination signal."
(aif (sys.fork?)
(if (0? it)
(apply exec prog args)
(wait-for it))
(_posix-err "fork")))
(defsub (system cmd)
"Start the shell command `cmd` and return its exit status."
(call "/bin/sh" "-c" cmd))