File: fts_cmd.ml

package info (click to toggle)
ocaml-ctypes 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 1,798
  • sloc: ml: 6,625; ansic: 1,584; makefile: 108
file content (43 lines) | stat: -rw-r--r-- 980 bytes parent folder | download | duplicates (4)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
(*
 * Copyright (c) 2013 Jeremy Yallop.
 *
 * This file is distributed under the terms of the MIT License.
 * See the file LICENSE for details.
 *)

open Fts

let usage = "fts_cmd path [ path .. ]"

let sort_by_name lp rp =
  let open Ctypes in
  let open FTSENT in
  String.compare (name !@lp) (name !@rp)

let ents ?compar path_argv =
  let fts : FTS.t = fts_open ~path_argv ?compar ~options:[] in
  Stream.from (fun _ -> fts_read fts)

let main paths =
  let indent = ref 0 in
  let show_path ent =
    Printf.printf "%*s%s\n" !indent "" (FTSENT.path ent);
  in
  Stream.iter
    FTSENT.(fun ent ->
      match info ent with
        | FTS_D -> begin
          show_path ent;
          incr indent
        end
        | FTS_F
        | FTS_SL
        | FTS_SLNONE -> show_path ent
        | FTS_DP -> decr indent
        | _ -> ())
    (ents ~compar:sort_by_name paths)

let () = 
  match List.tl (Array.to_list Sys.argv) with
    | [] -> prerr_endline usage
    | l  -> main l