-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
47 lines (44 loc) · 991 Bytes
/
main.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
#include "monty.h"
/* global struct to hold flag for queue and stack length */
var_t var;
/**
* main - Monty bytecode interpreter
* @argc: number of arguments passed
* @argv: array of argument strings
*
* Return: EXIT_SUCCESS on success or EXIT_FAILURE on failure
*/
int main(int argc, char *argv[])
{
stack_t *stack = NULL;
unsigned int line_number = 0;
FILE *fs = NULL;
char *lineptr = NULL, *op = NULL;
size_t n = 0;
var.queue = 0;
var.stack_len = 0;
if (argc != 2)
{
dprintf(STDOUT_FILENO, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fs = fopen(argv[1], "r");
if (fs == NULL)
{
dprintf(STDOUT_FILENO, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
on_exit(free_lineptr, &lineptr);
on_exit(free_stack, &stack);
on_exit(m_fs_close, fs);
while (getline(&lineptr, &n, fs) != -1)
{
line_number++;
op = strtok(lineptr, "\n\t\r ");
if (op != NULL && op[0] != '#')
{
get_op(op, &stack, line_number);
}
}
exit(EXIT_SUCCESS);
}