lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 5-Aug-04, at 1:03 PM, Alex Sandro Queiroz e Silva wrote:

Similarly, when you read the Lua Manual you know that everything there holds for every ANSI C-compliant platform. There is no need to test for anything. But if you need os.ext.delay, you know you must test if it exists because of the "ext" in the name. In the end, I think the clarity of the name pays off.

Not really. For example, the ANSI C library does not insist that
system() do anything other than return an error message, but
it provides a way for the caller to determine whether there is
any string which would work: (see second sentence)

> int system(const char *s);
>
> If s is not a null pointer, the function passes the string  s to be
> executed by a command processor, supplied by the target environment,
> and returns the status reported by the command processor. If s is a
> null pointer, the function returns nonzero only if the target
> environment supplies a command processor. Each implementation defines
> what strings its command processor accepts.

Unfortunately, the current implementation of os.execute does not reflect
this definition very well; there is no way to do the indicated test.

Similarly, ANSI C does not presuppose that the machine actually has any
notion of the current time: "The function returns the current calendar time, if the target environment can determine it; otherwise, it returns -1."

Lua's os.time() simply hands this error value back to the caller,
although it tests for the error code in the case where mktime()
is called.

Possible changes:

Current implementation:

static int io_execute (lua_State *L) {
  lua_pushnumber(L, system(luaL_checkstring(L, 1)));
  return 1;
}

Replace with some variation on:

static int io_execute (lua_State *L) {
  const char *cmd = NULL;
  int status;
  if (!lua_isnoneornil(L, 1)) cmd = luaL_checkstring(L, 1);
  system(cmd);
  lua_pushboolean(L, status == 0);
  lua_pushnumber(L, (lua_Number)status);
  return 2;
}

and:

static int io_time (lua_State *L) {
  if (lua_isnoneornil(L, 1))  /* called without args? */
    lua_pushnumber(L, time(NULL));  /* return current time */
  else {
    time_t t;
/* SNIP */
    t = mktime(&ts);
    if (t == (time_t)(-1))
      lua_pushnil(L);
    else
      lua_pushnumber(L, t);
  }
  return 1;
}

Replace with:

static int io_time (lua_State *L) {
  time_t t;
  if (lua_isnoneornil(L, 1))  /* called without args? */
    t = time(NULL);  /* return current time */
  else {
/* SNIP */
    t = mktime(&ts);
  }
  if (t == (time_t)(-1))
    lua_pushnil(L);
  else
    lua_pushnumber(L, t);
  return 1;
}

Sorry about not providing real patch files :(

Rici