-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.c
executable file
·116 lines (110 loc) · 2.76 KB
/
errors.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "monty.h"
/**
* err - Prints appropiate error messages determined by their error code.
* @error_code: The error codes are the following:
* (1) => The user does not give any file or more than one file to the program.
* (2) => The file provided is not a file that can be opened or read.
* (3) => The file provided contains an invalid instruction.
* (4) => When the program is unable to malloc more memory.
* (5) => When the parameter passed to the instruction "push" is not an int.
* (6) => When the stack it empty for pint.
* (7) => When the stack it empty for pop.
* (8) => When stack is too short for operation.
*/
void err(int error_code, ...)
{
va_list ag;
char *op;
int l_num;
va_start(ag, error_code);
switch (error_code)
{
case 1:
fprintf(stderr, "USAGE: monty file\n");
break;
case 2:
fprintf(stderr, "Error: Can't open file %s\n",
va_arg(ag, char *));
break;
case 3:
l_num = va_arg(ag, int);
op = va_arg(ag, char *);
fprintf(stderr, "L%d: unknown instruction %s\n", l_num, op);
break;
case 4:
fprintf(stderr, "Error: malloc failed\n");
break;
case 5:
fprintf(stderr, "L%d: usage: push integer\n", va_arg(ag, int));
break;
default:
break;
}
free_nodes();
exit(EXIT_FAILURE);
}
/**
* more_err - handles errors.
* @error_code: The error codes are the following:
* (6) => When the stack it empty for pint.
* (7) => When the stack it empty for pop.
* (8) => When stack is too short for operation.
* (9) => Division by zero.
*/
void more_err(int error_code, ...)
{
va_list ag;
char *op;
int l_num;
va_start(ag, error_code);
switch (error_code)
{
case 6:
fprintf(stderr, "L%d: can't pint, stack empty\n",
va_arg(ag, int));
break;
case 7:
fprintf(stderr, "L%d: can't pop an empty stack\n",
va_arg(ag, int));
break;
case 8:
l_num = va_arg(ag, unsigned int);
op = va_arg(ag, char *);
fprintf(stderr, "L%d: can't %s, stack too short\n", l_num, op);
break;
case 9:
fprintf(stderr, "L%d: division by zero\n",
va_arg(ag, unsigned int));
break;
default:
break;
}
free_nodes();
exit(EXIT_FAILURE);
}
/**
* string_err - handles errors.
* @error_code: The error codes are the following:
* (10) ~> The number inside a node is outside ASCII bounds.
* (11) ~> The stack is empty.
*/
void string_err(int error_code, ...)
{
va_list ag;
int l_num;
va_start(ag, error_code);
l_num = va_arg(ag, int);
switch (error_code)
{
case 10:
fprintf(stderr, "L%d: can't pchar, value out of range\n", l_num);
break;
case 11:
fprintf(stderr, "L%d: can't pchar, stack empty\n", l_num);
break;
default:
break;
}
free_nodes();
exit(EXIT_FAILURE);
}