-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathport_win.c
More file actions
126 lines (107 loc) · 2.09 KB
/
port_win.c
File metadata and controls
126 lines (107 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2008 The Native Client Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Misc functions missing from windows.
*/
#include <stdio.h>
#include <string.h>
#include "native_client/src/include/portability.h"
#if ( DO_NOT_USE_FAST_ASSEMBLER_VERSION_FOR_FFS || defined(_WIN64) || !defined(_MSC_VER) )
int ffs(int x) {
int r = 1;
if (!x) {
return 0;
}
if (!(x & 0xffff)) {
/* case 1 */
x >>= 16;
r += 16;
}
if (!(x & 0xff)) {
/* case 2 */
x >>= 8;
r += 8;
}
if (!(x & 0xf)) {
/* case 3 */
x >>= 4;
r += 4;
}
if (!(x & 3)) {
/* case 4 */
x >>= 2;
r += 2;
}
if (!(x & 1)) {
/* case 5 */
x >>= 1;
r += 1;
}
return r;
}
#else
int ffs(int v) {
uint32_t rv;
if (!v) return 0;
__asm {
bsf eax, v;
mov rv, eax;
}
return rv + 1;
}
#endif
/*
* Based on code by Hans Dietrich
* see http://www.codeproject.com/KB/cpp/xgetopt.aspx
*/
char *optarg; /* global argument pointer */
int optind = 0; /* global argv index */
int getopt(int argc, char *argv[], const char *optstring) {
char c, *cp;
static char *next = NULL;
if (optind == 0)
next = NULL;
optarg = NULL;
if (next == NULL || *next == ('\0')) {
if (optind == 0)
optind++;
if (optind >= argc ||
argv[optind][0] != ('-') ||
argv[optind][1] == ('\0')) {
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
if (strcmp(argv[optind], ("--")) == 0) {
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
next = argv[optind];
next++; /* skip past - */
optind++;
}
c = *next++;
cp = strchr(optstring, c);
if (cp == NULL || c == (':'))
return ('?');
cp++;
if (*cp == (':')) {
if (*next != ('\0')) {
optarg = next;
next = NULL;
} else if (optind < argc) {
optarg = argv[optind];
optind++;
} else {
return ('?');
}
}
return c;
}