-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonty.c
51 lines (46 loc) · 977 Bytes
/
monty.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
#include "monty.h"
/**
* main - main function of monty program
* @argc: number of arguments
* @argv: pointer to array of strings of arguments
* Return: 0 on success, -1 on failure
*/
int main(int argc, char *argv[])
{
FILE *fp;
stack_t *stack = NULL;
char *line = NULL;
char *opcode;
char *n;
unsigned int line_number;
size_t len = 0;
ssize_t read;
if (argc != 2)
{
printf("USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
line_number = 0;
while ((read = getline(&line, &len, fp)) != -1)
{
line_number++;
opcode = strtok(line, DELIMITERS);
if (opcode == NULL || strncmp(opcode, "#", 1) == 0)
continue;
if (strcmp(opcode, "push") == 0)
{
n = strtok(NULL, DELIMITERS);
push(&stack, line_number, n);
}
else
opcode_struct(opcode, &stack, line_number);
}
free_all(stack, line, fp);
return (EXIT_SUCCESS);
}