Previous: , Up: posix   [Index]


4.38 Scanning the file system with find

The library (vicare posix find) exports a simple API to scan file systems through the external program find. This library is a demonstration of the basic mechanism needed to execute an external program and read data from its standard output and standard error.

The source distribution of Vicare includes a simple demo program demo-find.sps in the tests subdirectory of the source tree.

The executable file find must be reachable in the current PATH; the library does not use default options for the command line of find; the ASCII text displayed on the standard output and standard error is captured for diagnostic.

Function: find option

Perform an operation spawning a subprocess that launches find. Each option must be a string representing a find command line argument. When successful return 3 values: a fixnum representing the exit status of the subprocess; two strings representing the output of find to, respectively, standard output and standard error; otherwise raise an exception.

Example:

#!r6rs
(import (vicare)
  (prefix (vicare posix) px.)
  (prefix (vicare posix find) find.)
  (only (vicare containers strings)
        string-tokenise)
  (only (vicare containers char-sets)
        char-set char-set-complement))

(define (scan-it root)
  (receive (status out err)
      (find.find root "-type" "f"
                 "-and" "-executable"
                 "-and" "-maxdepth" "2"
                 "-and" "-iname" "*bin*"
                 "-and" "-print0")
    (when (string? out)
      (for-each (lambda (hit)
                  (write hit)
                  (newline))
        (string-tokenise out
          (char-set-complement (char-set #\x00)))))
    (display err)
    (newline)
    (flush-output-port (current-output-port))))

(scan-it "/usr")