web
Last change
on this file since f6f3e91 was
f6f3e91,
checked in by Jessica B. Hamrick <jhamrick@…>, 15 years ago
|
Preserve directory hierarchy (not sure what happened to it)
|
-
Property mode set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | /* |
---|
2 | * Supporting functions for the Windows socket API. |
---|
3 | * |
---|
4 | * Provides supporting functions and wrappers that help with portability to |
---|
5 | * the Windows socket API. |
---|
6 | * |
---|
7 | * Written by Russ Allbery <rra@stanford.edu> |
---|
8 | * This work is hereby placed in the public domain by its author. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <config.h> |
---|
12 | #include <portable/system.h> |
---|
13 | #include <portable/socket.h> |
---|
14 | |
---|
15 | |
---|
16 | /* |
---|
17 | * Initializes the Windows socket library. The returned parameter provides |
---|
18 | * information about the socket library, none of which we care about. Return |
---|
19 | * true on success and false on failure. |
---|
20 | */ |
---|
21 | int |
---|
22 | socket_init(void) |
---|
23 | { |
---|
24 | WSADATA data; |
---|
25 | |
---|
26 | if (WSAStartup(MAKEWORD(2,2), &data)) |
---|
27 | return 0; |
---|
28 | return 1; |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | /* |
---|
33 | * On Windows, strerror cannot be used for socket errors (or any other errors |
---|
34 | * over sys_nerr). Try to use FormatMessage with a local static variable |
---|
35 | * instead. |
---|
36 | */ |
---|
37 | const char * |
---|
38 | socket_strerror(err) |
---|
39 | { |
---|
40 | const char *message = NULL; |
---|
41 | |
---|
42 | if (err >= sys_nerr) { |
---|
43 | char *p; |
---|
44 | DWORD f = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
---|
45 | | FORMAT_MESSAGE_IGNORE_INSERTS; |
---|
46 | static char *buffer = NULL; |
---|
47 | |
---|
48 | if (buffer != NULL) |
---|
49 | LocalFree(buffer); |
---|
50 | if (FormatMessage(f, NULL, err, 0, (LPTSTR) &buffer, 0, NULL) != 0) { |
---|
51 | p = strchr(buffer, '\r'); |
---|
52 | if (p != NULL) |
---|
53 | *p = '\0'; |
---|
54 | } |
---|
55 | message = buffer; |
---|
56 | } |
---|
57 | if (message == NULL) |
---|
58 | message = strerror(err); |
---|
59 | return message; |
---|
60 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.