1 /* common.c - Common functions
2
3 Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4 Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 The complete text of the GNU General Public License
21 can be found in /usr/share/common-licenses/GPL-3 file.
22 */
23
24 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
25 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
26
27 #include "vfatlib.h"
28
29 #define NDEBUG
30 #include <debug.h>
31
32 typedef struct _link {
33 void *data;
34 struct _link *next;
35 } LINK;
36
37 #ifdef __REACTOS__
38 DECLSPEC_NORETURN // __attribute((noreturn))
exit(int exitcode)39 void exit(int exitcode)
40 {
41 // DbgBreakPoint();
42 NtTerminateProcess(NtCurrentProcess(), exitcode);
43
44 /* Should never get here */
45 while(1);
46 }
47
48 DECLSPEC_NORETURN // __attribute((noreturn))
die_func(const char * msg,...)49 void die_func(const char *msg, ...) // die
50 #else
51 void die(const char *msg, ...)
52 #endif
53 {
54 va_list args;
55
56 va_start(args, msg);
57 #ifndef __REACTOS__
58 vfprintf(stderr, msg, args);
59 #else
60 DPRINT1("Unrecoverable problem!\n");
61 VfatPrintV((char*)msg, args);
62 #endif
63 va_end(args);
64 #ifndef __REACTOS__
65 fprintf(stderr, "\n");
66 #endif
67 exit(1);
68 }
69
70 #ifdef __REACTOS__
71 DECLSPEC_NORETURN // __attribute((noreturn))
pdie_func(const char * msg,...)72 void pdie_func(const char *msg, ...) // pdie
73 #else
74 void pdie(const char *msg, ...)
75 #endif
76 {
77 va_list args;
78
79 va_start(args, msg);
80 #ifndef __REACTOS__
81 vfprintf(stderr, msg, args);
82 #else
83 DPRINT1("Unrecoverable problem!\n");
84 VfatPrintV((char*)msg, args);
85 #endif
86 va_end(args);
87 #ifndef __REACTOS__
88 fprintf(stderr, ":%s\n", strerror(errno));
89 #endif
90 exit(1);
91 }
92
93 #ifndef __REACTOS__
alloc(int size)94 void *alloc(int size)
95 {
96 void *this;
97
98 if ((this = malloc(size)))
99 return this;
100 pdie("malloc");
101 return NULL; /* for GCC */
102 }
103 #else
vfalloc(int size)104 void *vfalloc(int size)
105 {
106 void *ptr;
107
108 ptr = RtlAllocateHeap(RtlGetProcessHeap(), 0, size);
109 if (ptr == NULL)
110 {
111 DPRINT1("Allocation failed!\n");
112 pdie("malloc");
113 return NULL;
114 }
115
116 return ptr;
117 }
118
vfcalloc(int size,int count)119 void *vfcalloc(int size, int count)
120 {
121 void *ptr;
122
123 ptr = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, size * count);
124 if (ptr == NULL)
125 {
126 DPRINT1("Allocation failed!\n");
127 return NULL;
128 }
129
130 return ptr;
131 }
132
vffree(void * ptr)133 void vffree(void *ptr)
134 {
135 RtlFreeHeap(RtlGetProcessHeap(), 0, ptr);
136 }
137 #endif
138
qalloc(void ** root,int size)139 void *qalloc(void **root, int size)
140 {
141 LINK *link;
142
143 #ifndef __REACTOS__
144 link = alloc(sizeof(LINK));
145 #else
146 link = vfalloc(sizeof(LINK));
147 #endif
148 link->next = *root;
149 *root = link;
150 #ifndef __REACTOS__
151 return link->data = alloc(size);
152 #else
153 return link->data = vfalloc(size);
154 #endif
155 }
156
qfree(void ** root)157 void qfree(void **root)
158 {
159 LINK *this;
160
161 while (*root) {
162 this = (LINK *) * root;
163 *root = this->next;
164 #ifndef __REACTOS__
165 free(this->data);
166 free(this);
167 #else
168 vffree(this->data);
169 vffree(this);
170 #endif
171 }
172 }
173
174 #ifdef __REACTOS__
175 #ifdef min
176 #undef min
177 #endif
178 #endif
min(int a,int b)179 int min(int a, int b)
180 {
181 return a < b ? a : b;
182 }
183
get_key(const char * valid,const char * prompt)184 char get_key(const char *valid, const char *prompt)
185 {
186 #ifndef __REACTOS__
187 int ch, okay;
188
189 while (1) {
190 if (prompt)
191 printf("%s ", prompt);
192 fflush(stdout);
193 while (ch = getchar(), ch == ' ' || ch == '\t') ;
194 if (ch == EOF)
195 exit(1);
196 if (!strchr(valid, okay = ch))
197 okay = 0;
198 while (ch = getchar(), ch != '\n' && ch != EOF) ;
199 if (ch == EOF)
200 exit(1);
201 if (okay)
202 return okay;
203 printf("Invalid input.\n");
204 }
205 #else
206 return 0;
207 #endif
208 }
209