forked from Kinnune/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction2.c
131 lines (120 loc) · 2.65 KB
/
function2.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* function2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: djames <djames@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/22 13:49:16 by djames #+# #+# */
/* Updated: 2023/07/19 14:10:33 by djames ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_built2(char **str)
{
int i;
i = 1;
if (str[0] != NULL && ft_strncmp(str[0], "pwd\0", 4) == 0)
{
ft_pwd();
i = 0;
}
else if (str[0] != NULL && ft_strncmp(str[0], "export\0", 7) == 0)
{
export_start(str);
i = 0;
}
else if (str[0] != NULL && ft_strncmp(str[0], "unset\0", 7) == 0)
{
if (str[1] != NULL)
remove_start(str);
i = 0;
}
else if (str[0] != NULL && ft_strncmp(str[0], "env\0", 4) == 0)
{
start_env(str);
i = 0;
}
return (i);
}
char *find_helper(int equal, int j, int i)
{
char *str2;
equal = ft_strlen(g_data.envir[i]) - 4;
str2 = malloc(sizeof(char) * (equal + 1));
if (!str2)
return (NULL);
equal = 0;
while (g_data.envir[i][j] != '\0')
{
str2[equal] = g_data.envir[i][j];
j++;
equal++;
}
str2[equal] = '\0';
return (str2);
}
void add_string(char *new_string)
{
int lenght;
int i;
char *remo;
char **temp;
i = 0;
remo = (find_equal_2(new_string));
if (remo == NULL)
return ;
if ((find_word(new_string)) == 1)
{
free(remo);
return ;
}
remove_string(remo);
lenght = ft_length_word(g_data.envir);
temp = malloc((lenght + 2) * (sizeof(char *)));
if (!temp)
exit(-1);
temp[lenght + 1] = NULL;
aux_add25(&i, lenght, temp);
temp[i] = ft_strdup(new_string);
if (!temp[i])
exit(-1);
ft_free_copy(temp, remo);
}
int find_25(char *str)
{
int i;
int equal;
i = 0;
equal = 0;
while (str[i] != '\0' && equal == 0)
{
if (str[i] == '=')
equal = 1;
i++;
}
return (equal);
}
int find_word(char *str)
{
int i;
int equal;
int length;
i = 0;
equal = find_25(str);
length = ft_strlen(str);
while (g_data.envir[i] != NULL)
{
if (ft_strncmp(g_data.envir[i], str, length) == 0)
{
if (equal == 1)
{
ft_copy(i);
return (0);
}
return (1);
}
i++;
}
return (0);
}