]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
fixed bug when opening empty files or nonexistent files
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 19 Dec 2017 18:51:31 +0000 (13:51 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 19 Dec 2017 18:51:31 +0000 (13:51 -0500)
lib/misc_prims.c

index 2bc626860ef8b06351be02c8b3df7d0096349027..fdb0a92f681bbb7830212550d9b971e54ca8b4f1 100644 (file)
@@ -12,14 +12,14 @@ CAMLprim value load_file(value path) {
     if (((fd = open(String_val(path), O_RDONLY, 0)) < 0) ||
         (fstat(fd, &sb) < 0) ||
         (sb.st_size == 0)) {
-        caml_failwith("could not open file");
+        str = caml_alloc_string(0);
     } else {
         str = caml_alloc_string(sb.st_size);
         while ((nread = read(fd, String_val(str), sb.st_size)) > 0);
         if (nread < 0)
             caml_failwith("read() failed");
     }
-    close(fd);
+    if (fd > 0) close(fd);
     CAMLreturn(str);
 }