module Config = struct type t = { all_directory_entries : bool; long_form : bool } let default = { all_directory_entries = false; long_form = false } let make ?(all_directory_entries = default.all_directory_entries) ?(long_form = default.long_form) () = { all_directory_entries; long_form } open Cmdliner let all_directory_entries = let doc = "Write out all directory entries, including those whose names begin with \ a period but excluding dot and dot-dot." in Arg.(value & flag & info [ "a" ] ~doc) let long_form = let doc = "Write out in long format." in Arg.(value & flag & info [ "l" ] ~doc) end let flow_as_formatter stdout = let outfns : Format.formatter_out_functions = { out_string = (fun s i len -> Eio.Flow.copy_string (String.sub s i len) stdout); out_width = Format.utf_8_scalar_width; out_flush = (fun () -> ()); out_newline = (fun () -> Eio.Flow.copy_string "\n" stdout); out_spaces = (fun n -> Eio.Flow.copy_string (String.make n ' ') stdout); out_indent = (fun n -> Eio.Flow.copy_string (String.make n ' ') stdout); } in Format.formatter_of_out_functions outfns let pp_file_mode ppf (kind, mode) = let kind_letter = match kind with | `Directory -> "d" | `Block_device -> "b" | `Character_special -> "c" | `Symbolic_link -> "l" | `Socket -> "s" | `Regular_file -> "-" | _ -> "o" in let check_perm perm mask c = if perm land mask <> 0 then c else "-" in let permissions pos ppf perm = Fmt.pf ppf "%s%s%s" (check_perm perm (0o400 lsr pos) "r") (check_perm perm (0o200 lsr pos) "w") (check_perm perm (0o100 lsr pos) "x") in Fmt.pf ppf "%s%a%a%a" kind_letter (permissions 0) mode (permissions 3) mode (permissions 6) mode let pp_long_form' ppf ((stat, name) : Eio.File.Stat.t * string) = match stat.kind with | `Character_special | `Block_device -> Fmt.pf ppf "%a %Ld %Ld %Ld %Ld %s %s" pp_file_mode (stat.kind, stat.perm) stat.nlink stat.uid stat.gid stat.dev "" name | _ -> Fmt.pf ppf "%a %Ld %Ld %Ld %a %s %s" pp_file_mode (stat.kind, stat.perm) stat.nlink stat.uid stat.gid Optint.Int63.pp stat.size "" name let pp_long_form ppf paths = Fmt.pf ppf "%a\n%!" Fmt.(list ~sep:(Fmt.any "\n") pp_long_form') paths let pp ppf paths = Fmt.pf ppf "%a\n%!" Fmt.(list ~sep:(Fmt.any " ") Fmt.string) paths let ls_path ~config stdout path = let d = Eio.Path.read_dir_entries path |> List.map snd in if config.Config.long_form then let d = List.map (fun s -> let fpath = Eio.Path.(path / s) in (Eio.Path.stat ~follow:false fpath, s)) d in pp_long_form stdout d else pp stdout d let ls ?(config = Config.default) env paths = let stdout = Eio.Stdenv.stdout env |> flow_as_formatter in match paths with | [ path ] -> ls_path ~config stdout path | _ -> ls_path ~config stdout env#fs open Cmdliner let paths = let doc = "Files -- pathnames to files." in Arg.(value & pos_all string [] & info [] ~doc) let term env = let make all_directory_entries long_form paths = let config = Config.make ~all_directory_entries ~long_form () in ls ~config env (List.map (Eio.Path.( / ) env#fs) paths) in let term = Term.(const make $ Config.all_directory_entries $ Config.long_form $ paths) in let info = let doc = "For each operand that names a file of a type other than directory or \ symbolic link to a directory, ls shall write the name of the file as \ well as any requested, associated information. For each operand that \ names a file of type directory, ls shall write the names of files \ contained within the directory as well as any requested, associated \ information." in Cmd.info "ls" ~doc in Cmd.v info term