maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils.c File Reference
#include "shell.h"
#include "env.h"
#include "libft.h"
#include <stdlib.h>
Include dependency graph for utils.c:

Go to the source code of this file.

Functions

char ** env_to_array (t_env *envp)
 
char * ft_strcdup (const char *s, int c)
 
void free_env (t_env *envp)
 
void print_t_env (t_env *tokens)
 

Function Documentation

◆ env_to_array()

char ** env_to_array ( t_env envp)

Definition at line 23 of file utils.c.

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}
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

References ft_strjoin(), s_env::key, s_env::next, and s_env::value.

Referenced by handle_exec_errors(), and test_init_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_env()

void free_env ( t_env envp)

Definition at line 73 of file utils.c.

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}

References s_env::key, s_env::next, and s_env::value.

Referenced by env_tests().

Here is the caller graph for this function:

◆ ft_strcdup()

char * ft_strcdup ( const char *  s,
int  c 
)

Definition at line 52 of file utils.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}

Referenced by add_env(), add_shell_var_node(), and handle_add_set_shell_variable().

Here is the caller graph for this function:

◆ print_t_env()

void print_t_env ( t_env tokens)

Definition at line 89 of file utils.c.

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}
int ft_printf(const char *format,...)
Definition ft_printf.c:37

References ft_printf(), s_env::key, s_env::next, and s_env::value.

Here is the call graph for this function: