-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpansions_vars_1.c
97 lines (88 loc) · 2.35 KB
/
expansions_vars_1.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
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansions_vars_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngasco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/14 11:27:49 by ngasco #+# #+# */
/* Updated: 2022/03/14 11:27:51 by ngasco ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/* 3a Isolate and create variable for variable name, e.g. PATH or USER */
char *ft_get_var_name(char *str, int start, int end)
{
char *result;
int i;
result = (char *)malloc(sizeof(char) * (end - start + 1));
if (!result)
exit(1);
i = 0;
while (start < end)
{
result[i] = str[start];
start++;
i++;
}
result[i] = '\0';
return (result);
}
/* 3b Take variable value and splice it into original str */
char *ft_splice_var_value(char *s, char *var, int start, int end)
{
char *result;
int i;
int x;
i = 0;
x = 0;
result = (char *)malloc(ft_strlen(s) - (end - start) + ft_strlen(var) + 1);
while (s[i] != '\0')
{
if (i == start)
{
ft_splice_var_value_utility(result, var, &x);
i += (end - start);
}
result[x] = s[i];
i++;
x++;
}
result[x] = '\0';
return (result);
}
/* Utility function for 3b in order to respect Norminette length restrictions */
void ft_splice_var_value_utility(char *result, char *var, int *x)
{
int y;
y = 0;
while (var[y] != '\0')
{
result[*x] = var[y];
y++;
*x = *x + 1;
}
}
/* 3c If variable value doesn't exist, just remove $VAR from string */
char *ft_remove_var_name(char *str, int start, int end)
{
int i;
int y;
char *result;
result = (char *)malloc(ft_strlen(str) - (end - start) + 1);
i = 0;
y = 0;
while (str[i] != '\0')
{
if (i == (start - 1) || (i == 0 && start == 0))
{
while (i < end)
i++;
}
result[y] = str[i];
i++;
y++;
}
result[y] = '\0';
return (result);
}