-
Notifications
You must be signed in to change notification settings - Fork 0
/
montyinter.c
71 lines (63 loc) · 1.23 KB
/
montyinter.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "monty.h"
#define _GNU_SOURCE
/**
* monty_inter - fn resembles monty interpreter
* @av: arg array
* Return: 0 upon success
*/
void monty_inter(char **av)
{
char *spec_file = av[1], *buf = NULL, **tokens = NULL;
size_t len = 0, lineno = 1;
ssize_t readline;
stack_t *structstack = NULL;
FILE *open_f;
open_f = fopen(spec_file, "r");
if (open_f == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", spec_file);
exit(EXIT_FAILURE);
}
for (lineno = 1; (readline = getline(&buf, &len, open_f)) != -1; lineno++)
{
line_token(&buf, &tokens, readline);
if (tokens != NULL)
instructchoose(&structstack, &tokens, lineno);
freetokens(&tokens);
}
fclose(open_f);
if (buf != NULL)
free(buf);
free_structstack(structstack);
}
/**
* freetokens - function that frees the tokens
* @tokens: to be freed
* Return: 0
*/
void freetokens(char ***tokens)
{
if (*tokens != NULL)
{
free(*tokens);
*tokens = NULL;
}
}
/**
* free_structstack - frees the stack
* @structstack: stack to free
* Return: Nothing
*/
void free_structstack(stack_t *structstack)
{
stack_t *new;
if (structstack != NULL)
{
while (structstack != NULL)
{
new = structstack->next;
free(structstack);
structstack = new;
}
}
}