-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_cd.c
70 lines (61 loc) · 1.82 KB
/
utils_cd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 13:54:10 by brenaudo #+# #+# */
/* Updated: 2023/01/13 14:12:14 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void init_ft_chdir(t_list **pwd, t_list **old_pwd)
{
*pwd = get_env_node("PWD");
*old_pwd = get_env_node("OLDPWD");
}
void free_and_null(char **tmp)
{
free_null(*tmp);
*tmp = NULL;
}
void print_error_dir_cd(char *dir)
{
char *err;
err = ft_strjoin("minishell: cd: ", dir);
perror(err);
free(err);
}
char *get_home_content(void)
{
t_list *env_node;
env_node = get_env_node("HOME");
if (env_node == NULL)
{
ft_putstr_fd("minishell: cd: HOME not set\n", 2);
return (NULL);
}
return (env_node->content + 5);
}
void ft_chdir_core(int size, char **tmp, t_list *pwd)
{
char *ret;
ret = NULL;
while (ret == NULL)
{
size *= 2;
free_and_null(tmp);
while (*tmp == NULL)
*tmp = ft_calloc(size, sizeof(char));
ret = getcwd(*tmp, size);
}
if (pwd)
{
free_null(pwd->content);
pwd->content = ft_strjoin("PWD=", *tmp);
}
else
ft_lstadd_back(g_glob->envp, ft_lstnew(ft_strjoin("PWD=", *tmp)));
free_null(*tmp);
}