There are many ways to get errno.
With the very cool cffi-wrappers you can use
(include "errno.h")
(defwrapper* "get_errno" :int () "return errno;")
which finesses the point about whether or not errno is a real variable or a thread-local variable or something else.
However, there is no guarantee that between calling your function and getting the errno, the Lisp environment will not call a C function that resets errno.
(progn (open ...) ;; something can happen here (get-errno))
In practice, we have observed this problem.
Each Lisp might have a different way of handing it.
In Allegro CL it is possible to build the wrapper for the foreign function so it collects the errno in a safe way, using def-foreign-call with :ERROR-VALUE :ERRNO.
In SBCL there is a native get-errno function to get the errno.
Is there any plan to add a semi-portable wrapper to this functionality for CFFI?
If not, would you accept a patch for it? I guess the obvious way is to modify (cffi:defcfun ...) to take a :after-collect-value argument, so that it could work not only for errno
On most Lisps I guess it might translate to something like this
(defmacro defcfun-with-collect-value ((name &key after-collect-value) ...) `(without-interrupts (values (call-foreign-function ,name ...) (funcall ,after-collect-value))))
Suggestions for better names and interfaces much appreciated.