-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_chellrc.c
64 lines (59 loc) · 1.81 KB
/
handle_chellrc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_chellrc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/04 17:20:45 by tgauvrit #+# #+# */
/* Updated: 2017/05/20 15:09:17 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
static void strip_comments(char *str)
{
int i;
int len;
int quote;
if (*str == '#')
*str = 0;
if (*str == '#')
return ;
i = 0;
quote = 0;
len = ft_strlen(str);
while (++i < len)
{
if ((str[i] == '\'' || str[i] == '\"') && (quote % (str[i] % 6)) == 0)
quote ^= str[i] % 6;
if (str[i] == '#' && quote == 0 && str[i - 1] != '\\')
str[i] = '\0';
else if (str[i] == '#' && quote == 0 && str[i - 1] == '\\')
{
ft_strcpy(str + i - 1, str + i);
--i;
}
}
}
void handle_chellrc(t_env *env)
{
int fd;
char *tmp;
char *input;
if (find_param(env->ev, "HOME") == -1)
return ;
tmp = ft_strdup(env->ev[find_param(env->ev, "HOME")] + 5);
tmp = ft_strjoinfree(tmp, "/.chellrc", 1);
if ((fd = open(tmp, O_RDONLY)) < 0)
return (free(tmp));
input = NULL;
while (get_next_line(fd, &input))
{
strip_comments(input);
if (ft_read(env, input, -1, 0) != 0)
break ;
}
close(fd);
free(tmp);
return ;
}