maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/07 17:38:25 by dmdemirk #+# #+# */
9/* Updated: 2024/09/06 12:00:46 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "shell.h"
14#include "env.h"
15#include "libft.h"
16#include <stdlib.h>
17
18char **env_to_array(t_env *envp);
19char *ft_strcdup(const char *s, int c);
20void free_env(t_env *envp);
21void print_t_env(t_env *tokens);
22
23char **env_to_array(t_env *envp)
24{
25 t_env *curr_node;
26 char **env_array;
27 int i;
28 char *temp;
29
30 i = 0;
31 curr_node = envp;
32 while (curr_node)
33 {
34 i++;
35 curr_node = curr_node->next;
36 }
37 env_array = (char **)malloc(sizeof(char *) * (i + 1));
38 i = 0;
39 curr_node = envp;
40 while (curr_node)
41 {
42 temp = ft_strjoin(curr_node->key, "=");
43 env_array[i] = ft_strjoin(temp, curr_node->value);
44 free(temp);
45 i++;
46 curr_node = curr_node->next;
47 }
48 env_array[i] = NULL;
49 return (env_array);
50}
51
52char *ft_strcdup(const char *s, int c)
53{
54 char *str;
55 size_t i;
56
57 i = 0;
58 while (s[i] && s[i] != c)
59 i++;
60 str = (char *)malloc(sizeof(char) * (i + 1));
61 if (!str)
62 return (NULL);
63 i = 0;
64 while (s[i] && s[i] != c)
65 {
66 str[i] = s[i];
67 i++;
68 }
69 str[i] = '\0';
70 return (str);
71}
72
73void free_env(t_env *envp)
74{
75 t_env *curr_node;
76 t_env *next_node;
77
78 curr_node = envp;
79 while (curr_node)
80 {
81 next_node = curr_node->next;
82 free(curr_node->key);
83 free(curr_node->value);
84 free(curr_node);
85 curr_node = next_node;
86 }
87}
88
89void print_t_env(t_env *tokens)
90{
91 t_env *token;
92 int i;
93
94 i = 0;
95 token = tokens;
96 while (token != NULL)
97 {
98 ft_printf("key[%d] -> %s \n", i, token->key);
99 ft_printf("value[%d] -> %s \n", i, token->value);
100 token = token->next;
101 i++;
102 }
103}
char ** env_to_array(t_env *envp)
Definition utils.c:23
void free_env(t_env *envp)
Definition utils.c:73
char * ft_strcdup(const char *s, int c)
Definition utils.c:52
void print_t_env(t_env *tokens)
Definition utils.c:89
int ft_printf(const char *format,...)
Definition ft_printf.c:37
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23
Definition env.h:17
struct s_env * next
Definition env.h:20
char * key
Definition env.h:18
char * value
Definition env.h:19