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