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

« back to all changes in this revision

Viewing changes to wpr3287/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
1
/*
2
 
 * Copyright 2007 by Paul Mattes.
3
 
 *  Permission to use, copy, modify, and distribute this software and its
4
 
 *  documentation for any purpose and without fee is hereby granted,
5
 
 *  provided that the above copyright notice appear in all copies and that
6
 
 *  both that copyright notice and this permission notice appear in
7
 
 *  supporting documentation.
8
 
 *
9
 
 * wc3270 and wpr3287 are distributed in the hope that they will be useful, but
10
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the file LICENSE for more details.
 
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.
12
26
 */
13
27
 
14
28
/*
29
43
 
30
44
#include "w3miscc.h"
31
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
 
32
76
/* Convert a network address to a string. */
33
77
const char *
34
78
inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
88
132
 
89
133
        return buffer;
90
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 /*]*/