-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd_command.c
More file actions
116 lines (110 loc) · 3.19 KB
/
cd_command.c
File metadata and controls
116 lines (110 loc) · 3.19 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
/*
** EPITECH PROJECT, 2020
** B-PSU-210-MPL-2-1-minishell2-guillaume.boudaille
** File description:
** cd_command.c
*/
#include "include/my.h"
void pwd_home(t_char_list **env)
{
t_char_list *node = *env;
t_char_list *cpy = node;
t_char_list *old = node;
while (my_strncmp("PWD=", node->value, 4) != 0) {
node = node->next;
}
while (my_strncmp("HOME=", cpy->value, 5) != 0) {
cpy = cpy->next;
}
while (my_strncmp("OLDPWD=", old->value, 5) != 0) {
old = old->next;
}
for (int i = 4; node->value[i - 1]; i++)
old->value[i + 4] = node->value[i];
for (int i = 5; cpy->value[i - 1]; i++)
node->value[i - 1] = cpy->value[i];
chdir(&cpy->value[5]);
}
void pwd_root(t_char_list **env, char *command)
{
t_char_list *node = *env;
t_char_list *old = node;
while (my_strncmp("PWD=", node->value, 4) != 0) {
node = node->next;
}
while (my_strncmp("OLDPWD=", old->value, 5) != 0) {
old = old->next;
}
if (chdir(command) < 0) {
print_cd_error(command);
return;
}
for (int i = 4; node->value[i - 1]; i++)
old->value[i + 4] = node->value[i];
for (int i = 0; command[i]; i++) {
node->value[i + 4] = command[i];
node->value[i + 5] = 0;
}
}
void pwd_less(t_char_list **env)
{
t_char_list *node = *env;
t_char_list *old = *env;
char *str = NULL;
while (my_strncmp("PWD=", node->value, 4) != 0)
node = node->next;
while (my_strncmp("OLDPWD=", old->value, 5) != 0)
old = old->next;
str = malloc(sizeof(char) * (my_strlen(node->value) + 1));
for (int i = 4; node->value[i - 1]; i++)
str[i - 4] = node->value[i];
for (int i = 8; old->value[i - 1]; i++)
node->value[i - 4] = old->value[i];
for (int i = 0; str[i]; i++) {
old->value[i + 8] = str[i];
old->value[i + 9] = 0;
}
chdir(node->value + 4);
}
int pwd_basic(t_char_list **env, char *command)
{
t_char_list *node = *env;
t_char_list *old = *env;
while (my_strncmp("PWD=", node->value, 4) != 0)
node = node->next;
while (my_strncmp("OLDPWD=", old->value, 5) != 0)
old = old->next;
if (command[0] == '/' && node->value[my_strlen(node->value) - 1] == '/')
command = command + 1;
if (command[0] != '/' && node->value[my_strlen(node->value) - 1] != '/')
command = my_strcat("/", command);
if (chdir(my_strcat(node->value + 4, command)) < 0) {
print_cd_error(command + 1);
return (0);
}
for (int i = 4; node->value[i - 1]; i++)
old->value[i + 4] = node->value[i];
node->value = my_strcat(node->value, command);
return (1);
}
int cd_command(t_char_list **env, char **command)
{
t_char_list *node = *env;
if (my_tablen(command) > 2) {
write(2, "cd: Too many arguments.\n", 24);
return (1);
}
if (my_tablen(command) == 1) {
pwd_home(&node);
return (1);
}
if (my_strncmp("-\0", command[1], 2) == 0) {
pwd_less(&node);
return (1);
}
if (my_strncmp("/\0", command[1], 1) == 0) {
pwd_root(&node, command[1]);
return (1);
}
return (pwd_basic(&node, command[1]));
}