| 661 | |
| 662 | |
| 663 | static int g_write (lua_State *L, FILE *f, int arg) { |
| 664 | int nargs = lua_gettop(L) - arg; |
| 665 | size_t totalbytes = 0; /* total number of bytes written */ |
| 666 | errno = 0; |
| 667 | for (; nargs--; arg++) { /* for each argument */ |
| 668 | char buff[LUA_N2SBUFFSZ]; |
| 669 | const char *s; |
| 670 | size_t numbytes; /* bytes written in one call to 'fwrite' */ |
| 671 | size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ |
| 672 | if (len > 0) { /* did conversion work (value was a number)? */ |
| 673 | s = buff; |
| 674 | len--; |
| 675 | } |
| 676 | else /* must be a string */ |
| 677 | s = luaL_checklstring(L, arg, &len); |
| 678 | numbytes = fwrite(s, sizeof(char), len, f); |
| 679 | totalbytes += numbytes; |
| 680 | if (numbytes < len) { /* write error? */ |
| 681 | int n = luaL_fileresult(L, 0, NULL); |
| 682 | lua_pushinteger(L, cast_st2S(totalbytes)); |
| 683 | return n + 1; /* return fail, error msg., error code, and counter */ |
| 684 | } |
| 685 | } |
| 686 | return 1; /* no errors; file handle already on stack top */ |
| 687 | } |
| 688 | |
| 689 | |
| 690 | static int io_write (lua_State *L) { |
no test coverage detected