diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-07 00:03:17 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-07 00:03:17 -0400 |
| commit | c454bfa3c4c11b30aa03cde266e76466d2971c20 (patch) | |
| tree | a49faba5510631f007fdabf54c956c1bfa35e4cd /examples/file.tm | |
| parent | 8670cbf9435c1ed20120f8106d2e58c77f57a552 (diff) | |
Some improvements
Diffstat (limited to 'examples/file.tm')
| -rw-r--r-- | examples/file.tm | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/examples/file.tm b/examples/file.tm index 7def4187..9d4197b7 100644 --- a/examples/file.tm +++ b/examples/file.tm @@ -38,6 +38,7 @@ func read(path:Text)->FileReadResult: char *gc_mem = GC_MALLOC_ATOMIC(sb.st_size+1); memcpy(gc_mem, mem, sb.st_size); gc_mem[sb.st_size] = '\0'; + close(fd); return file$FileReadResult$tagged$Success(Text$from_strn(gc_mem, sb.st_size)); } else { const int chunk_size = 256; @@ -130,7 +131,10 @@ struct LineReader(_file:@Memory): char *buf = NULL; size_t space = 0; ssize_t len = getline(&buf, &space, *(FILE**)$r.$_file); - if (len < 0) return (file$FileReadResult_t){1, .$Failure={Text("End of file")}}; + if (len < 0) { + file$_close_file($r.$_file); + return (file$FileReadResult_t){1, .$Failure={Text("End of file")}}; + } if (len > 0 && buf[len-1] == '\n') --len; char *line = GC_MALLOC_ATOMIC(len + 1); memcpy(line, buf, len); @@ -185,6 +189,7 @@ func command(cmd:Text)->FileReadResult: buf = GC_MALLOC_ATOMIC(chunk_size); } } while (just_read > 0); + pclose($f); contents; }) ):Text |
