-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·63 lines (60 loc) · 1.19 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
#include "monty.h"
/**
* refractor - to initialize in empty data
* @strct: global variable
* Return: (strct)
*/
strct_t refractor(strct_t strct)
{
strct.file = NULL;
strct.line = NULL;
strct.stack = NULL;
strct.line_number = 1;
strct.state = 0;
return (strct);
}
/**
* main - Entry point
* @argc: Number of arguments
* @argv: Pointer to arguments array
* Return: 0 on success or 1 on error
*/
int main(int argc, char **argv)
{
strct_t strct;
char *opcode, *val;
ssize_t read = 0;
size_t len = 0;
strct = refractor(strct);
if (argc != 2)
{
dprintf(STDERR_FILENO, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
strct.file = fopen(argv[1], "r");
if (!strct.file)
{
dprintf(STDERR_FILENO, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while ((read = getline(&strct.line, &len, strct.file)) != -1)
{
opcode = strtok(strct.line, " \t");
if (*opcode == '#' || *opcode == '\n')
{
strct.line_number++;
continue;
}
if (strcmp(opcode, "push") == 0)
{
val = strtok(NULL, " ");
strct = push1(val, strct);
strct.line_number++;
continue;
}
lst_opcode(&strct.stack, opcode, strct);
strct.line_number++;
}
free_collect(strct);
return (0);
}