-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
50 lines (43 loc) · 1.22 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
#include "monty.h"
/**
* main - Execute the monty program
* @argc: Argument count
* @argv: Argument vector
*
* Return: Always 0 if successful
*/
int main(UNUSED int argc, UNUSED char *argv[])
{
UNUSED FILE *fp_file = NULL; /* File pointer to input stream */
UNUSED char *cp_filename = NULL; /* pointer to filename */
UNUSED char *mode = "r"; /* Mode for opening file */
stack_t *stack = NULL; /* Stack to manipulate */
/* Set default state of the machine */
bytecode.opcode = NULL;
bytecode.opcode_arg = INT_MIN;
bytecode.state = LIFO; /* Operating in stack mode */
if (argc != 2)
{/* Ensure file name was passed on command line */
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
cp_filename = argv[1]; /* set name of file to run */
fp_file = fopen(cp_filename, mode);
if (fp_file == NULL)
{/* File doesn't exist */
fprintf(stderr, "Error: Can't open file %s\n", cp_filename);
exit(EXIT_FAILURE);
}
/* Execute instructions in the stream */
read_file(&stack, fp_file);
/* Clean up */
if (fclose(fp_file) == EOF)
{
fprintf(stderr, "Error: Can't close the file %s in stream %p\n",
cp_filename, (void *)fp_file);
exit(EXIT_FAILURE);
}
if (stack != NULL)
free_stack(&stack);
return (0);
}