-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty.c
57 lines (51 loc) · 1015 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
52
53
54
55
56
57
#include "monty.h"
FILE *file;
stack_t *top = NULL;
char **tokens = NULL;
char *line = NULL;
unsigned int line_number = 0;
/**
* main - the main entry of the monty program
* @argv: argument vector
* @argc: argument count
* Return: 0 on success, negative on failure
*/
int main(int argc, char **argv)
{
size_t len = 0;
ssize_t nread;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
tokens = malloc(TOK_SIZE * sizeof(char *));
check_malloc(tokens);
while ((nread = getline(&line, &len, file)) != -1)
{
printf("line %d: %s\n", line_number, line);
if (line == NULL)
{
fail_exit(0);
}
tokens = tokenize_line(line);
if (tokens == NULL)
return (0);
run_monty(tokens);
/**
for (i = 0; i < 3; i++)
{
printf(" command: %s\n", tokens[i]);
}
*/
line_number++;
}
fclose(file);
return (0);
}