ReactOS 0.4.16-dev-197-g92996da
common.c
Go to the documentation of this file.
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
32typedef struct _link {
33 void *data;
34 struct _link *next;
36
37#ifdef __REACTOS__
38DECLSPEC_NORETURN // __attribute((noreturn))
39void exit(int exitcode)
40{
41 // DbgBreakPoint();
43
44 /* Should never get here */
45 while(1);
46}
47
48DECLSPEC_NORETURN // __attribute((noreturn))
49void die_func(const char *msg, ...) // die
50#else
51void die(const char *msg, ...)
52#endif
53{
55
57#ifndef __REACTOS__
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__
71DECLSPEC_NORETURN // __attribute((noreturn))
72void pdie_func(const char *msg, ...) // pdie
73#else
74void pdie(const char *msg, ...)
75#endif
76{
78
80#ifndef __REACTOS__
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__
94void *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
104void *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
119void *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
133void vffree(void *ptr)
134{
135 RtlFreeHeap(RtlGetProcessHeap(), 0, ptr);
136}
137#endif
138
139void *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
157void 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
179int min(int a, int b)
180{
181 return a < b ? a : b;
182}
183
184char 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}
char * strchr(const char *String, int ch)
Definition: utclib.c:501
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define msg(x)
Definition: auth_time.c:54
#define DPRINT1
Definition: precomp.h:8
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
struct _root root
const WCHAR * link
Definition: db.cpp:997
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define printf
Definition: freeldr.h:97
BOOLEAN valid
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizeiptr size
Definition: glext.h:5919
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define stdout
Definition: stdio.h:99
#define EOF
Definition: stdio.h:24
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ _CRTIMP int __cdecl getchar(void)
Definition: file.c:3629
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define a
Definition: ke_i.h:78
#define b
Definition: ke_i.h:79
#define die(str)
Definition: mkdosfs.c:347
static PVOID ptr
Definition: dispmode.c:27
const char * strerror(int err)
Definition: compat_str.c:23
#define min(a, b)
Definition: monoChain.cc:55
NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, LONG ExitStatus)
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define DECLSPEC_NORETURN
Definition: ntbasedef.h:176
static unsigned __int64 next
Definition: rand_nt.c:6
#define alloc
Definition: rosglue.h:13
#define errno
Definition: errno.h:18
#define exit(n)
Definition: config.h:202
void * qalloc(void **root, int size)
Definition: common.c:139
void qfree(void **root)
Definition: common.c:157
void pdie(const char *msg,...)
Definition: common.c:74
struct _link LINK
char get_key(const char *valid, const char *prompt)
Definition: common.c:184
#define args
Definition: format.c:66
Definition: match.c:390
VOID VfatPrintV(PCHAR Format, va_list args)
Definition: vfatlib.c:342