-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_entry.c
More file actions
50 lines (44 loc) · 807 Bytes
/
Copy pathmain_entry.c
File metadata and controls
50 lines (44 loc) · 807 Bytes
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
#define _GNU_SOURCE
#include <stdio.h>
#include "monty.h"
unsigned int count_line = 0;
/**
* main - entry point for all the program
* @argc: number of arguments passed
* @argv: vector of the argument
*
* Return: 0 in success
*/
int main(int argc, char *argv[])
{
char **tokens = NULL;
stack_t *head = NULL;
char *buffer = NULL;
FILE *fp;
size_t n;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r+");
if (fp == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while ((getline(&buffer, &n, fp)) != -1)
{
count_line++;
tokens = strtok_opcode(buffer);
if (tokens)
{
call(tokens, &head);
free(tokens);
}
}
free(buffer);
free_stack(&head);
fclose(fp);
return (0);
}