-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ml
More file actions
39 lines (37 loc) · 1.4 KB
/
test.ml
File metadata and controls
39 lines (37 loc) · 1.4 KB
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
let read_file pathname =
let s = Filesystem.string_of_pathname pathname in
Printf.printf "## Reading (%d) %s\n" (String.length s) s;
let fd = Filesystem.openfile pathname [Unix.O_RDONLY] 0 in
let ch = Unix.in_channel_of_descr fd in
let rec loop () =
match try Some (input_line ch) with End_of_file -> None with
| Some l -> Printf.printf "%s : %s\n" s l; loop ()
| None -> ()
in
try loop (); close_in ch
with e -> (close_in ch; raise e)
let rec traverse dirname =
Printf.printf "# Directory: %s\n" (Filesystem.string_of_pathname dirname);
let handle = Filesystem.opendir dirname in
let rec loop () =
match try Some (Filesystem.readdir handle) with End_of_file -> None with
| None -> ()
| Some fname when String.length fname > 0 && fname.[0] = '.' ->
loop ()
| Some fname ->
(let fullname = Filesystem.append dirname fname in
if Filesystem.is_directory fullname then
traverse fullname
else
read_file fullname);
loop ()
in
try loop (); Filesystem.closedir handle
with e -> (Filesystem.closedir handle; raise e)
let _ =
try
Printf.printf "Input: %s\n" Sys.argv.(1);
let dir = Filesystem.pathname_of_relative_path Sys.argv.(1) in
traverse dir
with Unix.Unix_error (err, func, param) ->
Printf.eprintf "Unix.Unix_error (%s, %s, %s)\n" (Unix.error_message err) func param