lua-users home
lua-l archive

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


try these bits and pieces i've shamelessly stolen from others :-)

// ripped from lua-5.0.2/src/lib/liolib.c {

#define FILEHANDLE "FILE*"

static FILE **topfile (lua_State *L, int findex) {
  FILE **f = (FILE **)luaL_checkudata(L, findex, FILEHANDLE);
  if (f == NULL) luaL_argerror(L, findex, "bad file");
  return f;
}

static FILE *tofile (lua_State *L, int findex) {
  FILE **f = topfile(L, findex);
  if (*f == NULL)
    luaL_error(L, "attempt to use a closed file");
  return *f;
}

int jlib_getfd(lua_State *L) {
    FILE *f = tofile(L, 1);
    int fd = -1;
    if (f) {
        fd = fileno(f);
        lua_pushnumber(L, (double)fd);
        return 1;
    }
    return 0;
}

On Aug 19, 2006, at 2:36 PM, Norman Ramsey wrote:

I am using lua on a POSIX system, and I would like to
find a way to grap the underlying file descriptor associated
with a file, so that I can make it standard output.  E.g.,

local f = assert(io.popen('less', 'w'))
posix.dup2(fileno(f), 0)

My trouble is that I don't know how to implement 'fileno'.
Any suggestions?


Norman