From d513c3c66b3c71412d87a3f24b8f792b7b728e93 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Sep 2006 10:57:08 -0300 Subject: bug: os.date throws error when result is the empty string --- loslib.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'loslib.c') diff --git a/loslib.c b/loslib.c index 3a1e8409..05138417 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.18 2006/03/09 18:08:22 roberto Exp roberto $ +** $Id: loslib.c,v 1.19 2006/04/26 18:19:49 roberto Exp roberto $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -146,11 +146,22 @@ static int os_date (lua_State *L) { setboolfield(L, "isdst", stm->tm_isdst); } else { - char b[256]; - if (strftime(b, sizeof(b), s, stm)) - lua_pushstring(L, b); - else - return luaL_error(L, LUA_QL("date") " format too long"); + char cc[3]; + luaL_Buffer b; + cc[0] = '%'; cc[2] = '\0'; + luaL_buffinit(L, &b); + for (; *s; s++) { + if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ + luaL_addchar(&b, *s); + else { + size_t reslen; + char buff[200]; /* should be big enough for any conversion result */ + cc[1] = *(++s); + reslen = strftime(buff, sizeof(buff), cc, stm); + luaL_addlstring(&b, buff, reslen); + } + } + luaL_pushresult(&b); } return 1; } -- cgit v1.2.3-55-g6feb