-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode_functs1.c
More file actions
110 lines (99 loc) · 2.04 KB
/
Copy pathopcode_functs1.c
File metadata and controls
110 lines (99 loc) · 2.04 KB
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
#include "monty.h"
/**
* _push - pushes an element to the stack
*
* @stack: pointer to the top pointer of the stack
* @line_number: line number of the code
*
* Return: void
*/
void _push(stack_t **stack, unsigned int line_number)
{
int number, i;
line_number = gvar.ln;
if (!gvar.argv[1])
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_command(gvar.argv);
free_dlistint(*stack);
fclose(gvar.fd);
exit(EXIT_FAILURE);
}
for (i = 0; gvar.argv[1][i] != '\0'; i++)
{
if (!isdigit(gvar.argv[1][i]) && gvar.argv[1][0] != 45)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_command(gvar.argv);
free_dlistint(*stack);
fclose(gvar.fd);
exit(EXIT_FAILURE);
}
}
number = atoi(gvar.argv[1]);
add_dnodeint(stack, number);
}
/**
* _pall - prints all the values on the stack
*
* @stack: pointer to the top pointer of the stack
* @line_number: line number of the code
*
* Return: void
*/
void _pall(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;
(void)line_number;
if (*stack)
{
while (tmp != NULL)
{
printf("%d\n", tmp->n);
tmp = tmp->next;
}
}
}
/**
* _pint - prints the value at the top of the stack
*
* @stack: pointer to the top pointer of the stack
* @line_number: line number of the code
*
* Return: void
*/
void _pint(stack_t **stack, unsigned int line_number)
{
line_number = gvar.ln;
if (!(*stack))
{
fprintf(stderr, "L%d: can't pint, stack empty\n", line_number);
free_command(gvar.argv);
fclose(gvar.fd);
exit(EXIT_FAILURE);
}
printf("%d\n", (*stack)->n);
}
/**
* _pop - removes the top element of the stack
*
* @stack: pointer to the top pointer of the stack
* @line_number: line number of the code
*
* Return: void
*/
void _pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
line_number = gvar.ln;
if (!(*stack))
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
free_command(gvar.argv);
fclose(gvar.fd);
exit(EXIT_FAILURE);
}
temp = *stack;
*stack = (*stack)->next;
free(temp);
}