chickadee » library » system

system STRINGprocedure

Execute shell command. The functionality offered by this procedure depends on the capabilities of the host shell. If the forking of a subprocess failed, an exception is raised. Otherwise the return status of the subprocess is returned unaltered.

On a UNIX system, that value is the raw return value of waitpid(2), which contains signal, core dump and exit status. It is 0 on success. To pull out the signal number or exit status portably requires POSIX calls, but in a pinch you can use something like this:

;; Returns two values: #t if the process exited normally or #f otherwise;
;; and either the exit status, or the signal number if terminated via signal.
(define (process-status rc)
  (define (wait-signaled? x) (not (= 0 (bitwise-and x 127))))
  (define (wait-signal x) (bitwise-and x 127))
  (define (wait-exit-status x) (arithmetic-shift x -8))
  (if (wait-signaled? rc)
      (values #f (wait-signal rc))
      (values #t (wait-exit-status rc))))

#;> (process-status (system "exit 42"))
#t
42