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

Go to the source code of this file.

Functions

char * ft_remove_all_edge_quotes (char *str, char quote_type)
 
static char * remove_quotes (const char *value)
 
void add_new_env (t_env **env, const char *key, char *modified_value)
 
void set_env (t_env **env, const char *key, const char *value)
 

Function Documentation

◆ add_new_env()

void add_new_env ( t_env **  env,
const char *  key,
char *  modified_value 
)

Definition at line 31 of file utils_utils.c.

32{
33 t_env *new_env;
34
35 new_env = malloc(sizeof(t_env));
36 if (!new_env)
37 return ;
38 new_env->key = ft_strdup(key);
39 new_env->value = modified_value;
40 new_env->next = *env;
41 *env = new_env;
42}
char * ft_strdup(const char *s)
Definition ft_strdup.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_strdup(), s_env::key, s_env::next, and s_env::value.

Referenced by set_env().

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

◆ ft_remove_all_edge_quotes()

char * ft_remove_all_edge_quotes ( char *  str,
char  quote_type 
)

Definition at line 24 of file env.c.

25{
26 int start_quotes;
27 int end_quotes;
28 int len;
29 char *new_str;
30
31 len = ft_strlen(str);
32 start_quotes = 0;
33 while (str[start_quotes] == quote_type)
34 start_quotes++;
35 end_quotes = 0;
36 while (len - end_quotes - 1 >= 0 && str[len - end_quotes - 1] == quote_type)
37 end_quotes++;
38 if (start_quotes > 0 && start_quotes == end_quotes)
39 {
40 new_str = ft_strndup(str + start_quotes, len - 2 * start_quotes);
41 return (new_str);
42 }
43 return (ft_strdup(str));
44}
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15

References ft_strdup(), ft_strlen(), and ft_strndup().

Referenced by remove_quotes().

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

◆ remove_quotes()

static char * remove_quotes ( const char *  value)
static

Definition at line 20 of file utils_utils.c.

21{
22 char *temp;
23 char *result;
24
25 temp = ft_remove_all_edge_quotes((char *)value, '\"');
26 result = ft_remove_all_edge_quotes(temp, '\'');
27 free(temp);
28 return (result);
29}
char * ft_remove_all_edge_quotes(char *str, char quote_type)
Definition env.c:24

References ft_remove_all_edge_quotes().

Referenced by set_env().

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

◆ set_env()

void set_env ( t_env **  env,
const char *  key,
const char *  value 
)

Definition at line 44 of file utils_utils.c.

45{
46 t_env *current;
47 char *modified_value;
48
49 current = *env;
50 modified_value = NULL;
51 if (value[0] != '\0')
52 modified_value = remove_quotes(value);
53 else
54 modified_value = ft_strdup(value);
55 while (current)
56 {
57 if (ft_strcmp(current->key, key) == 0)
58 {
59 free(current->value);
60 current->value = modified_value;
61 return ;
62 }
63 current = current->next;
64 }
65 add_new_env(env, key, modified_value);
66}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
static char * remove_quotes(const char *value)
Definition utils_utils.c:20
void add_new_env(t_env **env, const char *key, char *modified_value)
Definition utils_utils.c:31

References add_new_env(), ft_strcmp(), ft_strdup(), s_env::key, s_env::next, remove_quotes(), and s_env::value.

Referenced by add_env(), builtin_cd(), test_set_env(), and test_unset_env().

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