-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_push.c
55 lines (47 loc) · 995 Bytes
/
op_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
51
52
53
54
55
#include "monty.h"
/**
* op_push - pushes an element onto the stack
* @stack: pointer to the top of the stack
* @line_number: current line number of the opcode in the Monty file
*/
void op_push(stack_t **stack, unsigned int line_number)
{
int n;
if (glob.arg == NULL || !is_number(glob.arg))
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*stack);
fclose(glob.file);
free(glob.line);
exit(EXIT_FAILURE);
}
n = atoi(glob.arg);
if (add_node(stack, n) == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
free_stack(*stack);
fclose(glob.file);
free(glob.line);
exit(EXIT_FAILURE);
}
}
/**
* is_number - checks if a string is a number
* @str: string to check
*
* Return: 1 if string is a number, 0 otherwise
*/
int is_number(char *str)
{
if (str == NULL || *str == '\0')
return (0);
if (*str == '-' || *str == '+')
str++;
while (*str != '\0')
{
if (*str < '0' || *str > '9')
return (0);
str++;
}
return (1);
}