With Allegro CL, there's an option you can pass (:error-value :os-specific) where it will give you errno or GetLastError() depending on what OS you're on.


On Thu, Aug 15, 2013 at 12:48 PM, Mark Cox <markcox80@gmail.com> wrote:
G'day Everyone,

On 13/08/2013, at 6:54 AM, Luís Oliveira wrote:

> How about an interface like this?
>
>  (with-errno
>    (some-foreign-function-with-the-errno-option-on)
>    (get-errno))
>
> or perhaps
>
>  (with-errno (errno)
>    (some-foreign-function-with-the-errno-option-on)
>    errno)

Isn't this problem more general than just errno? For example, the functions WSAGetLastError() and GetLastError() on Windows.

 // On Posix based systems:
   ssize_t rv = read(fd, buffer, buffer_size);
   if (rv == -1) {
     printf("ERROR: %s\n", strerror(errno));
     return FAILED;
   }

 // On Windows (Winsock):
   SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if (s == INVALID_SOCKET) {
     int last_error = WSAGetLastError();
     WCHAR buffer[1024];
     FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS,
                   0, last_error, 0, buffer, 1024, NULL);
     return FAILED;
   }

 // On Windows (Named Pipes)
   HANDLE h = CreateFileA(pipe_name, GENERIC_READ | GENERIC_WRITE, ... );
   if (h == INVALID_HANDLE_ERROR) {
     DWORD last_error = GetLastError();
     WCHAR buffer[1024];
     FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS,
                   0, last_error, 0, buffer, 1024, NULL);
     return FAILED;
   }

This entire thread has been enlightening.

Thanks
Mark