-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
82 lines (73 loc) · 2.03 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/16 15:59:45 by msoulaim #+# #+# */
/* Updated: 2022/12/16 13:32:04 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *get_cwd(t_shell *shell)
{
char buf[4096 + 1];
char *cwd;
char *put;
char *tmp;
tmp = NULL;
cwd = getcwd(buf, 4096);
(cwd == NULL) ? cwd = ft_strdup("you removed your f directory") : 0;
tmp = get_var_env("HOME", shell);
if (tmp == NULL || ft_strlen(tmp) == 0)
return (ft_strdup(cwd));
if (ft_strnequ(cwd, tmp, ft_strlen(tmp)) == 1)
{
put = replace_in_str(cwd, tmp, "~");
free(tmp);
return (put);
}
else
{
free(tmp);
return (ft_strdup(cwd));
}
}
void ft_put4str(char *a, char *b, char *c, char *d)
{
ft_putstr_fd(a, 2);
ft_putstr_fd(b, 2);
ft_putstr_fd(c, 2);
ft_putstr_fd(d, 2);
}
char **d_realloc(char **str, int new_size)
{
char **new;
int i;
new = (char **)ft_memalloc(sizeof(char *) * (new_size + 1));
i = -1;
while (str[++i] && i < new_size)
{
new[i] = ft_strdup(str[i]);
free(str[i]);
}
new[new_size] = NULL;
free(str);
return (new);
}
char *join_three(char *str1, char *str2, char *str3)
{
char *s;
char *ret;
s = ft_strjoin(str1, str2);
ret = ft_strjoin(s, str3);
(s) ? free(s) : 0;
return (ret);
}
int str2len(char **str)
{
if (*str)
return (str2len(str + 1) + 1);
return (0);
}