maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
stack_control.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* stack_control.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/05/31 20:39:16 by rmikhayl #+# #+# */
9/* Updated: 2024/06/11 15:38:29 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14
15void add_node(t_token **head, char *str)
16{
17 t_token *new_node;
18 t_token *curr_node;
19
20 new_node = (t_token *)malloc(sizeof(t_token));
21 if (!new_node)
22 return ;
23 new_node->data = str;
24 new_node->next = NULL;
25 if (*head == NULL)
26 {
27 *head = new_node;
28 return ;
29 }
30 curr_node = *head;
31 while (curr_node->next != NULL)
32 curr_node = curr_node->next;
33 curr_node->next = new_node;
34}
35
36void print_stack(t_token **stack)
37{
38 t_token *current_node;
39 int i;
40
41 i = 0;
42 current_node = *stack;
43 while (current_node != NULL)
44 {
45 ft_printf("input[%d] -> %s \n", i, current_node->data);
46 current_node = current_node->next;
47 i++;
48 }
49}
50
51void free_stack(t_token **stack)
52{
53 t_token *current;
54 t_token *next;
55
56 current = *stack;
57 while (current != NULL)
58 {
59 next = current->next;
60 free(current);
61 current = next;
62 }
63}
64
66{
67 int size;
68 t_token *current;
69
70 size = 0;
71 current = stack;
72 while (current != NULL)
73 {
74 size++;
75 current = current->next;
76 }
77 return (size);
78}
79
81{
82 int count;
83 int i;
84 char **arr;
85 t_token *current;
86
87 current = head;
88 count = calc_stack_size(current);
89 arr = (char **)malloc((count + 1) * sizeof(char *));
90 if (!arr)
91 return (NULL);
92 current = head;
93 i = 0;
94 while (i < count)
95 {
96 arr[i] = ft_strdup(current->data);
97 current = current->next;
98 i++;
99 }
100 arr[count] = NULL;
101 return (arr);
102}
int ft_printf(const char *format,...)
Definition ft_printf.c:37
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
char ** list_to_array(t_token *head)
int calc_stack_size(t_token *stack)
void add_node(t_token **head, char *str)
void free_stack(t_token **stack)
void print_stack(t_token **stack)
struct s_token * next
Definition tokens.h:45
char * data
Definition tokens.h:44