[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Luajit and errno
- From: Justin Cormack <justin@...>
- Date: Mon, 25 Apr 2011 15:27:14 +0100
Mike Pall wrote:
> So I've thought about adding an __errno attribute. If you declare
> a C function with this attribute, you get the errno as an extra
> return value (local ok, errno = ffi.C.mkdir(...)). This is fetched
> by low-level code immediately after the function returns. Alas,
> this means I have to figure out all of the variants for getting
> the errno value myself and teach them to the JIT compiler. :-/
Any further thoughts about this? This method does sound nice and easy to
use.
It does seem however that there should be platform dependent methods
though, so maybe it is not necessary. On Linux the code below
(__errno_location() is part of LSB) seems to work fine.
ffi.cdef [[
int open(const char *pathname, int flags);
int *__errno_location (void);
char *strerror(int errnum);
]]
local errnol = ffi.C.__errno_location()
fd = ffi.C.open("/tmp/notfound", 0)
if fd == -1 then
print("errno is " .. errnol[0])
print("error is " .. ffi.string(ffi.C.strerror(errnol[0])))
end