-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.c
43 lines (40 loc) · 927 Bytes
/
lines.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
#include "monty.h"
/**
* openfile - opens and reads a file to be interpreted
*
* @pathname: path of the file to be opened
* @stack: stack to perform the opcode with/to
*
* Return: void
*/
FILE *openfile(char *pathname, stack_t **stack)
{
FILE *fd;
size_t len = 0;
ssize_t read;
char *text = NULL, *cptext, *opcode, *arg;
int line = 0;
void (*func)(stack_t **, unsigned int);
/*Open the file and validate if worked*/
fd = fopen(pathname, "r");
if (fd == NULL)
error_handler(1, line, NULL, pathname);
/*We read one line and stores it in text*/
while ((read = getline(&text, &len, fd)) != -1)
{
line++;
cptext = strdup(text);
_split(cptext, &opcode, &arg);
func = get_func(opcode);
if (func == NULL)
error_handler(2, line, opcode, NULL);
if (func == _push)
validArg(arg, line);
func(stack, line);
free(cptext);
}
/*Confirming it is free'd*/
if (text)
free(text);
return (fd);
}