Repository navigation
Expand file tree
/
Copy pathreplace.c
More file actions
120 lines (110 loc) · 3 KB
/
Copy pathreplace.c
File metadata and controls
120 lines (110 loc) · 3 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkellum <nkellum@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/05 15:42:31 by nkellum #+# #+# */
/* Updated: 2019/08/07 12:42:02 by nkellum ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
** DESCRIPTION:
** This function replaces any tilde symbols in the arguments sent
** to minishell with the value of the HOME environment variable.
**
** RETURN VALUE:
** If HOME is not set or there are not arguments, NULL is returned.
** Otherwise, a new array is returned with the tilde characters
** replaced by the value of HOME.
**
** EXTERNAL FUNCTIONS:
** check_env is located in env.c
*/
char **replace_tilde(t_shell *shell, char **input)
{
int i;
int length;
char **new_array;
length = 0;
while (input[length])
length++;
if (length < 2)
return (NULL);
i = 1;
new_array = string_arr_cpy(input);
while (input[i])
{
free(new_array[i]);
if (contains(input[i], '~') && check_env(shell, "HOME") == -1)
{
ft_printf("HOME not set.\n");
return (NULL);
}
new_array[i] = replace_substring(input[i], "~", shell->home);
i++;
}
return (new_array);
}
/*
** DESCRIPTION:
** This function checks if the dollar symbol is found in any
** strings of the string array.
**
** RETURN VALUE:
** If the dollar symbol is found, 1 is returned. Otherwise, 0 is returned.
*/
int contains_dollar(char **input)
{
int i;
i = 0;
while (input[i])
{
if (contains(input[i], '$'))
return (1);
i++;
}
return (0);
}
/*
** DESCRIPTION:
** This function replaces the name of an environment variable
** in front of a dollar symbol with the variable's value in
** every string of input.
**
** RETURN VALUE:
** If no dollar symbols are found or there are not arguments, NULL is
** returned. Otherwise, a new array is returned with $ENVNAME replaced
** with their values.
**
** EXTERNAL FUNCTIONS:
** ft_getenv is located in env.c
*/
char **replace_dollar_env(t_shell *shell, char **input)
{
int i;
int length;
char **new_array;
length = 0;
while (input[length])
length++;
if (!contains_dollar(input) || length < 2)
return (NULL);
i = 1;
new_array = string_arr_cpy(input);
while (input[i])
{
if (ft_strlen(input[i]) > 1 && input[i][0] == '$')
{
if (check_env(shell, input[i] + 1) != -1)
{
free(new_array[i]);
new_array[i] = ft_getenv(shell, input[i] + 1);
}
}
i++;
}
return (new_array);
}