ReactOS  0.4.12-dev-918-g6c6e7b8
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 
32 typedef struct _link {
33  void *data;
34  struct _link *next;
35 } LINK;
36 
37 #ifdef __REACTOS__
38 DECLSPEC_NORETURN // __attribute((noreturn))
39 void exit(int exitcode)
40 {
41  // DbgBreakPoint();
43 
44  /* Should never get here */
45  while(1);
46 }
47 
48 DECLSPEC_NORETURN // __attribute((noreturn))
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__
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))
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__
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__
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
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 
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 
133 void vffree(void *ptr)
134 {
135  RtlFreeHeap(RtlGetProcessHeap(), 0, ptr);
136 }
137 #endif
138 
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 
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
179 int min(int a, int b)
180 {
181  return a < b ? a : b;
182 }
183 
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 }
void qfree(void **root)
Definition: common.c:157
void * alloc(int size)
Definition: common.c:94
VOID VfatPrintV(PCHAR Format, va_list args)
Definition: vfatlib.c:343
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
struct _root root
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
void * qalloc(void **root, int size)
Definition: common.c:139
int errno
Definition: match.c:390
const char * strerror(int err)
Definition: compat_str.c:23
void pdie(const char *msg,...)
Definition: common.c:74
#define DECLSPEC_NORETURN
Definition: ntbasedef.h:176
FILE * stdout
#define va_end(ap)
Definition: acmsvcex.h:90
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define a
Definition: ke_i.h:78
static PVOID ptr
Definition: dispmode.c:27
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
void die(const char *msg,...)
Definition: common.c:51
#define b
Definition: ke_i.h:79
#define NtCurrentProcess()
Definition: nt_native.h:1657
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
int min(int a, int b)
Definition: common.c:179
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLsizeiptr size
Definition: glext.h:5919
char get_key(const char *valid, const char *prompt)
Definition: common.c:184
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
struct _link LINK
_Check_return_ _CRTIMP int __cdecl getchar(void)
Definition: file.c:3627
static unsigned __int64 next
Definition: rand_nt.c:6
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define DPRINT1
Definition: precomp.h:8
#define msg(x)
Definition: auth_time.c:54
#define EOF
Definition: stdio.h:24
FILE * stderr
NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, LONG ExitStatus)
#define malloc
Definition: debug_ros.c:4
const WCHAR * link
Definition: db.cpp:985
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
void exit(int exitcode)
Definition: _exit.c:33
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define args
Definition: format.c:66
#define printf
Definition: config.h:203