-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_env.c
More file actions
125 lines (115 loc) · 2.89 KB
/
Copy pathhandle_env.c
File metadata and controls
125 lines (115 loc) · 2.89 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
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: THISISLWA <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/09 14:01:53 by THISISLWA #+# #+# */
/* Updated: 2017/05/09 14:01:55 by THISISLWA ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ck_env_content(t_minfo *info, int *i)
{
int find;
find = 0;
*i = 0;
while (info->env[*i])
{
if (!ft_strncmp(info->av[1], info->env[*i], ft_strlen(info->av[1])))
find = 1;
(*i)++;
}
return (find);
}
int ck_n_replace_env(t_minfo *info, const char *env_key, \
char *new_env_kv, int *i)
{
*i = 0;
while (info->env[*i])
{
if (!ft_strncmp(info->env[*i], env_key, ft_strlen(env_key)))
{
free(info->env[*i]);
info->env[*i] = ft_strdup(new_env_kv);
free(new_env_kv);
return (1);
}
(*i)++;
}
return (0);
}
void buitin_setenv(t_minfo *info, const char *env_key, const char *env_value)
{
int i;
char *new_env_kv;
char **new_env;
new_env = NULL;
new_env_kv = env_value ? ft_strcjoin(env_key, env_value, '=') :
ft_strjoin(env_key, "=");
if (ck_n_replace_env(info, env_key, new_env_kv, &i))
return ;
else
{
new_env = (char**)malloc(sizeof(char*) * (i + 2));
i = 0;
while (info->env[i])
{
new_env[i] = ft_strdup(info->env[i]);
free(info->env[i]);
i++;
}
free(info->env);
new_env[i] = ft_strdup(new_env_kv);
free(new_env_kv);
new_env[++i] = NULL;
}
info->env = new_env;
}
int get_cmd_env(t_minfo *info, int len)
{
int err;
int i;
int j;
err = 0;
i = 1;
j = 0;
info->cmd_env = (char**)malloc(sizeof(char*) * (len - 1));
while (info->av[i] && i < len - 1)
{
if ((err = check_str(info->av[i], '=')))
return (err);
info->cmd_env[j] = ft_strdup(info->av[i]);
j++;
i++;
}
info->cmd_env[j] = NULL;
return (0);
}
int handle_executable_file(t_minfo *info, int len)
{
int err;
struct stat s;
err = 0;
if (get_cmd_env(info, len))
return (1);
err = stat(info->av[len - 1], &s);
if (err == -1)
{
ft_fprintf(2, "minishell: no such file or directory: %s\n", \
info->av[len - 1]);
info->sign = 1;
}
else if (!(err = access(info->av[len - 1], X_OK)))
{
info->cmd_path = ft_strdup(info->av[len - 1]);
exc_command(info);
}
else
{
ft_fprintf(2, "%s: Permission denied.\n", info->av[len - 1]);
info->sign = 1;
}
return (0);
}