aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-02-06 16:38:47 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-02-06 16:38:47 -0200
commit53db607963177fb91e566f4790f15e3fbcbdad61 (patch)
tree7ca44a07cf042ce525310e5bc69879de5a7205de
parentcbf0c7a103bd69a2b8d8a55f676cf7bb08058909 (diff)
downloadlua-53db607963177fb91e566f4790f15e3fbcbdad61.tar.gz
lua-53db607963177fb91e566f4790f15e3fbcbdad61.tar.bz2
lua-53db607963177fb91e566f4790f15e3fbcbdad61.zip
avoid using 'fputs' in 'print' to avoid problems with embedded zeros
-rw-r--r--lbaselib.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 4c855975..8b024eaf 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.207 2008/07/03 14:23:35 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.208 2008/07/11 17:51:01 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -24,27 +24,33 @@
24/* 24/*
25** If your system does not support `stdout', you can just remove this function. 25** If your system does not support `stdout', you can just remove this function.
26** If you need, you can define your own `print' function, following this 26** If you need, you can define your own `print' function, following this
27** model but changing `fputs' to put the strings at a proper place 27** model but changing `writestring' to put the strings at a proper place
28** (a console window or a log file, for instance). 28** (a console window or a log file, for instance).
29*/ 29*/
30static void writestring (const char *s, size_t l) {
31 fwrite(s, sizeof(char), l, stdout);
32}
33
34
30static int luaB_print (lua_State *L) { 35static int luaB_print (lua_State *L) {
31 int n = lua_gettop(L); /* number of arguments */ 36 int n = lua_gettop(L); /* number of arguments */
32 int i; 37 int i;
33 lua_getglobal(L, "tostring"); 38 lua_getglobal(L, "tostring");
34 for (i=1; i<=n; i++) { 39 for (i=1; i<=n; i++) {
35 const char *s; 40 const char *s;
41 size_t l;
36 lua_pushvalue(L, -1); /* function to be called */ 42 lua_pushvalue(L, -1); /* function to be called */
37 lua_pushvalue(L, i); /* value to print */ 43 lua_pushvalue(L, i); /* value to print */
38 lua_call(L, 1, 1); 44 lua_call(L, 1, 1);
39 s = lua_tostring(L, -1); /* get result */ 45 s = lua_tolstring(L, -1, &l); /* get result */
40 if (s == NULL) 46 if (s == NULL)
41 return luaL_error(L, LUA_QL("tostring") " must return a string to " 47 return luaL_error(L, LUA_QL("tostring") " must return a string to "
42 LUA_QL("print")); 48 LUA_QL("print"));
43 if (i>1) fputs("\t", stdout); 49 if (i>1) writestring("\t", 1);
44 fputs(s, stdout); 50 writestring(s, l);
45 lua_pop(L, 1); /* pop result */ 51 lua_pop(L, 1); /* pop result */
46 } 52 }
47 fputs("\n", stdout); 53 writestring("\n", 1);
48 return 0; 54 return 0;
49} 55}
50 56