#open "printf";; let file_chars = ref 0 and file_lines = ref 0 and file_words = ref 0;; let total_chars = ref 0 and total_lines = ref 0 and total_words = ref 0;; let reset_count () = file_chars := 0; file_words := 0; file_lines := 0;; let cumulate () = total_chars := !total_chars + !file_chars; total_words := !total_words + !file_words; total_lines := !total_lines + !file_lines;; let rec counter f in_word = let c = input_char f in incr file_chars; match c with ` ` | `\t` -> if in_word then incr file_words else (); counter f false | `\n` -> incr file_lines; if in_word then incr file_words else (); counter f false | c -> counter f true;; let word_count_ch f = reset_count (); try counter f false with io__End_of_file -> begin cumulate (); close_in f end;; let output_results filename = printf " %9d %9d %9d %s\n" !file_lines !file_words !file_chars filename;; let ouput_total () = printf " %9d %9d %9d %s\n" !total_lines !total_words !total_chars "total";; let word_count_file filename = try let f = open_in filename in word_count_ch f; output_results (filename) with sys__Sys_error s -> begin printf "%s\n" s; exit 2 end;; let main () = let args = sys__command_line in let nb_files = vect_length args - 1 in for i = 1 to nb_files do word_count_file args.(i) done; if nb_files > 1 then ouput_total (); exit 0;; main();;