-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handling.c
109 lines (102 loc) · 2.22 KB
/
error_handling.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
#include "monty.h"
/**
* error_mesg - Prints appropiate error messages
* determined by their error code.
*
* @error_num: The error codes are the following:
*
* (1) => The user does not give any file or more
* than one file to the path
*
* (2) => The file provided isn't a file that can be opened or read
*
* (3) => The file provided contains an invalid given instruction
*
* (4) => When the program is unable to malloc more memory anymore
*
* (5) => When the parameter passed to the instruction
* "push" isn't int.
*
* (6) => When the stack is empty for pint.
*
* (7) => When the stack is empty for pop.
*
* (8) => When stack is too short to make operation on it
*
*/
void error_mesg(int error_num, ...)
{
va_list args;
char *op;
int l_num;
va_start(args, error_num);
switch (error_num)
{
case 1:
fprintf(stderr, "USAGE: monty file\n");
break;
case 2:
fprintf(stderr, "Error: Can't open file %s\n",
va_arg(args, char *));
break;
case 3:
l_num = va_arg(args, int);
op = va_arg(args, 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(args, int));
break;
default:
break;
}
freeNodes();
exit(EXIT_FAILURE);
}
/**
* AdvErrors - A function that handles the advanced errors.
*
* @error_num: The error codes are the following:
*
* (6) => When the stack is empty for pint.
*
* (7) => When the stack is empty for pop.
*
* (8) => When stack is too short
*
* (9) => when Division by zero happens
*/
void AdvErrors(int error_num, ...)
{
va_list args;
char *op;
int l_num;
va_start(args, error_num);
switch (error_num)
{
case 6:
fprintf(stderr, "L%d: can't pint, stack empty\n",
va_arg(args, int));
break;
case 7:
fprintf(stderr, "L%d: can't pop an empty stack\n",
va_arg(args, int));
break;
case 8:
l_num = va_arg(args, unsigned int);
op = va_arg(args, 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(args, unsigned int));
break;
default:
break;
}
freeNodes();
exit(EXIT_FAILURE);
}