-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_var_in_str.c
85 lines (76 loc) · 1.9 KB
/
replace_var_in_str.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
/*
** replace_var_in_str.c for in /home/fernan_w/Téléchargements/backup/process
**
** Made by
** Login <fernan_w@epitech.net>
**
** Started on Thu Jun 2 23:01:15 2016
** Last update Wed Jun 8 10:00:01 2016
*/
#include <stdlib.h>
#include "builtins.h"
#include "tools.h"
#include "shell.h"
#include "my.h"
static char *get_var_name(char *str, int nb)
{
int index;
char *name;
index = nb;
while (str && str[index] && str[index] != ' ' && str[index] != '/'
&& str[index] != '$' && str[index] != '=' && str[index] != ':')
index++;
if (!(name = malloc(sizeof(char *) * (index - nb + 1))))
return (NULL);
name[index - nb] = '\0';
index = 0;
while (str && str[nb] && str[nb] != ' ' && str[nb] != '='
&& str[nb] != '$' && str[nb] != ':' && str[nb] != '/')
name[index++] = str[nb++];
return (name);
}
static char *get_var_replace(char *name, t_mini *shell)
{
char *ptr;
if (!(ptr = find_str_in_tab(name, shell->env)))
if (!(ptr = find_str_in_tab(name, shell->secret_env)))
return (NULL);
return (clean_var(ptr));
}
static char *replace_val_in_str(char *str, char *name, char *val, int nb)
{
char *temp;
int index;
int cursor;
if (!( temp = malloc(sizeof(char *) *(my_strlen(str) + my_strlen(val) + 1
- my_strlen(name)))))
return (NULL);
index = 0;
cursor = 0;
while (index < nb)
temp[index++] = str[cursor++];
nb = 0;
while (val && val[nb])
temp[index++] = val[nb++];
cursor += my_strlen(name) + 1;
while (str && str[cursor])
temp[index++] = str[cursor++];
temp[index] = '\0';
free(name);
free(val);
free(str);
return (temp);
}
char *replace_var_in_str(char *str, t_mini *shell)
{
int nb;
char *name;
char *val;
while (str && (nb = have_var(str)) != -1)
{
name = get_var_name(str, nb + 1);
val = get_var_replace(name, shell);
str = replace_val_in_str(str, name, val, nb);
}
return (str);
}