Returning errno as a second value

Mark Cox markcox80 at gmail.com
Thu Aug 15 10:48:24 UTC 2013


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


More information about the cffi-devel mailing list