-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
55 lines (51 loc) · 1.02 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
#include "monty.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* check_file - check argument count
* and file extension
* @argc: argument count
* Return: void
*/
void check_file(int argc)
{
/* int filename_len; */
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
}
/**
* main - entry point
* @argc: argument count
* @argv: argument list
* Return: 0 on success
*/
int main(int argc, char **argv)
{
char line[100];
data_t data = { 0, "\0", "\0", NULL };
stack_t *main_stack = NULL;
check_file(argc);
data.file = fopen(argv[1], "r");
if (!data.file)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while (fgets(line, sizeof(line), data.file))
{
data.line_number++;
if (line[0] == '\n')
continue;
if (sscanf(line, "%s %s", data.opcode, data.argument) >= 1)
run_instruction(&data, &main_stack);
strcpy(data.opcode, "\0");
strcpy(data.argument, "\0");
}
free_stack(main_stack);
fclose(data.file);
return (0);
}