-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.c
More file actions
148 lines (132 loc) · 2.24 KB
/
Copy pathbuiltins.c
File metadata and controls
148 lines (132 loc) · 2.24 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "minishell.h"
int ft_echo(char **args)
{
int(i) = 1;
int(n) = 0;
while (ft_strncmp("-n", args[i], 2) == 0)
{
n = 1;
i++;
}
while (args[i])
{
printf("%s", args[i]);
if (args[i + 1])
printf(" ");
i++;
}
if (!n)
printf("\n");
return (0);
}
void edit_env_value(t_list *env, char *name, char *new_value)
{
t_env *env_var;
while (env)
{
env_var = (t_env *)env->content;
if (!ft_strncmp(env_var->name, name, ft_strlen(name) + 1))
{
free(env_var->value);
env_var->value = strdup(new_value);
if (!env_var->value)
return ;
break ;
}
env = env->next;
}
}
int cd_command(t_list *env, char *path)
{
char *old_pwd;
char *pwd;
old_pwd = getcwd(NULL, 0);
edit_env_value(env, "OLDPWD", old_pwd);
free(old_pwd);
if (chdir(path) != 0)
{
perror("ft_cd");
return (1);
}
pwd = getcwd(NULL, 0);
edit_env_value(env, "PWD", pwd);
free(pwd);
return (0);
}
int ft_cd(char **args, t_list *env)
{
if (args[1] == NULL)
return(cd_command(env, "/home"));
else
return(cd_command(env, args[1]));
return (1);
}
int err(const char *msg, int ret)
{
if (msg)
ft_printf("%s\n", msg);
return (ret);
}
int ft_pwd(void)
{
char *path;
path = getcwd(NULL, 0);
if (!path)
return (err("Error getting current directory", 1));
ft_printf("%s\n", path);
free(path);
return (0);
}
void free_and_relink(t_list *prev, t_list *current)
{
t_env *env_var;
prev->next = current->next;
env_var = (t_env *)current->content;
free(env_var->name);
free(env_var->value);
free(current->content);
free(current);
}
int ft_unset(char **args, t_list *env)
{
t_list *prev;
t_list *current;
t_env *env_var;
prev = NULL;
current = env;
while (current)
{
env_var = (t_env *)current->content;
if (ft_strncmp(env_var->name, args[1], ft_strlen(args[1]) + 1) == 0)
{
if (prev)
free_and_relink(prev, current);
else
env = current->next;
return (0);
}
prev = current;
current = current->next;
}
return (0);
}
int print_env(t_list *env)
{
t_list *tmp;
tmp = env;
while (tmp)
{
ft_printf("%s=%s\n", ((t_env *)tmp->content)->name,
((t_env *)tmp->content)->value);
tmp = tmp->next;
}
return (0);
}
void ft_exit(void)
{
ft_printf("exit\n");
free_cmd_list();
free_env();
rl_clear_history();
exit(0);
}