diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-07-06 12:16:51 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-07-06 12:16:51 -0300 |
| commit | 403e181d811620ca4054023a72cdff61566c8224 (patch) | |
| tree | 346c689229028a4bb0418880cf6cf7b7d2e7ee17 | |
| parent | 4af03c5ae10cfae115069dc5de8ec13b771df749 (diff) | |
| download | lua-403e181d811620ca4054023a72cdff61566c8224.tar.gz lua-403e181d811620ca4054023a72cdff61566c8224.tar.bz2 lua-403e181d811620ca4054023a72cdff61566c8224.zip | |
'strftime' puts its result directly into 'lua_Buffer'
| -rw-r--r-- | loslib.c | 14 |
1 files changed, 9 insertions, 5 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loslib.c,v 1.57 2015/04/10 17:41:04 roberto Exp roberto $ | 2 | ** $Id: loslib.c,v 1.58 2015/07/04 16:35:14 roberto Exp roberto $ |
| 3 | ** Standard Operating System library | 3 | ** Standard Operating System library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -253,6 +253,10 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { | |||
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | 255 | ||
| 256 | /* maximum size for an individual 'strftime' item */ | ||
| 257 | #define SIZETIMEFMT 250 | ||
| 258 | |||
| 259 | |||
| 256 | static int os_date (lua_State *L) { | 260 | static int os_date (lua_State *L) { |
| 257 | const char *s = luaL_optstring(L, 1, "%c"); | 261 | const char *s = luaL_optstring(L, 1, "%c"); |
| 258 | time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); | 262 | time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); |
| @@ -283,14 +287,14 @@ static int os_date (lua_State *L) { | |||
| 283 | cc[0] = '%'; | 287 | cc[0] = '%'; |
| 284 | luaL_buffinit(L, &b); | 288 | luaL_buffinit(L, &b); |
| 285 | while (*s) { | 289 | while (*s) { |
| 286 | if (*s != '%') /* no conversion specifier? */ | 290 | if (*s != '%') /* not a conversion specifier? */ |
| 287 | luaL_addchar(&b, *s++); | 291 | luaL_addchar(&b, *s++); |
| 288 | else { | 292 | else { |
| 289 | size_t reslen; | 293 | size_t reslen; |
| 290 | char buff[200]; /* should be big enough for any conversion result */ | 294 | char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); |
| 291 | s = checkoption(L, s + 1, cc); | 295 | s = checkoption(L, s + 1, cc); |
| 292 | reslen = strftime(buff, sizeof(buff), cc, stm); | 296 | reslen = strftime(buff, SIZETIMEFMT, cc, stm); |
| 293 | luaL_addlstring(&b, buff, reslen); | 297 | luaL_addsize(&b, reslen); |
| 294 | } | 298 | } |
| 295 | } | 299 | } |
| 296 | luaL_pushresult(&b); | 300 | luaL_pushresult(&b); |
