-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
75 lines (68 loc) · 1.2 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "monty.h"
/**
* error_usage - prints usage message and exits
*
* Return: nothing
*/
void error_usage(void)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
/**
* file_error - prints file error message and exits
* @argv: argv given by manin
*
* Return: nothing
*/
void file_error(char *argv)
{
fprintf(stderr, "Error: Can't open file %s\n", argv);
exit(EXIT_FAILURE);
}
int status = 0;
/**
* main - entry point
* @argv: list of arguments passed to our program
* @argc: ammount of args
*
* Return: nothing
*/
int main(int argc, char **argv)
{
FILE *file;
size_t buf_len = 0;
char *buffer = NULL;
char *str = NULL;
stack_t *stack = NULL;
unsigned int line_cnt = 1;
global.data_struct = 1;
if (argc != 2)
error_usage();
file = fopen(argv[1], "r");
if (!file)
file_error(argv[1]);
while (getline(&buffer, &buf_len, file) != -1)
{
if (status)
break;
if (*buffer == '\n')
{
line_cnt++;
continue;
}
str = strtok(buffer, " \t\n");
if (!str || *str == '#')
{
line_cnt++;
continue;
}
global.argument = strtok(NULL, " \t\n");
_opcode(&stack, str, line_cnt);
line_cnt++;
}
free(buffer);
free_stack(stack);
fclose(file);
exit(status);
}