-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.c
50 lines (45 loc) · 901 Bytes
/
push.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
#include "monty.h"
/**
* check_digit - check if string is a number
* @param: param to push
* @line_num: current line number
*
* Return: value of param
*/
int check_digit(char *param, unsigned int line_num)
{
int num;
num = atoi(param);
if (num == 0 && strcmp(param, "0") != 0)
{
dprintf(2, "L%u: usage: push integer\n", line_num);
free_all();
exit(EXIT_FAILURE);
}
return (num);
}
/**
* push - it adds element to the top of the stack
*
* @node: pointer to head of stack
* @line_num: current line number
*/
void push(stack_t **node, unsigned int line_num)
{
int num;
char *param;
param = strtok(NULL, SEPARATORS);
if (!param)
{
dprintf(2, "L%u: usage: push integer\n", line_num);
free_all();
exit(EXIT_FAILURE);
}
num = check_digit(param, line_num);
if (!add_node(node, num))
{
dprintf(2, "Error: malloc failed\n");
free_all();
exit(EXIT_SUCCESS);
}
}