1. Joby Poriyath
  2. ocaml-exercises

Commits

Joby Poriyath  committed f43b8ba

unix programming bits

  • Participants
  • Parent commits d4aa004
  • Branches master

Comments (0)

Files changed (4)

File .ocamlinit

View file
 #directory "_build";;
 #load "util.cmo";;
+#load "unix.cma";;

File echo.ml

View file
+open Misc
+
 let echo () = 
   for i = 1 to (Array.length (Sys.argv) - 1)
   do
   done;
   print_newline ()
 
-let _ = echo ()
+let open_invalid_file () =
+  let fd = Unix.openfile "invalid_file" [Unix.O_RDONLY] 0o644 in
+    Unix.close fd
+
+(* let _ = handle_unix_error open_invalid_file () *)
+
+
+
+  

File misc.ml

View file
+let rec restart_on_EINTR f x =
+  try f x 
+  with Unix.Unix_error(Unix.EINTR, _, _) -> restart_on_EINTR f x
+
+      
+let handle_unix_error f x =
+  try (restart_on_EINTR f x)
+  with Unix.Unix_error(err, fun_name, arg) -> 
+    prerr_string Sys.argv.(0);
+    prerr_string ": \"";
+    prerr_string fun_name;
+    prerr_string "\" failed";
+    if String.length arg > 0 then begin
+      prerr_string " on \"";
+      prerr_string arg;
+      prerr_string "\""
+    end;
+    prerr_string ": ";
+    prerr_endline (Unix.error_message err);
+    exit 2
+
+let try_finalize f x finally y = 
+  try f x 
+  with exn -> ignore (finally y); raise exn

File misc.mli

View file
+val handle_unix_error : ('a -> 'b) -> 'a -> 'b
+val try_finalize : ('a -> 'b) -> 'a -> ('c -> 'd) -> 'c -> 'b
+val restart_on_EINTR : ('a -> 'b) -> 'a -> 'b