From: Michael D. Lowis Date: Tue, 19 Dec 2017 18:51:31 +0000 (-0500) Subject: fixed bug when opening empty files or nonexistent files X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0ac518c9242397e63aed3b06c118d8128aae2195;p=archive%2Ftide-ocaml.git fixed bug when opening empty files or nonexistent files --- diff --git a/lib/misc_prims.c b/lib/misc_prims.c index 2bc6268..fdb0a92 100644 --- a/lib/misc_prims.c +++ b/lib/misc_prims.c @@ -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); }