Fix stringification of $! in VC10+ builds where errno > sys_nerr
authorSteve Hay <steve.m.hay@googlemail.com>
Thu, 5 Sep 2013 14:18:20 +0000 (15:18 +0100)
committerSteve Hay <steve.m.hay@googlemail.com>
Mon, 16 Sep 2013 14:37:32 +0000 (15:37 +0100)
commitcd6a3131ad2bbd2f1f502de34f7a253780ee0bbd
tree0e216112ab7c43da621259d80f4ebdcf84a9af7b
parentc9beaf974d06db3d9c7faecba5bb3e9e2a91767d
Fix stringification of $! in VC10+ builds where errno > sys_nerr

VC++ 2010 and above define a "POSIX supplement" of errno values ranging from
EADDRINUSE (100) to EWOULDBLOCK (140), but sys_nerr is still 43 and strerror()
returns "Unknown error" for them. We already avoid using strerror() if errno
> sys_nerr, but we treat such values as Windows error codes (i.e. <winerror.h>
values) and look up the corresponding system messages for them. There is no
better plan for these POSIX supplement errno values, but they must be
converted from <errno.h> values to <winerror.h> values first otherwise we
will look up the wrong system message. In practice, we only expect to find
errno > sys_nerr in the case of Windows sockets errors (we used to assign
WSAGetLastError() to errno), so we simply convert Exxx values to WSAExxx
values where possible, and use a default <winerror.h> value otherwise (namely,
ERROR_INVALID_FUNCTION).

This change fixes code such as this:

perl -le "$!=107; print $!"

which now outputs the expected "No connection could be made because the
target machine actively refused it." rather than "The program stopped
because an alternate diskette was not inserted." (in VC10+ builds).

Also: Fix the spelling of ECANCELED.
win32/include/sys/errno2.h
win32/win32.c
win32/win32sck.c