errno?
remexre opened this issue · comments
Nathan Ringo commented
Is there a way to access errno
?
Christopher Wellons commented
There's nothing in the Elisp API for accessing global variables, though
I think, in theory, the FFI "bytecode" is capable of it. However, errno
is a special case, not really being a global variable, and isn't
accessible via dlsym(). It would have to be its own special case.
If you're on Linux, you can access errno by calling the "private"
implementation function, __errno_location, which is how errno really
works. Here's the definition:
int *__errno_location(void);
Without testing it, that should be:
(ffi-call nil "__errno_location" [:pointer])
You're still stuck with the problem of dereferencing it, though. Man,
this FFI is really lacking, isn't it?
Nathan Ringo commented
Would it make sense to add a bytecode command to return errno along with the return value? The problem with fetching errno afterwards with a function call is that it'd get trashed when the C++ program writes values back to emacs.
Christopher Wellons commented
Whoops, you're absolutely right. Typically that write(2) shouldn't
clobber errno since it's only set on error -- and if there was an error
Emacs probably isn't getting the response from the subprocess anyway --
but it would be more correct to handle errno specially and carefully
anyway.
If you want to tackle this, I'll accept a good PR that implements it. I
don't have plans myself to add any new features to this FFI, treating it
more as a proof of concept. I think a much better approach would use the
new module system rather than a subprocess. I actually started to do
this a few months back, generating C code and building it with a C
compiler rather than use libffi, but I never finished and never put the
code online.