-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos.lisp
60 lines (53 loc) · 1.97 KB
/
os.lisp
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
;;; Copyright 2020 Google LLC
;;;
;;; Use of this source code is governed by an MIT-style
;;; license that can be found in the LICENSE file or at
;;; https://opensource.org/licenses/MIT.
;; Package provides rudimentary OS related functionality.
(defpackage :ace.core.os
(:use :cl :ace.core.defun)
(:export
#:getenv
#:cwd
#:program-name))
(in-package :ace.core.os)
(defun* getenv (variable &optional default)
"Return the value of the execution environment VARIABLE and if not found return DEFAULT."
(declare (self (string &optional (or null string)) (or null string)))
(or #+sbcl (sb-posix:getenv variable)
#+cmu (cdr (assoc variable ext:*environment-list* :test #'string=))
#+allegro (sys:getenv variable)
#+clisp (ext:getenv variable)
#+ecl (si:getenv variable)
#+lispworks (lispworks:environment-variable variable)
#+(and uiop (not (or sbcl cmu allegro clisp ecl lispworks)))
(uiop/os:getenv variable)
default))
(defun* (setf getenv) (value variable)
"Set the value of the execution environment VARIABLE.
If VALUE is NIL, the VARIABLE is unset from the environment."
(declare (self ((or null string) string) (or null string)))
#+sbcl
(if value
(sb-posix:setenv variable value 1)
(sb-posix:unsetenv variable))
value)
(defun* cwd ()
"Return the current working directory as string."
(declare (self () string))
(let* ((cwd (or #+sbcl (pathname (sb-posix:getcwd))
#-sbcl *default-pathname-defaults*))
(file (file-namestring cwd)))
(namestring
(if (plusp (length file))
;; Coerce to directory.
(make-pathname :directory
(append (pathname-directory cwd) (list file))
:name nil :type nil :version nil :defaults cwd)
cwd))))
(defun* program-name ()
"Return the program name for the current process."
(declare (self () string))
(or
#+sbcl (first sb-unix::*posix-argv*)
""))