lua-users home
lua-l archive

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


Given the overwhelming number of responses to my previous post, I thought
should chat to myself on the subject.

Question 1, Is this behaviour by design, or is it an oversight?
Question 2, Should package developers protect against closing these files?
Question 3, Is it better to leave the decision to the user and not close files?

For an example function, see ltn12.sink.file().

DB

David Burgess wrote:
> 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.
>