1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-23 05:36:52 +00:00

Disable buffering for files created with ev/to-file

This commit is contained in:
Michael Camilleri 2024-12-15 20:37:58 +09:00
parent 268ff666d2
commit 6ee05785d1
No known key found for this signature in database
GPG Key ID: 7EB218A48DF8B572
2 changed files with 9 additions and 2 deletions

View File

@ -3312,6 +3312,9 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
/* _close(fd); */ /* _close(fd); */
return NULL; return NULL;
} }
if (setvbuf(f, NULL, _IONBF, 0)) {
return NULL;
}
#else #else
int newHandle = dup(stream->handle); int newHandle = dup(stream->handle);
if (newHandle < 0) return NULL; if (newHandle < 0) return NULL;
@ -3320,6 +3323,10 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
close(newHandle); close(newHandle);
return NULL; return NULL;
} }
if (setvbuf(f, NULL, _IONBF, 0)) {
close(newHandle);
return NULL;
}
#endif #endif
return janet_makejfile(f, flags); return janet_makejfile(f, flags);
} }
@ -3327,7 +3334,8 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
JANET_CORE_FN(janet_cfun_to_file, JANET_CORE_FN(janet_cfun_to_file,
"(ev/to-file)", "(ev/to-file)",
"Create core/file copy of the stream. This value can be used " "Create core/file copy of the stream. This value can be used "
"when blocking IO behavior is needed.") { "when blocking IO behavior is needed. Buffering is turned off "
"for the file.") {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type); JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
JanetFile *iof = get_file_for_stream(stream); JanetFile *iof = get_file_for_stream(stream);

View File

@ -422,7 +422,6 @@
(expect-read bob "Whats your name?\n") (expect-read bob "Whats your name?\n")
(def fbob (ev/to-file bob)) (def fbob (ev/to-file bob))
(file/write fbob "bob") (file/write fbob "bob")
(file/flush fbob)
(:close fbob) (:close fbob)
(expect-read bob "Welcome bob\n") (expect-read bob "Welcome bob\n")
(def alice (net/connect test-host test-port)) (def alice (net/connect test-host test-port))