Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cmp return None for identical files #59

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/fileutils/FileUtil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ val which:

(** [cmp skip1 fln1 skip2 fln2] Compare files [fln1] and [fln2] starting at pos
[skip1] [skip2] and returning the first octect where a difference occurs.
Returns [Some -1] if one of the file is not readable or if it is not a
file.
Returns [Some -1] if one of the files is not readable or if it is not a
file. Returns [None] if given two identical files.
See {{:http://pubs.opengroup.org/onlinepubs/007904875/utilities/cmp.html}POSIX documentation}.
*)
val cmp:
Expand Down
5 changes: 4 additions & 1 deletion src/lib/fileutils/FileUtilCMP.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ let cmp ?(skip1 = 0) fln1 ?(skip2 = 0) fln2 =
let rec loop count s1 s2 =
match s1, s2 with
| Seq.Cons (v1, s1), Seq.Cons (v2, s2) when v1 = v2 -> loop (count + 1) (s1 ()) (s2 ())
| Seq.Nil, Seq.Nil -> (-1)
| _ -> count
in
let count = loop 0 (stream1 ()) (stream2 ()) in
clean_fd ();
Some count
match count with
| (-1) -> None
| x -> Some x
end else
Some (-1)

Expand Down