-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty.c
93 lines (74 loc) · 1.25 KB
/
monty.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "monty.h"
#include <stdio.h>
#include <string.h>
stack_t *head = NULL;
/**
*main - entry point
*@argc: argument count
*@argv: argument vector
*Return: 0 success
*/
int main(int argc, char *argv[])
{
FILE *fp = NULL;
char buffer[1024];
unsigned int line = 1;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL || argv[1] == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while (fgets(buffer, 1024, fp) != NULL)
{
line_parse(buffer, line);
line++;
}
fclose(fp);
freenodes();
return (0);
}
/**
*freenodes - frees nodes
*Return: void
*/
void freenodes(void)
{
stack_t *temp;
if (head == NULL)
return;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
/**
* en_queue - adds a new node to the queue
*
* @newnode: pointer to node to be added
* @line: line number
*/
void en_queue(stack_t **newnode, unsigned int line)
{
stack_t *temp;
(void)line;
if (newnode == NULL || *newnode == NULL)
exit(EXIT_FAILURE);
if (head == NULL)
{
head = *newnode;
return;
}
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = *newnode;
(*newnode)->prev = temp;
}