Permalink
Browse files

Get the MAC address on macosx/lispworks (by Camille Troillard). A cle…

…verer way to get the MAC address on linux (by Gordon Quad).
  • Loading branch information...
1 parent 4af78a4 commit b6a715bc99855846e44f9815255715fe403e68f6 @dardoria committed Apr 17, 2012
Showing with 76 additions and 44 deletions.
  1. +5 −0 .gitignore
  2. +71 −44 uuid.lisp
View
@@ -0,0 +1,5 @@
+*~
+*#
+*.fasl
+*.gz
+*.asc
View
115 uuid.lisp
@@ -98,52 +98,79 @@ characters.~@:>" string (length string)))
"Get MAC address of first ethernet device"
(let ((node
#+(and :linux (or :cmu :sbcl :allegro))
- ;; todo this can be simplified a bit
- (let ((proc #+(and :linux :cmu)
- (ext:run-program "/sbin/ifconfig"
- nil
- :pty nil
- :wait t
- :output :stream
- :error t
- :if-error-exists nil)
- #+(and :linux :sbcl)
- (sb-ext:run-program "/sbin/ifconfig"
- nil
- :output :stream
- :error t
- :if-error-exists nil
- :wait nil)
- #+(and :linux :allegro)
- (excl:run-shell-command "/sbin/ifconfig"
+ (or (let ((interface (first (remove "lo"
+ (mapcan (lambda (x) (last (pathname-directory x)))
+ (directory "/sys/class/net/*/"))
+ :test #'equal))))
+ (when (not (null interface))
+ (with-open-file (address (make-pathname :directory
+ (concatenate 'string
+ "sys/class/net/"
+ interface)
+ :name "address"))
+ (parse-integer (remove #\: (read-line address)) :radix 16))))
+
+ (let ((proc #+(and :linux :cmu)
+ (ext:run-program "/sbin/ifconfig"
+ nil
+ :pty nil
+ :wait t
+ :output :stream
+ :error t
+ :if-error-exists nil)
+ #+(and :linux :sbcl)
+ (sb-ext:run-program "/sbin/ifconfig"
+ nil
:output :stream
- :if-error-output-exists t
- :wait nil)))
- (prog1
- (loop for line = (read-line #+(and :linux :cmu)
- (extensions:process-output proc)
- #+(and :linux :sbcl)
- (sb-ext:process-output proc)
- #+(and :linux :allegro)
- proc
- nil)
- while line
- when (search "HWaddr" line :test #'string-equal)
- return (parse-integer (remove #\: (subseq line 38))
- :radix 16))
- #+(and :linux :allegro)
- (sys:reap-os-subprocess)))
+ :error t
+ :if-error-exists nil
+ :wait nil)
+ #+(and :linux :allegro)
+ (excl:run-shell-command "/sbin/ifconfig"
+ :output :stream
+ :if-error-output-exists t
+ :wait nil)
+ #+(and :macosx :lispworks)
+ (sys:run-shell-command "/sbin/ifconfig en0"
+ :output :stream
+ :if-error-output-exists t
+ :wait nil)))
+ (prog1
+ (loop for line = (read-line #+(and :linux :cmu)
+ (extensions:process-output proc)
+ #+(and :linux :sbcl)
+ (sb-ext:process-output proc)
+ #+(and :linux :allegro)
+ proc
+ nil)
+ while line
+ when (search "HWaddr" line :test #'string-equal)
+ return (parse-integer (remove #\: (subseq line 38))
+ :radix 16))
+ #+(and :linux :allegro)
+ (sys:reap-os-subprocess)))
+
+ #+(and :windows :clisp)
+ (let ((output (ext:run-program "ipconfig"
+ :arguments (list "/all")
+ :input nil
+ :output :stream
+ :wait t)))
+ (loop for line = (read-line output nil) while line
+ when (search "Physical" line :test #'string-equal)
+ return (parse-integer (remove #\- (subseq line 37)) :radix 16)))
- #+(and :windows :clisp)
- (let ((output (ext:run-program "ipconfig"
- :arguments (list "/all")
- :input nil
- :output :stream
- :wait t)))
- (loop for line = (read-line output nil) while line
- when (search "Physical" line :test #'string-equal)
- return (parse-integer (remove #\- (subseq line 37)) :radix 16)))
- ))
+ #+(and :macosx :lispworks)
+ (with-open-stream (stream
+ (sys:run-shell-command "/sbin/ifconfig en0 ether"
+ :output :stream
+ :if-error-output-exists t
+ :wait nil))
+ (loop for line = (read-line stream nil)
+ while line
+ when (search "ether" line :test #'string-equal)
+ return (parse-integer (remove #\: (subseq line 7))
+ :radix 16))))))
(when (not node)
(setf node (dpb #b01 (byte 8 0) (random #xffffffffffff *uuid-random-state*))))
node))

0 comments on commit b6a715b

Please sign in to comment.