aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-04-03 12:56:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-04-03 12:56:52 -0300
commit620f49a7aae8a5c982b21f0accbf2ff9019a55f6 (patch)
tree4ba9f6063434759963cc5207b4703248687be793
parent3f4f28010aa5065456f1edf97de1ab268cc49944 (diff)
downloadlua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.tar.gz
lua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.tar.bz2
lua-620f49a7aae8a5c982b21f0accbf2ff9019a55f6.zip
Tiny refactoring in io.flush
-rw-r--r--liolib.c13
-rw-r--r--testes/files.lua19
2 files changed, 25 insertions, 7 deletions
diff --git a/liolib.c b/liolib.c
index 3225ed5f..c8f165cb 100644
--- a/liolib.c
+++ b/liolib.c
@@ -732,18 +732,19 @@ static int f_setvbuf (lua_State *L) {
732} 732}
733 733
734 734
735 735static int aux_flush (lua_State *L, FILE *f) {
736static int io_flush (lua_State *L) {
737 FILE *f = getiofile(L, IO_OUTPUT);
738 errno = 0; 736 errno = 0;
739 return luaL_fileresult(L, fflush(f) == 0, NULL); 737 return luaL_fileresult(L, fflush(f) == 0, NULL);
740} 738}
741 739
742 740
743static int f_flush (lua_State *L) { 741static int f_flush (lua_State *L) {
744 FILE *f = tofile(L); 742 return aux_flush(L, tofile(L));
745 errno = 0; 743}
746 return luaL_fileresult(L, fflush(f) == 0, NULL); 744
745
746static int io_flush (lua_State *L) {
747 return aux_flush(L, getiofile(L, IO_OUTPUT));
747} 748}
748 749
749 750
diff --git a/testes/files.lua b/testes/files.lua
index 2c802047..53edf314 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -347,7 +347,7 @@ collectgarbage()
347 347
348assert(io.write(' ' .. t .. ' ')) 348assert(io.write(' ' .. t .. ' '))
349assert(io.write(';', 'end of file\n')) 349assert(io.write(';', 'end of file\n'))
350f:flush(); io.flush() 350assert(f:flush()); assert(io.flush())
351f:close() 351f:close()
352print('+') 352print('+')
353 353
@@ -461,6 +461,23 @@ do -- testing closing file in line iteration
461end 461end
462 462
463 463
464do print("testing flush")
465 local f = io.output("/dev/null")
466 assert(f:write("abcd")) -- write to buffer
467 assert(f:flush()) -- write to device
468 assert(f:write("abcd")) -- write to buffer
469 assert(io.flush()) -- write to device
470 assert(f:close())
471
472 local f = io.output("/dev/full")
473 assert(f:write("abcd")) -- write to buffer
474 assert(not f:flush()) -- cannot write to device
475 assert(f:write("abcd")) -- write to buffer
476 assert(not io.flush()) -- cannot write to device
477 assert(f:close())
478end
479
480
464-- test for multipe arguments in 'lines' 481-- test for multipe arguments in 'lines'
465io.output(file); io.write"0123456789\n":close() 482io.output(file); io.write"0123456789\n":close()
466for a,b in io.lines(file, 1, 1) do 483for a,b in io.lines(file, 1, 1) do