-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
68 lines (56 loc) · 1.06 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
#define PROG_NAME "asm"
#define MAX_ERRORS 1000
int errors = 0;
void *xmalloc(size_t size)
{
void *ptr;
if ((ptr = malloc(size)) == 0)
{
syserr("cannot allocate memory");
}
return ptr;
}
void *xrealloc(void *ptr, size_t size)
{
if ((ptr = realloc(ptr, size)) == 0)
{
syserr("cannot reallocate memory");
}
return ptr;
}
char *xstrdup(const char *str)
{
char *str2 = strdup(str);
if (str2 == NULL)
{
syserr("cannot allocate memory");
}
return str2;
}
void error(const char *file, int line, const char *msg)
{
fprintf(stderr, "%s:%d: Error: %s\n", file, line, msg);
errors++;
if (errors > MAX_ERRORS)
{
fatal("Too many errors.");
}
}
void fatal(const char *msg)
{
fprintf(stderr, "%s: Fatal error: %s\n", PROG_NAME, msg);
exit(-1);
}
void syserr(const char *msg)
{
perror(msg);
exit(-1);
}
void error_msg(const char *msg1, const char *msg2)
{
fprintf(stderr, "%s: Error: %s %s\n", PROG_NAME, msg1, msg2);
}