1 |
dpavlin |
6 |
/* |
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 |
dpavlin |
10 |
* $Id: misc.c,v 1.3 2005/06/20 05:52:47 debug Exp $ |
29 |
dpavlin |
6 |
*/ |
30 |
|
|
|
31 |
|
|
#include <stdio.h> |
32 |
|
|
#include <stdlib.h> |
33 |
dpavlin |
10 |
#include <string.h> |
34 |
dpavlin |
6 |
#include <fcntl.h> |
35 |
|
|
|
36 |
|
|
#include "misc.h" |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
/* |
40 |
|
|
* mystrtoull(): |
41 |
|
|
* |
42 |
|
|
* This function is used on OSes that don't have strtoull() in libc. |
43 |
|
|
*/ |
44 |
|
|
unsigned long long mystrtoull(const char *s, char **endp, int base) |
45 |
|
|
{ |
46 |
|
|
unsigned long long res = 0; |
47 |
|
|
int minus_sign = 0; |
48 |
|
|
|
49 |
|
|
if (s == NULL) |
50 |
|
|
return 0; |
51 |
|
|
|
52 |
|
|
/* TODO: Implement endp? */ |
53 |
|
|
if (endp != NULL) { |
54 |
|
|
fprintf(stderr, "mystrtoull(): endp isn't implemented\n"); |
55 |
|
|
exit(1); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
if (s[0] == '-') { |
59 |
|
|
minus_sign = 1; |
60 |
|
|
s++; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/* Guess base: */ |
64 |
|
|
if (base == 0) { |
65 |
|
|
if (s[0] == '0') { |
66 |
|
|
/* Just "0"? :-) */ |
67 |
|
|
if (!s[1]) |
68 |
|
|
return 0; |
69 |
|
|
if (s[1] == 'x' || s[1] == 'X') { |
70 |
|
|
base = 16; |
71 |
|
|
s += 2; |
72 |
|
|
} else { |
73 |
|
|
base = 8; |
74 |
|
|
s ++; |
75 |
|
|
} |
76 |
|
|
} else if (s[0] >= '1' && s[0] <= '9') |
77 |
|
|
base = 10; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
while (s[0]) { |
81 |
|
|
int c = s[0]; |
82 |
|
|
if (c >= '0' && c <= '9') |
83 |
|
|
c -= '0'; |
84 |
|
|
else if (c >= 'a' && c <= 'f') |
85 |
|
|
c = c - 'a' + 10; |
86 |
|
|
else if (c >= 'A' && c <= 'F') |
87 |
|
|
c = c - 'A' + 10; |
88 |
|
|
else |
89 |
|
|
break; |
90 |
|
|
switch (base) { |
91 |
|
|
case 8: res = (res << 3) | c; |
92 |
|
|
break; |
93 |
|
|
case 16:res = (res << 4) | c; |
94 |
|
|
break; |
95 |
|
|
default:res = (res * base) + c; |
96 |
|
|
} |
97 |
|
|
s++; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
if (minus_sign) |
101 |
|
|
res = (uint64_t) -(int64_t)res; |
102 |
|
|
return res; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
/* |
107 |
|
|
* mymkstemp(): |
108 |
|
|
* |
109 |
|
|
* mkstemp() replacement for systems that lack that function. This is NOT |
110 |
|
|
* really safe, but should at least allow the emulator to build and run. |
111 |
|
|
*/ |
112 |
|
|
int mymkstemp(char *template) |
113 |
|
|
{ |
114 |
|
|
int h = 0; |
115 |
|
|
char *p = template; |
116 |
|
|
|
117 |
|
|
while (*p) { |
118 |
|
|
if (*p == 'X') |
119 |
|
|
*p = 48 + random() % 10; |
120 |
|
|
p++; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
h = open(template, O_RDWR, 0600); |
124 |
|
|
return h; |
125 |
|
|
} |
126 |
|
|
|
127 |
dpavlin |
10 |
|
128 |
|
|
#ifdef USE_STRLCPY_REPLACEMENTS |
129 |
|
|
/* |
130 |
|
|
* mystrlcpy(): |
131 |
|
|
* |
132 |
|
|
* Quick hack strlcpy() replacement for systems that lack that function. |
133 |
|
|
* NOTE: No length checking is done. |
134 |
|
|
*/ |
135 |
|
|
size_t mystrlcpy(char *dst, const char *src, size_t size) |
136 |
|
|
{ |
137 |
|
|
strcpy(dst, src); |
138 |
|
|
return strlen(src); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
/* |
143 |
|
|
* mystrlcat(): |
144 |
|
|
* |
145 |
|
|
* Quick hack strlcat() replacement for systems that lack that function. |
146 |
|
|
* NOTE: No length checking is done. |
147 |
|
|
*/ |
148 |
|
|
size_t mystrlcat(char *dst, const char *src, size_t size) |
149 |
|
|
{ |
150 |
|
|
size_t orig_dst_len = strlen(dst); |
151 |
|
|
strcat(dst, src); |
152 |
|
|
return strlen(src) + orig_dst_len; |
153 |
|
|
} |
154 |
|
|
#endif |
155 |
|
|
|