-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_funcs2.c
140 lines (126 loc) · 3.06 KB
/
handler_funcs2.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "monty.h"
#include "lists.h"
/**
* sub_handler - handles the sub instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void sub_handler(stack_t **stack, unsigned int line_number)
{
int sub = 0;
stack_t *node = NULL;
stack_t *node_0 = get_dnodeint_at_index(*stack, 0);
stack_t *node_1 = get_dnodeint_at_index(*stack, 1);
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, SUB_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
sub = node_1->n - node_0->n;
delete_dnodeint_at_index(stack, 0);
delete_dnodeint_at_index(stack, 0);
node = add_dnodeint(stack, sub);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* div_handler - handles the div instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void div_handler(stack_t **stack, unsigned int line_number)
{
int div = 0;
stack_t *node = NULL;
stack_t *node_0 = get_dnodeint_at_index(*stack, 0);
stack_t *node_1 = get_dnodeint_at_index(*stack, 1);
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, DIV_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
if (node_0->n == 0)
{
dprintf(STDERR_FILENO, DIV_ZERO, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
div = node_1->n / node_0->n;
delete_dnodeint_at_index(stack, 0);
delete_dnodeint_at_index(stack, 0);
node = add_dnodeint(stack, div);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* mul_handler - handles the mul instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void mul_handler(stack_t **stack, unsigned int line_number)
{
int mul = 0;
stack_t *node = NULL;
stack_t *node_0 = get_dnodeint_at_index(*stack, 0);
stack_t *node_1 = get_dnodeint_at_index(*stack, 1);
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, MUL_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
mul = node_1->n * node_0->n;
delete_dnodeint_at_index(stack, 0);
delete_dnodeint_at_index(stack, 0);
node = add_dnodeint(stack, mul);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* mod_handler - handles the mod instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void mod_handler(stack_t **stack, unsigned int line_number)
{
int mod = 0;
stack_t *node = NULL;
stack_t *node_0 = get_dnodeint_at_index(*stack, 0);
stack_t *node_1 = get_dnodeint_at_index(*stack, 1);
if (dlistint_len(*stack) < 2)
{
dprintf(STDERR_FILENO, MOD_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
if (node_0->n == 0)
{
dprintf(STDERR_FILENO, DIV_ZERO, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
mod = node_1->n % node_0->n;
delete_dnodeint_at_index(stack, 0);
delete_dnodeint_at_index(stack, 0);
node = add_dnodeint(stack, mod);
if (!node)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}