mac-unix.scm
(module mac-unix mzscheme
        (require (planet "sutil.scm" ("oesterholt" "ho-utils.plt" 1 0)))
        (require (lib "process.ss"))
        (require (lib "pregexp.ss"))
        (provide get-mac-addresses-unix
                 re-mac)
        
        (define (unix-ifconfig)
          (letrec ((f (lambda (L)
                        (if (null? L)
                            (build-path "ifconfig")
                            (let ((path (build-path (car L) "ifconfig")))
                              (if (file-exists? path)
                                  path
                                  (f (cdr L))))))))
            (f '("/sbin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin" "/usr/local/sbin"))))
        
        (define (re-mac)
          (let ((part "[0-9a-fA-F][0-9a-fA-F]")
                (dash "[-:]"))
            (string-append "(" part dash part dash part dash part dash part dash part ")")))
        
        (define (get-addresses R)
          (let ((re (pregexp (re-mac))))
            (letrec ((f (lambda (R)
                          (let ((match (pregexp-match-positions re R)))
                            (if (eq? match #f)
                                (list)
                                (let ((pos (cadr match)))
                                  (cons (substring R (car pos) (cdr pos))
                                        (f (substr R (cdr pos))))))))))
              (f R))))
        
        (define (get-mac-addresses-unix)
          (let ((command (format "~a -a" (path->string (unix-ifconfig)))))
            (apply (lambda (in out pid err-in control)
                     (letrec ((result (lambda (in)
                                        (let ((S (read-string 1024 in)))
                                          (if (eof-object? S)
                                              ""
                                              (string-append S (result in)))))))
                       (let ((R (string-append (result in) (result err-in))))
                         (close-input-port  in)
                         (close-output-port out)
                         (close-input-port  err-in)
                         (control 'wait)
                         (let ((exitcode (control 'exit-code)))
                           (if (= 0 exitcode)
                               (get-addresses R)
                               (error (format "get-mac-addresses-unix: Cannot get addresses\ncommand: '~a'\noutput:\n~a"
                                              command
                                              R)))))))
                   (process command))))
        
        )