lua-users home
lua-l archive

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

In 5.1 the files io.stderr, io.stdout and io.stdin may be closed.

Given this behaviour methinks it would be helpful to provide support
for duplicating files (like the Win32 code below).

This means that the duplicates of the standard files can be passed to
package functions that unconditionally close files.
(e.g. Luasocket)

static int f_dup (lua_State *L) {
  FILE *f = tofile(L);
  const char *mode = luaL_optstring(L, 2, "wb");
  int fd  = _dup( _fileno( f ));
  FILE **pf = newfile(L);
  *pf = _fdopen(fd, mode);
  if (*pf == NULL && fd >= 0) _close(fd);
  return (*pf == NULL) ? pushresult(L, 0, "dup file") : 1;

David B.