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