root/system-utils.scm

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
(define-module (system-utils)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (path-utils)
  #:export (shell-command-to-string*
            mkdir-p))

(define* (shell-command-to-string* cmd #:optional . args)
  "run system program CMD with ARGS and return stdout as string"
  (with-output-to-string
    (lambda ()
      (let ((in-port (apply open-pipe* (cons OPEN_READ (cons cmd args)))))
        (let loop ((line (read-line in-port 'concat)))
          (or (eof-object? line)
              (begin
                (display line)
                (loop (read-line in-port 'concat)))))))))

(define (mkdir-p path)
  (define (step base d rest)
    (let ((new-base (string-append base "/" d)))
      (when (not (file-exists? new-base))
        (mkdir new-base))
      (if (null? rest)
          #t
          (step new-base (car rest) (cdr rest)))))
  (let ((dirs (string-split (path-abs path) #\/)))
    (if (null? dirs)
        #f
        (step "" (car dirs) (cdr dirs)))))