1 |
/* |
2 |
* Copyright (C) 2005 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. 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 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: misc.c,v 1.2 2005/05/07 02:13:29 debug Exp $ |
29 |
*/ |
30 |
|
31 |
#include <stdio.h> |
32 |
#include <stdlib.h> |
33 |
#include <fcntl.h> |
34 |
|
35 |
#include "misc.h" |
36 |
|
37 |
|
38 |
/* |
39 |
* mystrtoull(): |
40 |
* |
41 |
* This function is used on OSes that don't have strtoull() in libc. |
42 |
*/ |
43 |
unsigned long long mystrtoull(const char *s, char **endp, int base) |
44 |
{ |
45 |
unsigned long long res = 0; |
46 |
int minus_sign = 0; |
47 |
|
48 |
if (s == NULL) |
49 |
return 0; |
50 |
|
51 |
/* TODO: Implement endp? */ |
52 |
if (endp != NULL) { |
53 |
fprintf(stderr, "mystrtoull(): endp isn't implemented\n"); |
54 |
exit(1); |
55 |
} |
56 |
|
57 |
if (s[0] == '-') { |
58 |
minus_sign = 1; |
59 |
s++; |
60 |
} |
61 |
|
62 |
/* Guess base: */ |
63 |
if (base == 0) { |
64 |
if (s[0] == '0') { |
65 |
/* Just "0"? :-) */ |
66 |
if (!s[1]) |
67 |
return 0; |
68 |
if (s[1] == 'x' || s[1] == 'X') { |
69 |
base = 16; |
70 |
s += 2; |
71 |
} else { |
72 |
base = 8; |
73 |
s ++; |
74 |
} |
75 |
} else if (s[0] >= '1' && s[0] <= '9') |
76 |
base = 10; |
77 |
} |
78 |
|
79 |
while (s[0]) { |
80 |
int c = s[0]; |
81 |
if (c >= '0' && c <= '9') |
82 |
c -= '0'; |
83 |
else if (c >= 'a' && c <= 'f') |
84 |
c = c - 'a' + 10; |
85 |
else if (c >= 'A' && c <= 'F') |
86 |
c = c - 'A' + 10; |
87 |
else |
88 |
break; |
89 |
switch (base) { |
90 |
case 8: res = (res << 3) | c; |
91 |
break; |
92 |
case 16:res = (res << 4) | c; |
93 |
break; |
94 |
default:res = (res * base) + c; |
95 |
} |
96 |
s++; |
97 |
} |
98 |
|
99 |
if (minus_sign) |
100 |
res = (uint64_t) -(int64_t)res; |
101 |
return res; |
102 |
} |
103 |
|
104 |
|
105 |
/* |
106 |
* mymkstemp(): |
107 |
* |
108 |
* mkstemp() replacement for systems that lack that function. This is NOT |
109 |
* really safe, but should at least allow the emulator to build and run. |
110 |
*/ |
111 |
int mymkstemp(char *template) |
112 |
{ |
113 |
int h = 0; |
114 |
char *p = template; |
115 |
|
116 |
while (*p) { |
117 |
if (*p == 'X') |
118 |
*p = 48 + random() % 10; |
119 |
p++; |
120 |
} |
121 |
|
122 |
h = open(template, O_RDWR, 0600); |
123 |
return h; |
124 |
} |
125 |
|