-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmain.c
51 lines (48 loc) · 899 Bytes
/
mmain.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"
#include <stdio.h>
#define _GNU_SOURCE
#include <stdlib.h>
bus_t bus = {NULL, NULL, NULL, 0};
/**
* main - function for monty code interpreter
* @argc: argument count
* @argv: argument value
*
* Return: 0 on success
*/
int main(int argc, char *argv[])
{
char *content;
FILE *file;
size_t size = 0;
ssize_t read_line = 1;
stack_t *stack = NULL;
unsigned int counter = 0;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r");
bus.file = file;
if (!file)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while (read_line > 0)
{
content = NULL;
read_line = getline(&content, &size, file);
bus.content = content;
counter++;
if (read_line > 0)
{
execute(content, &stack, counter, file);
}
free(content);
}
free_stack(stack);
fclose(file);
return (0);
}