12

To get my .sbclrc file working on the two computers I use, I'd like a way to get the hostname and/or operating system from within sbcl. I know I could set and then look for an environment variable, but is there a more direct approach?

Update

I changed the question to refer to common lisp, since the answer from Ken is not specific to sbcl.

Dan Becker
  • 1,196
  • 10
  • 18

2 Answers2

20

I'd use the 'environment' functions:

* (machine-instance)
"myhostname"
* (machine-type)
"X86-64"
* (machine-version)
"Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz"
* (software-type)
"Linux"
* (software-version)
"2.6.32-3-amd64"
Ken
  • 256
  • 1
  • 3
  • Thank you, I don't know how I missed those! – Dan Becker Dec 07 '10 at 03:54
  • Albeit those functions are portable keep in mind that in other implementations they return totally different stuff. I've tried them with CLisp and I was surprised by most return values. – Bozhidar Batsov Dec 08 '10 at 07:30
  • 1
    Yes, if you want to be portable across both compilers *and* architectures, you'll need a bit more work here. `*features*` is a little more consistent between the two, but still differs, e.g., both SBCL and CLISP define `:UNIX` but only SBCL defines `:LINUX`. When you need it for something more than a personal config file, the trivial-features library is very handy: http://www.cliki.net/trivial-features – Ken Dec 08 '10 at 18:40
2
* (require :sb-bsd-sockets)
("SB-BSD-SOCKETS" "SB-GROVEL" "ASDF")

* (use-package :sb-bsd-sockets)
T

* (host-ent-name (get-host-by-name "localhost"))
"myhost.mydomain.ext"

* (find :win32 *features*)
:WIN32

* (find :linux *features*)
NIL

EDIT: I like @Ken's solution better. +1.

jtdubs
  • 13,585
  • 1
  • 17
  • 12