lua-users home
lua-l archive

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


if closing stdin/stderr/stdout I would have thought the friendly
thing to do would be to assign it to NULL,

fclose(stdin);
stdin=NULL;

This solves the problem on many systems.

db

On 4/17/07, David Manura <dm.gmane@math2.org> wrote:
The following code demonstrates what seems to be a bug in Lua 5.1.2.  It affects
at least Win32.

io.input():close()  -- breaks it
--io.input(io.open("NUL")) -- unbreaks it

loadfile("1.lua")  -- does not close file!
                   --   culprit in lauxlib.c:luaL_loadfile:
                   --   if (lf.f != stdin) fclose(lf.f);
collectgarbage()   -- no help

-- file 1.lua is locked here on win32!
-- (e.g. can't save in Wordpad (write.exe))

os.execute("notepad")  -- pause to allow user to test above

If you close io.input() and open another file (not even used as io.input), then
loadfile() will not properly close the file it opens. The problem it seems is
the file opened == stdin.

Here's a related example in C:

#include <stdio.h>

int main()
{
  FILE * f;
  fclose(stdin);
  f = fopen("tmp1", "w");
  if (f == stdin) printf("same\n");  /* prints "same" */
  return 0;
}

On Win32 (both VC2005 and GCC), this prints "same", and lhf reports it also does
so on macosx.  On at least two Linux systems with GCC it prints nothing.

There might be other places in the Lua code affected by similar issues (where
stdin, stdout, and stderr are used).