~ubuntu-branches/ubuntu/natty/ibm-3270/natty

« back to all changes in this revision

Viewing changes to ws3270/w3misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2009-12-14 11:48:53 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091214114853-mywixml32hct9jr1
Tags: 3.3.10ga4-2
* Fix section to match override.
* Use debhelper compat level 7.
* Use 3.0 (quilt) source format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007-2009, Paul Mattes.
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *     * Redistributions of source code must retain the above copyright
 
8
 *       notice, this list of conditions and the following disclaimer.
 
9
 *     * Redistributions in binary form must reproduce the above copyright
 
10
 *       notice, this list of conditions and the following disclaimer in the
 
11
 *       documentation and/or other materials provided with the distribution.
 
12
 *     * Neither the names of Paul Mattes nor the names of his contributors
 
13
 *       may be used to endorse or promote products derived from this software
 
14
 *       without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
/*
 
29
 *      w3misc.c
 
30
 *              Miscellaneous Win32 functions.
 
31
 */
 
32
 
 
33
#include "globals.h"
 
34
 
 
35
#if !defined(_WIN32) /*[*/
 
36
#error This module is only for Win32.
 
37
#endif /*]*/
 
38
 
 
39
#include <winsock2.h>
 
40
#include <ws2tcpip.h>
 
41
#include <stdio.h>
 
42
#include <errno.h>
 
43
 
 
44
#include "w3miscc.h"
 
45
 
 
46
/* Initialize Winsock. */
 
47
int
 
48
sockstart(void)
 
49
{
 
50
        static int initted = 0;
 
51
        WORD wVersionRequested;
 
52
        WSADATA wsaData;
 
53
 
 
54
        if (initted)
 
55
                return 0;
 
56
 
 
57
        initted = 1;
 
58
 
 
59
        wVersionRequested = MAKEWORD(2, 2);
 
60
 
 
61
        if (WSAStartup(wVersionRequested, &wsaData) != 0) {
 
62
                fprintf(stderr, "WSAStartup failed: %s\n",
 
63
                        win32_strerror(GetLastError()));
 
64
                return -1;
 
65
        }
 
66
 
 
67
        if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
 
68
                fprintf(stderr, "Bad winsock version: %d.%d\n",
 
69
                        LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
 
70
                return -1;
 
71
        }
 
72
 
 
73
        return 0;
 
74
}
 
75
 
 
76
/* Convert a network address to a string. */
 
77
const char *
 
78
inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
 
79
{
 
80
        union {
 
81
                struct sockaddr sa;
 
82
                struct sockaddr_in sin;
 
83
                struct sockaddr_in6 sin6;
 
84
        } sa;
 
85
        DWORD ssz;
 
86
        DWORD sz = cnt;
 
87
 
 
88
        memset(&sa, '\0', sizeof(sa));
 
89
 
 
90
        switch (af) {
 
91
        case AF_INET:
 
92
                sa.sin = *(struct sockaddr_in *)src;    /* struct copy */
 
93
                ssz = sizeof(struct sockaddr_in);
 
94
                break;
 
95
        case AF_INET6:
 
96
                sa.sin6 = *(struct sockaddr_in6 *)src;  /* struct copy */
 
97
                ssz = sizeof(struct sockaddr_in6);
 
98
                break;
 
99
        default:
 
100
                if (cnt > 0)
 
101
                        dst[0] = '\0';
 
102
                return NULL;
 
103
        }
 
104
 
 
105
        sa.sa.sa_family = af;
 
106
 
 
107
        if (WSAAddressToString(&sa.sa, ssz, NULL, dst, &sz) != 0) {
 
108
                if (cnt > 0)
 
109
                        dst[0] = '\0';
 
110
                return NULL;
 
111
        }
 
112
 
 
113
        return dst;
 
114
}
 
115
 
 
116
/* Decode a Win32 error number. */
 
117
const char *
 
118
win32_strerror(int e)
 
119
{
 
120
        static char buffer[4096];
 
121
 
 
122
        if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
 
123
            NULL,
 
124
            e,
 
125
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 
126
            buffer,
 
127
            sizeof(buffer),
 
128
            NULL) == 0) {
 
129
            
 
130
            sprintf(buffer, "Windows error %d", e);
 
131
        }
 
132
 
 
133
        return buffer;
 
134
}
 
135
 
 
136
#if defined(_MSC_VER) /*[*/
 
137
 
 
138
/* MinGW has gettimofday(), but MSVC does not. */
 
139
 
 
140
#include <windows.h>
 
141
#define SECS_BETWEEN_EPOCHS     11644473600ULL
 
142
#define SECS_TO_100NS           10000000ULL /* 10^7 */
 
143
 
 
144
int
 
145
gettimeofday(struct timeval *tv, void *ignored)
 
146
{
 
147
        FILETIME t;
 
148
        ULARGE_INTEGER u;
 
149
 
 
150
        GetSystemTimeAsFileTime(&t);
 
151
        memcpy(&u, &t, sizeof(ULARGE_INTEGER));
 
152
 
 
153
        /* Isolate seconds and move epochs. */
 
154
        tv->tv_sec = (DWORD)((u.QuadPart / SECS_TO_100NS) -
 
155
                                SECS_BETWEEN_EPOCHS);
 
156
        tv->tv_usec = (u.QuadPart % SECS_TO_100NS) / 10ULL;
 
157
        return 0;
 
158
}
 
159
 
 
160
/* MinGW has getopt(), but MSVC does not. */
 
161
 
 
162
char *optarg;
 
163
int optind = 1, opterr = 1, optopt;
 
164
static const char *nextchar = NULL;
 
165
 
 
166
int
 
167
getopt(int argc, char * const argv[], const char *optstring)
 
168
{
 
169
        char c;
 
170
        const char *s;
 
171
 
 
172
        if (optind == 1)
 
173
                nextchar = argv[optind++];
 
174
 
 
175
        do {
 
176
                if (nextchar == argv[optind - 1]) {
 
177
                        if (optind > argc) {
 
178
                                --optind; /* went too far */
 
179
                                return -1;
 
180
                        }
 
181
                        if (nextchar == NULL) {
 
182
                                --optind; /* went too far */
 
183
                                return -1;
 
184
                        }
 
185
                        if (!strcmp(nextchar, "--"))
 
186
                                return -1;
 
187
                        if (*nextchar++ != '-') {
 
188
                                --optind;
 
189
                                return -1;
 
190
                        }
 
191
                }
 
192
 
 
193
                if ((c = *nextchar++) == '\0')
 
194
                        nextchar = argv[optind++];
 
195
        } while (nextchar == argv[optind - 1]);
 
196
 
 
197
        s = strchr(optstring, c);
 
198
        if (s == NULL) {
 
199
                if (opterr)
 
200
                        fprintf(stderr, "Unknown option '%c'\n", c);
 
201
                return '?';
 
202
        }
 
203
        if (*(s + 1) == ':') {
 
204
                if (*nextchar) {
 
205
                        optarg = (char *)nextchar;
 
206
                        nextchar = argv[optind++];
 
207
                        return c;
 
208
                } else if (optind < argc && argv[optind] != NULL) {
 
209
                        optarg = (char *)argv[optind++];
 
210
                        nextchar = argv[optind++];
 
211
                        return c;
 
212
                } else {
 
213
                        if (opterr)
 
214
                                fprintf(stderr, "Missing value after '%c'\n",
 
215
                                        c);
 
216
                        return -1;
 
217
                }
 
218
        } else
 
219
                return c;
 
220
}
 
221
 
 
222
#endif /*]*/