-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
76 lines (67 loc) · 1.09 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "utils.h"
bool debug_printing;
void exit_error(const char* msg)
{
fprintf(stderr, "ERROR: %s\n", msg);
exit(EXIT_FAILURE);
}
void* malloc_wrapper(size_t size)
{
assert(size != 0);
void* temp = malloc(size);
assert(temp != NULL);
return temp;
}
void free_wrapper(void* ptr)
{
assert(ptr != NULL);
free(ptr);
return;
}
int debug_printf(const char* restrict format, ...)
{
if(!debug_printing)
return 0;
fprintf(stderr, "DEBUG: ");
va_list args;
va_start(args, format);
int temp = vfprintf(stderr, format, args);
va_end(args);
return temp;
}
void print_hex(const void* ptr, unsigned int size)
{
for(unsigned int i = 0; i < size; ++i)
{
const unsigned char* c_ptr = (const unsigned char*)ptr;
if(i > 0 && i % 16 == 0)
{
printf("\n");
}
printf("%02x ", c_ptr[i]);
}
printf("\n");
return;
}
int imin(int a, int b)
{
if(a < b)
{
return a;
}
return b;
}
unsigned int umin(unsigned int a, unsigned int b)
{
if(a < b)
{
return a;
}
return b;
}