misc.cc Source File

Back to the index.

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

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13