-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.c
74 lines (64 loc) · 1.81 KB
/
utilities.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kblack <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/26 22:45:44 by kblack #+# #+# */
/* Updated: 2019/03/28 22:48:54 by kblack ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *new_node(void)
{
t_env *new_node;
new_node = (t_env*)malloc(sizeof(t_env));
ft_bzero(new_node, sizeof(t_env));
return (new_node);
}
char *get_value(t_shell *sh, char *name)
{
t_env *tmp;
tmp = sh->env_info;
while (tmp)
{
if (ft_strcmp(tmp->key, name) == 0)
return (tmp->value);
tmp = tmp->next;
}
return (NULL);
}
void expansion(char *str, t_shell *sh)
{
t_env *list;
char *val;
list = sh->env_info;
if (str[0] == '~')
ft_printf("%s", get_value(sh, "HOME"));
else if (str[0] == '$')
{
str = str + 1;
if (!(val = get_value(sh, str)))
return ;
ft_printf("%s", val);
}
}
void check_args(char *str, t_shell *sh)
{
if (str[0] == '$' || str[0] == '~')
expansion(str, sh);
else
ft_printf("%s", str);
ft_putchar(' ');
}
int check_dir(char *name)
{
DIR *d_stream;
d_stream = opendir(name);
if (d_stream == NULL)
return (-1);
closedir(d_stream);
d_stream = NULL;
return (0);
}